diff --git a/apis/reaper/v1alpha1/reaper_types.go b/apis/reaper/v1alpha1/reaper_types.go index c9bbb820f..98734406c 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,20 @@ 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 != nil { + 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..ccef7b3e4 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" @@ -87,7 +86,7 @@ func (r *K8ssandraClusterReconciler) reconcileReaper( reaperTemplate := kc.Spec.Reaper.DeepCopy() if reaperTemplate != nil { - if reaperTemplate.DeploymentMode == reaper.DeploymentModeSingle && getSingleReaperDcName(kc) != actualDc.Name { + if reaperTemplate.DeploymentMode == reaperapi.DeploymentModeSingle && getSingleReaperDcName(kc) != actualDc.Name { logger.Info("DC is not Reaper DC: skipping Reaper deployment") reaperTemplate = nil } @@ -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/deployment.go b/pkg/reaper/deployment.go index 012934b74..fcf90217d 100644 --- a/pkg/reaper/deployment.go +++ b/pkg/reaper/deployment.go @@ -324,8 +324,7 @@ func computeVolumeClaims(reaper *api.Reaper) []corev1.PersistentVolumeClaim { vcs := make([]corev1.PersistentVolumeClaim, 0) - var volumeClaimsPec *corev1.PersistentVolumeClaimSpec - volumeClaimsPec = reaper.Spec.StorageConfig.DeepCopy() + volumeClaimsPec := reaper.Spec.StorageConfig.DeepCopy() pvc := &corev1.PersistentVolumeClaim{ ObjectMeta: metav1.ObjectMeta{ 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 diff --git a/pkg/reaper/resource_test.go b/pkg/reaper/resource_test.go index 86a904c5e..70daac3e5 100644 --- a/pkg/reaper/resource_test.go +++ b/pkg/reaper/resource_test.go @@ -38,7 +38,7 @@ func Test_computeReaperDcAvailability(t *testing.T) { }, }, Reaper: &reaperapi.ReaperClusterTemplate{ - DeploymentMode: DeploymentModePerDc, + DeploymentMode: reaperapi.DeploymentModePerDc, }, }, }, @@ -55,7 +55,7 @@ func Test_computeReaperDcAvailability(t *testing.T) { }, }, Reaper: &reaperapi.ReaperClusterTemplate{ - DeploymentMode: DeploymentModeSingle, + DeploymentMode: reaperapi.DeploymentModeSingle, }, }, },