Sub topic 12
Reusing optional features with components
This chapter assumes you can write a base and an overlay, and that you know how patches select their targets. Overlays are good at describing one environment. They are awkward when the same optional feature, such as metrics, must be switched on in some environments and left off in others.
The problem components solve
Suppose staging and production both need a metrics sidecar, but dev does not. You could copy the patch into both overlays. That brings back the duplication Kustomize is meant to remove. You could put the patch in the base. Then dev gets metrics too.
A component is a reusable bundle of Kustomize configuration that an overlay opts into. It can hold patches, resources, generators and transformers. An overlay lists the components it wants, and only those overlays get the feature.
Writing a component
A component lives in its own directory and has a kustomization.yaml with kind: Component. Its API version is kustomize.config.k8s.io/v1alpha1, not v1beta1.
apiVersion: kustomize.config.k8s.io/v1alpha1
kind: Component
patches:
- path: enable-metrics.yaml
target:
kind: Deployment
name: webThe file components/metrics/enable-metrics.yaml sits next to it. It is an ordinary patch. This one is a strategic merge patch that adds an annotation to the pod template:
apiVersion: apps/v1
kind: Deployment
metadata:
name: web
spec:
template:
metadata:
annotations:
prometheus.io/scrape: "true"
prometheus.io/port: "8080"Using a component from an overlay
An overlay adds the directory to its components field. The path is relative to the overlay's kustomization.yaml.
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
resources:
- ../../base
components:
- ../../components/metrics
namespace: productionThe dev overlay simply omits the components entry.
How components differ from bases
A base is a complete set of resources. It is meant to be built on its own, and an overlay refers to it under resources. A component is a fragment. It is applied to whatever the including overlay has already accumulated.
This has a practical effect. A component's patch targets resources that do not exist inside the component directory. The target Deployment/web is resolved when the overlay builds, against the resources the overlay pulled in. Because of that, running kubectl kustomize components/metrics on its own will fail. Always build an overlay that includes it.
| Property | Base (`resources`) | Component (`components`) |
|---|---|---|
| `kind` | `Kustomization` | `Component` |
| Builds on its own | Yes | No |
| Typical content | Full resources | Patches, generators, transformers |
| Applied to | Itself | The including overlay's resources |
Verifying the result
Render the overlay and check that the feature appears only where you expect it.
kubectl kustomize overlays/production | grep prometheus.io
kubectl kustomize overlays/dev | grep prometheus.ioThe first command prints the two annotations. The second prints nothing.
Common errors
- Wrong kind or version. Using
kind: Kustomizationin a component directory, then listing it undercomponents, fails. Usekind: Componentwithv1alpha1. - Listing a component under
resources. Kustomize then tries to build it as a base, and the patch finds no target. - Patch target not found. The component's patch names
web, but the overlay setsnamePrefix: prod-. Patches in a component are matched against the original names, so this normally works. If it fails, check that the base really contains a Deployment calledweb. - Order surprises. Two components that patch the same field can conflict. Keep each component focused on one concern.
Exercise
The dev overlay should not get the metrics feature, but staging and production should. Where do you reference ../../components/metrics, and why not in the base?
Show answer
Add it to the components field of the staging and production overlay kustomization.yaml files. Putting it in the base would apply it to every overlay, including dev.
Exercise
Why does kubectl kustomize components/metrics fail when run on its own?
Show answer
A component is a fragment with no resources of its own. Its patch targets Deployment/web, which only exists once an overlay has loaded the base, so there is nothing to patch.