From 81bc8e9be0da853c7c880bab4a2716e1ab49181f Mon Sep 17 00:00:00 2001 From: Olivier Michallat Date: Mon, 26 Aug 2024 11:04:33 -0700 Subject: [PATCH] Allow k8ssandra.io labels and annotations in services config (fixes #689) --- CHANGELOG.md | 1 + .../v1beta1/cassandradatacenter_webhook.go | 7 +++-- apis/cassandra/v1beta1/webhook_test.go | 27 ++++++++++++++++--- 3 files changed, 27 insertions(+), 8 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f64efb0a..0d611b1f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,6 +12,7 @@ Changelog for Cass Operator, new PRs should update the `main / unreleased` secti ## unreleased * [BUGFIX] [#687](https://github.com/k8ssandra/cass-operator/issues/687) Prevent a crash when when StorageClassName was not set in the CassandraDataVolumeClaimSpec +* [CHANGE] [#689](https://github.com/k8ssandra/cass-operator/issues/689) Allow k8ssandra.io labels and annotations in services config ## v1.22.0 diff --git a/apis/cassandra/v1beta1/cassandradatacenter_webhook.go b/apis/cassandra/v1beta1/cassandradatacenter_webhook.go index ce7bf31b..ca1b0af0 100644 --- a/apis/cassandra/v1beta1/cassandradatacenter_webhook.go +++ b/apis/cassandra/v1beta1/cassandradatacenter_webhook.go @@ -34,8 +34,7 @@ import ( ) const ( - datastaxPrefix string = "cassandra.datastax.com" - k8ssandraPrefix string = "k8ssandra.io" + datastaxPrefix string = "cassandra.datastax.com" ) var log = logf.Log.WithName("api") @@ -341,7 +340,7 @@ func ValidateServiceLabelsAndAnnotations(dc CassandraDatacenter) error { for svcName, config := range services { if containsReservedAnnotations(config) || containsReservedLabels(config) { - return attemptedTo(fmt.Sprintf("configure %s with reserved annotations and/or labels (prefixes %s and/or %s)", svcName, datastaxPrefix, k8ssandraPrefix)) + return attemptedTo("configure %s with reserved annotations and/or labels (prefix %s)", svcName, datastaxPrefix) } } @@ -365,7 +364,7 @@ func containsReservedLabels(config ServiceConfigAdditions) bool { func containsReservedPrefixes(config map[string]string) bool { for k := range config { - if strings.HasPrefix(k, datastaxPrefix) || strings.HasPrefix(k, k8ssandraPrefix) { + if strings.HasPrefix(k, datastaxPrefix) { // reserved prefix found return true } diff --git a/apis/cassandra/v1beta1/webhook_test.go b/apis/cassandra/v1beta1/webhook_test.go index 61f40223..68bc2fe0 100644 --- a/apis/cassandra/v1beta1/webhook_test.go +++ b/apis/cassandra/v1beta1/webhook_test.go @@ -327,7 +327,7 @@ func Test_ValidateSingleDatacenter(t *testing.T) { errString: "use multiple nodes per worker without cpu and memory requests and limits", }, { - name: "Prevent user specified reserved Service labels and annotations", + name: "Prevent user specified cassandra.datastax.com Service labels and annotations", dc: &CassandraDatacenter{ ObjectMeta: metav1.ObjectMeta{ Name: "exampleDC", @@ -337,13 +337,32 @@ func Test_ValidateSingleDatacenter(t *testing.T) { ServerVersion: "4.0.4", AdditionalServiceConfig: ServiceConfig{ DatacenterService: ServiceConfigAdditions{ - Labels: map[string]string{"k8ssandra.io/key1": "val1", "cassandra.datastax.com/key2": "val2"}, - Annotations: map[string]string{"k8ssandra.io/key3": "val3", "cassandra.datastax.com/key4": "val4"}, + Labels: map[string]string{"cassandra.datastax.com/key1": "val1"}, + Annotations: map[string]string{"cassandra.datastax.com/key2": "val2"}, }, }, }, }, - errString: "configure DatacenterService with reserved annotations and/or labels (prefixes cassandra.datastax.com and/or k8ssandra.io)", + errString: "configure DatacenterService with reserved annotations and/or labels (prefix cassandra.datastax.com)", + }, + { + name: "Allow user specified k8ssandra.io Service labels and annotations", + dc: &CassandraDatacenter{ + ObjectMeta: metav1.ObjectMeta{ + Name: "exampleDC", + }, + Spec: CassandraDatacenterSpec{ + ServerType: "cassandra", + ServerVersion: "4.0.4", + AdditionalServiceConfig: ServiceConfig{ + DatacenterService: ServiceConfigAdditions{ + Labels: map[string]string{"k8ssandra.io/key1": "val1"}, + Annotations: map[string]string{"k8ssandra.io/key2": "val2"}, + }, + }, + }, + }, + errString: "", }, { name: "Allow upgrade should not accept invalid values",