From 4215a6003303f2bc4b9442a71d9dee8d16620af9 Mon Sep 17 00:00:00 2001 From: Miles Garnsey <11435896+Miles-Garnsey@users.noreply.github.com> Date: Wed, 10 Jan 2024 01:50:46 +1100 Subject: [PATCH] Add AdditionalAnnotations field and logic to reconcile it (#601) * Add AdditionalAnnotations field and logic to reconcile it. * Ensure rack reconciliation adds annotations when they are missing. * Fix tests. * Final test fixes. * Update pod template spec annotations, not pod annotations. * Michael's feedback. * Revert changes to mergeInLabelsIfDifferent. * Changelog. --- CHANGELOG.md | 2 + .../v1beta1/cassandradatacenter_types.go | 3 + .../v1beta1/zz_generated.deepcopy.go | 7 +++ ...dra.datastax.com_cassandradatacenters.yaml | 8 +++ .../control/cassandratask_controller.go | 1 + pkg/oplabels/labels.go | 12 ++++ .../construct_podtemplatespec.go | 4 +- .../construct_podtemplatespec_test.go | 28 ++++++--- pkg/reconciliation/construct_service.go | 18 ++++++ pkg/reconciliation/construct_service_test.go | 63 ++++++++++++++++++- pkg/reconciliation/construct_statefulset.go | 21 ++++--- .../construct_statefulset_test.go | 12 ++++ pkg/reconciliation/constructor.go | 5 +- pkg/reconciliation/reconcile_racks.go | 63 ++++++++++++++++++- pkg/reconciliation/secrets.go | 9 ++- pkg/reconciliation/secrets_test.go | 5 ++ 16 files changed, 233 insertions(+), 28 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a7517f38..6149ef45 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,8 @@ Changelog for Cass Operator, new PRs should update the `main / unreleased` secti ## unreleased +* [FEATURE] [#601](https://github.com/k8ssandra/cass-operator/pull/601) Add additionalAnnotations field to CR so that all resources created by the operator can be annotated. + ## v1.18.2 * [BUGFIX] [#593](https://github.com/k8ssandra/cass-operator/issues/593) Update k8ssandra-client to 0.2.2 to fix the issue with clusterName config generation when using 4.1 diff --git a/apis/cassandra/v1beta1/cassandradatacenter_types.go b/apis/cassandra/v1beta1/cassandradatacenter_types.go index 28b5d1e2..23c0af57 100644 --- a/apis/cassandra/v1beta1/cassandradatacenter_types.go +++ b/apis/cassandra/v1beta1/cassandradatacenter_types.go @@ -238,6 +238,9 @@ type CassandraDatacenterSpec struct { // Additional Labels allows to define additional labels that will be included in all objects created by the operator. Note, user can override values set by default from the cass-operator and doing so could break cass-operator functionality. AdditionalLabels map[string]string `json:"additionalLabels,omitempty"` + // Additional Annotations allows to define additional labels that will be included in all objects created by the operator. Note, user can override values set by default from the cass-operator and doing so could break cass-operator functionality. + AdditionalAnnotations map[string]string `json:"additionalAnnotations,omitempty"` + // CDC allows configuration of the change data capture agent which can run within the Management API container. Use it to send data to Pulsar. CDC *CDCConfiguration `json:"cdc,omitempty"` diff --git a/apis/cassandra/v1beta1/zz_generated.deepcopy.go b/apis/cassandra/v1beta1/zz_generated.deepcopy.go index da773243..e296e4df 100644 --- a/apis/cassandra/v1beta1/zz_generated.deepcopy.go +++ b/apis/cassandra/v1beta1/zz_generated.deepcopy.go @@ -346,6 +346,13 @@ func (in *CassandraDatacenterSpec) DeepCopyInto(out *CassandraDatacenterSpec) { (*out)[key] = val } } + if in.AdditionalAnnotations != nil { + in, out := &in.AdditionalAnnotations, &out.AdditionalAnnotations + *out = make(map[string]string, len(*in)) + for key, val := range *in { + (*out)[key] = val + } + } if in.CDC != nil { in, out := &in.CDC, &out.CDC *out = new(CDCConfiguration) diff --git a/config/crd/bases/cassandra.datastax.com_cassandradatacenters.yaml b/config/crd/bases/cassandra.datastax.com_cassandradatacenters.yaml index 723d8f4d..48ebcb53 100644 --- a/config/crd/bases/cassandra.datastax.com_cassandradatacenters.yaml +++ b/config/crd/bases/cassandra.datastax.com_cassandradatacenters.yaml @@ -38,6 +38,14 @@ spec: spec: description: CassandraDatacenterSpec defines the desired state of a CassandraDatacenter properties: + additionalAnnotations: + additionalProperties: + type: string + description: Additional Annotations allows to define additional labels + that will be included in all objects created by the operator. Note, + user can override values set by default from the cass-operator and + doing so could break cass-operator functionality. + type: object additionalLabels: additionalProperties: type: string diff --git a/internal/controllers/control/cassandratask_controller.go b/internal/controllers/control/cassandratask_controller.go index c4b0125c..78b1803f 100644 --- a/internal/controllers/control/cassandratask_controller.go +++ b/internal/controllers/control/cassandratask_controller.go @@ -238,6 +238,7 @@ func (r *CassandraTaskReconciler) Reconcile(ctx context.Context, req ctrl.Reques utils.MergeMap(cassTask.Labels, dc.GetDatacenterLabels()) oplabels.AddOperatorLabels(cassTask.GetLabels(), dc) + oplabels.AddOperatorAnnotations(cassTask.GetAnnotations(), dc) // Starting the run, set the Active label so we can quickly fetch the active ones cassTask.GetLabels()[taskStatusLabel] = activeTaskLabelValue diff --git a/pkg/oplabels/labels.go b/pkg/oplabels/labels.go index 3deaed87..9f3d77e2 100644 --- a/pkg/oplabels/labels.go +++ b/pkg/oplabels/labels.go @@ -32,6 +32,18 @@ func AddOperatorLabels(m map[string]string, dc *api.CassandraDatacenter) { m[key] = api.CleanLabelValue(value) } } + +} + +func AddOperatorAnnotations(m map[string]string, dc *api.CassandraDatacenter) { + if m == nil { + m = make(map[string]string) + } + if len(dc.Spec.AdditionalAnnotations) != 0 { + for key, value := range dc.Spec.AdditionalAnnotations { + m[key] = value + } + } } func HasManagedByCassandraOperatorLabel(m map[string]string) bool { diff --git a/pkg/reconciliation/construct_podtemplatespec.go b/pkg/reconciliation/construct_podtemplatespec.go index 0ab215bc..b4cbd50d 100644 --- a/pkg/reconciliation/construct_podtemplatespec.go +++ b/pkg/reconciliation/construct_podtemplatespec.go @@ -809,12 +809,10 @@ func buildPodTemplateSpec(dc *api.CassandraDatacenter, rack api.Rack, addLegacyI // Annotations - podAnnotations := map[string]string{} - if baseTemplate.Annotations == nil { baseTemplate.Annotations = make(map[string]string) } - baseTemplate.Annotations = utils.MergeMap(baseTemplate.Annotations, podAnnotations) + oplabels.AddOperatorAnnotations(baseTemplate.Annotations, dc) // Affinity nodeAffinityLabels, nodeAffinityLabelsConfigurationError := rackNodeAffinitylabels(dc, rack.Name) diff --git a/pkg/reconciliation/construct_podtemplatespec_test.go b/pkg/reconciliation/construct_podtemplatespec_test.go index a09f3e4e..70db0e6a 100644 --- a/pkg/reconciliation/construct_podtemplatespec_test.go +++ b/pkg/reconciliation/construct_podtemplatespec_test.go @@ -1003,24 +1003,34 @@ func TestCassandraDatacenter_buildContainers_additional_labels(t *testing.T) { AdditionalLabels: map[string]string{ "Add": "Label", }, + AdditionalAnnotations: map[string]string{ + "Add": "Annotation", + }, }, } dc.Spec.PodTemplateSpec.Labels = map[string]string{"abc": "123"} spec, err := buildPodTemplateSpec(dc, dc.Spec.Racks[0], false) - got := spec.Labels + gotLabels := spec.Labels - expected := dc.GetRackLabels("testrack") - oplabels.AddOperatorLabels(expected, dc) - expected[api.CassNodeState] = stateReadyToStart - expected["app.kubernetes.io/managed-by"] = oplabels.ManagedByLabelValue - expected["abc"] = "123" - expected["Add"] = "Label" + expectedLabels := dc.GetRackLabels("testrack") + oplabels.AddOperatorLabels(expectedLabels, dc) + expectedLabels[api.CassNodeState] = stateReadyToStart + expectedLabels["app.kubernetes.io/managed-by"] = oplabels.ManagedByLabelValue + expectedLabels["abc"] = "123" + expectedLabels["Add"] = "Label" assert.NoError(t, err, "should not have gotten error when building podTemplateSpec") - if !reflect.DeepEqual(expected, got) { - t.Errorf("labels = %v, want %v", got, expected) + if !reflect.DeepEqual(expectedLabels, gotLabels) { + t.Errorf("labels = %v, want %v", gotLabels, expectedLabels) } + + gotAnns := spec.Annotations + expectedAnnotations := map[string]string{"Add": "Annotation"} + if !reflect.DeepEqual(expectedAnnotations, gotAnns) { + t.Errorf("labels = %v, want %v", gotAnns, expectedAnnotations) + } + } func TestCassandraDatacenter_buildPodTemplateSpec_overrideSecurityContext(t *testing.T) { diff --git a/pkg/reconciliation/construct_service.go b/pkg/reconciliation/construct_service.go index 3d15a963..9319f9cd 100644 --- a/pkg/reconciliation/construct_service.go +++ b/pkg/reconciliation/construct_service.go @@ -70,6 +70,9 @@ func newServiceForCassandraDatacenter(dc *api.CassandraDatacenter) *corev1.Servi } service.Spec.Ports = ports + anns := make(map[string]string) + oplabels.AddOperatorAnnotations(anns, dc) + service.ObjectMeta.Annotations = anns addAdditionalOptions(service, &dc.Spec.AdditionalServiceConfig.DatacenterService) @@ -121,6 +124,10 @@ func newSeedServiceForCassandraDatacenter(dc *api.CassandraDatacenter) *corev1.S oplabels.AddOperatorLabels(labels, dc) service.ObjectMeta.Labels = labels + anns := dc.GetAnnotations() + oplabels.AddOperatorAnnotations(anns, dc) + service.ObjectMeta.Annotations = anns + service.Spec.Selector = buildLabelSelectorForSeedService(dc) service.Spec.PublishNotReadyAddresses = true @@ -136,10 +143,13 @@ func newSeedServiceForCassandraDatacenter(dc *api.CassandraDatacenter) *corev1.S func newAdditionalSeedServiceForCassandraDatacenter(dc *api.CassandraDatacenter) *corev1.Service { labels := dc.GetDatacenterLabels() oplabels.AddOperatorLabels(labels, dc) + anns := dc.GetAnnotations() + oplabels.AddOperatorAnnotations(anns, dc) var service corev1.Service service.ObjectMeta.Name = dc.GetAdditionalSeedsServiceName() service.ObjectMeta.Namespace = dc.Namespace service.ObjectMeta.Labels = labels + service.ObjectMeta.Annotations = anns // We omit the label selector because we will create the endpoints manually service.Spec.Type = "ClusterIP" service.Spec.ClusterIP = "None" @@ -159,6 +169,9 @@ func newEndpointsForAdditionalSeeds(dc *api.CassandraDatacenter) (*corev1.Endpoi endpoints.ObjectMeta.Name = dc.GetAdditionalSeedsServiceName() endpoints.ObjectMeta.Namespace = dc.Namespace endpoints.ObjectMeta.Labels = labels + anns := dc.GetAnnotations() + oplabels.AddOperatorAnnotations(anns, dc) + endpoints.ObjectMeta.Annotations = anns addresses := make([]corev1.EndpointAddress, 0, len(dc.Spec.AdditionalSeeds)) for _, additionalSeed := range dc.Spec.AdditionalSeeds { @@ -290,5 +303,10 @@ func makeGenericHeadlessService(dc *api.CassandraDatacenter) *corev1.Service { service.Spec.Selector = selector service.Spec.Type = "ClusterIP" service.Spec.ClusterIP = "None" + + anns := make(map[string]string) + oplabels.AddOperatorAnnotations(anns, dc) + service.ObjectMeta.Annotations = anns + return &service } diff --git a/pkg/reconciliation/construct_service_test.go b/pkg/reconciliation/construct_service_test.go index a6860dac..d1e2bf5b 100644 --- a/pkg/reconciliation/construct_service_test.go +++ b/pkg/reconciliation/construct_service_test.go @@ -5,10 +5,11 @@ package reconciliation import ( "fmt" - "github.com/k8ssandra/cass-operator/pkg/oplabels" "reflect" "testing" + "github.com/k8ssandra/cass-operator/pkg/oplabels" + "github.com/stretchr/testify/assert" corev1 "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" @@ -115,6 +116,9 @@ func TestLabelsWithNewSeedServiceForCassandraDatacenter(t *testing.T) { AdditionalLabels: map[string]string{ "Add": "label", }, + AdditionalAnnotations: map[string]string{ + "Add": "annotation", + }, }, } @@ -134,6 +138,12 @@ func TestLabelsWithNewSeedServiceForCassandraDatacenter(t *testing.T) { if !reflect.DeepEqual(expected, service.Labels) { t.Errorf("service labels = \n %v \n, want \n %v", service.Labels, expected) } + if !reflect.DeepEqual(expected, service.Labels) { + t.Errorf("service labels = \n %v \n, want \n %v", service.Annotations, map[string]string{ + "Add": "annotation", + }) + } + } func TestLabelsWithNewNodePortServiceForCassandraDatacenter(t *testing.T) { @@ -174,6 +184,9 @@ func TestLabelsWithNewNodePortServiceForCassandraDatacenter(t *testing.T) { AdditionalLabels: map[string]string{ "Add": "label", }, + AdditionalAnnotations: map[string]string{ + "Add": "annotation", + }, }, } @@ -194,6 +207,11 @@ func TestLabelsWithNewNodePortServiceForCassandraDatacenter(t *testing.T) { if !reflect.DeepEqual(expected, service.Labels) { t.Errorf("service labels = \n %v \n, want \n %v", service.Labels, expected) } + if !reflect.DeepEqual(expected, service.Labels) { + t.Errorf("service labels = \n %v \n, want \n %v", service.Annotations, map[string]string{ + "Add": "annotation", + }) + } } func TestLabelsWithNewAllPodsServiceForCassandraDatacenter(t *testing.T) { @@ -234,6 +252,9 @@ func TestLabelsWithNewAllPodsServiceForCassandraDatacenter(t *testing.T) { AdditionalLabels: map[string]string{ "Add": "label", }, + AdditionalAnnotations: map[string]string{ + "Add": "annotation", + }, }, } @@ -255,6 +276,11 @@ func TestLabelsWithNewAllPodsServiceForCassandraDatacenter(t *testing.T) { if !reflect.DeepEqual(expected, service.Labels) { t.Errorf("service labels = \n %v \n, want \n %v", service.Labels, expected) } + if !reflect.DeepEqual(expected, service.Labels) { + t.Errorf("service labels = \n %v \n, want \n %v", service.Annotations, map[string]string{ + "Add": "annotation", + }) + } } func TestLabelsWithNewServiceForCassandraDatacenter(t *testing.T) { @@ -295,6 +321,9 @@ func TestLabelsWithNewServiceForCassandraDatacenter(t *testing.T) { AdditionalLabels: map[string]string{ "Add": "label", }, + AdditionalAnnotations: map[string]string{ + "Add": "annotation", + }, }, } @@ -315,6 +344,11 @@ func TestLabelsWithNewServiceForCassandraDatacenter(t *testing.T) { if !reflect.DeepEqual(expected, service.Labels) { t.Errorf("service labels = \n %v \n, want \n %v", service.Labels, expected) } + if !reflect.DeepEqual(expected, service.Labels) { + t.Errorf("service labels = \n %v \n, want \n %v", service.Annotations, map[string]string{ + "Add": "annotation", + }) + } } func TestLabelsWithNewAdditionalSeedServiceForCassandraDatacenter(t *testing.T) { @@ -355,6 +389,9 @@ func TestLabelsWithNewAdditionalSeedServiceForCassandraDatacenter(t *testing.T) AdditionalLabels: map[string]string{ "Add": "label", }, + AdditionalAnnotations: map[string]string{ + "Add": "annotation", + }, }, } @@ -375,6 +412,11 @@ func TestLabelsWithNewAdditionalSeedServiceForCassandraDatacenter(t *testing.T) if !reflect.DeepEqual(expected, service.Labels) { t.Errorf("service labels = \n %v \n, want \n %v", service.Labels, expected) } + if !reflect.DeepEqual(expected, service.Labels) { + t.Errorf("service labels = \n %v \n, want \n %v", service.Annotations, map[string]string{ + "Add": "annotation", + }) + } } func TestAddingAdditionalLabels(t *testing.T) { @@ -409,6 +451,25 @@ func TestAddingAdditionalLabels(t *testing.T) { } } +func TestAddingAdditionalAnnotations(t *testing.T) { + dc := &api.CassandraDatacenter{ + ObjectMeta: metav1.ObjectMeta{ + Name: "dc1", + }, + Spec: api.CassandraDatacenterSpec{ + ClusterName: "piclem", + ServerVersion: "4.0.1", + AdditionalAnnotations: map[string]string{ + "Add": "annotation", + }, + }, + } + + service := newServiceForCassandraDatacenter(dc) + + assert.Contains(t, service.Annotations, "Add") +} + func TestServicePorts(t *testing.T) { tests := []struct { name string diff --git a/pkg/reconciliation/construct_statefulset.go b/pkg/reconciliation/construct_statefulset.go index 6df42f97..e48f3ce6 100644 --- a/pkg/reconciliation/construct_statefulset.go +++ b/pkg/reconciliation/construct_statefulset.go @@ -78,6 +78,9 @@ func newStatefulSetForCassandraDatacenter( statefulSetSelectorLabels := dc.GetRackLabels(rackName) + anns := make(map[string]string) + oplabels.AddOperatorAnnotations(anns, dc) + var volumeClaimTemplates []corev1.PersistentVolumeClaim rack := dc.GetRack(rackName) @@ -90,8 +93,9 @@ func newStatefulSetForCassandraDatacenter( volumeClaimTemplates = []corev1.PersistentVolumeClaim{{ ObjectMeta: metav1.ObjectMeta{ - Labels: pvcLabels, - Name: PvcName, + Labels: pvcLabels, + Name: PvcName, + Annotations: anns, }, Spec: *dc.Spec.StorageConfig.CassandraDataVolumeClaimSpec, }} @@ -100,8 +104,9 @@ func newStatefulSetForCassandraDatacenter( if storage.PVCSpec != nil { pvc := corev1.PersistentVolumeClaim{ ObjectMeta: metav1.ObjectMeta{ - Name: storage.Name, - Labels: pvcLabels, + Name: storage.Name, + Labels: pvcLabels, + Annotations: anns, }, Spec: *storage.PVCSpec, } @@ -126,9 +131,10 @@ func newStatefulSetForCassandraDatacenter( result := &appsv1.StatefulSet{ ObjectMeta: metav1.ObjectMeta{ - Name: nsName.Name, - Namespace: nsName.Namespace, - Labels: statefulSetLabels, + Name: nsName.Name, + Namespace: nsName.Namespace, + Labels: statefulSetLabels, + Annotations: anns, }, Spec: appsv1.StatefulSetSpec{ Selector: &metav1.LabelSelector{ @@ -141,7 +147,6 @@ func newStatefulSetForCassandraDatacenter( VolumeClaimTemplates: volumeClaimTemplates, }, } - result.Annotations = map[string]string{} if sts != nil && sts.Spec.ServiceName != "" && sts.Spec.ServiceName != result.Spec.ServiceName { result.Spec.ServiceName = sts.Spec.ServiceName diff --git a/pkg/reconciliation/construct_statefulset_test.go b/pkg/reconciliation/construct_statefulset_test.go index 48728aeb..0d6d1760 100644 --- a/pkg/reconciliation/construct_statefulset_test.go +++ b/pkg/reconciliation/construct_statefulset_test.go @@ -79,6 +79,9 @@ func Test_newStatefulSetForCassandraDatacenter_additionalLabels(t *testing.T) { AdditionalLabels: map[string]string{ "Add": "label", }, + AdditionalAnnotations: map[string]string{ + "Add": "annotation", + }, }, } @@ -120,6 +123,15 @@ func Test_newStatefulSetForCassandraDatacenter_additionalLabels(t *testing.T) { assert.Equal(t, expectedStatefulsetLabels, volumeClaim.Labels) } + assert.Contains(t, + statefulset.Annotations, "Add") + + for _, volumeClaim := range statefulset.Spec.VolumeClaimTemplates { + assert.Contains(t, + volumeClaim.Annotations, + "Add") + } + } func Test_newStatefulSetForCassandraDatacenter_rackNodeAffinitylabels(t *testing.T) { diff --git a/pkg/reconciliation/constructor.go b/pkg/reconciliation/constructor.go index 3f03ae00..d8053671 100644 --- a/pkg/reconciliation/constructor.go +++ b/pkg/reconciliation/constructor.go @@ -22,12 +22,15 @@ func newPodDisruptionBudgetForDatacenter(dc *api.CassandraDatacenter) *policyv1. labels := dc.GetDatacenterLabels() oplabels.AddOperatorLabels(labels, dc) selectorLabels := dc.GetDatacenterLabels() + anns := map[string]string{} + oplabels.AddOperatorAnnotations(anns, dc) + pdb := &policyv1.PodDisruptionBudget{ ObjectMeta: metav1.ObjectMeta{ Name: dc.SanitizedName() + "-pdb", Namespace: dc.Namespace, Labels: labels, - Annotations: map[string]string{}, + Annotations: anns, }, Spec: policyv1.PodDisruptionBudgetSpec{ Selector: &metav1.LabelSelector{ diff --git a/pkg/reconciliation/reconcile_racks.go b/pkg/reconciliation/reconcile_racks.go index 04361737..ce519f94 100644 --- a/pkg/reconciliation/reconcile_racks.go +++ b/pkg/reconciliation/reconcile_racks.go @@ -440,6 +440,40 @@ func (rc *ReconciliationContext) CheckRackLabels() result.ReconcileResult { rc.Recorder.Eventf(rc.Datacenter, corev1.EventTypeNormal, events.LabeledRackResource, "Update rack labels for StatefulSet %s", statefulSet.Name) } + + stsAnns := statefulSet.GetAnnotations() + oplabels.AddOperatorAnnotations(stsAnns, rc.Datacenter) + if !reflect.DeepEqual(stsAnns, statefulSet.GetAnnotations()) { + rc.ReqLogger.Info("Updating annotations", + "statefulSet", statefulSet, + "current", stsAnns, + "desired", updatedLabels) + statefulSet.SetAnnotations(stsAnns) + + if err := rc.Client.Patch(rc.Ctx, statefulSet, patch); err != nil { + return result.Error(err) + } + + rc.Recorder.Eventf(rc.Datacenter, corev1.EventTypeNormal, events.LabeledRackResource, + "Update rack annotations for StatefulSet %s", statefulSet.Name) + } + + ptsAnns := statefulSet.Spec.Template.GetAnnotations() + oplabels.AddOperatorAnnotations(ptsAnns, rc.Datacenter) + if !reflect.DeepEqual(ptsAnns, statefulSet.GetAnnotations()) { + rc.ReqLogger.Info("Updating annotations", + "statefulSet", statefulSet, + "current", ptsAnns, + "desired", updatedLabels) + statefulSet.Spec.Template.SetAnnotations(ptsAnns) + + if err := rc.Client.Patch(rc.Ctx, statefulSet, patch); err != nil { + return result.Error(err) + } + + rc.Recorder.Eventf(rc.Datacenter, corev1.EventTypeNormal, events.LabeledRackResource, + "Update pod template spec rack annotations for StatefulSet %s", statefulSet.Name) + } } return result.Continue() @@ -1616,6 +1650,26 @@ func (rc *ReconciliationContext) ReconcilePods(statefulSet *appsv1.StatefulSet) rc.Recorder.Eventf(rc.Datacenter, corev1.EventTypeNormal, events.LabeledRackResource, "Update rack labels for PersistentVolumeClaim %s", pvc.Name) } + pvcAnns := pvc.GetAnnotations() + oplabels.AddOperatorAnnotations(pvcAnns, rc.Datacenter) + if !reflect.DeepEqual(pvcAnns, pvc.GetAnnotations()) { + rc.ReqLogger.Info("Updating annotations", + "PVC", pvc, + "current", pvc.GetAnnotations(), + "desired", pvcAnns) + pvc.SetAnnotations(pvcAnns) + pvcPatch := client.MergeFrom(pvc.DeepCopy()) + if err := rc.Client.Patch(rc.Ctx, pvc, pvcPatch); err != nil { + rc.ReqLogger.Error( + err, + "Unable to update pvc with annotation", + "PVC", pvc, + ) + } + + rc.Recorder.Eventf(rc.Datacenter, corev1.EventTypeNormal, events.LabeledRackResource, + "Update rack annotations for pvc %s", pvc.Name) + } } return nil @@ -2125,12 +2179,15 @@ func (rc *ReconciliationContext) cleanupAfterScaling() result.ReconcileResult { func (rc *ReconciliationContext) createTask(command taskapi.CassandraCommand) error { generatedName := fmt.Sprintf("%s-%d", command, time.Now().Unix()) dc := rc.Datacenter + anns := make(map[string]string) + oplabels.AddOperatorAnnotations(anns, dc) task := &taskapi.CassandraTask{ ObjectMeta: metav1.ObjectMeta{ - Name: generatedName, - Namespace: rc.Datacenter.Namespace, - Labels: dc.GetDatacenterLabels(), + Name: generatedName, + Namespace: rc.Datacenter.Namespace, + Labels: dc.GetDatacenterLabels(), + Annotations: anns, }, Spec: taskapi.CassandraTaskSpec{ Datacenter: corev1.ObjectReference{ diff --git a/pkg/reconciliation/secrets.go b/pkg/reconciliation/secrets.go index 89c3663f..1a0090e3 100644 --- a/pkg/reconciliation/secrets.go +++ b/pkg/reconciliation/secrets.go @@ -57,6 +57,8 @@ func buildDefaultSuperuserSecret(dc *api.CassandraDatacenter) (*corev1.Secret, e if dc.ShouldGenerateSuperuserSecret() { labels := make(map[string]string) oplabels.AddOperatorLabels(labels, dc) + anns := make(map[string]string) + oplabels.AddOperatorAnnotations(anns, dc) secretNamespacedName := dc.GetSuperuserSecretNamespacedName() secret = &corev1.Secret{ @@ -65,9 +67,10 @@ func buildDefaultSuperuserSecret(dc *api.CassandraDatacenter) (*corev1.Secret, e APIVersion: "v1", }, ObjectMeta: metav1.ObjectMeta{ - Name: secretNamespacedName.Name, - Namespace: secretNamespacedName.Namespace, - Labels: labels, + Name: secretNamespacedName.Name, + Namespace: secretNamespacedName.Namespace, + Labels: labels, + Annotations: anns, }, } username := api.CleanupForKubernetes(dc.Spec.ClusterName) + "-superuser" diff --git a/pkg/reconciliation/secrets_test.go b/pkg/reconciliation/secrets_test.go index 39e813d6..192ddaaf 100644 --- a/pkg/reconciliation/secrets_test.go +++ b/pkg/reconciliation/secrets_test.go @@ -28,6 +28,7 @@ func Test_buildDefaultSuperuserSecret(t *testing.T) { AdditionalLabels: map[string]string{ "piclem": "add", }, + AdditionalAnnotations: map[string]string{"add": "annotation"}, }, } secret, err := buildDefaultSuperuserSecret(dc) @@ -59,6 +60,10 @@ func Test_buildDefaultSuperuserSecret(t *testing.T) { "piclem": "add", } + if !reflect.DeepEqual(map[string]string{"add": "annotation"}, secret.Annotations) { + t.Errorf("annotations = \n %v \n, want \n %v", secret.Annotations, map[string]string{"add": "annotation"}) + } + if !reflect.DeepEqual(expectedSecretLabels, secret.Labels) { t.Errorf("labels = \n %v \n, want \n %v", secret.Labels, expectedSecretLabels) }