Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix name override issues #1246

Merged
merged 5 commits into from
Mar 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion CHANGELOG/CHANGELOG-1.14.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,6 @@ Changelog for the K8ssandra Operator, new PRs should update the `unreleased` sec
When cutting a new release, update the `unreleased` heading to the tag being generated and date, like `## vX.Y.Z - YYYY-MM-DD` and create a new placeholder section for `unreleased` entries.

## unreleased
* [BUGFIX] [#1226](https://github.com/k8ssandra/k8ssandra-operator/issues/1226) Medusa purge cronjob should be created in the operator namespace
* [BUGFIX] [#1226](https://github.com/k8ssandra/k8ssandra-operator/issues/1226) Medusa purge cronjob should be created in the operator namespace
* [BUGFIX] [#1141](https://github.com/k8ssandra/k8ssandra-operator/issues/1141) Use DC name override when naming secondary resources
* [BUGFIX] [#1138](https://github.com/k8ssandra/k8ssandra-operator/issues/1138) Use cluster name override for metrics agent ConfigMap
11 changes: 10 additions & 1 deletion apis/k8ssandra/v1alpha1/k8ssandracluster_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ package v1alpha1

import (
"fmt"
"k8s.io/apimachinery/pkg/util/validation"
"strings"

"github.com/Masterminds/semver/v3"
"github.com/k8ssandra/k8ssandra-operator/pkg/clientcache"
Expand Down Expand Up @@ -69,8 +71,15 @@ func (r *K8ssandraCluster) ValidateCreate() error {

func (r *K8ssandraCluster) validateK8ssandraCluster() error {
hasClusterStorageConfig := r.Spec.Cassandra.DatacenterOptions.StorageConfig != nil
// Verify given k8s-contexts are correct
for _, dc := range r.Spec.Cassandra.Datacenters {
dns1035Errs := validation.IsDNS1035Label(dc.Meta.Name)
if len(dns1035Errs) > 0 {
return fmt.Errorf(
"invalid DC name (you might want to use datacenterName to override the name used in Cassandra): %s",
strings.Join(dns1035Errs, ", "))
}

// Verify given k8s-context is correct
_, err := clientCache.GetRemoteClient(dc.K8sContext)
if err != nil {
// No client found for this context name, reject
Expand Down
14 changes: 14 additions & 0 deletions apis/k8ssandra/v1alpha1/k8ssandracluster_webhook_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ func TestWebhook(t *testing.T) {
t.Run("NumTokensValidationInUpdate", testNumTokensInUpdate)
t.Run("StsNameTooLong", testStsNameTooLong)
t.Run("MedusaPrefixMissing", testMedusaPrefixMissing)
t.Run("InvalidDcName", testInvalidDcName)
}

func testContextValidation(t *testing.T) {
Expand Down Expand Up @@ -219,6 +220,7 @@ func testStorageConfigValidation(t *testing.T) {
required.NoError(err)

cluster.Spec.Cassandra.Datacenters = append(cluster.Spec.Cassandra.Datacenters, CassandraDatacenterTemplate{
Meta: EmbeddedObjectMeta{Name: "dc2"},
K8sContext: "envtest",
Size: 1,
})
Expand Down Expand Up @@ -376,6 +378,7 @@ func createClusterObjWithCassandraConfig(name, namespace string) *K8ssandraClust

Datacenters: []CassandraDatacenterTemplate{
{
Meta: EmbeddedObjectMeta{Name: "dc1"},
K8sContext: "envtest",
Size: 1,
},
Expand Down Expand Up @@ -451,3 +454,14 @@ func testMedusaPrefixMissing(t *testing.T) {
err = k8sClient.Create(ctx, clusterWithPrefix)
required.NoError(err)
}

func testInvalidDcName(t *testing.T) {
required := require.New(t)
createNamespace(required, "ns")

clusterWithBadDcName := createMinimalClusterObj("bad-dc-name", "ns")
clusterWithBadDcName.Spec.Cassandra.Datacenters[0].Meta.Name = "DC1"
err := k8sClient.Create(ctx, clusterWithBadDcName)
required.Error(err)
required.Contains(err.Error(), "invalid DC name")
}
2 changes: 1 addition & 1 deletion controllers/k8ssandra/cassandra_telemetry_reconciler.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func (r *K8ssandraClusterReconciler) reconcileCassandraDCTelemetry(
cfg := telemetry.PrometheusResourcer{
MonitoringTargetNS: actualDc.Namespace,
MonitoringTargetName: actualDc.Name,
ServiceMonitorName: kc.SanitizedName() + "-" + actualDc.DatacenterName() + "-" + "cass-servicemonitor",
ServiceMonitorName: cassdcapi.CleanupForKubernetes(kc.CassClusterName() + "-" + actualDc.DatacenterName() + "-" + "cass-servicemonitor"),
Logger: logger,
CommonLabels: mustLabels(kc.Name, kc.Namespace, actualDc.DatacenterName(), commonLabels),
}
Expand Down
4 changes: 2 additions & 2 deletions controllers/k8ssandra/per_node_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,10 @@ func (r *K8ssandraClusterReconciler) reconcileDefaultPerNodeConfiguration(

kcKey := utils.GetKey(kc)

perNodeConfigKey := nodeconfig.NewDefaultPerNodeConfigMapKey(kcKey, dcConfig)
perNodeConfigKey := nodeconfig.NewDefaultPerNodeConfigMapKey(kc, dcConfig)
dcLogger = dcLogger.WithValues("PerNodeConfigMap", perNodeConfigKey)

desiredPerNodeConfig := nodeconfig.NewDefaultPerNodeConfigMap(kcKey, dcConfig)
desiredPerNodeConfig := nodeconfig.NewDefaultPerNodeConfigMap(kcKey, kc, dcConfig)
if desiredPerNodeConfig != nil {
annotations.AddHashAnnotation(desiredPerNodeConfig)
}
Expand Down
2 changes: 1 addition & 1 deletion controllers/reaper/vector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,6 @@ func TestBuildVectorAgentConfigMap(t *testing.T) {
vectorToml := "Test"
vectorConfigMap := reaperpkg.CreateVectorConfigMap("k8ssandra-operator", vectorToml, test.NewCassandraDatacenter("testDc", "k8ssandra-operator"))
assert.Equal(t, vectorToml, vectorConfigMap.Data["vector.toml"])
assert.Equal(t, "test-cluster-testDc-reaper-vector", vectorConfigMap.Name)
assert.Equal(t, "test-cluster-testdc-reaper-vector", vectorConfigMap.Name)
assert.Equal(t, "k8ssandra-operator", vectorConfigMap.Namespace)
}
2 changes: 1 addition & 1 deletion controllers/stargate/vector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,6 @@ func TestBuildVectorAgentConfigMap(t *testing.T) {
vectorToml := "Test"
vectorConfigMap := stargatepkg.CreateVectorConfigMap("k8ssandra-operator", vectorToml, test.NewCassandraDatacenter("testDc", "k8ssandra-operator"))
assert.Equal(t, vectorToml, vectorConfigMap.Data["vector.toml"])
assert.Equal(t, "test-cluster-testDc-stargate-vector", vectorConfigMap.Name)
assert.Equal(t, "test-cluster-testdc-stargate-vector", vectorConfigMap.Name)
assert.Equal(t, "k8ssandra-operator", vectorConfigMap.Namespace)
}
11 changes: 6 additions & 5 deletions pkg/nodeconfig/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package nodeconfig

import (
"fmt"
cassdcapi "github.com/k8ssandra/cass-operator/apis/cassandra/v1beta1"
"strings"

"github.com/k8ssandra/k8ssandra-operator/pkg/labels"
Expand All @@ -16,9 +17,9 @@ import (

// NewDefaultPerNodeConfigMap generates a ConfigMap that contains default per-node configuration
// files for this DC. It returns nil if this DC does not require any per-node configuration.
func NewDefaultPerNodeConfigMap(kcKey types.NamespacedName, dcConfig *cassandra.DatacenterConfig) *corev1.ConfigMap {
func NewDefaultPerNodeConfigMap(kcKey types.NamespacedName, kc *k8ssandraapi.K8ssandraCluster, dcConfig *cassandra.DatacenterConfig) *corev1.ConfigMap {

configKey := NewDefaultPerNodeConfigMapKey(kcKey, dcConfig)
configKey := NewDefaultPerNodeConfigMapKey(kc, dcConfig)
perNodeConfig := newPerNodeConfigMap(kcKey, configKey)

// append all the default per-node configuration to the ConfigMap;
Expand All @@ -36,10 +37,10 @@ func NewDefaultPerNodeConfigMap(kcKey types.NamespacedName, dcConfig *cassandra.
return nil
}

func NewDefaultPerNodeConfigMapKey(kcKey types.NamespacedName, dcConfig *cassandra.DatacenterConfig) types.NamespacedName {
func NewDefaultPerNodeConfigMapKey(kc *k8ssandraapi.K8ssandraCluster, dcConfig *cassandra.DatacenterConfig) types.NamespacedName {
return types.NamespacedName{
Name: fmt.Sprintf("%s-%s-per-node-config", kcKey.Name, dcConfig.CassDcName()),
Namespace: utils.FirstNonEmptyString(dcConfig.Meta.Namespace, kcKey.Namespace),
Name: cassdcapi.CleanupForKubernetes(kc.CassClusterName() + "-" + dcConfig.CassDcName() + "-per-node-config"),
Namespace: utils.FirstNonEmptyString(dcConfig.Meta.Namespace, kc.Namespace),
}
}

Expand Down
5 changes: 4 additions & 1 deletion pkg/nodeconfig/generate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,10 @@ func TestNewDefaultPerNodeConfigMap(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := NewDefaultPerNodeConfigMap(tt.kcKey, tt.dc)
kc := &api.K8ssandraCluster{
ObjectMeta: metav1.ObjectMeta{Name: tt.kcKey.Name, Namespace: tt.kcKey.Namespace},
}
got := NewDefaultPerNodeConfigMap(tt.kcKey, kc, tt.dc)
assert.Equal(t, tt.want, got)
})
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/reaper/vector.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ const (
// VectorAgentConfigMapName generates a ConfigMap name based on
// the K8s sanitized cluster name and datacenter name.
func VectorAgentConfigMapName(clusterName, dcName string) string {
return fmt.Sprintf("%s-%s-%s", cassdcapi.CleanupForKubernetes(clusterName), dcName, vectorConfigMap)
return fmt.Sprintf("%s-%s-%s", cassdcapi.CleanupForKubernetes(clusterName), cassdcapi.CleanupForKubernetes(dcName), vectorConfigMap)
}

func CreateVectorConfigMap(namespace, vectorToml string, dc cassdcapi.CassandraDatacenter) *corev1.ConfigMap {
Expand Down
2 changes: 1 addition & 1 deletion pkg/stargate/deployments.go
Original file line number Diff line number Diff line change
Expand Up @@ -513,5 +513,5 @@ func createPodMeta(stargate *api.Stargate, deploymentName string) meta.Tags {
}

func GeneratedConfigMapName(clusterName, dcName string) string {
return fmt.Sprintf("%s-%s-%s", cassdcapi.CleanupForKubernetes(clusterName), dcName, cassandraConfigMap)
return fmt.Sprintf("%s-%s-%s", cassdcapi.CleanupForKubernetes(clusterName), cassdcapi.CleanupForKubernetes(dcName), cassandraConfigMap)
}
2 changes: 1 addition & 1 deletion pkg/stargate/vector.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ const (
// VectorAgentConfigMapName generates a ConfigMap name based on
// the K8s sanitized cluster name and datacenter name.
func VectorAgentConfigMapName(clusterName, dcName string) string {
return fmt.Sprintf("%s-%s-%s", cassdcapi.CleanupForKubernetes(clusterName), dcName, vectorConfigMap)
return fmt.Sprintf("%s-%s-%s", cassdcapi.CleanupForKubernetes(clusterName), cassdcapi.CleanupForKubernetes(dcName), vectorConfigMap)
}

func CreateVectorConfigMap(namespace, vectorToml string, dc cassdcapi.CassandraDatacenter) *corev1.ConfigMap {
Expand Down
8 changes: 6 additions & 2 deletions pkg/telemetry/cassandra_agent/cassandra_agent_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,13 +135,17 @@
cm := corev1.ConfigMap{
ObjectMeta: metav1.ObjectMeta{
Namespace: c.DcNamespace,
Name: c.Kluster.Name + "-" + c.DcName + "-metrics-agent-config",
Name: c.configMapName(),
},
Data: map[string]string{filepath.Base(agentConfigLocation): string(yamlData)},
}
return &cm, nil
}

func (c Configurator) configMapName() string {
return cassdcapi.CleanupForKubernetes(c.Kluster.CassClusterName() + "-" + c.DcName + "-metrics-agent-config")
}

func (c Configurator) ReconcileTelemetryAgentConfig(dc *cassdcapi.CassandraDatacenter) result.ReconcileResult {
//Reconcile the agent's ConfigMap
desiredCm, err := c.GetTelemetryAgentConfigMap()
Expand Down Expand Up @@ -182,7 +186,7 @@
},
},
LocalObjectReference: corev1.LocalObjectReference{
Name: c.Kluster.Name + "-" + c.DcName + "-metrics-agent-config",
Name: c.configMapName(),

Check warning on line 189 in pkg/telemetry/cassandra_agent/cassandra_agent_config.go

View check run for this annotation

Codecov / codecov/patch

pkg/telemetry/cassandra_agent/cassandra_agent_config.go#L189

Added line #L189 was not covered by tests
},
},
},
Expand Down
2 changes: 1 addition & 1 deletion pkg/telemetry/vector.go
Original file line number Diff line number Diff line change
Expand Up @@ -330,5 +330,5 @@ func BuildVectorAgentConfigMap(namespace, k8cName, dcName, k8cNamespace, vectorT
}

func VectorAgentConfigMapName(k8cName, dcName string) string {
return fmt.Sprintf("%s-%s-cass-vector", k8cName, dcName)
return cassdcapi.CleanupForKubernetes(fmt.Sprintf("%s-%s-cass-vector", k8cName, dcName))
}
Loading