Skip to content

Commit

Permalink
Add AdditionalAnnotations field and logic to reconcile it.
Browse files Browse the repository at this point in the history
  • Loading branch information
Miles-Garnsey committed Dec 19, 2023
1 parent 4424704 commit 3aacb8e
Show file tree
Hide file tree
Showing 13 changed files with 130 additions and 25 deletions.
3 changes: 3 additions & 0 deletions apis/cassandra/v1beta1/cassandradatacenter_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`

Expand Down
2 changes: 1 addition & 1 deletion internal/controllers/control/cassandratask_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ func (r *CassandraTaskReconciler) Reconcile(ctx context.Context, req ctrl.Reques
}

utils.MergeMap(cassTask.Labels, dc.GetDatacenterLabels())
oplabels.AddOperatorLabels(cassTask.GetLabels(), dc)
oplabels.AddOperatorTags(cassTask.GetLabels(), dc)

// Starting the run, set the Active label so we can quickly fetch the active ones
cassTask.GetLabels()[taskStatusLabel] = activeTaskLabelValue
Expand Down
8 changes: 7 additions & 1 deletion pkg/oplabels/labels.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ const (
CreatedByLabelValue = ManagedByLabelValue
)

func AddOperatorLabels(m map[string]string, dc *api.CassandraDatacenter) {
func AddOperatorTags(m map[string]string, dc *api.CassandraDatacenter) {
m[ManagedByLabel] = ManagedByLabelValue
m[NameLabel] = NameLabelValue
m[VersionLabel] = dc.Spec.ServerVersion
Expand All @@ -32,6 +32,12 @@ func AddOperatorLabels(m map[string]string, dc *api.CassandraDatacenter) {
m[key] = api.CleanLabelValue(value)
}
}

if len(dc.Spec.AdditionalAnnotations) != 0 {
for key, value := range dc.Spec.AdditionalAnnotations {
m[key] = api.CleanLabelValue(value)
}
}
}

func HasManagedByCassandraOperatorLabel(m map[string]string) bool {
Expand Down
2 changes: 1 addition & 1 deletion pkg/reconciliation/construct_podtemplatespec.go
Original file line number Diff line number Diff line change
Expand Up @@ -799,7 +799,7 @@ func buildPodTemplateSpec(dc *api.CassandraDatacenter, rack api.Rack, addLegacyI
// Labels

podLabels := dc.GetRackLabels(rack.Name)
oplabels.AddOperatorLabels(podLabels, dc)
oplabels.AddOperatorTags(podLabels, dc)
podLabels[api.CassNodeState] = stateReadyToStart

if baseTemplate.Labels == nil {
Expand Down
30 changes: 20 additions & 10 deletions pkg/reconciliation/construct_podtemplatespec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -977,7 +977,7 @@ func TestCassandraDatacenter_buildPodTemplateSpec_labels_merge(t *testing.T) {
got := spec.Labels

expected := dc.GetRackLabels("testrack")
oplabels.AddOperatorLabels(expected, dc)
oplabels.AddOperatorTags(expected, dc)
expected[api.CassNodeState] = stateReadyToStart
expected["app.kubernetes.io/managed-by"] = oplabels.ManagedByLabelValue
expected["abc"] = "123"
Expand All @@ -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.AddOperatorTags(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) {
Expand Down
8 changes: 4 additions & 4 deletions pkg/reconciliation/construct_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ func newSeedServiceForCassandraDatacenter(dc *api.CassandraDatacenter) *corev1.S
service.ObjectMeta.Name = dc.GetSeedServiceName()

labels := dc.GetClusterLabels()
oplabels.AddOperatorLabels(labels, dc)
oplabels.AddOperatorTags(labels, dc)
service.ObjectMeta.Labels = labels

service.Spec.Selector = buildLabelSelectorForSeedService(dc)
Expand All @@ -135,7 +135,7 @@ func newSeedServiceForCassandraDatacenter(dc *api.CassandraDatacenter) *corev1.S
// whether the additional seed pods are ready or not
func newAdditionalSeedServiceForCassandraDatacenter(dc *api.CassandraDatacenter) *corev1.Service {
labels := dc.GetDatacenterLabels()
oplabels.AddOperatorLabels(labels, dc)
oplabels.AddOperatorTags(labels, dc)
var service corev1.Service
service.ObjectMeta.Name = dc.GetAdditionalSeedsServiceName()
service.ObjectMeta.Namespace = dc.Namespace
Expand All @@ -154,7 +154,7 @@ func newAdditionalSeedServiceForCassandraDatacenter(dc *api.CassandraDatacenter)

func newEndpointsForAdditionalSeeds(dc *api.CassandraDatacenter) (*corev1.Endpoints, error) {
labels := dc.GetDatacenterLabels()
oplabels.AddOperatorLabels(labels, dc)
oplabels.AddOperatorTags(labels, dc)
endpoints := corev1.Endpoints{}
endpoints.ObjectMeta.Name = dc.GetAdditionalSeedsServiceName()
endpoints.ObjectMeta.Namespace = dc.Namespace
Expand Down Expand Up @@ -282,7 +282,7 @@ func newAllPodsServiceForCassandraDatacenter(dc *api.CassandraDatacenter) *corev
// inside the k8s cluster.
func makeGenericHeadlessService(dc *api.CassandraDatacenter) *corev1.Service {
labels := dc.GetDatacenterLabels()
oplabels.AddOperatorLabels(labels, dc)
oplabels.AddOperatorTags(labels, dc)
selector := dc.GetDatacenterLabels()
var service corev1.Service
service.ObjectMeta.Namespace = dc.Namespace
Expand Down
69 changes: 68 additions & 1 deletion pkg/reconciliation/construct_service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -115,6 +116,9 @@ func TestLabelsWithNewSeedServiceForCassandraDatacenter(t *testing.T) {
AdditionalLabels: map[string]string{
"Add": "label",
},
AdditionalAnnotations: map[string]string{
"Add": "annotation",
},
},
}

Expand All @@ -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) {
Expand Down Expand Up @@ -174,6 +184,9 @@ func TestLabelsWithNewNodePortServiceForCassandraDatacenter(t *testing.T) {
AdditionalLabels: map[string]string{
"Add": "label",
},
AdditionalAnnotations: map[string]string{
"Add": "annotation",
},
},
}

Expand All @@ -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) {
Expand Down Expand Up @@ -234,6 +252,9 @@ func TestLabelsWithNewAllPodsServiceForCassandraDatacenter(t *testing.T) {
AdditionalLabels: map[string]string{
"Add": "label",
},
AdditionalAnnotations: map[string]string{
"Add": "annotation",
},
},
}

Expand All @@ -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) {
Expand Down Expand Up @@ -295,6 +321,9 @@ func TestLabelsWithNewServiceForCassandraDatacenter(t *testing.T) {
AdditionalLabels: map[string]string{
"Add": "label",
},
AdditionalAnnotations: map[string]string{
"Add": "annotation",
},
},
}

Expand All @@ -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) {
Expand Down Expand Up @@ -355,6 +389,9 @@ func TestLabelsWithNewAdditionalSeedServiceForCassandraDatacenter(t *testing.T)
AdditionalLabels: map[string]string{
"Add": "label",
},
AdditionalAnnotations: map[string]string{
"Add": "annotation",
},
},
}

Expand All @@ -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) {
Expand Down Expand Up @@ -409,6 +451,31 @@ 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",
},
},
}

expected := map[string]string{
"Add": "annotation",
}

service := newServiceForCassandraDatacenter(dc)

if !reflect.DeepEqual(expected, service.Annotations) {
t.Errorf("service Annotations = %v, want %v", service.Annotations, expected)
}
}

func TestServicePorts(t *testing.T) {
tests := []struct {
name string
Expand Down
4 changes: 2 additions & 2 deletions pkg/reconciliation/construct_statefulset.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,10 @@ func newStatefulSetForCassandraDatacenter(
// see https://github.com/kubernetes/kubernetes/pull/74941
// pvc labels are ignored before k8s 1.15.0
pvcLabels := dc.GetRackLabels(rackName)
oplabels.AddOperatorLabels(pvcLabels, dc)
oplabels.AddOperatorTags(pvcLabels, dc)

statefulSetLabels := dc.GetRackLabels(rackName)
oplabels.AddOperatorLabels(statefulSetLabels, dc)
oplabels.AddOperatorTags(statefulSetLabels, dc)

statefulSetSelectorLabels := dc.GetRackLabels(rackName)

Expand Down
14 changes: 14 additions & 0 deletions pkg/reconciliation/construct_statefulset_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,9 @@ func Test_newStatefulSetForCassandraDatacenter_additionalLabels(t *testing.T) {
AdditionalLabels: map[string]string{
"Add": "label",
},
AdditionalAnnotations: map[string]string{
"Add": "annotation",
},
},
}

Expand Down Expand Up @@ -120,6 +123,17 @@ func Test_newStatefulSetForCassandraDatacenter_additionalLabels(t *testing.T) {
assert.Equal(t, expectedStatefulsetLabels, volumeClaim.Labels)
}

assert.Equal(t,
map[string]string{
"Add": "annotation"},
statefulset.Annotations)

for _, volumeClaim := range statefulset.Spec.VolumeClaimTemplates {
assert.Equal(t, map[string]string{
"Add": "annotation"},
volumeClaim.Annotations)
}

}

func Test_newStatefulSetForCassandraDatacenter_rackNodeAffinitylabels(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion pkg/reconciliation/constructor.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import (
func newPodDisruptionBudgetForDatacenter(dc *api.CassandraDatacenter) *policyv1.PodDisruptionBudget {
minAvailable := intstr.FromInt(int(dc.Spec.Size - 1))
labels := dc.GetDatacenterLabels()
oplabels.AddOperatorLabels(labels, dc)
oplabels.AddOperatorTags(labels, dc)
selectorLabels := dc.GetDatacenterLabels()
pdb := &policyv1.PodDisruptionBudget{
ObjectMeta: metav1.ObjectMeta{
Expand Down
6 changes: 3 additions & 3 deletions pkg/reconciliation/reconcile_racks.go
Original file line number Diff line number Diff line change
Expand Up @@ -1634,15 +1634,15 @@ func mergeInLabelsIfDifferent(existingLabels, newLabels map[string]string) (bool
// resource. It will return the updated map and a boolean denoting whether the resource needs to be updated with the new labels.
func shouldUpdateLabelsForClusterResource(resourceLabels map[string]string, dc *api.CassandraDatacenter) (bool, map[string]string) {
desired := dc.GetClusterLabels()
oplabels.AddOperatorLabels(desired, dc)
oplabels.AddOperatorTags(desired, dc)
return mergeInLabelsIfDifferent(resourceLabels, desired)
}

// shouldUpdateLabelsForRackResource will compare the labels passed in with what the labels should be for a rack level
// resource. It will return the updated map and a boolean denoting whether the resource needs to be updated with the new labels.
func shouldUpdateLabelsForRackResource(resourceLabels map[string]string, dc *api.CassandraDatacenter, rackName string) (bool, map[string]string) {
desired := dc.GetRackLabels(rackName)
oplabels.AddOperatorLabels(desired, dc)
oplabels.AddOperatorTags(desired, dc)
return mergeInLabelsIfDifferent(resourceLabels, desired)
}

Expand Down Expand Up @@ -2149,7 +2149,7 @@ func (rc *ReconciliationContext) createTask(command taskapi.CassandraCommand) er
Status: taskapi.CassandraTaskStatus{},
}

oplabels.AddOperatorLabels(task.GetLabels(), dc)
oplabels.AddOperatorTags(task.GetLabels(), dc)

if err := rc.Client.Create(rc.Ctx, task); err != nil {
return err
Expand Down
2 changes: 1 addition & 1 deletion pkg/reconciliation/secrets.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func buildDefaultSuperuserSecret(dc *api.CassandraDatacenter) (*corev1.Secret, e

if dc.ShouldGenerateSuperuserSecret() {
labels := make(map[string]string)
oplabels.AddOperatorLabels(labels, dc)
oplabels.AddOperatorTags(labels, dc)

secretNamespacedName := dc.GetSuperuserSecretNamespacedName()
secret = &corev1.Secret{
Expand Down
5 changes: 5 additions & 0 deletions pkg/reconciliation/secrets_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -59,6 +60,10 @@ func Test_buildDefaultSuperuserSecret(t *testing.T) {
"piclem": "add",
}

if !reflect.DeepEqual(map[string]string{"add": "annotation"}, secret.Annotations) {
t.Errorf("labels = \n %v \n, want \n %v", secret.Labels, expectedSecretLabels)
}

if !reflect.DeepEqual(expectedSecretLabels, secret.Labels) {
t.Errorf("labels = \n %v \n, want \n %v", secret.Labels, expectedSecretLabels)
}
Expand Down

0 comments on commit 3aacb8e

Please sign in to comment.