From dc08a943885d4961240ede16c3ad6bb557a18060 Mon Sep 17 00:00:00 2001 From: Karol Szwaj Date: Tue, 28 Nov 2023 10:15:54 +0100 Subject: [PATCH] update test Signed-off-by: Karol Szwaj --- apis/k8ssandra/v1alpha1/k8ssandracluster_types.go | 6 +++--- apis/k8ssandra/v1alpha1/k8ssandracluster_webhook.go | 7 +++++-- .../v1alpha1/k8ssandracluster_webhook_test.go | 13 +++++++++++++ pkg/cassandra/datacenter.go | 6 ++++-- 4 files changed, 25 insertions(+), 7 deletions(-) diff --git a/apis/k8ssandra/v1alpha1/k8ssandracluster_types.go b/apis/k8ssandra/v1alpha1/k8ssandracluster_types.go index 30babc51b..60743c4f4 100644 --- a/apis/k8ssandra/v1alpha1/k8ssandracluster_types.go +++ b/apis/k8ssandra/v1alpha1/k8ssandracluster_types.go @@ -526,9 +526,9 @@ const ( ServerDistributionDse = ServerDistribution("dse") ) -func (kc *K8ssandraCluster) DefaultNumTokens(serverVersion *semver.Version) int64 { +func (kc *K8ssandraCluster) DefaultNumTokens(serverVersion *semver.Version) float64 { if kc.Spec.Cassandra.ServerType == ServerDistributionCassandra && serverVersion.Major() == 3 { - return int64(256) + return float64(256) } - return int64(16) + return float64(16) } diff --git a/apis/k8ssandra/v1alpha1/k8ssandracluster_webhook.go b/apis/k8ssandra/v1alpha1/k8ssandracluster_webhook.go index 2d059ae0e..344ad0d1b 100644 --- a/apis/k8ssandra/v1alpha1/k8ssandracluster_webhook.go +++ b/apis/k8ssandra/v1alpha1/k8ssandracluster_webhook.go @@ -118,17 +118,20 @@ func (r *K8ssandraCluster) ValidateUpdate(old runtime.Object) error { if oldCassConfig != nil && newCassConfig != nil { oldNumTokens, oldNumTokensExists := oldCassConfig.CassandraYaml["num_tokens"] newNumTokens, newNumTokensExists := newCassConfig.CassandraYaml["num_tokens"] - + // Check if num_tokens is not present in old configuration if !oldNumTokensExists { + // Determine the default num_tokens based on Cassandra version cassVersion, err := semver.NewVersion(oldCluster.Spec.Cassandra.ServerVersion) if err != nil { return err } defaultNumTokens := oldCluster.DefaultNumTokens(cassVersion) - if newNumTokensExists && int64(newNumTokens.(float64)) != defaultNumTokens { + // If new configuration has num_tokens and it's not equal to the default, return an error + if newNumTokensExists && newNumTokens.(float64) != defaultNumTokens { return ErrNumTokens } } else { + // If num_tokens is present in both old and new configurations, check if they are equal if oldNumTokens != newNumTokens { return ErrNumTokens } diff --git a/apis/k8ssandra/v1alpha1/k8ssandracluster_webhook_test.go b/apis/k8ssandra/v1alpha1/k8ssandracluster_webhook_test.go index cedcffeb4..4025a21b6 100644 --- a/apis/k8ssandra/v1alpha1/k8ssandracluster_webhook_test.go +++ b/apis/k8ssandra/v1alpha1/k8ssandracluster_webhook_test.go @@ -176,6 +176,19 @@ func testNumTokensInUpdate(t *testing.T) { // This should be acceptable change, since 3.11.10 defaulted to 256 and so it is the same value err = k8sClient.Update(ctx, cluster) require.NoError(err) + cluster2 := createMinimalClusterObj("numtokens-wrong-test-update", "numtokensupdate-namespace") + cluster2.Spec.Cassandra.ServerVersion = "3.11.10" + cluster2.Spec.Cassandra.DatacenterOptions.CassandraConfig = &CassandraConfig{} + err = k8sClient.Create(ctx, cluster2) + require.NoError(err) + + // Now update to 4.1.3 + cluster.Spec.Cassandra.DatacenterOptions.CassandraConfig.CassandraYaml = unstructured.Unstructured{"num_tokens": 33} + cluster.Spec.Cassandra.ServerVersion = "4.1.3" + + // This should be disallowed, since it is changing default num_tokens + err = k8sClient.Update(ctx, cluster) + require.Error(err) } func testReaperKeyspaceValidation(t *testing.T) { diff --git a/pkg/cassandra/datacenter.go b/pkg/cassandra/datacenter.go index eae9ff0f9..753303c1e 100644 --- a/pkg/cassandra/datacenter.go +++ b/pkg/cassandra/datacenter.go @@ -514,9 +514,11 @@ func ValidateConfig(kc *api.K8ssandraCluster, desiredDc, actualDc *cassdcapi.Cas if cassConfig := kc.Spec.Cassandra.CassandraConfig; cassConfig == nil { if semver.MustParse(actualDc.Spec.ServerVersion).Major() == 3 && semver.MustParse(desiredDc.Spec.ServerVersion).Major() > 3 { desiredCassYaml["num_tokens"] = actualCassYaml["num_tokens"] - // Assigning desiredCassYaml to desiredDc.Spec.Config desiredConfig["cassandra-yaml"] = desiredCassYaml - newConfig, _ := json.Marshal(desiredConfig) + newConfig, err := json.Marshal(desiredConfig) + if err != nil { + return nil, err + } desiredDc.Spec.Config = newConfig } }