Skip to content
Escape Hatches

Escape Hatches

Everything genop generates is one of two things:

  • Regenerated on every genop apply — topology-driven, rewritten in full each time. Never hand-edit these regions; your edit is gone on the next apply. The WithApplySteps/WithDeletionSteps lists and the rest of the // +genop:scaffold:cmd:controllers:begin/end block in main.go are the main example.
  • Written once, yours forever — created the first time (genop create api/genop create step), never touched again by any later genop command. Step files, operator_config.go, and the code between marked regions in main.go are all like this.

Tip

Before reaching for a hatch, check the topology. Anything expressible in .genop.yaml — adding a step or hook, the operator: block (retry policy, backoff, deletion policy, …), webhooks, metrics — is done by editing the file and running genop apply, which regenerates the wiring for you. The hatches below are for what the topology can’t express.

Two mechanisms use the second kind of file as an escape hatch for anything the topology can’t express.

The operator itself — Configure<Kind>Operator

genop apply regenerates the operator-builder wiring in main.go on every run. internal/controllers/<version>/<kindLower>/operator_config.go is generated once and never rewritten:

func ConfigureVPCOperator(
    b *operators.OperatorBuilder[*infrav1alpha1.VPCStatus, *infrav1alpha1.VPC],
) *operators.OperatorBuilder[*infrav1alpha1.VPCStatus, *infrav1alpha1.VPC] {
    return b.WithDeletionPolicyProvider(myCustomPolicy(b.GetClient().Scheme()))
}

The regenerated wiring always chains it in via WithConfigOverride — the last call before Build(), after every option the operator: block in .genop.yaml set — so it can add options genop doesn’t surface, or override ones it does. b.GetClient() exposes Scheme(), RESTMapper() and the usual CRUD methods; b.GetDynamicClient() is the same dynamic client the generated wiring uses.

Individual steps — New<Step> constructors

Each apply/delete step file also gets a constructor, generated once alongside the struct:

// apply/mountvolume.go
type MountVolume struct {
    K8sClient client.Client
}

func NewMountVolume(k8sClient client.Client, dynamicClient dynamic.Interface) *MountVolume {
    return &MountVolume{K8sClient: k8sClient}
}

main.go’s regenerated WithApplySteps list calls applyv1alpha1instance.NewMountVolume(mgr.GetClient(), dynamicClient) instead of a bare &MountVolume{} literal — so adding a field and wiring it in the constructor is a single, permanent hand-edit to one file, never touched again by genop apply.

Sharing one dependency across steps and operators

Say two steps on the same resource — MountVolume and CreateInstance — need the same cloud-provider client, and so does a step on a different resource (Volume). It’s a plain Go package-level var, set from main.go’s already-safe area (outside every // +genop:scaffold:... region).

genop create step scaffolds a deps.go alongside every apply//delete/ step file, once, skip-if-exists — the natural place for it:

// apply/deps.go — scaffolded once by genop create step; edit freely
package apply

// Add package-level vars here for dependencies shared by more than one step
// in this package - set them from main.go before the operator is built.

Add the var and import your client:

import "myproject/internal/cloudprovider"

// Client is the shared client used by every apply step in this package.
// Nil until main.go sets it during startup, before the operator is built.
var Client cloudprovider.Client

This is process-global mutable state: fine for a single operator process, but it means, for example, parallel tests that want different dependency instances need to coordinate/serialize on the var rather than each getting an isolated instance.

// apply/mountvolume.go — write-once, genop-generated; you add the field + wiring
func NewMountVolume(k8sClient client.Client, dynamicClient dynamic.Interface) *MountVolume {
    return &MountVolume{K8sClient: k8sClient, CloudProviderClient: Client}
}

A resource in a different package (e.g. Volume’s own apply package) needs its own copy of the var, set to the same client instance:

// main.go, between marker regions — already safe today
cloudProviderClient, err := cloudprovider.NewClient(cloudProviderURL, cloudProviderAPIKey)
if err != nil {
    setupLog.Error(err, "unable to instantiate cloud provider client")
    os.Exit(1)
}
applyv1alpha1instance.Client = cloudProviderClient
applyv1alpha1volume.Client = cloudProviderClient

One client, constructed once, shared into both packages by name. Every genop apply afterward only ever touches the New...(mgr.GetClient(), dynamicClient) call sites.

deps.go, and any other file you hand-write in an apply//delete/ directory under any name, is never flagged as drift or pruned — genop’s drift detection only ever claims files it generated itself: a step file carries a +genop:step marker in its doc comment, and only a file carrying that marker is ever considered for pruning when its step is removed from .genop.yaml. Nothing else in that directory is genop’s business.

The same deps.go pattern is how custom Kubernetes events are wired in — see Events.