From e40bee115d01d068f1359b3d473f400205b8ad18 Mon Sep 17 00:00:00 2001 From: Radovan Zvoncek Date: Wed, 10 Jul 2024 10:37:16 +0300 Subject: [PATCH] Ensure SINGLE deplomyment mode when Reaper uses memory storage --- apis/reaper/v1alpha1/reaper_types.go | 17 ++++++-- apis/reaper/v1alpha1/reaper_types_test.go | 47 +++++++++++++++++++++++ controllers/k8ssandra/reaper.go | 5 ++- pkg/reaper/resource.go | 5 +-- 4 files changed, 66 insertions(+), 8 deletions(-) create mode 100644 apis/reaper/v1alpha1/reaper_types_test.go diff --git a/apis/reaper/v1alpha1/reaper_types.go b/apis/reaper/v1alpha1/reaper_types.go index c9bbb820f..10663b6bb 100644 --- a/apis/reaper/v1alpha1/reaper_types.go +++ b/apis/reaper/v1alpha1/reaper_types.go @@ -32,13 +32,12 @@ import ( var SingleReplica = int32(1) const ( + DeploymentModeSingle = "SINGLE" + DeploymentModePerDc = "PER_DC" ReaperLabel = "k8ssandra.io/reaper" DefaultKeyspace = "reaper_db" StorageTypeCassandra = "cassandra" StorageTypeMemory = "memory" - DefaultStorageClass = "standard" - DefaultStorageSize = "1Gi" - DefaultAccessMode = corev1.ReadWriteOnce ) type ReaperTemplate struct { @@ -242,6 +241,18 @@ type ReaperClusterTemplate struct { DeploymentMode string `json:"deploymentMode,omitempty"` } +// EnsureDeploymentMode ensures that a deployment mode is SINGLE if we use a memory storage type. This is to prevent +// several instances of in-memory Reapers that would interfere with each other. +func (t *ReaperClusterTemplate) EnsureDeploymentMode() bool { + if t.StorageType == StorageTypeMemory { + if t.DeploymentMode != DeploymentModeSingle { + t.DeploymentMode = DeploymentModeSingle + return true + } + } + return false +} + // CassandraDatacenterRef references the target Cassandra DC that Reaper should manage. // TODO this object could be used by Stargate too; which currently cannot locate DCs outside of its own namespace. type CassandraDatacenterRef struct { diff --git a/apis/reaper/v1alpha1/reaper_types_test.go b/apis/reaper/v1alpha1/reaper_types_test.go new file mode 100644 index 000000000..9acde8cf6 --- /dev/null +++ b/apis/reaper/v1alpha1/reaper_types_test.go @@ -0,0 +1,47 @@ +/* +Copyright 2021. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package v1alpha1 + +import ( + "github.com/stretchr/testify/assert" + "testing" +) + +func TestEnsureDeploymentMode(t *testing.T) { + t.Run("SINGLE forced when memory storage type", func(t *testing.T) { + rct := &ReaperClusterTemplate{ + ReaperTemplate: ReaperTemplate{ + StorageType: StorageTypeMemory, + }, + DeploymentMode: DeploymentModePerDc, + } + changed := rct.EnsureDeploymentMode() + assert.True(t, changed) + assert.Equal(t, DeploymentModeSingle, rct.DeploymentMode) + }) + t.Run("PER_DC kept when using cassandra storage type", func(t *testing.T) { + rct := &ReaperClusterTemplate{ + ReaperTemplate: ReaperTemplate{ + StorageType: StorageTypeCassandra, + }, + DeploymentMode: DeploymentModePerDc, + } + changed := rct.EnsureDeploymentMode() + assert.False(t, changed) + assert.Equal(t, DeploymentModePerDc, rct.DeploymentMode) + }) +} diff --git a/controllers/k8ssandra/reaper.go b/controllers/k8ssandra/reaper.go index 4145de48b..62cc2cd49 100644 --- a/controllers/k8ssandra/reaper.go +++ b/controllers/k8ssandra/reaper.go @@ -18,7 +18,6 @@ package k8ssandra import ( "context" - "github.com/go-logr/logr" cassdcapi "github.com/k8ssandra/cass-operator/apis/cassandra/v1beta1" api "github.com/k8ssandra/k8ssandra-operator/apis/k8ssandra/v1alpha1" @@ -97,6 +96,10 @@ func (r *K8ssandraClusterReconciler) reconcileReaper( } } + if updated := reaperTemplate.EnsureDeploymentMode(); updated { + logger.Info("When using 'memory' storage type, Reaper can only be deployed with SINGLE deployment mode") + } + actualReaper := &reaperapi.Reaper{} if reaperTemplate != nil { diff --git a/pkg/reaper/resource.go b/pkg/reaper/resource.go index d26992cf4..dccb430e8 100644 --- a/pkg/reaper/resource.go +++ b/pkg/reaper/resource.go @@ -15,9 +15,6 @@ import ( ) const ( - DeploymentModeSingle = "SINGLE" - DeploymentModePerDc = "PER_DC" - DatacenterAvailabilityEach = "EACH" DatacenterAvailabilityAll = "ALL" ) @@ -104,7 +101,7 @@ func NewReaper( // See https://cassandra-reaper.io/docs/usage/multi_dc/. // If we have more than one DC, and each DC has its own Reaper instance, use EACH; otherwise, use ALL. func computeReaperDcAvailability(kc *k8ssandraapi.K8ssandraCluster) string { - if kc.Spec.Reaper.DeploymentMode == DeploymentModeSingle || len(kc.Spec.Cassandra.Datacenters) == 1 { + if kc.Spec.Reaper.DeploymentMode == reaperapi.DeploymentModeSingle || len(kc.Spec.Cassandra.Datacenters) == 1 { return DatacenterAvailabilityAll } return DatacenterAvailabilityEach