Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Monitor mastername changes. #703

Closed
wants to merge 14 commits into from
32 changes: 32 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,38 @@ By default, no service annotations will be applied to the Redis nor Sentinel ser

In order to apply custom service Annotations, you can provide the `serviceAnnotations` option inside redis/sentinel spec. An example can be found in the [custom annotations example file](example/redisfailover/custom-annotations.yaml).

### Disable `mymaster` in Sentinel Cluster Management

By default the sentinel identifies the cluster with `mymaster`, due to the ephemeral nature of Kubernetes. There is a high likelihood of the colliding the sentinel across redisfailover deployments. To avoid this we can set the `disableMyMaster` to `false` under the `sentinel` specification.

```
apiVersion: databases.spotahome.com/v1
kind: RedisFailover
metadata:
name: redisfailover
namespace: disable-mymaster
spec:
sentinel:
replicas: 3
resources:
requests:
cpu: 100m
limits:
memory: 100Mi
disableMyMaster: true
redis:
replicas: 3
resources:
requests:
cpu: 100m
memory: 100Mi
limits:
cpu: 400m
memory: 500Mi
```

So this will use the name of the redisfailover object and use that instead of `mymaster`. Yes, if you have multiple same redisfailover names across namesapces, you would still run into collision, so please ensure they are not.

### Control of label propagation.
By default the operator will propagate all labels on the CRD down to the resources that it creates. This can be problematic if the
labels on the CRD are not fully under your own control (for example: being deployed by a gitops operator)
Expand Down
9 changes: 9 additions & 0 deletions api/redisfailover/v1/master.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package v1

func (r *RedisFailover) MasterName() string {
if r.Spec.Sentinel.DisableMyMaster {
return r.Name
} else {
return "mymaster"
}
}
52 changes: 52 additions & 0 deletions api/redisfailover/v1/master_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package v1

import (
"testing"

"github.com/stretchr/testify/assert"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

func generateRedisFailoverMasterName(name string, disableMyMaster bool) *RedisFailover {
return &RedisFailover{
ObjectMeta: metav1.ObjectMeta{
Name: name,
Namespace: "namespace",
},
Spec: RedisFailoverSpec{
Sentinel: SentinelSettings{
DisableMyMaster: disableMyMaster,
},
},
}
}

func TestMyMaster(t *testing.T) {
tests := []struct {
name string
expectation string
disableMyMaster bool
}{
{
name: "use default mymaster",
expectation: "mymaster",
},
{
name: "passing-false",
expectation: "mymaster",
disableMyMaster: false,
},
{
name: "passing-true",
expectation: "passing-true",
disableMyMaster: true,
},
}

for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
rf := generateRedisFailoverMasterName(test.name, test.disableMyMaster)
assert.Equal(t, test.expectation, rf.MasterName())
})
}
}
1 change: 1 addition & 0 deletions api/redisfailover/v1/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ type SentinelSettings struct {
CustomReadinessProbe *corev1.Probe `json:"customReadinessProbe,omitempty"`
CustomStartupProbe *corev1.Probe `json:"customStartupProbe,omitempty"`
DisablePodDisruptionBudget bool `json:"disablePodDisruptionBudget,omitempty"`
DisableMyMaster bool `json:"disableMyMaster,omitempty"`
}

// AuthSettings contains settings about auth
Expand Down
2 changes: 1 addition & 1 deletion api/redisfailover/v1/validate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func TestValidate(t *testing.T) {
expectedError: "BootstrapNode must include a host when provided",
},
{
name: "SentinelCustomConfig provided",
name: "SentinelCustomConfig not provided",
rfName: "test",
},
{
Expand Down
4 changes: 2 additions & 2 deletions docker/development/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
FROM golang:1.20-alpine

ENV CODEGEN_VERSION="1.11.9"
ENV CODEGEN_VERSION="1.27.16"

RUN apk --no-cache add \
bash \
Expand All @@ -17,7 +17,7 @@ RUN wget http://github.com/kubernetes/code-generator/archive/kubernetes-${CODEGE
touch /go/src/k8s.io/kubernetes/hack/boilerplate/boilerplate.go.txt

# Mock creator
ARG MOCKERY_VERSION="2.9.6"
ARG MOCKERY_VERSION="2.45.0"
RUN wget -c https://github.com/vektra/mockery/releases/download/v${MOCKERY_VERSION}/mockery_${MOCKERY_VERSION}_$(uname -o)_$(uname -m).tar.gz -O - | tar -xz -C /go/bin/

# Create user
Expand Down
1 change: 1 addition & 0 deletions example/redisfailover/basic.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ apiVersion: databases.spotahome.com/v1
kind: RedisFailover
metadata:
name: redisfailover
namespace: basic
spec:
sentinel:
replicas: 3
Expand Down
23 changes: 23 additions & 0 deletions example/redisfailover/disable-mymaster.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
apiVersion: databases.spotahome.com/v1
kind: RedisFailover
metadata:
name: redisfailover
namespace: disable-mymaster
spec:
sentinel:
replicas: 3
resources:
requests:
cpu: 100m
limits:
memory: 100Mi
disableMyMaster: true
redis:
replicas: 3
resources:
requests:
cpu: 100m
memory: 100Mi
limits:
cpu: 400m
memory: 500Mi
2 changes: 2 additions & 0 deletions manifests/databases.spotahome.com_redisfailovers.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8797,6 +8797,8 @@ spec:
format: int32
type: integer
type: object
disableMyMaster:
type: boolean
disablePodDisruptionBudget:
type: boolean
dnsPolicy:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8797,6 +8797,8 @@ spec:
format: int32
type: integer
type: object
disableMyMaster:
type: boolean
disablePodDisruptionBudget:
type: boolean
dnsPolicy:
Expand Down
27 changes: 21 additions & 6 deletions mocks/log/Logger.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 13 additions & 6 deletions mocks/operator/redisfailover/RedisFailover.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
Loading