Transforming Existing Generators
Wrap an existing generator to adjust its input parameters or output objects
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:
| Function | Returns | Description |
|---|---|---|
ReconcilerNameFromContext(ctx) | string | The name of the reconciler driving the reconciliation. |
LocalClientFromContext(ctx) | cluster.Client | Client for the cluster the component object lives in. |
ClientFromContext(ctx) | cluster.Client | Client for the deployment target cluster (may differ from the local client for remote deployments). |
ComponentFromContext(ctx) | Component | The component object currently being reconciled. |
ComponentNameFromContext(ctx) | string | The name of the component being reconciled. |
ComponentNamespaceFromContext(ctx) | string | The namespace of the component being reconciled. |
ComponentDigestFromContext(ctx) | string | The digest computed for the component’s current state. |
ComponentRevisionFromContext(ctx) | int64 | The 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.
Wrap an existing generator to adjust its input parameters or output objects
Render a Helm chart into dependent objects
Render a (templatized) kustomization into dependent objects