Skip to content
Reconciler Lifecycle

Reconciler Lifecycle

When a custom resource is created, updated, or deleted, genop runs your reconcilers in order. You never write a Reconcile(ctx, req) function — instead, you implement focused interfaces.

Create / Update

Implement ApplyStep[S, T]:

func (r *ProvisionDatabase) GetConditionType() string {
    return "DatabaseProvisioned"
}

func (r *ProvisionDatabase) Apply(ctx context.Context, logger logr.Logger, resource *v1alpha1.Database) (ctrl.Result, string, error) {
    // Your logic here: call an external API, create a child resource, etc.
    return operators.Succeed()
}

Delete

Implement DeletionStep[S, T]:

func (r *DeprovisionDatabase) GetConditionType() string {
    return "DatabaseProvisioned"
}

func (r *DeprovisionDatabase) Delete(ctx context.Context, logger logr.Logger, resource *v1alpha1.Database) (ctrl.Result, string, error) {
    // Cleanup logic: delete external resources, revoke credentials, etc.
    return operators.Succeed()
}

You don’t need to implement both. If your resource has no cleanup logic, omit the delete step and remove it from WithDeletionSteps in main.go.

GetConditionType() declares which Kubernetes condition this step manages. The value is hardcoded in each generated struct — it comes from the conditionType field in .genop.yaml, set at scaffolding time.

Response types

Your reconciler returns a Response that tells genop what to do next:

FunctionWhen to use
operators.Succeed()The reconciler completed successfully. Proceed to the next reconciler in the chain.
operators.Requeue(duration, message)The reconciler needs to retry after a fixed delay (e.g. waiting for an external resource to be ready).
operators.Fail(message)The reconciler failed. Status is set to Failed, backoff is applied automatically.
operators.FailWithError(err)Same as Fail, wrapping an existing error instead of a message.

If you return an error alongside any response, genop treats it as a failure and applies backoff — see Backoff and retry.

Reconciler chain

Reconcilers run in order as listed in WithApplySteps / WithDeletionSteps:

databaseOperator, err := operators.NewOperatorBuilder(
    mgr.GetClient(), dynamicClient, controllerName,
    eventBroadcaster.NewRecorder(mgr.GetScheme(), controllerName),
    &v1alpha1.Database{},
).
    WithApplySteps([]operators.ApplyStep[*v1alpha1.DatabaseStatus, *v1alpha1.Database]{
        applyv1alpha1database.NewProvisionDatabase(mgr.GetClient(), dynamicClient),
    }).
    WithDeletionSteps([]operators.DeletionStep[*v1alpha1.DatabaseStatus, *v1alpha1.Database]{
        deletev1alpha1database.NewDeprovisionDatabase(mgr.GetClient(), dynamicClient),
    }).
    Build()
if err != nil {
    setupLog.Error(err, "Failed to create operator", "controller", "Database")
    os.Exit(1)
}

if err := (&v1alpha1database.DatabaseReconciler{Operator: databaseOperator}).SetupWithManager(mgr); err != nil {
    setupLog.Error(err, "Failed to create controller", "controller", "Database")
    os.Exit(1)
}

Build() validates the configuration and returns the operator; wiring it into the manager is a separate step, on the generated <Kind>Reconciler wrapper (controller.go), not on the builder itself.

If a reconciler requeues or fails, the chain stops — subsequent reconcilers won’t run until the next reconciliation cycle. See Adding a reconciler for the genop create step command that scaffolds one.

Sharing state between reconcilers

Use the operator context to pass data between reconcilers in the same chain:

// In the first reconciler
conn, err := connectToDatabase(...)
operators.PutInContext(ctx, "db-connection", conn)

// In a later reconciler
conn, err := operators.GetFromContext[*DatabaseConnection](ctx, "db-connection")

The context is scoped to a single reconciliation cycle and is not persisted.