Skip to content

Commit

Permalink
Address review comments from @unmarshall: simplify `operatorContext.D…
Browse files Browse the repository at this point in the history
…ata` and `GetBoolValueOrDefault()` function
  • Loading branch information
shreyas-s-rao committed Feb 6, 2025
1 parent 97282ed commit 0f9cdd8
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 33 deletions.
5 changes: 1 addition & 4 deletions internal/controller/etcd/reconcile_status.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,10 +82,7 @@ func (r *Reconciler) inspectStatefulSetAndMutateETCDStatus(ctx component.Operato
}

func (r *Reconciler) mutateObservedGeneration(ctx component.OperatorContext, etcd *druidv1alpha1.Etcd, _ logr.Logger) ctrlutils.ReconcileStepResult {
canReconcileSpec := utils.GetBoolValueOrDefault(ctx.Data, reconciliationContextDataKeyCanReconcileSpec, false)
shortCircuitSpecReconcile := utils.GetBoolValueOrDefault(ctx.Data, reconciliationContextDataKeyShortCircuitSpecReconcile, true)

if canReconcileSpec && !shortCircuitSpecReconcile {
if utils.GetBoolValueOrDefault(ctx.Data, reconciliationContextDataKeyWasSpecReconciled, false) {
etcd.Status.ObservedGeneration = &etcd.Generation
}
return ctrlutils.ContinueReconcile()
Expand Down
20 changes: 10 additions & 10 deletions internal/controller/etcd/reconciler.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,7 @@ import (
"sigs.k8s.io/controller-runtime/pkg/manager"
)

const (
reconciliationContextDataKeyCanReconcileSpec = "canReconcileSpec"
reconciliationContextDataKeyShortCircuitSpecReconcile = "shortCircuitSpecReconcile"
)
const reconciliationContextDataKeyWasSpecReconciled = "wasSpecReconciled"

// Reconciler reconciles the Etcd resource spec and status.
type Reconciler struct {
Expand Down Expand Up @@ -99,7 +96,8 @@ type reconcileFn func(ctx component.OperatorContext, objectKey client.ObjectKey)
// and if there is a need then reconcile spec.
// 3. Status Reconciliation: Always update the status of the Etcd component to reflect its current state,
// as well as status fields derived from spec reconciliation.
// 4. Scheduled Requeue: Requeue the reconciliation request after a defined period (EtcdStatusSyncPeriod) to maintain sync.
// 4. Remove operation-reconcile annotation if it was set and if spec reconciliation had succeeded.
// 5. Scheduled Requeue: Requeue the reconciliation request after a defined period (EtcdStatusSyncPeriod) to maintain sync.
func (r *Reconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
runID := string(controller.ReconcileIDFromContext(ctx))
operatorCtx := component.NewOperatorContext(ctx, r.logger, runID)
Expand All @@ -116,8 +114,9 @@ func (r *Reconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Resu
var reconcileSpecResult ctrlutils.ReconcileStepResult
if canReconcileSpec {
reconcileSpecResult = r.reconcileSpec(operatorCtx, req.NamespacedName)
operatorCtx.Data[reconciliationContextDataKeyCanReconcileSpec] = strconv.FormatBool(canReconcileSpec)
operatorCtx.Data[reconciliationContextDataKeyShortCircuitSpecReconcile] = strconv.FormatBool(ctrlutils.ShortCircuitReconcileFlow(reconcileSpecResult))
if !ctrlutils.ShortCircuitReconcileFlow(reconcileSpecResult) {
operatorCtx.Data[reconciliationContextDataKeyWasSpecReconciled] = strconv.FormatBool(true)
}
}

reconcileStatusResult := r.reconcileStatus(operatorCtx, req.NamespacedName)
Expand All @@ -126,6 +125,10 @@ func (r *Reconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Resu
return reconcileStatusResult.ReconcileResult()
}

if reconcileSpecResult.NeedsRequeue() {
return reconcileSpecResult.ReconcileResult()
}

// Operation annotation is removed at the end of reconciliation flow, after both spec and status
// have been successfully reconciled. This ensures that if there are any errors in either spec or status
// reconcile flow upon addition of operation annotation, then the next requeue of reconciliation will attempt
Expand All @@ -141,9 +144,6 @@ func (r *Reconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Resu
}
}

if reconcileSpecResult.NeedsRequeue() {
return reconcileSpecResult.ReconcileResult()
}
return ctrlutils.ReconcileAfter(r.config.EtcdStatusSyncPeriod, "Periodic Requeue").ReconcileResult()
}

Expand Down
22 changes: 3 additions & 19 deletions internal/utils/miscellaneous.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,30 +83,14 @@ func ComputeScheduleInterval(cronSchedule string) (time.Duration, error) {
return nextNextScheduledTime.Sub(nextScheduledTime), nil
}

func getValueOrError(data map[string]string, key string) (string, error) {
value, ok := data[key]
if !ok {
return "", fmt.Errorf("key %s not found in data", key)
}
return value, nil
}

func parseBoolOrError(value string) (bool, error) {
result, err := strconv.ParseBool(value)
if err != nil {
return false, fmt.Errorf("failed to parse boolean value: %w", err)
}
return result, nil
}

// GetBoolValueOrDefault returns the boolean value for the given key from the data map,
// and defaults to defaultValue if the key is not found or the value is not a valid boolean.
func GetBoolValueOrDefault(data map[string]string, key string, defaultValue bool) bool {
value, err := getValueOrError(data, key)
if err != nil {
value, ok := data[key]
if !ok {
return defaultValue
}
result, err := parseBoolOrError(value)
result, err := strconv.ParseBool(value)
if err != nil {
return defaultValue
}
Expand Down

0 comments on commit 0f9cdd8

Please sign in to comment.