Skip to content

Commit

Permalink
Merge pull request #1091 from AntoineJac/test-global-config
Browse files Browse the repository at this point in the history
Test global config with info share tag
  • Loading branch information
AntoineJac authored Nov 9, 2023
2 parents 77682ca + 1174b58 commit 1680dfb
Show file tree
Hide file tree
Showing 8 changed files with 152 additions and 6 deletions.
34 changes: 34 additions & 0 deletions cmd/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,24 @@ func syncMain(ctx context.Context, filenames []string, dry bool, parallelism,
return err
}

dumpConfig.LookUpSelectorTagsConsumers, err = determineLookUpSelectorTagsConsumers(*targetContent)
if err != nil {
fmt.Printf("Error adding global entities: %v\n", err)
}

if dumpConfig.LookUpSelectorTagsConsumers != nil {
consumersGlobal, err := dump.GetAllConsumers(ctx, kongClient, dumpConfig.LookUpSelectorTagsConsumers)
if err != nil {
fmt.Printf("Error retrieving global consumers: %v\n", err)
}
for _, c := range consumersGlobal {
targetContent.Consumers = append(targetContent.Consumers, file.FConsumer{Consumer: *c})
if err != nil {
fmt.Printf("Error adding global consumer %v: %v\n", *c.Username, err)
}
}
}

if utils.Kong340Version.LTE(parsedKongVersion) {
dumpConfig.IsConsumerGroupScopedPluginSupported = true
}
Expand Down Expand Up @@ -321,6 +339,22 @@ func syncMain(ctx context.Context, filenames []string, dry bool, parallelism,
return nil
}

func determineLookUpSelectorTagsConsumers(targetContent file.Content) ([]string, error) {
if targetContent.Info != nil {
if targetContent.Info.LookUpSelectorTags.Consumers != nil {
if len(targetContent.Info.LookUpSelectorTags.Consumers) == 0 {
return nil, fmt.Errorf("global consumers specify but no global tags")
}
if len(targetContent.Info.LookUpSelectorTags.Consumers) > 0 {
utils.RemoveDuplicates(&targetContent.Info.LookUpSelectorTags.Consumers)
sort.Strings(targetContent.Info.LookUpSelectorTags.Consumers)
return targetContent.Info.LookUpSelectorTags.Consumers, nil
}
}
}
return nil, nil
}

func determineSelectorTag(targetContent file.Content, config dump.Config) ([]string, error) {
if targetContent.Info != nil {
if len(targetContent.Info.SelectorTags) > 0 {
Expand Down
39 changes: 39 additions & 0 deletions dump/dump.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ type Config struct {
// tags.
SelectorTags []string

// LookUpSelectorTagsConsumers can be used to lookup entities using other
// tags.
LookUpSelectorTagsConsumers []string

// KonnectControlPlane
KonnectControlPlane string

Expand Down Expand Up @@ -93,6 +97,13 @@ func getConsumerConfiguration(ctx context.Context, group *errgroup.Group,
if err != nil {
return fmt.Errorf("consumers: %w", err)
}
if config.LookUpSelectorTagsConsumers != nil {
updatedConsumers, err := GetAllGlobalConsumers(ctx, client, config.LookUpSelectorTagsConsumers, consumers)
if err != nil {
return fmt.Errorf("error retrieving global consumers: %w", err)
}
consumers = updatedConsumers
}
state.Consumers = consumers
return nil
})
Expand Down Expand Up @@ -499,6 +510,34 @@ func GetAllConsumers(ctx context.Context,
return consumers, nil
}

// GetAllGlobalConsumers queries Kong for all the globlal consumers using client.
// Please use this method with caution if you have a lot of consumers.
func GetAllGlobalConsumers(ctx context.Context,
client *kong.Client, tags []string,
consumers []*kong.Consumer,
) ([]*kong.Consumer, error) {
opt := newOpt(tags)

for {
s, nextopt, err := client.Consumers.List(ctx, opt)
if err != nil {
return nil, err
}
if err := ctx.Err(); err != nil {
return nil, err
}
if err := ctx.Err(); err != nil {
return nil, err
}
consumers = append(consumers, s...)
if nextopt == nil {
break
}
opt = nextopt
}
return consumers, nil
}

// GetAllUpstreams queries Kong for all the Upstreams using client.
func GetAllUpstreams(ctx context.Context,
client *kong.Client, tags []string,
Expand Down
25 changes: 21 additions & 4 deletions file/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"context"
"errors"
"fmt"
"reflect"
"sort"

"github.com/blang/semver/v4"
"github.com/kong/deck/konnect"
Expand All @@ -22,9 +24,10 @@ type stateBuilder struct {
defaulter *utils.Defaulter
kongVersion semver.Version

selectTags []string
skipCACerts bool
intermediate *state.KongState
selectTags []string
lookupTagsConsumers []string
skipCACerts bool
intermediate *state.KongState

client *kong.Client
ctx context.Context
Expand Down Expand Up @@ -321,7 +324,21 @@ func (b *stateBuilder) consumers() {
c.ID = kong.String(*consumer.ID)
}
}
utils.MustMergeTags(&c.Consumer, b.selectTags)

// Convert c.Tags from []*string to []string
stringTags := make([]string, len(c.Tags))
for i, tag := range c.Tags {
if tag != nil {
stringTags[i] = *tag
}
}
sort.Strings(stringTags)
sort.Strings(b.lookupTagsConsumers)
// Now compare the two []string slices
if !reflect.DeepEqual(stringTags, b.lookupTagsConsumers) {
utils.MustMergeTags(&c.Consumer, b.selectTags)
}

if consumer != nil {
c.Consumer.CreatedAt = consumer.CreatedAt
}
Expand Down
16 changes: 16 additions & 0 deletions file/kong_json_schema.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions file/reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,10 @@ func Get(ctx context.Context, fileContent *Content, opt RenderConfig, dumpConfig
builder.selectTags = dumpConfig.SelectorTags
}

if len(dumpConfig.LookUpSelectorTagsConsumers) > 0 {
builder.lookupTagsConsumers = dumpConfig.LookUpSelectorTagsConsumers
}

if fileContent.Transform != nil && !*fileContent.Transform {
return nil, ErrorTransformFalseNotSupported
}
Expand Down
12 changes: 10 additions & 2 deletions file/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -597,8 +597,16 @@ type KongDefaults struct {
// Info contains meta-data of the file.
// +k8s:deepcopy-gen=true
type Info struct {
SelectorTags []string `json:"select_tags,omitempty" yaml:"select_tags,omitempty"`
Defaults KongDefaults `json:"defaults,omitempty" yaml:"defaults,omitempty"`
SelectorTags []string `json:"select_tags,omitempty" yaml:"select_tags,omitempty"`
LookUpSelectorTags *LookUpSelectorTags `json:"lookup_tags,omitempty" yaml:"lookup_tags,omitempty"`
Defaults KongDefaults `json:"defaults,omitempty" yaml:"defaults,omitempty"`
}

// LookUpSelectorTags contains tags to lookup
// for corresponding entities already in Kong.
// +k8s:deepcopy-gen=true
type LookUpSelectorTags struct {
Consumers []string `json:"consumers,omitempty" yaml:"consumers,omitempty"`
}

// Konnect contains configuration specific to Konnect.
Expand Down
26 changes: 26 additions & 0 deletions file/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ github.com/alecthomas/jsonschema v0.0.0-20191017121752-4bb6e3fae4f2 h1:swGeCLPiU
github.com/alecthomas/jsonschema v0.0.0-20191017121752-4bb6e3fae4f2/go.mod h1:Juc2PrI3wtNfUwptSvAIeNx+HrETwHQs6nf+TkOJlOA=
github.com/atomicgo/cursor v0.0.1/go.mod h1:cBON2QmmrysudxNBFthvMtN32r3jxVRIvzkUiF/RuIk=
github.com/benbjohnson/clock v1.1.0 h1:Q92kusRqC1XV2MjkWETPvjJVqKetz1OzxZB7mHJLju8=
github.com/benbjohnson/clock v1.1.0/go.mod h1:J11/hYXuz8f4ySSvYwY0FKfm+ezbsZBKZxNJlLklBHA=
github.com/blang/semver/v4 v4.0.0 h1:1PFHFE6yCCTv8C1TeyNNarDzntLi7wMI5i/pzqYIsAM=
github.com/blang/semver/v4 v4.0.0/go.mod h1:IbckMUScFkM3pff0VJDNKRiT6TG/YpiHIM2yvyW5YoQ=
github.com/cenkalti/backoff/v4 v4.2.1 h1:y4OZtCnogmCPw98Zjyt5a6+QwPLGkiQsYW5oUqylYbM=
Expand Down Expand Up @@ -429,6 +430,7 @@ go.opencensus.io v0.22.5/go.mod h1:5pWMHQbX5EPX2/62yrJeAkowc+lfs/XD7Uxpq3pI6kk=
go.uber.org/atomic v1.11.0 h1:ZvwS0R+56ePWxUNi+Atn9dWONBPp/AUETXlHW0DxSjE=
go.uber.org/atomic v1.11.0/go.mod h1:LUxbIzbOniOlMKjJjyPfpl4v+PKK2cNJn91OQbhoJI0=
go.uber.org/goleak v1.1.11 h1:wy28qYRKZgnJTxGxvye5/wgWr1EKjmUDGYox5mGlRlI=
go.uber.org/goleak v1.1.11/go.mod h1:cwTWslyiVhfpKIDGSZEM2HlOvcqm+tG4zioyIeLoqMQ=
go.uber.org/multierr v1.11.0 h1:blXXJkSxSSfBVBlC76pxqeO+LN3aDfLQo+309xJstO0=
go.uber.org/multierr v1.11.0/go.mod h1:20+QtiLqy0Nd6FdQB9TLXag12DsQkrbs3htMFfDN80Y=
go.uber.org/zap v1.24.0 h1:FiJd5l1UOLj0wCgbSE0rwwXHzEdAZS6hiiSnxJN/D60=
Expand Down

0 comments on commit 1680dfb

Please sign in to comment.