annotate-apply-time-mutations: Inline Comment Example #
Overview #
This example shows how to use the annotate-apply-time-mutations function with
inline field comments.
The scenario: a Deployment needs configuration values from other resources that
are only available after those resources are reconciled — a replica count from a
cluster config and a database host and port. Inline apply-time-mutation
comments on the target fields tell kpt live apply to defer the substitutions
to apply time.
This example exercises several inline comment patterns:
- Full field replacement with no token (replicas, DB_PORT)
- Token substitution with prefix/suffix (DB_URL)
- An existing annotation that is not modified (unmodified-key)
- A label that is not modified (unmodified-label)
Running annotate-apply-time-mutations function on the example package will:
- Scan resources for
apply-time-mutationinline comments. - Generate the
config.kubernetes.io/apply-time-mutationannotation on the target resources, whichkpt live applyreads to perform the substitution.
Fetch the example package #
Get the example package by running the following commands:
kpt pkg get https://github.com/kptdev/krm-functions-catalog.git/examples/annotate-apply-time-mutations-inline-comment
Function invocation #
Invoke the function with the following command:
kpt fn eval annotate-apply-time-mutations-inline-comment --image ghcr.io/kptdev/krm-functions-catalog/annotate-apply-time-mutations:latest
Expected result #
- File resources.yaml will include
config.kubernetes.io/apply-time-mutationannotations generated from the inline comments. - The DB_URL field with prefix/suffix will be updated with a generated replacement token.
- Fields without comments, existing annotations, and labels are unchanged.
When kpt live apply is subsequently run, it will use these annotations to
wait for source resources to be reconciled and then substitute the referenced
field values into the target resources.