Generators

The Generator interface, built-in generators, and how to transform them

A generator is the recipe that turns a component’s parameters into the concrete, applyable resource manifests of its dependent objects. Where the Component interface models the desired and observed state of a component, the Generator interface encapsulates how the dependent objects are rendered from the component’s parameterization (its spec):

package manifests

// Resource generator interface.
// When called from the reconciler, the arguments namespace and name will match the
// component's namespace and name or, if the component or its spec implement the
// PlacementConfiguration interface, the return values of the GetDeploymentNamespace(),
// GetDeploymentName() methods (if non-empty). The parameters argument will be assigned
// the return value of the component's GetSpec() method.
type Generator interface {
	Generate(ctx context.Context, namespace string, name string, parameters types.Unstructurable) ([]client.Object, error)
}

The returned []client.Object is exactly the manifest list that the framework hands to the low-level object reconciler, which then creates, updates, and deletes the dependent objects in the cluster.

The generation logic can be implemented natively in Go: a component controller is free to provide its own Generator, assembling and returning client.Object values by whatever means it likes. In practice, however, most components are described through some form of templating. For these common cases, the framework ships two ready-made generators:

Both are passed to component.NewReconciler[T]() as the resourceGenerator argument.

Additional contextual information can be retrieved inside Generate() from the passed context.Context via the accessor functions in package pkg/component. Each of them returns an error if the requested value is not present in the context:

FunctionReturnsDescription
ReconcilerNameFromContext(ctx)stringThe name of the reconciler driving the reconciliation.
LocalClientFromContext(ctx)cluster.ClientClient for the cluster the component object lives in.
ClientFromContext(ctx)cluster.ClientClient for the deployment target cluster (may differ from the local client for remote deployments).
ComponentFromContext(ctx)ComponentThe component object currently being reconciled.
ComponentNameFromContext(ctx)stringThe name of the component being reconciled.
ComponentNamespaceFromContext(ctx)stringThe namespace of the component being reconciled.
ComponentDigestFromContext(ctx)stringThe digest computed for the component’s current state.
ComponentRevisionFromContext(ctx)int64The revision counter of the component being reconciled.

Generators may optionally implement the SchemeBuilder interface

package types

type SchemeBuilder interface {
	AddToScheme(scheme *runtime.Scheme) error
}

in order to enhance the scheme used by the dependent objects deployer.

Topics


Transforming Existing Generators

Wrap an existing generator to adjust its input parameters or output objects

Helm Generator

Render a Helm chart into dependent objects

Kustomize Generator

Render a (templatized) kustomization into dependent objects