From 22fde790721c88fffa8594c490082738edfa3209 Mon Sep 17 00:00:00 2001 From: Gabriele Gerbino Date: Fri, 19 Jan 2024 20:32:32 +0100 Subject: [PATCH] feat: add default_lookup_tags support for routes --- pkg/dump/dump.go | 24 ++++++++++++++++++++++-- pkg/file/builder.go | 16 ++++++++++++++++ pkg/file/kong_json_schema.json | 14 ++++++++++---- pkg/file/reader.go | 4 ++++ pkg/file/types.go | 1 + pkg/file/zz_generated.deepcopy.go | 5 +++++ 6 files changed, 58 insertions(+), 6 deletions(-) diff --git a/pkg/dump/dump.go b/pkg/dump/dump.go index 2217f0d..09db796 100644 --- a/pkg/dump/dump.go +++ b/pkg/dump/dump.go @@ -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 @@ -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 }) diff --git a/pkg/file/builder.go b/pkg/file/builder.go index 92d16e4..bce525c 100644 --- a/pkg/file/builder.go +++ b/pkg/file/builder.go @@ -26,6 +26,7 @@ type stateBuilder struct { selectTags []string lookupTagsConsumers []string + lookupTagsRoutes []string skipCACerts bool intermediate *state.KongState @@ -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 { diff --git a/pkg/file/kong_json_schema.json b/pkg/file/kong_json_schema.json index 64c4687..13fa79c 100644 --- a/pkg/file/kong_json_schema.json +++ b/pkg/file/kong_json_schema.json @@ -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" @@ -1349,6 +1349,12 @@ "type": "string" }, "type": "array" + }, + "routes": { + "items": { + "type": "string" + }, + "type": "array" } }, "additionalProperties": false, diff --git a/pkg/file/reader.go b/pkg/file/reader.go index c7ac57d..d73f9be 100644 --- a/pkg/file/reader.go +++ b/pkg/file/reader.go @@ -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 } diff --git a/pkg/file/types.go b/pkg/file/types.go index 8b8bfc2..09fa016 100644 --- a/pkg/file/types.go +++ b/pkg/file/types.go @@ -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. diff --git a/pkg/file/zz_generated.deepcopy.go b/pkg/file/zz_generated.deepcopy.go index 00a9086..a7ab0b1 100644 --- a/pkg/file/zz_generated.deepcopy.go +++ b/pkg/file/zz_generated.deepcopy.go @@ -835,6 +835,11 @@ func (in *LookUpSelectorTags) DeepCopyInto(out *LookUpSelectorTags) { *out = make([]string, len(*in)) copy(*out, *in) } + if in.Routes != nil { + in, out := &in.Routes, &out.Routes + *out = make([]string, len(*in)) + copy(*out, *in) + } return }