diff --git a/CHANGELOG.md b/CHANGELOG.md index 0e1d5dee..2c29c2b4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,8 @@ Changelog for Cass Operator, new PRs should update the `main / unreleased` secti ## unreleased +* [CHANGE] [#689]((https://github.com/k8ssandra/cass-operator/issues/689) Allow some internal labels and annotaions in services config + ## v1.22.0 * [FEATURE] [#263]((https://github.com/k8ssandra/cass-operator/issues/263) Allow increasing the size of CassandraDataVolumeClaimSpec if the selected StorageClass supports it. This feature is currently behind a opt-in feature flag and requires an annotation ``cassandra.datastax.com/allow-storage-changes: true`` to be set in the CassandraDatacenter. diff --git a/apis/cassandra/v1beta1/cassandradatacenter_webhook.go b/apis/cassandra/v1beta1/cassandradatacenter_webhook.go index ce7bf31b..421ccb73 100644 --- a/apis/cassandra/v1beta1/cassandradatacenter_webhook.go +++ b/apis/cassandra/v1beta1/cassandradatacenter_webhook.go @@ -20,6 +20,7 @@ import ( "encoding/json" "errors" "fmt" + "slices" "strings" "github.com/google/go-cmp/cmp" @@ -38,6 +39,10 @@ const ( k8ssandraPrefix string = "k8ssandra.io" ) +// Some exceptions to the reserved prefixes above: +var allowedServiceLabels = []string{"k8ssandra.io/cluster-name", "k8ssandra.io/cluster-namespace"} +var allowedServiceAnnotations []string + var log = logf.Log.WithName("api") func (dc *CassandraDatacenter) SetupWebhookWithManager(mgr ctrl.Manager) error { @@ -333,9 +338,9 @@ func ValidateServiceLabelsAndAnnotations(dc CassandraDatacenter) error { services := map[string]ServiceConfigAdditions{ "AdditionalSeedService": addSeedSvc, - "AllPOdsService": allPodsSvc, + "AllPodsService": allPodsSvc, "DatacenterService": dcSvc, - "NodePOrtService": nodePortSvc, + "NodePortService": nodePortSvc, "SeedService": seedSvc, } @@ -356,15 +361,18 @@ func ValidateServiceLabelsAndAnnotations(dc CassandraDatacenter) error { } func containsReservedAnnotations(config ServiceConfigAdditions) bool { - return containsReservedPrefixes(config.Annotations) + return containsReservedPrefixes(config.Annotations, allowedServiceAnnotations) } func containsReservedLabels(config ServiceConfigAdditions) bool { - return containsReservedPrefixes(config.Labels) + return containsReservedPrefixes(config.Labels, allowedServiceLabels) } -func containsReservedPrefixes(config map[string]string) bool { +func containsReservedPrefixes(config map[string]string, exceptions []string) bool { for k := range config { + if slices.Contains(exceptions, k) { + continue + } if strings.HasPrefix(k, datastaxPrefix) || strings.HasPrefix(k, k8ssandraPrefix) { // reserved prefix found return true diff --git a/apis/cassandra/v1beta1/webhook_test.go b/apis/cassandra/v1beta1/webhook_test.go index 61f40223..100c7f71 100644 --- a/apis/cassandra/v1beta1/webhook_test.go +++ b/apis/cassandra/v1beta1/webhook_test.go @@ -345,6 +345,27 @@ func Test_ValidateSingleDatacenter(t *testing.T) { }, errString: "configure DatacenterService with reserved annotations and/or labels (prefixes cassandra.datastax.com and/or k8ssandra.io)", }, + { + name: "Allow exceptions for user specified reserved 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/cluster-name": "kc", + "k8ssandra.io/cluster-namespace": "ns", + }, + }, + }, + }, + }, + errString: "", + }, { name: "Allow upgrade should not accept invalid values", dc: &CassandraDatacenter{