From de22a21be474a898212d654674a9628f53357493 Mon Sep 17 00:00:00 2001 From: xiaozhuang Date: Wed, 18 Dec 2024 16:49:39 +0800 Subject: [PATCH] feat: redis-cluster add TopologySpreadConstraints --- api/common_types.go | 34 ++++++++++++++------------ api/v1beta1/rediscluster_conversion.go | 12 +++++++++ api/zz_generated.deepcopy.go | 14 +++++++++++ pkg/k8sutils/redis-cluster.go | 9 ++++--- pkg/k8sutils/redis-cluster_test.go | 2 ++ 5 files changed, 52 insertions(+), 19 deletions(-) diff --git a/api/common_types.go b/api/common_types.go index 378bda1bc..883c39ddb 100644 --- a/api/common_types.go +++ b/api/common_types.go @@ -105,27 +105,29 @@ type Sidecar struct { // RedisLeader interface will have the redis leader configuration // +k8s:deepcopy-gen=true type RedisLeader struct { - Replicas *int32 `json:"replicas,omitempty"` - RedisConfig *RedisConfig `json:"redisConfig,omitempty"` - Affinity *corev1.Affinity `json:"affinity,omitempty"` - PodDisruptionBudget *RedisPodDisruptionBudget `json:"pdb,omitempty"` - ReadinessProbe *corev1.Probe `json:"readinessProbe,omitempty" protobuf:"bytes,11,opt,name=readinessProbe"` - LivenessProbe *corev1.Probe `json:"livenessProbe,omitempty" protobuf:"bytes,12,opt,name=livenessProbe"` - Tolerations *[]corev1.Toleration `json:"tolerations,omitempty"` - NodeSelector map[string]string `json:"nodeSelector,omitempty"` + Replicas *int32 `json:"replicas,omitempty"` + RedisConfig *RedisConfig `json:"redisConfig,omitempty"` + Affinity *corev1.Affinity `json:"affinity,omitempty"` + PodDisruptionBudget *RedisPodDisruptionBudget `json:"pdb,omitempty"` + ReadinessProbe *corev1.Probe `json:"readinessProbe,omitempty" protobuf:"bytes,11,opt,name=readinessProbe"` + LivenessProbe *corev1.Probe `json:"livenessProbe,omitempty" protobuf:"bytes,12,opt,name=livenessProbe"` + Tolerations *[]corev1.Toleration `json:"tolerations,omitempty"` + NodeSelector map[string]string `json:"nodeSelector,omitempty"` + TopologySpreadConstraints []corev1.TopologySpreadConstraint `json:"topologySpreadConstraints,omitempty"` } // RedisFollower interface will have the redis follower configuration // +k8s:deepcopy-gen=true type RedisFollower struct { - Replicas *int32 `json:"replicas,omitempty"` - RedisConfig *RedisConfig `json:"redisConfig,omitempty"` - Affinity *corev1.Affinity `json:"affinity,omitempty"` - PodDisruptionBudget *RedisPodDisruptionBudget `json:"pdb,omitempty"` - ReadinessProbe *corev1.Probe `json:"readinessProbe,omitempty" protobuf:"bytes,11,opt,name=readinessProbe"` - LivenessProbe *corev1.Probe `json:"livenessProbe,omitempty" protobuf:"bytes,12,opt,name=livenessProbe"` - Tolerations *[]corev1.Toleration `json:"tolerations,omitempty"` - NodeSelector map[string]string `json:"nodeSelector,omitempty"` + Replicas *int32 `json:"replicas,omitempty"` + RedisConfig *RedisConfig `json:"redisConfig,omitempty"` + Affinity *corev1.Affinity `json:"affinity,omitempty"` + PodDisruptionBudget *RedisPodDisruptionBudget `json:"pdb,omitempty"` + ReadinessProbe *corev1.Probe `json:"readinessProbe,omitempty" protobuf:"bytes,11,opt,name=readinessProbe"` + LivenessProbe *corev1.Probe `json:"livenessProbe,omitempty" protobuf:"bytes,12,opt,name=livenessProbe"` + Tolerations *[]corev1.Toleration `json:"tolerations,omitempty"` + NodeSelector map[string]string `json:"nodeSelector,omitempty"` + TopologySpreadConstraints []corev1.TopologySpreadConstraint `json:"topologySpreadConstraints,omitempty"` } // RedisPodDisruptionBudget configure a PodDisruptionBudget on the resource (leader/follower) diff --git a/api/v1beta1/rediscluster_conversion.go b/api/v1beta1/rediscluster_conversion.go index 0ee4f3018..02064cda8 100644 --- a/api/v1beta1/rediscluster_conversion.go +++ b/api/v1beta1/rediscluster_conversion.go @@ -48,6 +48,9 @@ func (src *RedisCluster) ConvertTo(dstRaw conversion.Hub) error { if src.Spec.RedisLeader.NodeSelector != nil { dst.Spec.RedisLeader.NodeSelector = src.Spec.RedisLeader.NodeSelector } + if src.Spec.RedisLeader.TopologySpreadConstraints != nil { + dst.Spec.RedisLeader.TopologySpreadConstraints = src.Spec.RedisLeader.TopologySpreadConstraints + } // RedisFollower dst.Spec.RedisFollower = redisv1beta2.RedisFollower{} @@ -75,6 +78,9 @@ func (src *RedisCluster) ConvertTo(dstRaw conversion.Hub) error { if src.Spec.RedisFollower.NodeSelector != nil { dst.Spec.RedisFollower.NodeSelector = src.Spec.RedisFollower.NodeSelector } + if src.Spec.RedisFollower.TopologySpreadConstraints != nil { + dst.Spec.RedisFollower.TopologySpreadConstraints = src.Spec.RedisFollower.TopologySpreadConstraints + } // RedisExporter if src.Spec.RedisExporter != nil { dst.Spec.RedisExporter = &redisv1beta2.RedisExporter{} @@ -168,6 +174,9 @@ func (dst *RedisCluster) ConvertFrom(srcRaw conversion.Hub) error { if src.Spec.RedisLeader.NodeSelector != nil { dst.Spec.RedisLeader.NodeSelector = src.Spec.RedisLeader.NodeSelector } + if src.Spec.RedisLeader.TopologySpreadConstraints != nil { + dst.Spec.RedisLeader.TopologySpreadConstraints = src.Spec.RedisLeader.TopologySpreadConstraints + } // RedisFollower dst.Spec.RedisFollower = RedisFollower{} @@ -195,6 +204,9 @@ func (dst *RedisCluster) ConvertFrom(srcRaw conversion.Hub) error { if src.Spec.RedisFollower.NodeSelector != nil { dst.Spec.RedisFollower.NodeSelector = src.Spec.RedisFollower.NodeSelector } + if src.Spec.RedisFollower.TopologySpreadConstraints != nil { + dst.Spec.RedisFollower.TopologySpreadConstraints = src.Spec.RedisFollower.TopologySpreadConstraints + } // RedisExporter if src.Spec.RedisExporter != nil { dst.Spec.RedisExporter = &RedisExporter{} diff --git a/api/zz_generated.deepcopy.go b/api/zz_generated.deepcopy.go index 004200305..176a705b1 100644 --- a/api/zz_generated.deepcopy.go +++ b/api/zz_generated.deepcopy.go @@ -240,6 +240,13 @@ func (in *RedisFollower) DeepCopyInto(out *RedisFollower) { (*out)[key] = val } } + if in.TopologySpreadConstraints != nil { + in, out := &in.TopologySpreadConstraints, &out.TopologySpreadConstraints + *out = make([]v1.TopologySpreadConstraint, len(*in)) + for i := range *in { + (*in)[i].DeepCopyInto(&(*out)[i]) + } + } } // DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new RedisFollower. @@ -303,6 +310,13 @@ func (in *RedisLeader) DeepCopyInto(out *RedisLeader) { (*out)[key] = val } } + if in.TopologySpreadConstraints != nil { + in, out := &in.TopologySpreadConstraints, &out.TopologySpreadConstraints + *out = make([]v1.TopologySpreadConstraint, len(*in)) + for i := range *in { + (*in)[i].DeepCopyInto(&(*out)[i]) + } + } } // DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new RedisLeader. diff --git a/pkg/k8sutils/redis-cluster.go b/pkg/k8sutils/redis-cluster.go index beb6ac9b7..f63f96abe 100644 --- a/pkg/k8sutils/redis-cluster.go +++ b/pkg/k8sutils/redis-cluster.go @@ -24,6 +24,7 @@ type RedisClusterSTS struct { ReadinessProbe *corev1.Probe LivenessProbe *corev1.Probe NodeSelector map[string]string + TopologySpreadConstraints []corev1.TopologySpreadConstraint Tolerations *[]corev1.Toleration } @@ -219,9 +220,11 @@ func CreateRedisLeader(ctx context.Context, cr *redisv1beta2.RedisCluster, cl ku Affinity: cr.Spec.RedisLeader.Affinity, TerminationGracePeriodSeconds: cr.Spec.RedisLeader.TerminationGracePeriodSeconds, NodeSelector: cr.Spec.RedisLeader.NodeSelector, - Tolerations: cr.Spec.RedisLeader.Tolerations, - ReadinessProbe: cr.Spec.RedisLeader.ReadinessProbe, - LivenessProbe: cr.Spec.RedisLeader.LivenessProbe, + TopologySpreadConstraints: cr.Spec.RedisLeader.TopologySpreadConstraints, + + Tolerations: cr.Spec.RedisLeader.Tolerations, + ReadinessProbe: cr.Spec.RedisLeader.ReadinessProbe, + LivenessProbe: cr.Spec.RedisLeader.LivenessProbe, } if cr.Spec.RedisLeader.RedisConfig != nil { prop.ExternalConfig = cr.Spec.RedisLeader.RedisConfig.AdditionalRedisConfig diff --git a/pkg/k8sutils/redis-cluster_test.go b/pkg/k8sutils/redis-cluster_test.go index 90993475d..b8881fee9 100644 --- a/pkg/k8sutils/redis-cluster_test.go +++ b/pkg/k8sutils/redis-cluster_test.go @@ -174,6 +174,7 @@ func Test_generateRedisClusterParams(t *testing.T) { ReadinessProbe: input.Spec.RedisLeader.ReadinessProbe, LivenessProbe: input.Spec.RedisLeader.LivenessProbe, NodeSelector: input.Spec.RedisLeader.NodeSelector, + TopologySpreadConstraints: input.Spec.RedisLeader.TopologySpreadConstraints, Tolerations: input.Spec.RedisLeader.Tolerations, }) assert.EqualValues(t, expectedLeaderSTS, actualLeaderSTS, "Expected %+v, got %+v", expectedLeaderSTS, actualLeaderSTS) @@ -187,6 +188,7 @@ func Test_generateRedisClusterParams(t *testing.T) { ReadinessProbe: input.Spec.RedisFollower.ReadinessProbe, LivenessProbe: input.Spec.RedisFollower.LivenessProbe, NodeSelector: input.Spec.RedisFollower.NodeSelector, + TopologySpreadConstraints: input.Spec.RedisFollower.TopologySpreadConstraints, Tolerations: input.Spec.RedisFollower.Tolerations, }) assert.EqualValues(t, expectedFollowerSTS, actualFollowerSTS, "Expected %+v, got %+v", expectedFollowerSTS, actualFollowerSTS)