Skip to content

Commit

Permalink
feat: add default_lookup_tags support for routes
Browse files Browse the repository at this point in the history
  • Loading branch information
GGabriele committed Jan 22, 2024
1 parent 7722bba commit 22fde79
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 6 deletions.
24 changes: 22 additions & 2 deletions pkg/dump/dump.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,11 @@ type Config struct {
// tags.
SelectorTags []string

// LookUpSelectorTagsConsumers can be used to ensure state lookup for entities using
// LookUpSelectorTags* 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.
// consumers or routes associated with tags different from those in the sync command.
LookUpSelectorTagsConsumers []string
LookUpSelectorTagsRoutes []string

// KonnectControlPlane
KonnectControlPlane string
Expand Down Expand Up @@ -211,6 +212,25 @@ func getProxyConfiguration(ctx context.Context, group *errgroup.Group,
if err != nil {
return fmt.Errorf("routes: %w", err)
}
if config.LookUpSelectorTagsRoutes != nil {
globalRoutes, err := GetAllRoutes(ctx, client, config.LookUpSelectorTagsRoutes)
if err != nil {
return fmt.Errorf("error retrieving global routes: %w", err)
}
// if globalRoutes are not present, add them.
for _, globalRoute := range globalRoutes {
found := false
for _, route := range routes {
if *globalRoute.ID == *route.ID {
found = true
break
}
}
if !found {
routes = append(routes, globalRoute)
}
}
}
state.Routes = routes
return nil
})
Expand Down
16 changes: 16 additions & 0 deletions pkg/file/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ type stateBuilder struct {

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

Expand Down Expand Up @@ -1087,6 +1088,21 @@ func (b *stateBuilder) ingestRoute(r FRoute) error {
}
}

stringTags := make([]string, len(r.Tags))
for i, tag := range r.Tags {
if tag != nil {
stringTags[i] = *tag
}
}
sort.Strings(stringTags)
sort.Strings(b.lookupTagsRoutes)
// if the consumer tags and the lookup tags are the same, it means
// that the route is a global route retrieved from upstream,
// therefore we don't want to merge its tags with the selected tags.
if !reflect.DeepEqual(stringTags, b.lookupTagsRoutes) {
utils.MustMergeTags(&r.Route, b.selectTags)
}

utils.MustMergeTags(&r, b.selectTags)
stripPath, err := getStripPathBasedOnProtocols(r.Route)
if err != nil {
Expand Down
14 changes: 10 additions & 4 deletions pkg/file/kong_json_schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -1213,14 +1213,14 @@
},
"Info": {
"properties": {
"defaults": {
"$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"
},
"defaults": {
"$schema": "http://json-schema.org/draft-04/schema#",
"$ref": "#/definitions/KongDefaults"
},
"select_tags": {
"items": {
"type": "string"
Expand Down Expand Up @@ -1349,6 +1349,12 @@
"type": "string"
},
"type": "array"
},
"routes": {
"items": {
"type": "string"
},
"type": "array"
}
},
"additionalProperties": false,
Expand Down
4 changes: 4 additions & 0 deletions pkg/file/reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,10 @@ func Get(ctx context.Context, fileContent *Content, opt RenderConfig, dumpConfig
builder.lookupTagsConsumers = dumpConfig.LookUpSelectorTagsConsumers
}

if len(dumpConfig.LookUpSelectorTagsRoutes) > 0 {
builder.lookupTagsRoutes = dumpConfig.LookUpSelectorTagsRoutes
}

if fileContent.Transform != nil && !*fileContent.Transform {
return nil, ErrorTransformFalseNotSupported
}
Expand Down
1 change: 1 addition & 0 deletions pkg/file/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -607,6 +607,7 @@ type Info struct {
// +k8s:deepcopy-gen=true
type LookUpSelectorTags struct {
Consumers []string `json:"consumers,omitempty" yaml:"consumers,omitempty"`
Routes []string `json:"routes,omitempty" yaml:"routes,omitempty"`
}

// Konnect contains configuration specific to Konnect.
Expand Down
5 changes: 5 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 22fde79

Please sign in to comment.