From 74d3ac504e79b935ef7d3613f5668e0f983ce4ff Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Oliver=20B=C3=A4hler?= Date: Thu, 7 Dec 2023 16:49:35 +0100 Subject: [PATCH] fix(controller): respect metadata of replicated items (#922) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * fix(controller): respect metadata of replicated items Signed-off-by: Oliver Bähler * chore(makefile): fix dev-setup Signed-off-by: Oliver Bähler --------- Signed-off-by: Oliver Bähler --- Makefile | 4 ++-- controllers/resources/processor.go | 19 +++++++++++++++++-- e2e/tenantresource_test.go | 28 ++++++++++++++++++++++++---- 3 files changed, 43 insertions(+), 8 deletions(-) diff --git a/Makefile b/Makefile index 9a38f499..592c6e74 100644 --- a/Makefile +++ b/Makefile @@ -166,7 +166,7 @@ dev-setup: {'op': 'replace', 'path': '/webhooks/5/clientConfig', 'value':{'url':\"$${WEBHOOK_URL}/pods\",'caBundle':\"$${CA_BUNDLE}\"}},\ {'op': 'replace', 'path': '/webhooks/6/clientConfig', 'value':{'url':\"$${WEBHOOK_URL}/persistentvolumeclaims\",'caBundle':\"$${CA_BUNDLE}\"}},\ {'op': 'replace', 'path': '/webhooks/7/clientConfig', 'value':{'url':\"$${WEBHOOK_URL}/services\",'caBundle':\"$${CA_BUNDLE}\"}},\ - {'op': 'replace', 'path': '/webhooks/8/clientConfig', 'value':{'url':\"$${WEBHOOK_URL}/tenants\",'caBundle':\"$${CA_BUNDLE}\"}},\ + {'op': 'replace', 'path': '/webhooks/8/clientConfig', 'value':{'url':\"$${WEBHOOK_URL}/tenantresource-objects\",'caBundle':\"$${CA_BUNDLE}\"}},\ {'op': 'replace', 'path': '/webhooks/9/clientConfig', 'value':{'url':\"$${WEBHOOK_URL}/tenants\",'caBundle':\"$${CA_BUNDLE}\"}}\ ]" && \ kubectl patch crd tenants.capsule.clastix.io \ @@ -242,7 +242,7 @@ apidocs-gen: ## Download crdoc locally if necessary. $(call go-install-tool,$(APIDOCS_GEN),fybrik.io/crdoc@$(APIDOCS_GEN_VERSION)) GINKGO := $(shell pwd)/bin/ginkgo -GINGKO_VERSION := v2.13.1 +GINGKO_VERSION := v2.13.2 ginkgo: ## Download ginkgo locally if necessary. $(call go-install-tool,$(GINKGO),github.com/onsi/ginkgo/v2/ginkgo@$(GINGKO_VERSION)) diff --git a/controllers/resources/processor.go b/controllers/resources/processor.go index 3f2b8164..244aaa28 100644 --- a/controllers/resources/processor.go +++ b/controllers/resources/processor.go @@ -124,6 +124,7 @@ func (r *Processor) HandleSection(ctx context.Context, tnt capsulev1beta2.Tenant objLabels[Label] = fmt.Sprintf("%d", resourceIndex) objLabels[tenantLabel] = tnt.GetName() + // processed will contain the sets of resources replicated, both for the raw and the Namespaced ones: // these are required to perform a final pruning once the replication has been occurred. processed := sets.NewString() @@ -265,8 +266,22 @@ func (r *Processor) createOrUpdate(ctx context.Context, obj *unstructured.Unstru rv := actual.GetResourceVersion() actual.SetUnstructuredContent(desired.Object) - actual.SetLabels(labels) - actual.SetAnnotations(annotations) + combinedLabels := obj.GetLabels() + if combinedLabels == nil { + combinedLabels = make(map[string]string) + } + for key, value := range labels { + combinedLabels[key] = value + } + actual.SetLabels(combinedLabels) + combinedAnnotations := obj.GetAnnotations() + if combinedAnnotations == nil { + combinedAnnotations = make(map[string]string) + } + for key, value := range annotations { + combinedAnnotations[key] = value + } + actual.SetAnnotations(combinedAnnotations) actual.SetResourceVersion(rv) actual.SetUID(UID) diff --git a/e2e/tenantresource_test.go b/e2e/tenantresource_test.go index 416e3d50..df97353b 100644 --- a/e2e/tenantresource_test.go +++ b/e2e/tenantresource_test.go @@ -63,6 +63,13 @@ var _ = Describe("Creating a TenantResource object", func() { Type: corev1.SecretTypeOpaque, } + testLabels := map[string]string{ + "labels.energy.io": "namespaced", + } + testAnnotations := map[string]string{ + "annotations.energy.io": "namespaced", + } + tr := &capsulev1beta2.TenantResource{ ObjectMeta: metav1.ObjectMeta{ Name: "replicate-energies", @@ -101,7 +108,9 @@ var _ = Describe("Creating a TenantResource object", func() { APIVersion: "v1", }, ObjectMeta: metav1.ObjectMeta{ - Name: "raw-secret-1", + Name: "raw-secret-1", + Labels: testLabels, + Annotations: testAnnotations, }, Type: corev1.SecretTypeOpaque, Data: map[string][]byte{ @@ -119,7 +128,9 @@ var _ = Describe("Creating a TenantResource object", func() { APIVersion: "v1", }, ObjectMeta: metav1.ObjectMeta{ - Name: "raw-secret-2", + Name: "raw-secret-2", + Labels: testLabels, + Annotations: testAnnotations, }, Type: corev1.SecretTypeOpaque, Data: map[string][]byte{ @@ -137,7 +148,9 @@ var _ = Describe("Creating a TenantResource object", func() { APIVersion: "v1", }, ObjectMeta: metav1.ObjectMeta{ - Name: "raw-secret-3", + Name: "raw-secret-3", + Labels: testLabels, + Annotations: testAnnotations, }, Type: corev1.SecretTypeOpaque, Data: map[string][]byte{ @@ -288,11 +301,18 @@ var _ = Describe("Creating a TenantResource object", func() { _, err := HaveKeyWithValue(k, v).Match(secret.GetLabels()) Expect(err).ToNot(HaveOccurred()) } - + for k, v := range testLabels { + _, err := HaveKeyWithValue(k, v).Match(secret.GetLabels()) + Expect(err).ToNot(HaveOccurred()) + } for k, v := range tr.Spec.Resources[0].AdditionalMetadata.Annotations { _, err := HaveKeyWithValue(k, v).Match(secret.GetAnnotations()) Expect(err).ToNot(HaveOccurred()) } + for k, v := range testAnnotations { + _, err := HaveKeyWithValue(k, v).Match(secret.GetAnnotations()) + Expect(err).ToNot(HaveOccurred()) + } } })