Skip to content

Commit

Permalink
[Add]- Support redis sentinel pdb (#589)
Browse files Browse the repository at this point in the history
* Support redis sentinel pdb

Signed-off-by: drivebyer <[email protected]>

* Adjust example minAvailable

Signed-off-by: drivebyer <[email protected]>

---------

Signed-off-by: drivebyer <[email protected]>
  • Loading branch information
drivebyer authored Aug 27, 2023
1 parent caf446e commit cf88a21
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 0 deletions.
5 changes: 5 additions & 0 deletions controllers/redissentinel_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,11 @@ func (r *RedisSentinelReconciler) Reconcile(ctx context.Context, req ctrl.Reques
return ctrl.Result{}, err
}

err = k8sutils.ReconcileSentinelPodDisruptionBudget(instance, instance.Spec.PodDisruptionBudget)
if err != nil {
return ctrl.Result{}, err
}

// Create the Service for Redis Sentinel
err = k8sutils.CreateRedisSentinelService(instance)
if err != nil {
Expand Down
3 changes: 3 additions & 0 deletions example/redis-sentinel.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ spec:
podSecurityContext:
runAsUser: 1000
fsGroup: 1000
pdb:
enabled: false
minAvailable: 1
redisSentinelConfig:
redisReplicationName : redis-replication
kubernetesConfig:
Expand Down
50 changes: 50 additions & 0 deletions k8sutils/poddisruption.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,29 @@ func ReconcileRedisPodDisruptionBudget(cr *redisv1beta1.RedisCluster, role strin
}
}

func ReconcileSentinelPodDisruptionBudget(cr *redisv1beta1.RedisSentinel, pdbParams *redisv1beta1.RedisPodDisruptionBudget) error {
pdbName := cr.ObjectMeta.Name + "-sentinel"
logger := pdbLogger(cr.Namespace, pdbName)
if pdbParams != nil && pdbParams.Enabled {
labels := getRedisLabels(cr.ObjectMeta.Name, "sentinel", "sentinel", cr.ObjectMeta.GetLabels())
annotations := generateStatefulSetsAnots(cr.ObjectMeta)
pdbMeta := generateObjectMetaInformation(pdbName, cr.Namespace, labels, annotations)
pdbDef := generateSentinelPodDisruptionBudgetDef(cr, "sentinel", pdbMeta, pdbParams)
return CreateOrUpdatePodDisruptionBudget(pdbDef)
} else {
// Check if one exists, and delete it.
_, err := GetPodDisruptionBudget(cr.Namespace, pdbName)
if err == nil {
return deletePodDisruptionBudget(cr.Namespace, pdbName)
} else if err != nil && errors.IsNotFound(err) {
logger.V(1).Info("Reconciliation Successful, no PodDisruptionBudget Found.")
// Its ok if its not found, as we're deleting anyway
return nil
}
return err
}
}

// generatePodDisruptionBudgetDef will create a PodDisruptionBudget definition
func generatePodDisruptionBudgetDef(cr *redisv1beta1.RedisCluster, role string, pdbMeta metav1.ObjectMeta, pdbParams *redisv1beta1.RedisPodDisruptionBudget) *policyv1.PodDisruptionBudget {
lblSelector := LabelSelectors(map[string]string{
Expand Down Expand Up @@ -65,6 +88,33 @@ func generatePodDisruptionBudgetDef(cr *redisv1beta1.RedisCluster, role string,
return pdbTemplate
}

// generatePodDisruptionBudgetDef will create a PodDisruptionBudget definition
func generateSentinelPodDisruptionBudgetDef(cr *redisv1beta1.RedisSentinel, role string, pdbMeta metav1.ObjectMeta, pdbParams *redisv1beta1.RedisPodDisruptionBudget) *policyv1.PodDisruptionBudget {
lblSelector := LabelSelectors(map[string]string{
"app": fmt.Sprintf("%s-%s", cr.ObjectMeta.Name, role),
"role": role,
})
pdbTemplate := &policyv1.PodDisruptionBudget{
TypeMeta: generateMetaInformation("PodDisruptionBudget", "policy/v1"),
ObjectMeta: pdbMeta,
Spec: policyv1.PodDisruptionBudgetSpec{
Selector: lblSelector,
},
}
if pdbParams.MinAvailable != nil {
pdbTemplate.Spec.MinAvailable = &intstr.IntOrString{Type: intstr.Int, IntVal: int32(*pdbParams.MinAvailable)}
}
if pdbParams.MaxUnavailable != nil {
pdbTemplate.Spec.MaxUnavailable = &intstr.IntOrString{Type: intstr.Int, IntVal: int32(*pdbParams.MaxUnavailable)}
}
// If we don't have a value for either, assume quorum: (N/2)+1
if pdbTemplate.Spec.MaxUnavailable == nil && pdbTemplate.Spec.MinAvailable == nil {
pdbTemplate.Spec.MinAvailable = &intstr.IntOrString{Type: intstr.Int, IntVal: int32((*cr.Spec.Size / 2) + 1)}
}
AddOwnerRefToObject(pdbTemplate, redisSentinelAsOwner(cr))
return pdbTemplate
}

// CreateOrUpdateService method will create or update Redis service
func CreateOrUpdatePodDisruptionBudget(pdbDef *policyv1.PodDisruptionBudget) error {
logger := pdbLogger(pdbDef.Namespace, pdbDef.Name)
Expand Down

0 comments on commit cf88a21

Please sign in to comment.