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 e40bee1
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 8 deletions.
17 changes: 14 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,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 {
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)
})
}
5 changes: 4 additions & 1 deletion 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 @@ -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
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

0 comments on commit e40bee1

Please sign in to comment.