Skip to content

Commit

Permalink
Feat/default add look up tags REBASED (#31)
Browse files Browse the repository at this point in the history
* add lookup tag

* edit comment
  • Loading branch information
AntoineJac authored Jan 15, 2024
1 parent 3c360b8 commit 14fe4d3
Show file tree
Hide file tree
Showing 6 changed files with 102 additions and 6 deletions.
24 changes: 24 additions & 0 deletions pkg/dump/dump.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ type Config struct {
// tags.
SelectorTags []string

// LookUpSelectorTagsConsumers can be used to ensure state lookup for entities using
// these tags. This functionality is essential when using a plugin that references
// consumers associated with tags different from those in the sync command.
LookUpSelectorTagsConsumers []string

// KonnectControlPlane
KonnectControlPlane string

Expand Down Expand Up @@ -93,6 +98,25 @@ func getConsumerConfiguration(ctx context.Context, group *errgroup.Group,
if err != nil {
return fmt.Errorf("consumers: %w", err)
}
if config.LookUpSelectorTagsConsumers != nil {
globalConsumers, err := GetAllConsumers(ctx, client, config.LookUpSelectorTagsConsumers)
if err != nil {
return fmt.Errorf("error retrieving global consumers: %w", err)
}
// if globalConsumers are not present, add them.
for _, globalConsumer := range globalConsumers {
found := false
for _, consumer := range consumers {
if *globalConsumer.ID == *consumer.ID {
found = true
break
}
}
if !found {
consumers = append(consumers, globalConsumer)
}
}
}
state.Consumers = consumers
return nil
})
Expand Down
26 changes: 22 additions & 4 deletions pkg/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/go-database-reconciler/pkg/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,22 @@ func (b *stateBuilder) consumers() {
c.ID = kong.String(*consumer.ID)
}
}
utils.MustMergeTags(&c.Consumer, b.selectTags)

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)
// if the consumer tags and the lookup tags are the same, it means
// that the consumer is a global consumer retrieved from upstream,
// therefore we don't want to merge its tags with the selected tags.
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 pkg/file/kong_json_schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -1217,6 +1217,10 @@
"$schema": "http://json-schema.org/draft-04/schema#",
"$ref": "#/definitions/KongDefaults"
},
"default_lookup_tags": {
"$schema": "http://json-schema.org/draft-04/schema#",
"$ref": "#/definitions/LookUpSelectorTags"
},
"select_tags": {
"items": {
"type": "string"
Expand Down Expand Up @@ -1338,6 +1342,18 @@
"additionalProperties": false,
"type": "object"
},
"LookUpSelectorTags": {
"properties": {
"consumers": {
"items": {
"type": "string"
},
"type": "array"
}
},
"additionalProperties": false,
"type": "object"
},
"MTLSAuth": {
"required": [
"id",
Expand Down
4 changes: 4 additions & 0 deletions pkg/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 pkg/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:"default_lookup_tags,omitempty" yaml:"default_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 pkg/file/zz_generated.deepcopy.go

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

0 comments on commit 14fe4d3

Please sign in to comment.