Skip to content

Commit

Permalink
k8s: using conditions, fix
Browse files Browse the repository at this point in the history
  • Loading branch information
alejandroEsc committed Aug 30, 2023
1 parent ede1b3d commit ae0743c
Showing 1 changed file with 26 additions and 27 deletions.
53 changes: 26 additions & 27 deletions src/go/k8s/controllers/redpanda/redpanda_decommission_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ import (
// +kubebuilder:rbac:groups="",resources=persistentvolumes,verbs=get;list;update;patch;watch

const (
RedpandaDecommissionLabelKey = "app.redpanda.io/decommission-start"
RedpandaDecommissionCondition = v1.StatefulSetConditionType("RedpandaDecommission")
)

Expand Down Expand Up @@ -86,31 +85,31 @@ func (r *DecommissionReconciler) Reconcile(c context.Context, req ctrl.Request)
}
}

if decommissionCondition == nil {
decommissionCondition = &v1.StatefulSetCondition{
Type: RedpandaDecommissionCondition,
Status: corev1.ConditionUnknown,
}
}

var err error
var result ctrl.Result

if decommissionCondition != nil {
switch decommissionCondition.Status {
case corev1.ConditionFalse:
log.Info(fmt.Sprintf("is decomission started; condition is : %s", corev1.ConditionFalse))
timeStampParsed := decommissionCondition.LastTransitionTime.Time
result, err = r.reconcileDecommission(ctx, timeStampParsed, sts)
if err != nil {
log.Info(fmt.Sprintf("found error trying to reconcile decommission, we will requeue: %s", err))
}
case corev1.ConditionTrue:
patch := client.MergeFrom(sts.DeepCopy())
decommissionCondition.Status = corev1.ConditionUnknown
if patchErr := r.Patch(ctx, sts, patch); patchErr != nil {
return ctrl.Result{}, fmt.Errorf("unable to delete decommission annotation: %w", patchErr)
}
case corev1.ConditionUnknown:
result, _, err = r.verifyIfNeedDecommission(ctx, sts)
if err != nil {
log.Info(fmt.Sprintf("found error trying to verify if we need to decommission: %s", err))
}
switch decommissionCondition.Status {
case corev1.ConditionFalse:
log.Info(fmt.Sprintf("is decomission started; condition is : %s", corev1.ConditionFalse))
timeStampParsed := decommissionCondition.LastTransitionTime.Time
result, err = r.reconcileDecommission(ctx, timeStampParsed, sts)
if err != nil {
log.Info(fmt.Sprintf("found error trying to reconcile decommission, we will requeue: %s", err))
}
} else {
case corev1.ConditionTrue:
patch := client.MergeFrom(sts.DeepCopy())
decommissionCondition.Status = corev1.ConditionUnknown
if patchErr := r.Patch(ctx, sts, patch); patchErr != nil {
return ctrl.Result{}, fmt.Errorf("unable to delete decommission annotation: %w", patchErr)
}
case corev1.ConditionUnknown:
result, _, err = r.verifyIfNeedDecommission(ctx, sts)
if err != nil {
log.Info(fmt.Sprintf("found error trying to verify if we need to decommission: %s", err))
Expand Down Expand Up @@ -229,15 +228,15 @@ func (r *DecommissionReconciler) verifyIfNeedDecommission(ctx context.Context, s
}
}

if decommissionCondition != nil {
decommissionCondition.Status = corev1.ConditionFalse
} else {
if decommissionCondition == nil {
decommissionCondition = &v1.StatefulSetCondition{
Type: RedpandaDecommissionCondition,
LastTransitionTime: metav1.Now(),
Type: RedpandaDecommissionCondition,
}
}

decommissionCondition.Status = corev1.ConditionFalse
decommissionCondition.LastTransitionTime = metav1.Now()

if patchErr := r.Patch(ctx, sts, patch); patchErr != nil {
return ctrl.Result{}, valuesMap, fmt.Errorf("unable to delete decommission annotation: %w", patchErr)
}
Expand Down

0 comments on commit ae0743c

Please sign in to comment.