From 0c60a1aed9c75052ffa041a6b80d87c1f4af6a6b Mon Sep 17 00:00:00 2001 From: Mirza Kopic Date: Tue, 7 Apr 2026 16:58:17 +0200 Subject: [PATCH 1/5] feat(konnector): annotate synced objects with source namespace and UID --- .../claimedresources_reconciler.go | 19 +++ .../claimedresources_reconciler_test.go | 110 ++++++++++++++++++ .../serviceexport/spec/spec_reconcile.go | 19 +++ .../serviceexport/spec/spec_reconcile_test.go | 41 +++++++ pkg/konnector/types/types.go | 16 +++ test/e2e/bind/happy-case_test.go | 6 + 6 files changed, 211 insertions(+) create mode 100644 pkg/konnector/controllers/cluster/claimedresources/claimedresources_reconciler_test.go diff --git a/pkg/konnector/controllers/cluster/claimedresources/claimedresources_reconciler.go b/pkg/konnector/controllers/cluster/claimedresources/claimedresources_reconciler.go index 294085453..2a30c7b2c 100644 --- a/pkg/konnector/controllers/cluster/claimedresources/claimedresources_reconciler.go +++ b/pkg/konnector/controllers/cluster/claimedresources/claimedresources_reconciler.go @@ -28,6 +28,7 @@ import ( "k8s.io/apimachinery/pkg/util/runtime" "k8s.io/klog/v2" + konnectortypes "github.com/kube-bind/kube-bind/pkg/konnector/types" kubebindv1alpha2 "github.com/kube-bind/kube-bind/sdk/apis/kubebind/v1alpha2" ) @@ -117,6 +118,8 @@ func (r *readReconciler) reconcile(ctx context.Context, providerNamespace, name logger.Info("Creating missing consumer object", "consumerNamespace", consumerNS, "consumerName", providerObj.GetName()) candidate := candidateFromOwnerObj(consumerNS, providerObj) + setSourceMetadataAnnotations(candidate, providerObj.GetNamespace(), string(providerObj.GetUID()), + konnectortypes.ProviderNamespaceAnnotationKey, konnectortypes.ProviderUIDAnnotationKey) r.makeProviderOwner(candidate) if _, err := r.createConsumerObject(ctx, candidate); err != nil { @@ -132,6 +135,8 @@ func (r *readReconciler) reconcile(ctx context.Context, providerNamespace, name } candidate := candidateFromOwnerObj(consumerNS, providerObj) + setSourceMetadataAnnotations(candidate, providerObj.GetNamespace(), string(providerObj.GetUID()), + konnectortypes.ProviderNamespaceAnnotationKey, konnectortypes.ProviderUIDAnnotationKey) current := candidateFromOwnerObj(consumerNS, consumerObj) if !equality.Semantic.DeepEqual(candidate, current) { logger.Info("Updating consumer object data", "consumerNamespace", consumerNS, "consumerName", consumerObj.GetName()) @@ -161,6 +166,8 @@ func (r *readReconciler) reconcile(ctx context.Context, providerNamespace, name } candidate := candidateFromOwnerObj(providerNamespace, ownerCandidate) + setSourceMetadataAnnotations(candidate, ownerCandidate.GetNamespace(), string(ownerCandidate.GetUID()), + konnectortypes.ConsumerNamespaceAnnotationKey, konnectortypes.ConsumerUIDAnnotationKey) r.makeConsumerOwner(candidate) if errors.IsNotFound(providerErr) { @@ -231,6 +238,18 @@ func candidateFromOwnerObj(downstreamNS string, obj *unstructured.Unstructured) return candidate } +// setSourceMetadataAnnotations sets source metadata annotations on a synced +// object so the receiving side can trace where the object came from. +func setSourceMetadataAnnotations(candidate *unstructured.Unstructured, sourceNS, sourceUID, nsKey, uidKey string) { + annotations := candidate.GetAnnotations() + if annotations == nil { + annotations = map[string]string{} + } + annotations[nsKey] = sourceNS + annotations[uidKey] = sourceUID + candidate.SetAnnotations(annotations) +} + // determineOwner determines the owner of a resource given at least one object exists either on the // consumer or provider side. func determineOwner(providerObj, consumerObj *unstructured.Unstructured) (kubebindv1alpha2.Owner, error) { diff --git a/pkg/konnector/controllers/cluster/claimedresources/claimedresources_reconciler_test.go b/pkg/konnector/controllers/cluster/claimedresources/claimedresources_reconciler_test.go new file mode 100644 index 000000000..f3ea17827 --- /dev/null +++ b/pkg/konnector/controllers/cluster/claimedresources/claimedresources_reconciler_test.go @@ -0,0 +1,110 @@ +/* +Copyright 2026 The Kube Bind Authors. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package claimedresources + +import ( + "testing" + + "github.com/stretchr/testify/require" + "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" + + konnectortypes "github.com/kube-bind/kube-bind/pkg/konnector/types" +) + +func TestSetSourceMetadataAnnotations(t *testing.T) { + tests := []struct { + name string + obj *unstructured.Unstructured + sourceNS string + sourceUID string + nsKey string + uidKey string + }{ + { + name: "provider annotations on empty object", + obj: &unstructured.Unstructured{}, + sourceNS: "provider-ns", + sourceUID: "provider-uid-123", + nsKey: konnectortypes.ProviderNamespaceAnnotationKey, + uidKey: konnectortypes.ProviderUIDAnnotationKey, + }, + { + name: "consumer annotations on empty object", + obj: &unstructured.Unstructured{}, + sourceNS: "consumer-ns", + sourceUID: "consumer-uid-456", + nsKey: konnectortypes.ConsumerNamespaceAnnotationKey, + uidKey: konnectortypes.ConsumerUIDAnnotationKey, + }, + { + name: "preserves existing annotations", + obj: func() *unstructured.Unstructured { + obj := &unstructured.Unstructured{} + obj.SetAnnotations(map[string]string{ + "existing-key": "existing-value", + }) + return obj + }(), + sourceNS: "my-ns", + sourceUID: "my-uid", + nsKey: konnectortypes.ProviderNamespaceAnnotationKey, + uidKey: konnectortypes.ProviderUIDAnnotationKey, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + t.Parallel() + + setSourceMetadataAnnotations(tt.obj, tt.sourceNS, tt.sourceUID, tt.nsKey, tt.uidKey) + + annotations := tt.obj.GetAnnotations() + require.Equal(t, tt.sourceNS, annotations[tt.nsKey]) + require.Equal(t, tt.sourceUID, annotations[tt.uidKey]) + + // verify existing annotations are preserved + if tt.name == "preserves existing annotations" { + require.Equal(t, "existing-value", annotations["existing-key"]) + } + }) + } +} + +func TestCandidateFromOwnerObjPreservesKubeBindAnnotations(t *testing.T) { + obj := &unstructured.Unstructured{} + obj.SetAnnotations(map[string]string{ + konnectortypes.ProviderNamespaceAnnotationKey: "provider-ns", + konnectortypes.ProviderUIDAnnotationKey: "provider-uid", + konnectortypes.ConsumerNamespaceAnnotationKey: "consumer-ns", + konnectortypes.ConsumerUIDAnnotationKey: "consumer-uid", + "kcp.io/cluster": "should-be-stripped", + "user-annotation": "should-be-kept", + }) + obj.SetNamespace("original-ns") + obj.SetName("test-obj") + + candidate := candidateFromOwnerObj("target-ns", obj) + + annotations := candidate.GetAnnotations() + require.Equal(t, "provider-ns", annotations[konnectortypes.ProviderNamespaceAnnotationKey]) + require.Equal(t, "provider-uid", annotations[konnectortypes.ProviderUIDAnnotationKey]) + require.Equal(t, "consumer-ns", annotations[konnectortypes.ConsumerNamespaceAnnotationKey]) + require.Equal(t, "consumer-uid", annotations[konnectortypes.ConsumerUIDAnnotationKey]) + require.Equal(t, "should-be-kept", annotations["user-annotation"]) + _, hasKcp := annotations["kcp.io/cluster"] + require.False(t, hasKcp, "kcp.io/cluster annotation should be stripped") +} diff --git a/pkg/konnector/controllers/cluster/serviceexport/spec/spec_reconcile.go b/pkg/konnector/controllers/cluster/serviceexport/spec/spec_reconcile.go index e3a2688e2..08a1216a8 100644 --- a/pkg/konnector/controllers/cluster/serviceexport/spec/spec_reconcile.go +++ b/pkg/konnector/controllers/cluster/serviceexport/spec/spec_reconcile.go @@ -106,6 +106,9 @@ func (r *reconciler) reconcile(ctx context.Context, obj *unstructured.Unstructur return err } + // Annotate the provider-side object with the consumer source metadata. + r.setSourceAnnotations(upstream, obj.GetNamespace(), string(obj.GetUID())) + // let the isolation perform any changes it desires if err := r.isolationStrategy.MutateMetadataAndSpec(upstream, *providerKey); err != nil { return err @@ -147,6 +150,9 @@ func (r *reconciler) reconcile(ctx context.Context, obj *unstructured.Unstructur return err } + // (Re)set the consumer source metadata annotations. + r.setSourceAnnotations(upstream, obj.GetNamespace(), string(obj.GetUID())) + // just in case, checking for finalizer if obj, err = r.ensureDownstreamFinalizer(ctx, obj); err != nil { return err @@ -244,3 +250,16 @@ func (r *reconciler) setClusterNamespaceAnnotation(obj *unstructured.Unstructure return nil } + +// setSourceAnnotations sets consumer source metadata annotations on a +// provider-side object so the provider can trace the origin of the synced +// object. +func (r *reconciler) setSourceAnnotations(obj *unstructured.Unstructured, consumerNamespace, consumerUID string) { + annotations := obj.GetAnnotations() + if annotations == nil { + annotations = map[string]string{} + } + annotations[konnectortypes.ConsumerNamespaceAnnotationKey] = consumerNamespace + annotations[konnectortypes.ConsumerUIDAnnotationKey] = consumerUID + obj.SetAnnotations(annotations) +} diff --git a/pkg/konnector/controllers/cluster/serviceexport/spec/spec_reconcile_test.go b/pkg/konnector/controllers/cluster/serviceexport/spec/spec_reconcile_test.go index 0c0847ea0..150f6495e 100644 --- a/pkg/konnector/controllers/cluster/serviceexport/spec/spec_reconcile_test.go +++ b/pkg/konnector/controllers/cluster/serviceexport/spec/spec_reconcile_test.go @@ -77,6 +77,47 @@ func TestInjectClusterNamespace(t *testing.T) { } } +func TestSetSourceAnnotations(t *testing.T) { + tests := []struct { + name string + obj *unstructured.Unstructured + consumerNamespace string + consumerUID string + }{ + { + name: "no existing annotations", + obj: &unstructured.Unstructured{}, + consumerNamespace: "my-namespace", + consumerUID: "abc-123-def", + }, + { + name: "with existing cluster namespace annotation", + obj: newObjectWithClusterNs("kube-bind-zlp9m"), + consumerNamespace: "other-namespace", + consumerUID: "xyz-456-ghi", + }, + { + name: "cluster-scoped object with empty namespace", + obj: &unstructured.Unstructured{}, + consumerNamespace: "", + consumerUID: "uid-789", + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + t.Parallel() + + rec := &reconciler{} + rec.setSourceAnnotations(tt.obj, tt.consumerNamespace, tt.consumerUID) + + annotations := tt.obj.GetAnnotations() + require.Equal(t, tt.consumerNamespace, annotations[konnectortypes.ConsumerNamespaceAnnotationKey]) + require.Equal(t, tt.consumerUID, annotations[konnectortypes.ConsumerUIDAnnotationKey]) + }) + } +} + func newObjectWithClusterNs(providerNamespace string) *unstructured.Unstructured { obj := &unstructured.Unstructured{} ans := map[string]string{ diff --git a/pkg/konnector/types/types.go b/pkg/konnector/types/types.go index 9adce955c..192bef654 100644 --- a/pkg/konnector/types/types.go +++ b/pkg/konnector/types/types.go @@ -21,3 +21,19 @@ package types // This annotation is set on all synced objects, regardless of scope or // isolation mode. const ClusterNamespaceAnnotationKey = "kube-bind.io/cluster-namespace" + +// ConsumerNamespaceAnnotationKey is the annotation key set on provider-side +// objects to identify the consumer namespace the source object lives in. +const ConsumerNamespaceAnnotationKey = "kube-bind.io/consumer-namespace" + +// ConsumerUIDAnnotationKey is the annotation key set on provider-side objects +// to uniquely identify the consumer source object. +const ConsumerUIDAnnotationKey = "kube-bind.io/consumer-uid" + +// ProviderNamespaceAnnotationKey is the annotation key set on consumer-side +// objects to identify the provider namespace the source object lives in. +const ProviderNamespaceAnnotationKey = "kube-bind.io/provider-namespace" + +// ProviderUIDAnnotationKey is the annotation key set on consumer-side objects +// to uniquely identify the provider source object. +const ProviderUIDAnnotationKey = "kube-bind.io/provider-uid" diff --git a/test/e2e/bind/happy-case_test.go b/test/e2e/bind/happy-case_test.go index 64106f3a2..d8972b4d0 100644 --- a/test/e2e/bind/happy-case_test.go +++ b/test/e2e/bind/happy-case_test.go @@ -446,6 +446,12 @@ func testHappyCase( consumer.providerContractNamespace = ourInstance.GetAnnotations()[types.ClusterNamespaceAnnotationKey] require.NotEmpty(t, consumer.providerContractNamespace, "cluster namespace annotation must always exist") require.NotEqual(t, consumer.providerContractNamespace, "unknown") + + // Verify consumer source metadata annotations are set on the provider-side object. + consumerNS := ourInstance.GetAnnotations()[types.ConsumerNamespaceAnnotationKey] + require.NotEmpty(t, consumerNS, "consumer-namespace annotation must exist on provider object") + consumerUID := ourInstance.GetAnnotations()[types.ConsumerUIDAnnotationKey] + require.NotEmpty(t, consumerUID, "consumer-uid annotation must exist on provider object") }, }, // Request included namespace, so we check it first From b9549201b7d5591baec9541358a080bcc0fe1546 Mon Sep 17 00:00:00 2001 From: Mirza Kopic Date: Wed, 8 Apr 2026 09:47:45 +0200 Subject: [PATCH 2/5] fix: liniting --- .../claimedresources_reconciler_test.go | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/pkg/konnector/controllers/cluster/claimedresources/claimedresources_reconciler_test.go b/pkg/konnector/controllers/cluster/claimedresources/claimedresources_reconciler_test.go index f3ea17827..2c804527f 100644 --- a/pkg/konnector/controllers/cluster/claimedresources/claimedresources_reconciler_test.go +++ b/pkg/konnector/controllers/cluster/claimedresources/claimedresources_reconciler_test.go @@ -27,12 +27,12 @@ import ( func TestSetSourceMetadataAnnotations(t *testing.T) { tests := []struct { - name string - obj *unstructured.Unstructured - sourceNS string + name string + obj *unstructured.Unstructured + sourceNS string sourceUID string - nsKey string - uidKey string + nsKey string + uidKey string }{ { name: "provider annotations on empty object", From 6a9c9d74563483932087af0f2035b80b855840eb Mon Sep 17 00:00:00 2001 From: Mirza Kopic Date: Wed, 8 Apr 2026 10:50:22 +0200 Subject: [PATCH 3/5] fix: e2e test case --- test/e2e/bind/happy-case_test.go | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/test/e2e/bind/happy-case_test.go b/test/e2e/bind/happy-case_test.go index d8972b4d0..864e28c8c 100644 --- a/test/e2e/bind/happy-case_test.go +++ b/test/e2e/bind/happy-case_test.go @@ -448,9 +448,10 @@ func testHappyCase( require.NotEqual(t, consumer.providerContractNamespace, "unknown") // Verify consumer source metadata annotations are set on the provider-side object. - consumerNS := ourInstance.GetAnnotations()[types.ConsumerNamespaceAnnotationKey] - require.NotEmpty(t, consumerNS, "consumer-namespace annotation must exist on provider object") - consumerUID := ourInstance.GetAnnotations()[types.ConsumerUIDAnnotationKey] + annotations := ourInstance.GetAnnotations() + _, hasConsumerNS := annotations[types.ConsumerNamespaceAnnotationKey] + require.True(t, hasConsumerNS, "consumer-namespace annotation must exist on provider object") + consumerUID := annotations[types.ConsumerUIDAnnotationKey] require.NotEmpty(t, consumerUID, "consumer-uid annotation must exist on provider object") }, }, From e2923a1c86b4f76227c12de23b00f66f3bd74163 Mon Sep 17 00:00:00 2001 From: Mirza Kopic Date: Wed, 8 Apr 2026 17:27:52 +0200 Subject: [PATCH 4/5] fix: comments --- .../claimedresources_reconciler.go | 18 ++-------- .../claimedresources_reconciler_test.go | 36 ++++++++++++------- .../serviceexport/spec/spec_reconcile.go | 19 +++------- .../serviceexport/spec/spec_reconcile_test.go | 31 +++++++++++----- pkg/konnector/types/types.go | 16 +++++++++ test/e2e/bind/happy-case_test.go | 15 +++++--- 6 files changed, 80 insertions(+), 55 deletions(-) diff --git a/pkg/konnector/controllers/cluster/claimedresources/claimedresources_reconciler.go b/pkg/konnector/controllers/cluster/claimedresources/claimedresources_reconciler.go index 2a30c7b2c..0db7a89e8 100644 --- a/pkg/konnector/controllers/cluster/claimedresources/claimedresources_reconciler.go +++ b/pkg/konnector/controllers/cluster/claimedresources/claimedresources_reconciler.go @@ -118,7 +118,7 @@ func (r *readReconciler) reconcile(ctx context.Context, providerNamespace, name logger.Info("Creating missing consumer object", "consumerNamespace", consumerNS, "consumerName", providerObj.GetName()) candidate := candidateFromOwnerObj(consumerNS, providerObj) - setSourceMetadataAnnotations(candidate, providerObj.GetNamespace(), string(providerObj.GetUID()), + konnectortypes.SetSourceMetadataAnnotations(candidate, providerObj.GetNamespace(), string(providerObj.GetUID()), konnectortypes.ProviderNamespaceAnnotationKey, konnectortypes.ProviderUIDAnnotationKey) r.makeProviderOwner(candidate) @@ -135,7 +135,7 @@ func (r *readReconciler) reconcile(ctx context.Context, providerNamespace, name } candidate := candidateFromOwnerObj(consumerNS, providerObj) - setSourceMetadataAnnotations(candidate, providerObj.GetNamespace(), string(providerObj.GetUID()), + konnectortypes.SetSourceMetadataAnnotations(candidate, providerObj.GetNamespace(), string(providerObj.GetUID()), konnectortypes.ProviderNamespaceAnnotationKey, konnectortypes.ProviderUIDAnnotationKey) current := candidateFromOwnerObj(consumerNS, consumerObj) if !equality.Semantic.DeepEqual(candidate, current) { @@ -166,7 +166,7 @@ func (r *readReconciler) reconcile(ctx context.Context, providerNamespace, name } candidate := candidateFromOwnerObj(providerNamespace, ownerCandidate) - setSourceMetadataAnnotations(candidate, ownerCandidate.GetNamespace(), string(ownerCandidate.GetUID()), + konnectortypes.SetSourceMetadataAnnotations(candidate, ownerCandidate.GetNamespace(), string(ownerCandidate.GetUID()), konnectortypes.ConsumerNamespaceAnnotationKey, konnectortypes.ConsumerUIDAnnotationKey) r.makeConsumerOwner(candidate) @@ -238,18 +238,6 @@ func candidateFromOwnerObj(downstreamNS string, obj *unstructured.Unstructured) return candidate } -// setSourceMetadataAnnotations sets source metadata annotations on a synced -// object so the receiving side can trace where the object came from. -func setSourceMetadataAnnotations(candidate *unstructured.Unstructured, sourceNS, sourceUID, nsKey, uidKey string) { - annotations := candidate.GetAnnotations() - if annotations == nil { - annotations = map[string]string{} - } - annotations[nsKey] = sourceNS - annotations[uidKey] = sourceUID - candidate.SetAnnotations(annotations) -} - // determineOwner determines the owner of a resource given at least one object exists either on the // consumer or provider side. func determineOwner(providerObj, consumerObj *unstructured.Unstructured) (kubebindv1alpha2.Owner, error) { diff --git a/pkg/konnector/controllers/cluster/claimedresources/claimedresources_reconciler_test.go b/pkg/konnector/controllers/cluster/claimedresources/claimedresources_reconciler_test.go index 2c804527f..842a34b0c 100644 --- a/pkg/konnector/controllers/cluster/claimedresources/claimedresources_reconciler_test.go +++ b/pkg/konnector/controllers/cluster/claimedresources/claimedresources_reconciler_test.go @@ -27,12 +27,13 @@ import ( func TestSetSourceMetadataAnnotations(t *testing.T) { tests := []struct { - name string - obj *unstructured.Unstructured - sourceNS string - sourceUID string - nsKey string - uidKey string + name string + obj *unstructured.Unstructured + sourceNS string + sourceUID string + nsKey string + uidKey string + expectedAnnotations map[string]string }{ { name: "provider annotations on empty object", @@ -41,6 +42,10 @@ func TestSetSourceMetadataAnnotations(t *testing.T) { sourceUID: "provider-uid-123", nsKey: konnectortypes.ProviderNamespaceAnnotationKey, uidKey: konnectortypes.ProviderUIDAnnotationKey, + expectedAnnotations: map[string]string{ + konnectortypes.ProviderNamespaceAnnotationKey: "provider-ns", + konnectortypes.ProviderUIDAnnotationKey: "provider-uid-123", + }, }, { name: "consumer annotations on empty object", @@ -49,6 +54,10 @@ func TestSetSourceMetadataAnnotations(t *testing.T) { sourceUID: "consumer-uid-456", nsKey: konnectortypes.ConsumerNamespaceAnnotationKey, uidKey: konnectortypes.ConsumerUIDAnnotationKey, + expectedAnnotations: map[string]string{ + konnectortypes.ConsumerNamespaceAnnotationKey: "consumer-ns", + konnectortypes.ConsumerUIDAnnotationKey: "consumer-uid-456", + }, }, { name: "preserves existing annotations", @@ -63,6 +72,11 @@ func TestSetSourceMetadataAnnotations(t *testing.T) { sourceUID: "my-uid", nsKey: konnectortypes.ProviderNamespaceAnnotationKey, uidKey: konnectortypes.ProviderUIDAnnotationKey, + expectedAnnotations: map[string]string{ + konnectortypes.ProviderNamespaceAnnotationKey: "my-ns", + konnectortypes.ProviderUIDAnnotationKey: "my-uid", + "existing-key": "existing-value", + }, }, } @@ -70,15 +84,11 @@ func TestSetSourceMetadataAnnotations(t *testing.T) { t.Run(tt.name, func(t *testing.T) { t.Parallel() - setSourceMetadataAnnotations(tt.obj, tt.sourceNS, tt.sourceUID, tt.nsKey, tt.uidKey) + konnectortypes.SetSourceMetadataAnnotations(tt.obj, tt.sourceNS, tt.sourceUID, tt.nsKey, tt.uidKey) annotations := tt.obj.GetAnnotations() - require.Equal(t, tt.sourceNS, annotations[tt.nsKey]) - require.Equal(t, tt.sourceUID, annotations[tt.uidKey]) - - // verify existing annotations are preserved - if tt.name == "preserves existing annotations" { - require.Equal(t, "existing-value", annotations["existing-key"]) + for key, expected := range tt.expectedAnnotations { + require.Equal(t, expected, annotations[key], "annotation %s mismatch", key) } }) } diff --git a/pkg/konnector/controllers/cluster/serviceexport/spec/spec_reconcile.go b/pkg/konnector/controllers/cluster/serviceexport/spec/spec_reconcile.go index 08a1216a8..6fa9eabe5 100644 --- a/pkg/konnector/controllers/cluster/serviceexport/spec/spec_reconcile.go +++ b/pkg/konnector/controllers/cluster/serviceexport/spec/spec_reconcile.go @@ -107,7 +107,8 @@ func (r *reconciler) reconcile(ctx context.Context, obj *unstructured.Unstructur } // Annotate the provider-side object with the consumer source metadata. - r.setSourceAnnotations(upstream, obj.GetNamespace(), string(obj.GetUID())) + konnectortypes.SetSourceMetadataAnnotations(upstream, obj.GetNamespace(), string(obj.GetUID()), + konnectortypes.ConsumerNamespaceAnnotationKey, konnectortypes.ConsumerUIDAnnotationKey) // let the isolation perform any changes it desires if err := r.isolationStrategy.MutateMetadataAndSpec(upstream, *providerKey); err != nil { @@ -151,7 +152,8 @@ func (r *reconciler) reconcile(ctx context.Context, obj *unstructured.Unstructur } // (Re)set the consumer source metadata annotations. - r.setSourceAnnotations(upstream, obj.GetNamespace(), string(obj.GetUID())) + konnectortypes.SetSourceMetadataAnnotations(upstream, obj.GetNamespace(), string(obj.GetUID()), + konnectortypes.ConsumerNamespaceAnnotationKey, konnectortypes.ConsumerUIDAnnotationKey) // just in case, checking for finalizer if obj, err = r.ensureDownstreamFinalizer(ctx, obj); err != nil { @@ -250,16 +252,3 @@ func (r *reconciler) setClusterNamespaceAnnotation(obj *unstructured.Unstructure return nil } - -// setSourceAnnotations sets consumer source metadata annotations on a -// provider-side object so the provider can trace the origin of the synced -// object. -func (r *reconciler) setSourceAnnotations(obj *unstructured.Unstructured, consumerNamespace, consumerUID string) { - annotations := obj.GetAnnotations() - if annotations == nil { - annotations = map[string]string{} - } - annotations[konnectortypes.ConsumerNamespaceAnnotationKey] = consumerNamespace - annotations[konnectortypes.ConsumerUIDAnnotationKey] = consumerUID - obj.SetAnnotations(annotations) -} diff --git a/pkg/konnector/controllers/cluster/serviceexport/spec/spec_reconcile_test.go b/pkg/konnector/controllers/cluster/serviceexport/spec/spec_reconcile_test.go index 150f6495e..896dbab3c 100644 --- a/pkg/konnector/controllers/cluster/serviceexport/spec/spec_reconcile_test.go +++ b/pkg/konnector/controllers/cluster/serviceexport/spec/spec_reconcile_test.go @@ -79,28 +79,42 @@ func TestInjectClusterNamespace(t *testing.T) { func TestSetSourceAnnotations(t *testing.T) { tests := []struct { - name string - obj *unstructured.Unstructured - consumerNamespace string - consumerUID string + name string + obj *unstructured.Unstructured + consumerNamespace string + consumerUID string + expectedAnnotations map[string]string }{ { name: "no existing annotations", obj: &unstructured.Unstructured{}, consumerNamespace: "my-namespace", consumerUID: "abc-123-def", + expectedAnnotations: map[string]string{ + konnectortypes.ConsumerNamespaceAnnotationKey: "my-namespace", + konnectortypes.ConsumerUIDAnnotationKey: "abc-123-def", + }, }, { name: "with existing cluster namespace annotation", obj: newObjectWithClusterNs("kube-bind-zlp9m"), consumerNamespace: "other-namespace", consumerUID: "xyz-456-ghi", + expectedAnnotations: map[string]string{ + konnectortypes.ConsumerNamespaceAnnotationKey: "other-namespace", + konnectortypes.ConsumerUIDAnnotationKey: "xyz-456-ghi", + konnectortypes.ClusterNamespaceAnnotationKey: "kube-bind-zlp9m", + }, }, { name: "cluster-scoped object with empty namespace", obj: &unstructured.Unstructured{}, consumerNamespace: "", consumerUID: "uid-789", + expectedAnnotations: map[string]string{ + konnectortypes.ConsumerNamespaceAnnotationKey: "", + konnectortypes.ConsumerUIDAnnotationKey: "uid-789", + }, }, } @@ -108,12 +122,13 @@ func TestSetSourceAnnotations(t *testing.T) { t.Run(tt.name, func(t *testing.T) { t.Parallel() - rec := &reconciler{} - rec.setSourceAnnotations(tt.obj, tt.consumerNamespace, tt.consumerUID) + konnectortypes.SetSourceMetadataAnnotations(tt.obj, tt.consumerNamespace, tt.consumerUID, + konnectortypes.ConsumerNamespaceAnnotationKey, konnectortypes.ConsumerUIDAnnotationKey) annotations := tt.obj.GetAnnotations() - require.Equal(t, tt.consumerNamespace, annotations[konnectortypes.ConsumerNamespaceAnnotationKey]) - require.Equal(t, tt.consumerUID, annotations[konnectortypes.ConsumerUIDAnnotationKey]) + for key, expected := range tt.expectedAnnotations { + require.Equal(t, expected, annotations[key], "annotation %s mismatch", key) + } }) } } diff --git a/pkg/konnector/types/types.go b/pkg/konnector/types/types.go index 192bef654..1c3ab8d47 100644 --- a/pkg/konnector/types/types.go +++ b/pkg/konnector/types/types.go @@ -16,6 +16,10 @@ limitations under the License. package types +import ( + "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" +) + // ClusterNamespaceAnnotationKey is the annotation key to identify the // cluster namespace that any synced object on the provider side belongs to. // This annotation is set on all synced objects, regardless of scope or @@ -37,3 +41,15 @@ const ProviderNamespaceAnnotationKey = "kube-bind.io/provider-namespace" // ProviderUIDAnnotationKey is the annotation key set on consumer-side objects // to uniquely identify the provider source object. const ProviderUIDAnnotationKey = "kube-bind.io/provider-uid" + +// SetSourceMetadataAnnotations sets source metadata annotations on a synced +// object so the receiving side can trace where the object came from. +func SetSourceMetadataAnnotations(obj *unstructured.Unstructured, sourceNS, sourceUID, nsKey, uidKey string) { + annotations := obj.GetAnnotations() + if annotations == nil { + annotations = map[string]string{} + } + annotations[nsKey] = sourceNS + annotations[uidKey] = sourceUID + obj.SetAnnotations(annotations) +} diff --git a/test/e2e/bind/happy-case_test.go b/test/e2e/bind/happy-case_test.go index 864e28c8c..033c4bdfb 100644 --- a/test/e2e/bind/happy-case_test.go +++ b/test/e2e/bind/happy-case_test.go @@ -680,19 +680,26 @@ func testHappyCase( }) require.NoError(t, err) + var consumerObj *unstructured.Unstructured require.Eventually(t, func() bool { - var obj *unstructured.Unstructured var err error if consumerResourceScope == apiextensionsv1.NamespaceScoped { - obj, err = consumer.consumerClient.Namespace(consumer.consumerObjectNamespace).Get(ctx, instanceName, metav1.GetOptions{}) + consumerObj, err = consumer.consumerClient.Namespace(consumer.consumerObjectNamespace).Get(ctx, instanceName, metav1.GetOptions{}) } else { - obj, err = consumer.consumerClient.Get(ctx, instanceName, metav1.GetOptions{}) + consumerObj, err = consumer.consumerClient.Get(ctx, instanceName, metav1.GetOptions{}) } require.NoError(t, err) - value, _, err := unstructured.NestedString(obj.Object, "status", "result") + value, _, err := unstructured.NestedString(consumerObj.Object, "status", "result") require.NoError(t, err) return value == fmt.Sprintf("Ready to ride from %s", consumer.name) }, wait.ForeverTestTimeout, time.Millisecond*100, "waiting for the %s instance to be updated downstream for %s", serviceGVR.Resource, consumer.name) + + // Verify provider source metadata annotations are set on the consumer-side object. + consumerAnnotations := consumerObj.GetAnnotations() + _, hasProviderNS := consumerAnnotations[types.ProviderNamespaceAnnotationKey] + require.True(t, hasProviderNS, "provider-namespace annotation must exist on consumer object") + providerUID := consumerAnnotations[types.ProviderUIDAnnotationKey] + require.NotEmpty(t, providerUID, "provider-uid annotation must exist on consumer object") }, }, From 7456354c9cd598be346208506543a81814c74ab7 Mon Sep 17 00:00:00 2001 From: Mirza Kopic Date: Thu, 16 Apr 2026 17:01:21 +0200 Subject: [PATCH 5/5] fix e2e test --- .../serviceexport/status/status_controller.go | 3 +++ .../serviceexport/status/status_reconcile.go | 16 ++++++++++++++++ 2 files changed, 19 insertions(+) diff --git a/pkg/konnector/controllers/cluster/serviceexport/status/status_controller.go b/pkg/konnector/controllers/cluster/serviceexport/status/status_controller.go index 8bc96badc..c4b1da98a 100644 --- a/pkg/konnector/controllers/cluster/serviceexport/status/status_controller.go +++ b/pkg/konnector/controllers/cluster/serviceexport/status/status_controller.go @@ -110,6 +110,9 @@ func NewController( return obj.DeepCopy(), nil }, + updateConsumerObject: func(ctx context.Context, obj *unstructured.Unstructured) (*unstructured.Unstructured, error) { + return consumerClient.Resource(gvr).Namespace(obj.GetNamespace()).Update(ctx, obj, metav1.UpdateOptions{}) + }, updateConsumerObjectStatus: func(ctx context.Context, obj *unstructured.Unstructured) (*unstructured.Unstructured, error) { return consumerClient.Resource(gvr).Namespace(obj.GetNamespace()).UpdateStatus(ctx, obj, metav1.UpdateOptions{}) }, diff --git a/pkg/konnector/controllers/cluster/serviceexport/status/status_reconcile.go b/pkg/konnector/controllers/cluster/serviceexport/status/status_reconcile.go index 69c7d44ff..efd7a9d48 100644 --- a/pkg/konnector/controllers/cluster/serviceexport/status/status_reconcile.go +++ b/pkg/konnector/controllers/cluster/serviceexport/status/status_reconcile.go @@ -27,12 +27,14 @@ import ( "k8s.io/klog/v2" "github.com/kube-bind/kube-bind/pkg/konnector/controllers/cluster/serviceexport/isolation" + konnectortypes "github.com/kube-bind/kube-bind/pkg/konnector/types" ) type reconciler struct { isolationStrategy isolation.Strategy getConsumerObject func(ns, name string) (*unstructured.Unstructured, error) + updateConsumerObject func(ctx context.Context, obj *unstructured.Unstructured) (*unstructured.Unstructured, error) updateConsumerObjectStatus func(ctx context.Context, obj *unstructured.Unstructured) (*unstructured.Unstructured, error) deleteProviderObject func(ctx context.Context, ns, name string) error @@ -77,6 +79,20 @@ func (r *reconciler) reconcile(ctx context.Context, obj *unstructured.Unstructur return err } + // Set provider source metadata annotations on the consumer object so + // the consumer side can trace where the object came from. This requires + // a full object update because UpdateStatus does not persist metadata. + annotated := downstream.DeepCopy() + konnectortypes.SetSourceMetadataAnnotations(annotated, obj.GetNamespace(), string(obj.GetUID()), + konnectortypes.ProviderNamespaceAnnotationKey, konnectortypes.ProviderUIDAnnotationKey) + if !reflect.DeepEqual(downstream.GetAnnotations(), annotated.GetAnnotations()) { + logger.Info("Updating downstream object provider annotations") + var err error + if downstream, err = r.updateConsumerObject(ctx, annotated); err != nil { + return err + } + } + // let the isolation perform any changes it desires if err := r.isolationStrategy.MutateStatus(downstream, *consumerKey); err != nil { return err