Skip to content

Commit

Permalink
feat: support setting minReadySeconds on the stateful sets
Browse files Browse the repository at this point in the history
Signed-off-by: Matt Robinson <mattrobinsonsre@gmail.com>
  • Loading branch information
mattrobinsonsre committed Jul 11, 2024
1 parent 0dddaea commit 969c8d4
Show file tree
Hide file tree
Showing 23 changed files with 93 additions and 8 deletions.
1 change: 1 addition & 0 deletions api/common_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ type KubernetesConfig struct {
UpdateStrategy appsv1.StatefulSetUpdateStrategy `json:"updateStrategy,omitempty"`
Service *ServiceConfig `json:"service,omitempty"`
IgnoreAnnotations []string `json:"ignoreAnnotations,omitempty"`
MinReadySeconds *int32 `json:"minReadySeconds,omitempty"`
}

// ServiceConfig define the type of service to be created and its annotations
Expand Down
5 changes: 5 additions & 0 deletions api/zz_generated.deepcopy.go

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

6 changes: 6 additions & 0 deletions charts/redis-operator/crds/redis-cluster.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,9 @@ spec:
type: string
type: object
type: array
minReadySeconds:
format: int32
type: integer
redisSecret:
description: ExistingPasswordSecret is the struct to access the
existing secret
Expand Down Expand Up @@ -6749,6 +6752,9 @@ spec:
type: string
type: object
type: array
minReadySeconds:
format: int32
type: integer
redisSecret:
description: ExistingPasswordSecret is the struct to access the
existing secret
Expand Down
6 changes: 6 additions & 0 deletions charts/redis-operator/crds/redis-replication.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1109,6 +1109,9 @@ spec:
type: string
type: object
type: array
minReadySeconds:
format: int32
type: integer
redisSecret:
description: ExistingPasswordSecret is the struct to access the
existing secret
Expand Down Expand Up @@ -6141,6 +6144,9 @@ spec:
type: string
type: object
type: array
minReadySeconds:
format: int32
type: integer
redisSecret:
description: ExistingPasswordSecret is the struct to access the
existing secret
Expand Down
6 changes: 6 additions & 0 deletions charts/redis-operator/crds/redis-sentinel.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1110,6 +1110,9 @@ spec:
type: string
type: object
type: array
minReadySeconds:
format: int32
type: integer
redisSecret:
description: ExistingPasswordSecret is the struct to access the
existing secret
Expand Down Expand Up @@ -3595,6 +3598,9 @@ spec:
type: string
type: object
type: array
minReadySeconds:
format: int32
type: integer
redisSecret:
description: ExistingPasswordSecret is the struct to access the
existing secret
Expand Down
6 changes: 6 additions & 0 deletions charts/redis-operator/crds/redis.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1107,6 +1107,9 @@ spec:
type: string
type: object
type: array
minReadySeconds:
format: int32
type: integer
redisSecret:
description: ExistingPasswordSecret is the struct to access the
existing secret
Expand Down Expand Up @@ -6133,6 +6136,9 @@ spec:
type: string
type: object
type: array
minReadySeconds:
format: int32
type: integer
redisSecret:
description: ExistingPasswordSecret is the struct to access the
existing secret
Expand Down
6 changes: 6 additions & 0 deletions config/crd/bases/redis.redis.opstreelabs.in_redis.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1107,6 +1107,9 @@ spec:
type: string
type: object
type: array
minReadySeconds:
format: int32
type: integer
redisSecret:
description: ExistingPasswordSecret is the struct to access the
existing secret
Expand Down Expand Up @@ -6133,6 +6136,9 @@ spec:
type: string
type: object
type: array
minReadySeconds:
format: int32
type: integer
redisSecret:
description: ExistingPasswordSecret is the struct to access the
existing secret
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,9 @@ spec:
type: string
type: object
type: array
minReadySeconds:
format: int32
type: integer
redisSecret:
description: ExistingPasswordSecret is the struct to access the
existing secret
Expand Down Expand Up @@ -6749,6 +6752,9 @@ spec:
type: string
type: object
type: array
minReadySeconds:
format: int32
type: integer
redisSecret:
description: ExistingPasswordSecret is the struct to access the
existing secret
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1109,6 +1109,9 @@ spec:
type: string
type: object
type: array
minReadySeconds:
format: int32
type: integer
redisSecret:
description: ExistingPasswordSecret is the struct to access the
existing secret
Expand Down Expand Up @@ -6141,6 +6144,9 @@ spec:
type: string
type: object
type: array
minReadySeconds:
format: int32
type: integer
redisSecret:
description: ExistingPasswordSecret is the struct to access the
existing secret
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1110,6 +1110,9 @@ spec:
type: string
type: object
type: array
minReadySeconds:
format: int32
type: integer
redisSecret:
description: ExistingPasswordSecret is the struct to access the
existing secret
Expand Down Expand Up @@ -3595,6 +3598,9 @@ spec:
type: string
type: object
type: array
minReadySeconds:
format: int32
type: integer
redisSecret:
description: ExistingPasswordSecret is the struct to access the
existing secret
Expand Down
5 changes: 5 additions & 0 deletions k8sutils/redis-cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ type RedisClusterService struct {

// generateRedisClusterParams generates Redis cluster information
func generateRedisClusterParams(cr *redisv1beta2.RedisCluster, replicas int32, externalConfig *string, params RedisClusterSTS) statefulSetParameters {
var minreadyseconds int32 = 0
if cr.Spec.KubernetesConfig.MinReadySeconds != nil {
minreadyseconds = *cr.Spec.KubernetesConfig.MinReadySeconds
}
res := statefulSetParameters{
Replicas: &replicas,
ClusterMode: true,
Expand All @@ -47,6 +51,7 @@ func generateRedisClusterParams(cr *redisv1beta2.RedisCluster, replicas int32, e
UpdateStrategy: cr.Spec.KubernetesConfig.UpdateStrategy,
IgnoreAnnotations: cr.Spec.KubernetesConfig.IgnoreAnnotations,
HostNetwork: cr.Spec.HostNetwork,
MinReadySeconds: minreadyseconds,
}
if cr.Spec.RedisExporter != nil {
res.EnableMetrics = cr.Spec.RedisExporter.Enabled
Expand Down
2 changes: 2 additions & 0 deletions k8sutils/redis-cluster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ func Test_generateRedisClusterParams(t *testing.T) {
FSGroup: ptr.To(int64(1000)),
},
PriorityClassName: "high-priority",
MinReadySeconds: 5,
Affinity: &corev1.Affinity{
NodeAffinity: &corev1.NodeAffinity{
RequiredDuringSchedulingIgnoredDuringExecution: &corev1.NodeSelector{
Expand Down Expand Up @@ -94,6 +95,7 @@ func Test_generateRedisClusterParams(t *testing.T) {
FSGroup: ptr.To(int64(1000)),
},
PriorityClassName: "high-priority",
MinReadySeconds: 5,
Affinity: &corev1.Affinity{
NodeAffinity: &corev1.NodeAffinity{
RequiredDuringSchedulingIgnoredDuringExecution: &corev1.NodeSelector{
Expand Down
6 changes: 6 additions & 0 deletions k8sutils/redis-replication.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ func CreateReplicationRedis(cr *redisv1beta2.RedisReplication, cl kubernetes.Int
labels := getRedisLabels(cr.ObjectMeta.Name, replication, "replication", cr.ObjectMeta.Labels)
annotations := generateStatefulSetsAnots(cr.ObjectMeta, cr.Spec.KubernetesConfig.IgnoreAnnotations)
objectMetaInfo := generateObjectMetaInformation(stateFulName, cr.Namespace, labels, annotations)

err := CreateOrUpdateStateFul(
cl,
logger,
Expand All @@ -81,6 +82,10 @@ func CreateReplicationRedis(cr *redisv1beta2.RedisReplication, cl kubernetes.Int

func generateRedisReplicationParams(cr *redisv1beta2.RedisReplication) statefulSetParameters {
replicas := cr.Spec.GetReplicationCounts("Replication")
var minreadyseconds int32 = 0
if cr.Spec.KubernetesConfig.MinReadySeconds != nil {
minreadyseconds = *cr.Spec.KubernetesConfig.MinReadySeconds
}
res := statefulSetParameters{
Replicas: &replicas,
ClusterMode: false,
Expand All @@ -93,6 +98,7 @@ func generateRedisReplicationParams(cr *redisv1beta2.RedisReplication) statefulS
TerminationGracePeriodSeconds: cr.Spec.TerminationGracePeriodSeconds,
UpdateStrategy: cr.Spec.KubernetesConfig.UpdateStrategy,
IgnoreAnnotations: cr.Spec.KubernetesConfig.IgnoreAnnotations,
MinReadySeconds: minreadyseconds,
}
if cr.Spec.KubernetesConfig.ImagePullSecrets != nil {
res.ImagePullSecrets = cr.Spec.KubernetesConfig.ImagePullSecrets
Expand Down
2 changes: 1 addition & 1 deletion k8sutils/redis-replication_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ func Test_generateRedisReplicationParams(t *testing.T) {
FSGroup: ptr.To(int64(1000)),
},
PriorityClassName: "high-priority",
MinReadySeconds: 5,
Affinity: &corev1.Affinity{
NodeAffinity: &corev1.NodeAffinity{
RequiredDuringSchedulingIgnoredDuringExecution: &corev1.NodeSelector{
Expand Down Expand Up @@ -86,7 +87,6 @@ func Test_generateRedisReplicationParams(t *testing.T) {
if err != nil {
t.Fatalf("Failed to unmarshal file %s: %v", path, err)
}

actual := generateRedisReplicationParams(input)
assert.EqualValues(t, expected, actual, "Expected %+v, got %+v", expected, actual)
}
Expand Down
5 changes: 5 additions & 0 deletions k8sutils/redis-sentinel.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,10 @@ func (service RedisSentinelSTS) CreateRedisSentinelSetup(ctx context.Context, cl

// Create Redis Sentile Params for the statefulset
func generateRedisSentinelParams(cr *redisv1beta2.RedisSentinel, replicas int32, externalConfig *string, affinity *corev1.Affinity) statefulSetParameters {
var minreadyseconds int32 = 0
if cr.Spec.KubernetesConfig.MinReadySeconds != nil {
minreadyseconds = *cr.Spec.KubernetesConfig.MinReadySeconds
}
res := statefulSetParameters{
Replicas: &replicas,
ClusterMode: false,
Expand All @@ -99,6 +103,7 @@ func generateRedisSentinelParams(cr *redisv1beta2.RedisSentinel, replicas int32,
ServiceAccountName: cr.Spec.ServiceAccountName,
UpdateStrategy: cr.Spec.KubernetesConfig.UpdateStrategy,
IgnoreAnnotations: cr.Spec.KubernetesConfig.IgnoreAnnotations,
MinReadySeconds: minreadyseconds,
}

if cr.Spec.KubernetesConfig.ImagePullSecrets != nil {
Expand Down
1 change: 1 addition & 0 deletions k8sutils/redis-sentinel_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ func Test_generateRedisSentinelParams(t *testing.T) {
FSGroup: ptr.To(int64(1000)),
},
PriorityClassName: "high-priority",
MinReadySeconds: 5,
Affinity: &corev1.Affinity{
NodeAffinity: &corev1.NodeAffinity{
RequiredDuringSchedulingIgnoredDuringExecution: &corev1.NodeSelector{
Expand Down
5 changes: 5 additions & 0 deletions k8sutils/redis-standalone.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,10 @@ func CreateStandaloneRedis(cr *redisv1beta2.Redis, cl kubernetes.Interface) erro
// generateRedisStandalone generates Redis standalone information
func generateRedisStandaloneParams(cr *redisv1beta2.Redis) statefulSetParameters {
replicas := int32(1)
var minreadyseconds int32 = 0
if cr.Spec.KubernetesConfig.MinReadySeconds != nil {
minreadyseconds = *cr.Spec.KubernetesConfig.MinReadySeconds
}
res := statefulSetParameters{
Replicas: &replicas,
ClusterMode: false,
Expand All @@ -89,6 +93,7 @@ func generateRedisStandaloneParams(cr *redisv1beta2.Redis) statefulSetParameters
Tolerations: cr.Spec.Tolerations,
UpdateStrategy: cr.Spec.KubernetesConfig.UpdateStrategy,
IgnoreAnnotations: cr.Spec.KubernetesConfig.IgnoreAnnotations,
MinReadySeconds: minreadyseconds,
}
if cr.Spec.KubernetesConfig.ImagePullSecrets != nil {
res.ImagePullSecrets = cr.Spec.KubernetesConfig.ImagePullSecrets
Expand Down
7 changes: 4 additions & 3 deletions k8sutils/redis-standalone_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,10 @@ import (
func Test_generateRedisStandaloneParams(t *testing.T) {
path := filepath.Join("..", "tests", "testdata", "redis-standalone.yaml")
expected := statefulSetParameters{
Replicas: ptr.To(int32(1)),
ClusterMode: false,
NodeConfVolume: false,
Replicas: ptr.To(int32(1)),
ClusterMode: false,
NodeConfVolume: false,
MinReadySeconds: 5,
// Metadata: metav1.ObjectMeta{
// Name: "redis-standalone",
// Namespace: "redis",
Expand Down
10 changes: 6 additions & 4 deletions k8sutils/statefulset.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ type statefulSetParameters struct {
TerminationGracePeriodSeconds *int64
IgnoreAnnotations []string
HostNetwork bool
MinReadySeconds int32
}

// containerParameters will define container input params
Expand Down Expand Up @@ -279,10 +280,11 @@ func generateStatefulSetsDef(stsMeta metav1.ObjectMeta, params statefulSetParame
TypeMeta: generateMetaInformation("StatefulSet", "apps/v1"),
ObjectMeta: stsMeta,
Spec: appsv1.StatefulSetSpec{
Selector: LabelSelectors(stsMeta.GetLabels()),
ServiceName: fmt.Sprintf("%s-headless", stsMeta.Name),
Replicas: params.Replicas,
UpdateStrategy: params.UpdateStrategy,
Selector: LabelSelectors(stsMeta.GetLabels()),
ServiceName: fmt.Sprintf("%s-headless", stsMeta.Name),
Replicas: params.Replicas,
UpdateStrategy: params.UpdateStrategy,
MinReadySeconds: params.MinReadySeconds,
Template: corev1.PodTemplateSpec{
ObjectMeta: metav1.ObjectMeta{
Labels: stsMeta.GetLabels(),
Expand Down
1 change: 1 addition & 0 deletions tests/testdata/redis-cluster.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ spec:
name: redis-secret
key: password
ignoreAnnotations: [opstreelabs.in/ignore]
minReadySeconds: 5
podSecurityContext:
runAsUser: 1000
fsGroup: 1000
Expand Down
1 change: 1 addition & 0 deletions tests/testdata/redis-replication.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ spec:
name: redis-secret
key: password
ignoreAnnotations: [opstreelabs.in/ignore]
minReadySeconds: 5
redisExporter:
enabled: true
image: quay.io/opstree/redis-exporter:v1.44.0
Expand Down
1 change: 1 addition & 0 deletions tests/testdata/redis-sentinel.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ spec:
name: redis-secret
key: password
ignoreAnnotations: [opstreelabs.in/ignore]
minReadySeconds: 5
redisExporter:
enabled: true
image: quay.io/opstree/redis-exporter:v1.44.0
Expand Down
1 change: 1 addition & 0 deletions tests/testdata/redis-standalone.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ spec:
name: redis-secret
key: password
ignoreAnnotations: [opstreelabs.in/ignore]
minReadySeconds: 5
redisExporter:
enabled: true
image: quay.io/opstree/redis-exporter:v1.44.0
Expand Down

0 comments on commit 969c8d4

Please sign in to comment.