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

TopologySpreadContraints, Custom Port, Ping User and Kustomize Examples #429

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/bin
.bash_history
.vscode
/tmp
6 changes: 3 additions & 3 deletions api/redisfailover/v1/defaults.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ package v1
const (
defaultRedisNumber = 3
defaultSentinelNumber = 3
defaultSentinelExporterImage = "quay.io/oliver006/redis_exporter:v1.33.0-alpine"
defaultExporterImage = "quay.io/oliver006/redis_exporter:v1.33.0-alpine"
defaultSentinelExporterImage = "quay.io/oliver006/redis_exporter:v1.43.0"
defaultExporterImage = "quay.io/oliver006/redis_exporter:v1.43.0"
defaultImage = "redis:6.2.6-alpine"
defaultRedisPort = "6379"
defaultRedisPort = 6379
)

var (
Expand Down
89 changes: 46 additions & 43 deletions api/redisfailover/v1/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,53 +37,56 @@ type RedisCommandRename struct {

// RedisSettings defines the specification of the redis cluster
type RedisSettings struct {
Image string `json:"image,omitempty"`
ImagePullPolicy corev1.PullPolicy `json:"imagePullPolicy,omitempty"`
Replicas int32 `json:"replicas,omitempty"`
Resources corev1.ResourceRequirements `json:"resources,omitempty"`
CustomConfig []string `json:"customConfig,omitempty"`
CustomCommandRenames []RedisCommandRename `json:"customCommandRenames,omitempty"`
Command []string `json:"command,omitempty"`
ShutdownConfigMap string `json:"shutdownConfigMap,omitempty"`
Storage RedisStorage `json:"storage,omitempty"`
Exporter RedisExporter `json:"exporter,omitempty"`
Affinity *corev1.Affinity `json:"affinity,omitempty"`
SecurityContext *corev1.PodSecurityContext `json:"securityContext,omitempty"`
ContainerSecurityContext *corev1.SecurityContext `json:"containerSecurityContext,omitempty"`
ImagePullSecrets []corev1.LocalObjectReference `json:"imagePullSecrets,omitempty"`
Tolerations []corev1.Toleration `json:"tolerations,omitempty"`
NodeSelector map[string]string `json:"nodeSelector,omitempty"`
PodAnnotations map[string]string `json:"podAnnotations,omitempty"`
ServiceAnnotations map[string]string `json:"serviceAnnotations,omitempty"`
HostNetwork bool `json:"hostNetwork,omitempty"`
DNSPolicy corev1.DNSPolicy `json:"dnsPolicy,omitempty"`
PriorityClassName string `json:"priorityClassName,omitempty"`
ServiceAccountName string `json:"serviceAccountName,omitempty"`
TerminationGracePeriodSeconds int64 `json:"terminationGracePeriod,omitempty"`
Image string `json:"image,omitempty"`
ImagePullPolicy corev1.PullPolicy `json:"imagePullPolicy,omitempty"`
Replicas int32 `json:"replicas,omitempty"`
Port int32 `json:"port,omitempty"`
Resources corev1.ResourceRequirements `json:"resources,omitempty"`
CustomConfig []string `json:"customConfig,omitempty"`
CustomCommandRenames []RedisCommandRename `json:"customCommandRenames,omitempty"`
Command []string `json:"command,omitempty"`
ShutdownConfigMap string `json:"shutdownConfigMap,omitempty"`
Storage RedisStorage `json:"storage,omitempty"`
Exporter RedisExporter `json:"exporter,omitempty"`
Affinity *corev1.Affinity `json:"affinity,omitempty"`
SecurityContext *corev1.PodSecurityContext `json:"securityContext,omitempty"`
ContainerSecurityContext *corev1.SecurityContext `json:"containerSecurityContext,omitempty"`
ImagePullSecrets []corev1.LocalObjectReference `json:"imagePullSecrets,omitempty"`
Tolerations []corev1.Toleration `json:"tolerations,omitempty"`
TopologySpreadConstraints []corev1.TopologySpreadConstraint `json:"topologySpreadConstraints,omitempty"`
NodeSelector map[string]string `json:"nodeSelector,omitempty"`
PodAnnotations map[string]string `json:"podAnnotations,omitempty"`
ServiceAnnotations map[string]string `json:"serviceAnnotations,omitempty"`
HostNetwork bool `json:"hostNetwork,omitempty"`
DNSPolicy corev1.DNSPolicy `json:"dnsPolicy,omitempty"`
PriorityClassName string `json:"priorityClassName,omitempty"`
ServiceAccountName string `json:"serviceAccountName,omitempty"`
TerminationGracePeriodSeconds int64 `json:"terminationGracePeriod,omitempty"`
}

// SentinelSettings defines the specification of the sentinel cluster
type SentinelSettings struct {
Image string `json:"image,omitempty"`
ImagePullPolicy corev1.PullPolicy `json:"imagePullPolicy,omitempty"`
Replicas int32 `json:"replicas,omitempty"`
Resources corev1.ResourceRequirements `json:"resources,omitempty"`
CustomConfig []string `json:"customConfig,omitempty"`
Command []string `json:"command,omitempty"`
Affinity *corev1.Affinity `json:"affinity,omitempty"`
SecurityContext *corev1.PodSecurityContext `json:"securityContext,omitempty"`
ContainerSecurityContext *corev1.SecurityContext `json:"containerSecurityContext,omitempty"`
ImagePullSecrets []corev1.LocalObjectReference `json:"imagePullSecrets,omitempty"`
Tolerations []corev1.Toleration `json:"tolerations,omitempty"`
NodeSelector map[string]string `json:"nodeSelector,omitempty"`
PodAnnotations map[string]string `json:"podAnnotations,omitempty"`
ServiceAnnotations map[string]string `json:"serviceAnnotations,omitempty"`
Exporter SentinelExporter `json:"exporter,omitempty"`
ConfigCopy SentinelConfigCopy `json:"configCopy,omitempty"`
HostNetwork bool `json:"hostNetwork,omitempty"`
DNSPolicy corev1.DNSPolicy `json:"dnsPolicy,omitempty"`
PriorityClassName string `json:"priorityClassName,omitempty"`
ServiceAccountName string `json:"serviceAccountName,omitempty"`
Image string `json:"image,omitempty"`
ImagePullPolicy corev1.PullPolicy `json:"imagePullPolicy,omitempty"`
Replicas int32 `json:"replicas,omitempty"`
Resources corev1.ResourceRequirements `json:"resources,omitempty"`
CustomConfig []string `json:"customConfig,omitempty"`
Command []string `json:"command,omitempty"`
Affinity *corev1.Affinity `json:"affinity,omitempty"`
SecurityContext *corev1.PodSecurityContext `json:"securityContext,omitempty"`
ContainerSecurityContext *corev1.SecurityContext `json:"containerSecurityContext,omitempty"`
ImagePullSecrets []corev1.LocalObjectReference `json:"imagePullSecrets,omitempty"`
Tolerations []corev1.Toleration `json:"tolerations,omitempty"`
TopologySpreadConstraints []corev1.TopologySpreadConstraint `json:"topologySpreadConstraints,omitempty"`
NodeSelector map[string]string `json:"nodeSelector,omitempty"`
PodAnnotations map[string]string `json:"podAnnotations,omitempty"`
ServiceAnnotations map[string]string `json:"serviceAnnotations,omitempty"`
Exporter SentinelExporter `json:"exporter,omitempty"`
ConfigCopy SentinelConfigCopy `json:"configCopy,omitempty"`
HostNetwork bool `json:"hostNetwork,omitempty"`
DNSPolicy corev1.DNSPolicy `json:"dnsPolicy,omitempty"`
PriorityClassName string `json:"priorityClassName,omitempty"`
ServiceAccountName string `json:"serviceAccountName,omitempty"`
}

// AuthSettings contains settings about auth
Expand Down
7 changes: 6 additions & 1 deletion api/redisfailover/v1/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package v1
import (
"errors"
"fmt"
"strconv"
)

const (
Expand All @@ -22,7 +23,7 @@ func (r *RedisFailover) Validate() error {
}

if r.Spec.BootstrapNode.Port == "" {
r.Spec.BootstrapNode.Port = defaultRedisPort
r.Spec.BootstrapNode.Port = strconv.Itoa(defaultRedisPort)
}
initialRedisCustomConfig = bootstrappingRedisCustomConfig
}
Expand All @@ -45,6 +46,10 @@ func (r *RedisFailover) Validate() error {
r.Spec.Sentinel.Replicas = defaultSentinelNumber
}

if r.Spec.Redis.Port <= 0 {
r.Spec.Redis.Port = defaultRedisPort
}

if r.Spec.Redis.Exporter.Image == "" {
r.Spec.Redis.Exporter.Image = defaultExporterImage
}
Expand Down
1 change: 1 addition & 0 deletions api/redisfailover/v1/validate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ func TestValidate(t *testing.T) {
Redis: RedisSettings{
Image: defaultImage,
Replicas: defaultRedisNumber,
Port: defaultRedisPort,
Exporter: RedisExporter{
Image: defaultExporterImage,
},
Expand Down
5 changes: 5 additions & 0 deletions example/kustomize/base/kustomization.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization

resources:
- rf.yaml
68 changes: 68 additions & 0 deletions example/kustomize/base/rf.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
# The base ensures the following
# - Redis has three replicas
# - Sentinel has three replicas
# - A label whitelist `redis.failover.io` to propogate additional info
# - Redis is distributed evenly across AZs(Ref: topologySpreadConstraints)
# - Sentinel is distributed evenly across AZs(Ref: topologySpreadConstraints)
# - Redis and their Sentinels do not at any given time coexist(Ref: affinity.podAntiAffinity)
---
apiVersion: databases.spotahome.com/v1
kind: RedisFailover
metadata:
name: redis
labels:
redis.failover.io/tenant: base
spec:
sentinel:
replicas: 3
exporter:
enabled: true
topologySpreadConstraints:
- labelSelector:
matchLabels:
app.kubernetes.io/component: sentinel
maxSkew: 1
topologyKey: topology.kubernetes.io/zone
whenUnsatisfiable: DoNotSchedule
- labelSelector:
matchLabels:
app.kubernetes.io/component: sentinel
maxSkew: 1
topologyKey: kubernetes.io/hostname
whenUnsatisfiable: ScheduleAnyway
affinity:
podAntiAffinity:
requiredDuringSchedulingIgnoredDuringExecution:
- labelSelector:
matchLabels:
app.kubernetes.io/component: redis
redis.failover.io/tenant: base
topologyKey: "kubernetes.io/hostname"
redis:
replicas: 3
exporter:
enabled: true
topologySpreadConstraints:
- labelSelector:
matchLabels:
app.kubernetes.io/component: redis
maxSkew: 1
topologyKey: topology.kubernetes.io/zone
whenUnsatisfiable: DoNotSchedule
- labelSelector:
matchLabels:
app.kubernetes.io/component: redis
maxSkew: 1
topologyKey: kubernetes.io/hostname
whenUnsatisfiable: ScheduleAnyway
affinity:
podAntiAffinity:
requiredDuringSchedulingIgnoredDuringExecution:
- labelSelector:
matchLabels:
app.kubernetes.io/component: sentinel
redis.failover.io/tenant: base
topologyKey: "kubernetes.io/hostname"
terminationGracePeriod: 100
labelWhitelist:
- ^redis.failover.io.*
32 changes: 32 additions & 0 deletions example/kustomize/overlays/multiple/kustomization.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization

bases:
- "../../base"

patches:
- resources.yaml
- replicas.yaml
- port.yaml

commonLabels:
redis.failover.io/tenant: multiple
redis.failover.io/product: testing

nameSuffix: "-multiple"

namespace: multiple

patchesJson6902:
- target:
group: databases.spotahome.com
version: v1
kind: RedisFailover
name: redis
patch: |-
- op: replace
path: /spec/redis/affinity/podAntiAffinity/requiredDuringSchedulingIgnoredDuringExecution/0/labelSelector/matchLabels/redis.failover.io~1tenant
value: multiple
- op: replace
path: /spec/sentinel/affinity/podAntiAffinity/requiredDuringSchedulingIgnoredDuringExecution/0/labelSelector/matchLabels/redis.failover.io~1tenant
value: multiple
8 changes: 8 additions & 0 deletions example/kustomize/overlays/multiple/port.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
apiVersion: databases.spotahome.com/v1
kind: RedisFailover
metadata:
name: redis
spec:
redis:
port: 12345
7 changes: 7 additions & 0 deletions example/kustomize/overlays/multiple/replicas.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
apiVersion: databases.spotahome.com/v1
kind: RedisFailover
metadata:
name: redis
spec:
redis:
replicas: 5
13 changes: 13 additions & 0 deletions example/kustomize/overlays/multiple/resources.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
apiVersion: databases.spotahome.com/v1
kind: RedisFailover
metadata:
name: redis
spec:
redis:
resources:
requests:
cpu: 100m
memory: 100Mi
limits:
cpu: 200m
memory: 500Mi
30 changes: 30 additions & 0 deletions example/kustomize/overlays/ports/kustomization.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization

bases:
- "../../base"

commonLabels:
redis.failover.io/tenant: ports
redis.failover.io/product: testing

nameSuffix: "-ports"

namespace: ports
patches:
- port.yaml
- resources.yaml

patchesJson6902:
- target:
group: databases.spotahome.com
version: v1
kind: RedisFailover
name: redis
patch: |-
- op: replace
path: /spec/redis/affinity/podAntiAffinity/requiredDuringSchedulingIgnoredDuringExecution/0/labelSelector/matchLabels/redis.failover.io~1tenant
value: ports
- op: replace
path: /spec/sentinel/affinity/podAntiAffinity/requiredDuringSchedulingIgnoredDuringExecution/0/labelSelector/matchLabels/redis.failover.io~1tenant
value: ports
8 changes: 8 additions & 0 deletions example/kustomize/overlays/ports/port.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
apiVersion: databases.spotahome.com/v1
kind: RedisFailover
metadata:
name: redis
spec:
redis:
port: 12345
19 changes: 19 additions & 0 deletions example/kustomize/overlays/ports/resources.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
apiVersion: databases.spotahome.com/v1
kind: RedisFailover
metadata:
name: redis
spec:
sentinel:
resources:
requests:
cpu: 100m
limits:
memory: 100Mi
redis:
resources:
requests:
cpu: 100m
memory: 100Mi
limits:
cpu: 200m
memory: 500Mi
30 changes: 30 additions & 0 deletions example/kustomize/overlays/resources/kustomization.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization

bases:
- "../../base"

patches:
- resources.yaml

commonLabels:
redis.failover.io/tenant: resources
redis.failover.io/product: testing

nameSuffix: "-resources"

namespace: resources

patchesJson6902:
- target:
group: databases.spotahome.com
version: v1
kind: RedisFailover
name: redis
patch: |-
- op: replace
path: /spec/redis/affinity/podAntiAffinity/requiredDuringSchedulingIgnoredDuringExecution/0/labelSelector/matchLabels/redis.failover.io~1tenant
value: resources
- op: replace
path: /spec/sentinel/affinity/podAntiAffinity/requiredDuringSchedulingIgnoredDuringExecution/0/labelSelector/matchLabels/redis.failover.io~1tenant
value: resources
13 changes: 13 additions & 0 deletions example/kustomize/overlays/resources/resources.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
apiVersion: databases.spotahome.com/v1
kind: RedisFailover
metadata:
name: redis
spec:
redis:
resources:
requests:
cpu: 100m
memory: 100Mi
limits:
cpu: 200m
memory: 500Mi
Loading