Skip to content

Commit

Permalink
Allow some internal labels and annotaions in services config (fixes #689
Browse files Browse the repository at this point in the history
)
  • Loading branch information
olim7t committed Aug 23, 2024
1 parent 7199188 commit 26fe263
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 5 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
18 changes: 13 additions & 5 deletions apis/cassandra/v1beta1/cassandradatacenter_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"encoding/json"
"errors"
"fmt"
"slices"
"strings"

"github.com/google/go-cmp/cmp"
Expand All @@ -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 {
Expand Down Expand Up @@ -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,
}

Expand All @@ -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
Expand Down
21 changes: 21 additions & 0 deletions apis/cassandra/v1beta1/webhook_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down

0 comments on commit 26fe263

Please sign in to comment.