diff --git a/controller/konnect/ops/ops_k8s_meta.go b/controller/konnect/ops/objects_metadata.go similarity index 69% rename from controller/konnect/ops/ops_k8s_meta.go rename to controller/konnect/ops/objects_metadata.go index c14f5750e..a183e2043 100644 --- a/controller/konnect/ops/ops_k8s_meta.go +++ b/controller/konnect/ops/objects_metadata.go @@ -2,9 +2,14 @@ package ops import ( "fmt" + "slices" + "sort" "github.com/samber/lo" - "sigs.k8s.io/controller-runtime/pkg/client" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "k8s.io/apimachinery/pkg/runtime/schema" + + "github.com/kong/kubernetes-configuration/pkg/metadata" ) const ( @@ -30,30 +35,29 @@ const ( KubernetesVersionLabelKey = "k8s-version" ) -// WithKubernetesMetadataLabels returns a map of user-provided labels to be assigned to a Konnect entity with the origin -// Kubernetes object's metadata added. These can be assigned to a Konnect entitiy that supports labels (e.g. ControlPlane). -func WithKubernetesMetadataLabels(obj client.Object, userSetLabels map[string]string) map[string]string { - labels := map[string]string{ - KubernetesNameLabelKey: obj.GetName(), - KubernetesUIDLabelKey: string(obj.GetUID()), - KubernetesGenerationLabelKey: fmt.Sprintf("%d", obj.GetGeneration()), - KubernetesKindLabelKey: obj.GetObjectKind().GroupVersionKind().Kind, - KubernetesGroupLabelKey: obj.GetObjectKind().GroupVersionKind().GroupVersion().Group, - KubernetesVersionLabelKey: obj.GetObjectKind().GroupVersionKind().GroupVersion().Version, - } - if k8sNamespace := obj.GetNamespace(); k8sNamespace != "" { - labels[KubernetesNamespaceLabelKey] = k8sNamespace - } - for k, v := range userSetLabels { - labels[k] = v - } - return labels +// ObjectWithMetadata is an interface that accepts an object with Kubernetes metadata and object Kind information. +type ObjectWithMetadata interface { + metav1.Object + GetObjectKind() schema.ObjectKind +} + +// GenerateTagsForObject generates tags for the given object based on its Kubernetes metadata and annotations. +// An optional set of tags can be passed to be included in the generated tags (e.g. tags from the spec). +// It returns a slice of unique, sorted strings for deterministic output. +func GenerateTagsForObject(obj ObjectWithMetadata, additionalTags ...string) []string { + var ( + annotationTags = metadata.ExtractTags(obj) + k8sMetaTags = generateKubernetesMetadataTags(obj) + res = lo.Uniq(slices.Concat(annotationTags, k8sMetaTags, additionalTags)) + ) + sort.Strings(res) + return res } -// GenerateKubernetesMetadataTags generates a list of tags from a Kubernetes object's metadata. The tags are formatted as +// generateKubernetesMetadataTags generates a list of tags from a Kubernetes object's metadata. The tags are formatted as // "key:value". These can be attached to a Konnect entity that doesn't support labels, but supports tags (e.g. Route, Service, // Consumer, etc.). -func GenerateKubernetesMetadataTags(obj client.Object) []string { +func generateKubernetesMetadataTags(obj ObjectWithMetadata) []string { // Use a list of Entry instead of a builtin map to preserve the order of the labels. labels := []lo.Entry[string, string]{ {Key: KubernetesGenerationLabelKey, Value: fmt.Sprintf("%d", obj.GetGeneration())}, @@ -72,3 +76,23 @@ func GenerateKubernetesMetadataTags(obj client.Object) []string { } return tags } + +// WithKubernetesMetadataLabels returns a map of user-provided labels to be assigned to a Konnect entity with the origin +// Kubernetes object's metadata added. These can be assigned to a Konnect entity that supports labels (e.g. ControlPlane). +func WithKubernetesMetadataLabels(obj ObjectWithMetadata, userSetLabels map[string]string) map[string]string { + labels := map[string]string{ + KubernetesNameLabelKey: obj.GetName(), + KubernetesUIDLabelKey: string(obj.GetUID()), + KubernetesGenerationLabelKey: fmt.Sprintf("%d", obj.GetGeneration()), + KubernetesKindLabelKey: obj.GetObjectKind().GroupVersionKind().Kind, + KubernetesGroupLabelKey: obj.GetObjectKind().GroupVersionKind().GroupVersion().Group, + KubernetesVersionLabelKey: obj.GetObjectKind().GroupVersionKind().GroupVersion().Version, + } + if k8sNamespace := obj.GetNamespace(); k8sNamespace != "" { + labels[KubernetesNamespaceLabelKey] = k8sNamespace + } + for k, v := range userSetLabels { + labels[k] = v + } + return labels +} diff --git a/controller/konnect/ops/ops_k8s_meta_test.go b/controller/konnect/ops/objects_metadata_test.go similarity index 59% rename from controller/konnect/ops/ops_k8s_meta_test.go rename to controller/konnect/ops/objects_metadata_test.go index 2fa80200e..fa0c0807b 100644 --- a/controller/konnect/ops/ops_k8s_meta_test.go +++ b/controller/konnect/ops/objects_metadata_test.go @@ -5,7 +5,6 @@ import ( "github.com/stretchr/testify/require" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" - "k8s.io/apimachinery/pkg/runtime" "github.com/kong/gateway-operator/controller/konnect/ops" ) @@ -16,10 +15,6 @@ type testObjectKind struct { metav1.ObjectMeta } -func (b *testObjectKind) DeepCopyObject() runtime.Object { - return b -} - func TestWithKubernetesMetadataLabels(t *testing.T) { testCases := []struct { name string @@ -82,63 +77,116 @@ func TestWithKubernetesMetadataLabels(t *testing.T) { } } -func TestGenerateKubernetesMetadataTags(t *testing.T) { +func TestGenerateTagsForObject(t *testing.T) { + namespacedObject := func() testObjectKind { + return testObjectKind{ + ObjectMeta: metav1.ObjectMeta{ + Name: "test-object", + Namespace: "test-namespace", + UID: "test-uid", + Generation: 2, + }, + TypeMeta: metav1.TypeMeta{ + Kind: "TestObjectKind", + APIVersion: "test.objects.io/v1", + }, + } + } + clusterScopedObject := func() testObjectKind { + return testObjectKind{ + ObjectMeta: metav1.ObjectMeta{ + Name: "test-object", + UID: "test-uid", + Generation: 2, + }, + TypeMeta: metav1.TypeMeta{ + Kind: "TestObjectKind", + APIVersion: "test.objects.io/v1", + }, + } + } + testCases := []struct { - name string - obj testObjectKind - expectedTags []string + name string + obj testObjectKind + additionalTags []string + expectedTags []string }{ { name: "all object's expected fields are set", - obj: testObjectKind{ - TypeMeta: metav1.TypeMeta{ - Kind: "TestObjectKind", - APIVersion: "test.objects.io/v1", - }, - ObjectMeta: metav1.ObjectMeta{ - Name: "test-object", - Namespace: "test-namespace", - UID: "test-uid", - Generation: 2, - }, - }, + obj: namespacedObject(), expectedTags: []string{ "k8s-generation:2", "k8s-group:test.objects.io", "k8s-kind:TestObjectKind", "k8s-name:test-object", + "k8s-namespace:test-namespace", "k8s-uid:test-uid", "k8s-version:v1", - "k8s-namespace:test-namespace", }, }, { name: "namespace is not set (cluster-scoped object)", - obj: testObjectKind{ - TypeMeta: metav1.TypeMeta{ - Kind: "TestObjectKind", - APIVersion: "test.objects.io/v1", - }, - ObjectMeta: metav1.ObjectMeta{ - Name: "test-object", - UID: "test-uid", - Generation: 2, - }, + obj: clusterScopedObject(), + expectedTags: []string{ + "k8s-generation:2", + "k8s-group:test.objects.io", + "k8s-kind:TestObjectKind", + "k8s-name:test-object", + "k8s-uid:test-uid", + "k8s-version:v1", }, + }, + { + name: "annotation tags are set", + obj: func() testObjectKind { + obj := namespacedObject() + obj.ObjectMeta.Annotations = map[string]string{ + "konghq.com/tags": "tag1,tag2", + } + return obj + }(), + expectedTags: []string{ + "k8s-generation:2", + "k8s-group:test.objects.io", + "k8s-kind:TestObjectKind", + "k8s-name:test-object", + "k8s-namespace:test-namespace", + "k8s-uid:test-uid", + "k8s-version:v1", + "tag1", + "tag2", + }, + }, + { + name: "additional tags are passed with a duplicate", + obj: func() testObjectKind { + obj := namespacedObject() + obj.ObjectMeta.Annotations = map[string]string{ + "konghq.com/tags": "tag1,tag2,duplicate-tag", + } + return obj + }(), + additionalTags: []string{"tag3", "duplicate-tag"}, expectedTags: []string{ + "duplicate-tag", "k8s-generation:2", "k8s-group:test.objects.io", "k8s-kind:TestObjectKind", "k8s-name:test-object", + "k8s-namespace:test-namespace", "k8s-uid:test-uid", "k8s-version:v1", + "tag1", + "tag2", + "tag3", }, }, } for _, tc := range testCases { t.Run(tc.name, func(t *testing.T) { - tags := ops.GenerateKubernetesMetadataTags(&tc.obj) + tags := ops.GenerateTagsForObject(&tc.obj, tc.additionalTags...) require.Equal(t, tc.expectedTags, tags) }) } diff --git a/controller/konnect/ops/ops_credentialacl.go b/controller/konnect/ops/ops_credentialacl.go index 29522e66a..9e0402435 100644 --- a/controller/konnect/ops/ops_credentialacl.go +++ b/controller/konnect/ops/ops_credentialacl.go @@ -4,7 +4,6 @@ import ( "context" "errors" "fmt" - "slices" sdkkonnectcomp "github.com/Kong/sdk-konnect-go/models/components" sdkkonnectops "github.com/Kong/sdk-konnect-go/models/operations" @@ -14,7 +13,6 @@ import ( ctrllog "sigs.k8s.io/controller-runtime/pkg/log" configurationv1alpha1 "github.com/kong/kubernetes-configuration/api/configuration/v1alpha1" - "github.com/kong/kubernetes-configuration/pkg/metadata" ) func createKongCredentialACL( @@ -149,16 +147,8 @@ func deleteKongCredentialACL( func kongCredentialACLToACLWithoutParents( cred *configurationv1alpha1.KongCredentialACL, ) sdkkonnectcomp.ACLWithoutParents { - var ( - specTags = cred.Spec.Tags - annotationTags = metadata.ExtractTags(cred) - k8sTags = GenerateKubernetesMetadataTags(cred) - ) - // Deduplicate tags to avoid rejection by Konnect. - tags := lo.Uniq(slices.Concat(specTags, annotationTags, k8sTags)) - return sdkkonnectcomp.ACLWithoutParents{ Group: lo.ToPtr(cred.Spec.Group), - Tags: tags, + Tags: GenerateTagsForObject(cred, cred.Spec.Tags...), } } diff --git a/controller/konnect/ops/ops_credentialapikey.go b/controller/konnect/ops/ops_credentialapikey.go index a1b3437aa..2bac554b4 100644 --- a/controller/konnect/ops/ops_credentialapikey.go +++ b/controller/konnect/ops/ops_credentialapikey.go @@ -4,7 +4,6 @@ import ( "context" "errors" "fmt" - "slices" sdkkonnectcomp "github.com/Kong/sdk-konnect-go/models/components" sdkkonnectops "github.com/Kong/sdk-konnect-go/models/operations" @@ -14,7 +13,6 @@ import ( ctrllog "sigs.k8s.io/controller-runtime/pkg/log" configurationv1alpha1 "github.com/kong/kubernetes-configuration/api/configuration/v1alpha1" - "github.com/kong/kubernetes-configuration/pkg/metadata" ) func createKongCredentialAPIKey( @@ -128,16 +126,8 @@ func deleteKongCredentialAPIKey( func kongCredentialAPIKeyToKeyAuthWithoutParents( cred *configurationv1alpha1.KongCredentialAPIKey, ) sdkkonnectcomp.KeyAuthWithoutParents { - var ( - specTags = cred.Spec.Tags - annotationTags = metadata.ExtractTags(cred) - k8sTags = GenerateKubernetesMetadataTags(cred) - ) - // Deduplicate tags to avoid rejection by Konnect. - tags := lo.Uniq(slices.Concat(specTags, annotationTags, k8sTags)) - return sdkkonnectcomp.KeyAuthWithoutParents{ Key: lo.ToPtr(cred.Spec.Key), - Tags: tags, + Tags: GenerateTagsForObject(cred, cred.Spec.Tags...), } } diff --git a/controller/konnect/ops/ops_credentialbasicauth.go b/controller/konnect/ops/ops_credentialbasicauth.go index 8f140fdb7..82cd4324a 100644 --- a/controller/konnect/ops/ops_credentialbasicauth.go +++ b/controller/konnect/ops/ops_credentialbasicauth.go @@ -4,7 +4,6 @@ import ( "context" "errors" "fmt" - "slices" sdkkonnectcomp "github.com/Kong/sdk-konnect-go/models/components" sdkkonnectops "github.com/Kong/sdk-konnect-go/models/operations" @@ -14,7 +13,6 @@ import ( ctrllog "sigs.k8s.io/controller-runtime/pkg/log" configurationv1alpha1 "github.com/kong/kubernetes-configuration/api/configuration/v1alpha1" - "github.com/kong/kubernetes-configuration/pkg/metadata" ) func createKongCredentialBasicAuth( @@ -127,17 +125,9 @@ func deleteKongCredentialBasicAuth( func kongCredentialBasicAuthToBasicAuthWithoutParents( cred *configurationv1alpha1.KongCredentialBasicAuth, ) sdkkonnectcomp.BasicAuthWithoutParents { - var ( - specTags = cred.Spec.Tags - annotationTags = metadata.ExtractTags(cred) - k8sTags = GenerateKubernetesMetadataTags(cred) - ) - // Deduplicate tags to avoid rejection by Konnect. - tags := lo.Uniq(slices.Concat(specTags, annotationTags, k8sTags)) - return sdkkonnectcomp.BasicAuthWithoutParents{ Password: lo.ToPtr(cred.Spec.Password), Username: lo.ToPtr(cred.Spec.Username), - Tags: tags, + Tags: GenerateTagsForObject(cred, cred.Spec.Tags...), } } diff --git a/controller/konnect/ops/ops_kongcacertificate.go b/controller/konnect/ops/ops_kongcacertificate.go index e5c2a5c55..f950c01f1 100644 --- a/controller/konnect/ops/ops_kongcacertificate.go +++ b/controller/konnect/ops/ops_kongcacertificate.go @@ -4,17 +4,14 @@ import ( "context" "errors" "fmt" - "slices" sdkkonnectcomp "github.com/Kong/sdk-konnect-go/models/components" sdkkonnectops "github.com/Kong/sdk-konnect-go/models/operations" sdkkonnecterrs "github.com/Kong/sdk-konnect-go/models/sdkerrors" - "github.com/samber/lo" "sigs.k8s.io/controller-runtime/pkg/client" ctrllog "sigs.k8s.io/controller-runtime/pkg/log" configurationv1alpha1 "github.com/kong/kubernetes-configuration/api/configuration/v1alpha1" - "github.com/kong/kubernetes-configuration/pkg/metadata" ) // createCACertificate creates a KongCACertificate in Konnect. @@ -117,14 +114,9 @@ func deleteCACertificate( } func kongCACertificateToCACertificateInput(cert *configurationv1alpha1.KongCACertificate) sdkkonnectcomp.CACertificateInput { - var ( - annotationTags = metadata.ExtractTags(cert) - specTags = cert.Spec.Tags - k8sMetaTags = GenerateKubernetesMetadataTags(cert) - ) return sdkkonnectcomp.CACertificateInput{ Cert: cert.Spec.Cert, // Deduplicate tags to avoid rejection by Konnect. - Tags: lo.Uniq(slices.Concat(annotationTags, specTags, k8sMetaTags)), + Tags: GenerateTagsForObject(cert, cert.Spec.Tags...), } } diff --git a/controller/konnect/ops/ops_kongcertificate.go b/controller/konnect/ops/ops_kongcertificate.go index 451f17a92..cf03c6743 100644 --- a/controller/konnect/ops/ops_kongcertificate.go +++ b/controller/konnect/ops/ops_kongcertificate.go @@ -4,17 +4,14 @@ import ( "context" "errors" "fmt" - "slices" sdkkonnectcomp "github.com/Kong/sdk-konnect-go/models/components" sdkkonnectops "github.com/Kong/sdk-konnect-go/models/operations" sdkkonnecterrs "github.com/Kong/sdk-konnect-go/models/sdkerrors" - "github.com/samber/lo" "sigs.k8s.io/controller-runtime/pkg/client" ctrllog "sigs.k8s.io/controller-runtime/pkg/log" configurationv1alpha1 "github.com/kong/kubernetes-configuration/api/configuration/v1alpha1" - "github.com/kong/kubernetes-configuration/pkg/metadata" ) // createCertificate creates a KongCertificate in Konnect. @@ -138,15 +135,9 @@ func deleteCertificate( } func kongCertificateToCertificateInput(cert *configurationv1alpha1.KongCertificate) sdkkonnectcomp.CertificateInput { - var ( - annotationTags = metadata.ExtractTags(cert) - specTags = cert.Spec.Tags - k8sMetaTags = GenerateKubernetesMetadataTags(cert) - ) return sdkkonnectcomp.CertificateInput{ Cert: cert.Spec.Cert, Key: cert.Spec.Key, - // Deduplicate tags to avoid rejection by Konnect. - Tags: lo.Uniq(slices.Concat(annotationTags, specTags, k8sMetaTags)), + Tags: GenerateTagsForObject(cert, cert.Spec.Tags...), } } diff --git a/controller/konnect/ops/ops_kongconsumer.go b/controller/konnect/ops/ops_kongconsumer.go index e50871573..890e9818c 100644 --- a/controller/konnect/ops/ops_kongconsumer.go +++ b/controller/konnect/ops/ops_kongconsumer.go @@ -21,7 +21,6 @@ import ( configurationv1 "github.com/kong/kubernetes-configuration/api/configuration/v1" configurationv1beta1 "github.com/kong/kubernetes-configuration/api/configuration/v1beta1" - "github.com/kong/kubernetes-configuration/pkg/metadata" ) func createConsumer( @@ -329,7 +328,7 @@ func kongConsumerToSDKConsumerInput( ) sdkkonnectcomp.ConsumerInput { return sdkkonnectcomp.ConsumerInput{ CustomID: &consumer.CustomID, - Tags: append(metadata.ExtractTags(consumer), GenerateKubernetesMetadataTags(consumer)...), + Tags: GenerateTagsForObject(consumer), Username: &consumer.Username, } } diff --git a/controller/konnect/ops/ops_kongconsumergroup.go b/controller/konnect/ops/ops_kongconsumergroup.go index 7f46bcfc2..0bf2b7371 100644 --- a/controller/konnect/ops/ops_kongconsumergroup.go +++ b/controller/konnect/ops/ops_kongconsumergroup.go @@ -12,7 +12,6 @@ import ( ctrllog "sigs.k8s.io/controller-runtime/pkg/log" configurationv1beta1 "github.com/kong/kubernetes-configuration/api/configuration/v1beta1" - "github.com/kong/kubernetes-configuration/pkg/metadata" ) func createConsumerGroup( @@ -116,7 +115,7 @@ func kongConsumerGroupToSDKConsumerGroupInput( group *configurationv1beta1.KongConsumerGroup, ) sdkkonnectcomp.ConsumerGroupInput { return sdkkonnectcomp.ConsumerGroupInput{ - Tags: append(metadata.ExtractTags(group), GenerateKubernetesMetadataTags(group)...), + Tags: GenerateTagsForObject(group), Name: group.Spec.Name, } } diff --git a/controller/konnect/ops/ops_kongkey.go b/controller/konnect/ops/ops_kongkey.go index d3fa8eace..58eeb06ac 100644 --- a/controller/konnect/ops/ops_kongkey.go +++ b/controller/konnect/ops/ops_kongkey.go @@ -4,7 +4,6 @@ import ( "context" "errors" "fmt" - "slices" sdkkonnectcomp "github.com/Kong/sdk-konnect-go/models/components" sdkkonnectops "github.com/Kong/sdk-konnect-go/models/operations" @@ -14,7 +13,6 @@ import ( ctrllog "sigs.k8s.io/controller-runtime/pkg/log" configurationv1alpha1 "github.com/kong/kubernetes-configuration/api/configuration/v1alpha1" - "github.com/kong/kubernetes-configuration/pkg/metadata" ) // createKey creates a KongKey in Konnect. @@ -133,17 +131,11 @@ func deleteKey( } func kongKeyToKeyInput(key *configurationv1alpha1.KongKey) sdkkonnectcomp.KeyInput { - var ( - annotationTags = metadata.ExtractTags(key) - specTags = key.Spec.Tags - k8sMetaTags = GenerateKubernetesMetadataTags(key) - ) k := sdkkonnectcomp.KeyInput{ Jwk: key.Spec.JWK, Kid: key.Spec.KID, Name: key.Spec.Name, - // Deduplicate tags to avoid rejection by Konnect. - Tags: lo.Uniq(slices.Concat(annotationTags, specTags, k8sMetaTags)), + Tags: GenerateTagsForObject(key, key.Spec.Tags...), } if key.Spec.PEM != nil { k.Pem = &sdkkonnectcomp.Pem{ @@ -151,5 +143,12 @@ func kongKeyToKeyInput(key *configurationv1alpha1.KongKey) sdkkonnectcomp.KeyInp PublicKey: lo.ToPtr(key.Spec.PEM.PublicKey), } } + if konnectStatus := key.Status.Konnect; konnectStatus != nil { + if keySetID := konnectStatus.GetKeySetID(); keySetID != "" { + k.Set = &sdkkonnectcomp.Set{ + ID: lo.ToPtr(konnectStatus.GetKeySetID()), + } + } + } return k } diff --git a/controller/konnect/ops/ops_kongkeyset.go b/controller/konnect/ops/ops_kongkeyset.go index 86f9c7f5a..fbf568948 100644 --- a/controller/konnect/ops/ops_kongkeyset.go +++ b/controller/konnect/ops/ops_kongkeyset.go @@ -4,7 +4,6 @@ import ( "context" "errors" "fmt" - "slices" sdkkonnectcomp "github.com/Kong/sdk-konnect-go/models/components" sdkkonnectops "github.com/Kong/sdk-konnect-go/models/operations" @@ -14,7 +13,6 @@ import ( ctrllog "sigs.k8s.io/controller-runtime/pkg/log" configurationv1alpha1 "github.com/kong/kubernetes-configuration/api/configuration/v1alpha1" - "github.com/kong/kubernetes-configuration/pkg/metadata" ) // createKeySet creates a KongKeySet in Konnect. @@ -133,14 +131,8 @@ func deleteKeySet( } func kongKeySetToKeySetInput(keySet *configurationv1alpha1.KongKeySet) sdkkonnectcomp.KeySetInput { - var ( - annotationTags = metadata.ExtractTags(keySet) - specTags = keySet.Spec.Tags - k8sMetaTags = GenerateKubernetesMetadataTags(keySet) - ) return sdkkonnectcomp.KeySetInput{ Name: lo.ToPtr(keySet.Spec.Name), - // Deduplicate tags to avoid rejection by Konnect. - Tags: lo.Uniq(slices.Concat(annotationTags, specTags, k8sMetaTags)), + Tags: GenerateTagsForObject(keySet, keySet.Spec.Tags...), } } diff --git a/controller/konnect/ops/ops_kongpluginbinding.go b/controller/konnect/ops/ops_kongpluginbinding.go index da9e1d409..ad3f8e867 100644 --- a/controller/konnect/ops/ops_kongpluginbinding.go +++ b/controller/konnect/ops/ops_kongpluginbinding.go @@ -5,7 +5,6 @@ import ( "encoding/json" "errors" "fmt" - "slices" sdkkonnectcomp "github.com/Kong/sdk-konnect-go/models/components" sdkkonnectops "github.com/Kong/sdk-konnect-go/models/operations" @@ -152,14 +151,7 @@ func kongPluginBindingToSDKPluginInput( return nil, err } - var ( - pluginBindingAnnotationTags = metadata.ExtractTags(pluginBinding) - pluginAnnotationTags = metadata.ExtractTags(plugin) - pluginBindingK8sTags = GenerateKubernetesMetadataTags(pluginBinding) - ) - // Deduplicate tags to avoid rejection by Konnect. - tags := lo.Uniq(slices.Concat(pluginBindingAnnotationTags, pluginAnnotationTags, pluginBindingK8sTags)) - + tags := GenerateTagsForObject(pluginBinding, metadata.ExtractTags(plugin)...) return kongPluginWithTargetsToKongPluginInput(plugin, targets, tags) } diff --git a/controller/konnect/ops/ops_kongroute.go b/controller/konnect/ops/ops_kongroute.go index dd20a652c..12c7bff8e 100644 --- a/controller/konnect/ops/ops_kongroute.go +++ b/controller/konnect/ops/ops_kongroute.go @@ -4,18 +4,15 @@ import ( "context" "errors" "fmt" - "slices" sdkkonnectgo "github.com/Kong/sdk-konnect-go" sdkkonnectcomp "github.com/Kong/sdk-konnect-go/models/components" sdkkonnectops "github.com/Kong/sdk-konnect-go/models/operations" sdkkonnecterrs "github.com/Kong/sdk-konnect-go/models/sdkerrors" - "github.com/samber/lo" "sigs.k8s.io/controller-runtime/pkg/client" ctrllog "sigs.k8s.io/controller-runtime/pkg/log" configurationv1alpha1 "github.com/kong/kubernetes-configuration/api/configuration/v1alpha1" - "github.com/kong/kubernetes-configuration/pkg/metadata" ) func createRoute( @@ -114,14 +111,6 @@ func deleteRoute( func kongRouteToSDKRouteInput( route *configurationv1alpha1.KongRoute, ) sdkkonnectcomp.RouteInput { - var ( - specTags = route.Spec.KongRouteAPISpec.Tags - annotationTags = metadata.ExtractTags(route) - k8sTags = GenerateKubernetesMetadataTags(route) - ) - // Deduplicate tags to avoid rejection by Konnect. - tags := lo.Uniq(slices.Concat(specTags, annotationTags, k8sTags)) - r := sdkkonnectcomp.RouteInput{ Destinations: route.Spec.KongRouteAPISpec.Destinations, Headers: route.Spec.KongRouteAPISpec.Headers, @@ -139,7 +128,7 @@ func kongRouteToSDKRouteInput( Snis: route.Spec.KongRouteAPISpec.Snis, Sources: route.Spec.KongRouteAPISpec.Sources, StripPath: route.Spec.KongRouteAPISpec.StripPath, - Tags: tags, + Tags: GenerateTagsForObject(route, route.Spec.KongRouteAPISpec.Tags...), } if route.Status.Konnect != nil && route.Status.Konnect.ServiceID != "" { r.Service = &sdkkonnectcomp.RouteService{ diff --git a/controller/konnect/ops/ops_kongservice.go b/controller/konnect/ops/ops_kongservice.go index 15ddcbb2d..8d17864c2 100644 --- a/controller/konnect/ops/ops_kongservice.go +++ b/controller/konnect/ops/ops_kongservice.go @@ -4,17 +4,14 @@ import ( "context" "errors" "fmt" - "slices" sdkkonnectcomp "github.com/Kong/sdk-konnect-go/models/components" sdkkonnectops "github.com/Kong/sdk-konnect-go/models/operations" sdkkonnecterrs "github.com/Kong/sdk-konnect-go/models/sdkerrors" - "github.com/samber/lo" "sigs.k8s.io/controller-runtime/pkg/client" ctrllog "sigs.k8s.io/controller-runtime/pkg/log" configurationv1alpha1 "github.com/kong/kubernetes-configuration/api/configuration/v1alpha1" - "github.com/kong/kubernetes-configuration/pkg/metadata" ) func createService( @@ -147,14 +144,6 @@ func deleteService( func kongServiceToSDKServiceInput( svc *configurationv1alpha1.KongService, ) sdkkonnectcomp.ServiceInput { - var ( - specTags = svc.Spec.Tags - annotationTags = metadata.ExtractTags(svc) - k8sTags = GenerateKubernetesMetadataTags(svc) - ) - // Deduplicate tags to avoid rejection by Konnect. - tags := lo.Uniq(slices.Concat(specTags, annotationTags, k8sTags)) - return sdkkonnectcomp.ServiceInput{ URL: svc.Spec.KongServiceAPISpec.URL, ConnectTimeout: svc.Spec.KongServiceAPISpec.ConnectTimeout, @@ -166,7 +155,7 @@ func kongServiceToSDKServiceInput( Protocol: svc.Spec.KongServiceAPISpec.Protocol, ReadTimeout: svc.Spec.KongServiceAPISpec.ReadTimeout, Retries: svc.Spec.KongServiceAPISpec.Retries, - Tags: tags, + Tags: GenerateTagsForObject(svc, svc.Spec.KongServiceAPISpec.Tags...), TLSVerify: svc.Spec.KongServiceAPISpec.TLSVerify, TLSVerifyDepth: svc.Spec.KongServiceAPISpec.TLSVerifyDepth, WriteTimeout: svc.Spec.KongServiceAPISpec.WriteTimeout, diff --git a/controller/konnect/ops/ops_kongsni.go b/controller/konnect/ops/ops_kongsni.go index 2bedd1c5d..4977dec56 100644 --- a/controller/konnect/ops/ops_kongsni.go +++ b/controller/konnect/ops/ops_kongsni.go @@ -5,17 +5,14 @@ import ( "errors" "fmt" "net/http" - "slices" sdkkonnectcomp "github.com/Kong/sdk-konnect-go/models/components" sdkkonnectops "github.com/Kong/sdk-konnect-go/models/operations" sdkkonnecterrs "github.com/Kong/sdk-konnect-go/models/sdkerrors" - "github.com/samber/lo" "sigs.k8s.io/controller-runtime/pkg/client" ctrllog "sigs.k8s.io/controller-runtime/pkg/log" configurationv1alpha1 "github.com/kong/kubernetes-configuration/api/configuration/v1alpha1" - "github.com/kong/kubernetes-configuration/pkg/metadata" ) func createSNI( @@ -149,16 +146,8 @@ func deleteSNI( } func kongSNIToSNIWithoutParents(sni *configurationv1alpha1.KongSNI) sdkkonnectcomp.SNIWithoutParents { - var ( - specTags = sni.Spec.Tags - annotationTags = metadata.ExtractTags(sni) - k8sTags = GenerateKubernetesMetadataTags(sni) - ) - // Deduplicate tags to avoid rejection by Konnect. - tags := lo.Uniq(slices.Concat(specTags, annotationTags, k8sTags)) - return sdkkonnectcomp.SNIWithoutParents{ Name: sni.Spec.Name, - Tags: tags, + Tags: GenerateTagsForObject(sni, sni.Spec.Tags...), } } diff --git a/controller/konnect/ops/ops_kongtarget.go b/controller/konnect/ops/ops_kongtarget.go index 53bd97812..b6dedfc8b 100644 --- a/controller/konnect/ops/ops_kongtarget.go +++ b/controller/konnect/ops/ops_kongtarget.go @@ -5,7 +5,6 @@ import ( "errors" "fmt" "net/http" - "slices" sdkkonnectcomp "github.com/Kong/sdk-konnect-go/models/components" sdkkonnectops "github.com/Kong/sdk-konnect-go/models/operations" @@ -15,7 +14,6 @@ import ( ctrllog "sigs.k8s.io/controller-runtime/pkg/log" configurationv1alpha1 "github.com/kong/kubernetes-configuration/api/configuration/v1alpha1" - "github.com/kong/kubernetes-configuration/pkg/metadata" ) func createTarget( @@ -123,17 +121,9 @@ func deleteTarget( } func kongTargetToTargetWithoutParents(target *configurationv1alpha1.KongTarget) sdkkonnectcomp.TargetWithoutParents { - var ( - specTags = target.Spec.KongTargetAPISpec.Tags - annotationTags = metadata.ExtractTags(target) - k8sTags = GenerateKubernetesMetadataTags(target) - ) - // Deduplicate tags to avoid rejection by Konnect. - tags := lo.Uniq(slices.Concat(specTags, annotationTags, k8sTags)) - return sdkkonnectcomp.TargetWithoutParents{ Target: lo.ToPtr(target.Spec.Target), Weight: lo.ToPtr(int64(target.Spec.Weight)), - Tags: tags, + Tags: GenerateTagsForObject(target, target.Spec.Tags...), } } diff --git a/controller/konnect/ops/ops_kongupstream.go b/controller/konnect/ops/ops_kongupstream.go index 6eebb369b..18d2193f0 100644 --- a/controller/konnect/ops/ops_kongupstream.go +++ b/controller/konnect/ops/ops_kongupstream.go @@ -4,17 +4,14 @@ import ( "context" "errors" "fmt" - "slices" sdkkonnectcomp "github.com/Kong/sdk-konnect-go/models/components" sdkkonnectops "github.com/Kong/sdk-konnect-go/models/operations" sdkkonnecterrs "github.com/Kong/sdk-konnect-go/models/sdkerrors" - "github.com/samber/lo" "sigs.k8s.io/controller-runtime/pkg/client" ctrllog "sigs.k8s.io/controller-runtime/pkg/log" configurationv1alpha1 "github.com/kong/kubernetes-configuration/api/configuration/v1alpha1" - "github.com/kong/kubernetes-configuration/pkg/metadata" ) func createUpstream( @@ -147,14 +144,6 @@ func deleteUpstream( func kongUpstreamToSDKUpstreamInput( upstream *configurationv1alpha1.KongUpstream, ) sdkkonnectcomp.UpstreamInput { - var ( - specTags = upstream.Spec.Tags - annotationTags = metadata.ExtractTags(upstream) - k8sTags = GenerateKubernetesMetadataTags(upstream) - ) - // Deduplicate tags to avoid rejection by Konnect. - tags := lo.Uniq(slices.Concat(specTags, annotationTags, k8sTags)) - return sdkkonnectcomp.UpstreamInput{ Algorithm: upstream.Spec.Algorithm, ClientCertificate: upstream.Spec.ClientCertificate, @@ -172,7 +161,7 @@ func kongUpstreamToSDKUpstreamInput( HostHeader: upstream.Spec.HostHeader, Name: upstream.Spec.Name, Slots: upstream.Spec.Slots, - Tags: tags, + Tags: GenerateTagsForObject(upstream, upstream.Spec.Tags...), UseSrvName: upstream.Spec.UseSrvName, } } diff --git a/controller/konnect/ops/ops_kongvault.go b/controller/konnect/ops/ops_kongvault.go index bcf88fecc..ef15ab1ce 100644 --- a/controller/konnect/ops/ops_kongvault.go +++ b/controller/konnect/ops/ops_kongvault.go @@ -5,7 +5,6 @@ import ( "encoding/json" "errors" "fmt" - "slices" sdkkonnectcomp "github.com/Kong/sdk-konnect-go/models/components" sdkkonnectops "github.com/Kong/sdk-konnect-go/models/operations" @@ -15,7 +14,6 @@ import ( ctrllog "sigs.k8s.io/controller-runtime/pkg/log" configurationv1alpha1 "github.com/kong/kubernetes-configuration/api/configuration/v1alpha1" - "github.com/kong/kubernetes-configuration/pkg/metadata" ) func createVault(ctx context.Context, sdk VaultSDK, vault *configurationv1alpha1.KongVault) error { @@ -131,23 +129,16 @@ func deleteVault(ctx context.Context, sdk VaultSDK, vault *configurationv1alpha1 } func kongVaultToVaultInput(vault *configurationv1alpha1.KongVault) (sdkkonnectcomp.VaultInput, error) { - var ( - specTags = vault.Spec.Tags - annotationTags = metadata.ExtractTags(vault) - k8sTags = GenerateKubernetesMetadataTags(vault) - ) vaultConfig := map[string]any{} err := json.Unmarshal(vault.Spec.Config.Raw, &vaultConfig) if err != nil { return sdkkonnectcomp.VaultInput{}, err } - // Deduplicate tags to avoid rejection by Konnect. - tags := lo.Uniq(slices.Concat(specTags, annotationTags, k8sTags)) input := sdkkonnectcomp.VaultInput{ Config: vaultConfig, Name: vault.Spec.Backend, Prefix: vault.Spec.Prefix, - Tags: tags, + Tags: GenerateTagsForObject(vault, vault.Spec.Tags...), } if vault.Spec.Description != "" { input.Description = lo.ToPtr(vault.Spec.Description) diff --git a/test/envtest/kongconsumercredential_acl_test.go b/test/envtest/kongconsumercredential_acl_test.go index c28ee7495..e4406a4d4 100644 --- a/test/envtest/kongconsumercredential_acl_test.go +++ b/test/envtest/kongconsumercredential_acl_test.go @@ -73,9 +73,9 @@ func TestKongConsumerCredential_ACL(t *testing.T) { "k8s-group:configuration.konghq.com", "k8s-kind:KongCredentialACL", "k8s-name:" + KongCredentialACL.Name, + "k8s-namespace:" + ns.Name, "k8s-uid:" + string(KongCredentialACL.GetUID()), "k8s-version:v1alpha1", - "k8s-namespace:" + ns.Name, } factory := ops.NewMockSDKFactory(t) diff --git a/test/envtest/kongconsumercredential_basicauth_test.go b/test/envtest/kongconsumercredential_basicauth_test.go index 940f17492..3906e400b 100644 --- a/test/envtest/kongconsumercredential_basicauth_test.go +++ b/test/envtest/kongconsumercredential_basicauth_test.go @@ -74,9 +74,9 @@ func TestKongConsumerCredential_BasicAuth(t *testing.T) { "k8s-group:configuration.konghq.com", "k8s-kind:KongCredentialBasicAuth", "k8s-name:" + KongCredentialBasicAuth.Name, + "k8s-namespace:" + ns.Name, "k8s-uid:" + string(KongCredentialBasicAuth.GetUID()), "k8s-version:v1alpha1", - "k8s-namespace:" + ns.Name, } factory := ops.NewMockSDKFactory(t)