Skip to content

Commit

Permalink
Ensure SINGLE deplomyment mode when Reaper uses memory storage
Browse files Browse the repository at this point in the history
  • Loading branch information
rzvoncek committed Jul 10, 2024
1 parent d3a10bb commit 0d359a0
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 13 deletions.
19 changes: 16 additions & 3 deletions apis/reaper/v1alpha1/reaper_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down
47 changes: 47 additions & 0 deletions apis/reaper/v1alpha1/reaper_types_test.go
Original file line number Diff line number Diff line change
@@ -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)
})
}
7 changes: 5 additions & 2 deletions controllers/k8ssandra/reaper.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
}
Expand All @@ -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 {
Expand Down
3 changes: 1 addition & 2 deletions pkg/reaper/deployment.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down
5 changes: 1 addition & 4 deletions pkg/reaper/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,6 @@ import (
)

const (
DeploymentModeSingle = "SINGLE"
DeploymentModePerDc = "PER_DC"

DatacenterAvailabilityEach = "EACH"
DatacenterAvailabilityAll = "ALL"
)
Expand Down Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions pkg/reaper/resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func Test_computeReaperDcAvailability(t *testing.T) {
},
},
Reaper: &reaperapi.ReaperClusterTemplate{
DeploymentMode: DeploymentModePerDc,
DeploymentMode: reaperapi.DeploymentModePerDc,
},
},
},
Expand All @@ -55,7 +55,7 @@ func Test_computeReaperDcAvailability(t *testing.T) {
},
},
Reaper: &reaperapi.ReaperClusterTemplate{
DeploymentMode: DeploymentModeSingle,
DeploymentMode: reaperapi.DeploymentModeSingle,
},
},
},
Expand Down

0 comments on commit 0d359a0

Please sign in to comment.