-
Notifications
You must be signed in to change notification settings - Fork 263
docs: add KRM function developer guide #4726
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
efiacor
wants to merge
3
commits into
kptdev:main
Choose a base branch
from
Nordix:docs/krm-function-developer-guide
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| --- | ||
| title: KRM Function Developer Guide | ||
| linkTitle: KRM Function Developer Guide | ||
| description: Write your own KRM functions with the Go SDK. | ||
| toc_hide: false | ||
| menu: | ||
| main: | ||
| parent: "Guides" | ||
| --- | ||
| This guide walks through writing KRM functions with the | ||
| [Go SDK](https://github.com/kptdev/krm-functions-sdk). Start with the tutorial, | ||
| then dig into the topic guides as needed. | ||
|
|
||
| - [Tutorial]({{% relref "/guides/krm-functions/tutorial" %}}) — build a working | ||
| function end to end, with embedded documentation, golden tests, and support for | ||
| `--help`, `--doc`, and standalone file mode. | ||
| - [Interfaces]({{% relref "/guides/krm-functions/interfaces" %}}) — choose between | ||
| `fn.Runner` (transformers, validators) and `fn.ResourceListProcessor` | ||
| (generators, complex functions). | ||
| - [Testing]({{% relref "/guides/krm-functions/testing" %}}) — golden test patterns | ||
| and unit testing in depth. | ||
| - [Containerizing]({{% relref "/guides/krm-functions/containerizing" %}}) — package | ||
| your function as a container image. | ||
|
|
||
| For a complete working example, see | ||
| [`go/get-started/`](https://github.com/kptdev/krm-functions-sdk/tree/main/go/get-started). |
148 changes: 148 additions & 0 deletions
148
documentation/content/en/guides/krm-functions/containerizing.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,148 @@ | ||
| --- | ||
| title: Containerizing | ||
| linkTitle: Containerizing | ||
| description: Package a KRM function as a container image. | ||
| toc_hide: false | ||
| menu: | ||
| main: | ||
| parent: "KRM Function Developer Guide" | ||
| weight: 40 | ||
| --- | ||
| KRM functions are distributed as container images. This guide covers building | ||
| and running containerized functions. | ||
|
|
||
| ## Dockerfile | ||
|
|
||
| The [krm-functions-catalog](https://github.com/kptdev/krm-functions-catalog) | ||
| provides a shared Dockerfile at `build/docker/go/Dockerfile` that all the catalog | ||
| functions use. It accepts `BUILDER_IMAGE` and `BASE_IMAGE` as build args. | ||
|
|
||
| For standalone functions or local development, use a multi-stage build with a | ||
| minimal base image. The function binary should be statically linked (no CGO), so | ||
| it can run on `scratch` or `distroless`: | ||
|
|
||
| ```dockerfile | ||
| FROM golang:1.26-alpine AS builder | ||
| ENV CGO_ENABLED=0 | ||
| WORKDIR /go/src/ | ||
| COPY go.mod go.sum ./ | ||
| RUN go mod download | ||
| COPY . . | ||
| RUN go build -o /usr/local/bin/function ./ | ||
|
|
||
| FROM scratch | ||
| COPY --from=builder /usr/local/bin/function /usr/local/bin/function | ||
| ENTRYPOINT ["function"] | ||
| ``` | ||
|
|
||
| Key points: | ||
| - `CGO_ENABLED=0` produces a static binary that runs on `scratch`. | ||
| - The `scratch` base image has zero overhead — no shell, no OS packages. | ||
| - If you need TLS certificates (e.g., for network calls), use `gcr.io/distroless/static` instead of `scratch`. | ||
| - Copy only the binary to the final image to minimize size. | ||
|
|
||
| ### Alternative with distroless | ||
|
|
||
| ```dockerfile | ||
| FROM golang:1.26-alpine AS builder | ||
| ENV CGO_ENABLED=0 | ||
| WORKDIR /go/src/ | ||
| COPY go.mod go.sum ./ | ||
| RUN go mod download | ||
| COPY . . | ||
| RUN go build -o /usr/local/bin/function ./ | ||
|
|
||
| FROM gcr.io/distroless/static:nonroot | ||
| COPY --from=builder /usr/local/bin/function /usr/local/bin/function | ||
| ENTRYPOINT ["function"] | ||
| ``` | ||
|
|
||
| ## Building | ||
|
|
||
| ```bash | ||
| docker build -t ghcr.io/kptdev/krm-functions-catalog/my-function:v0.1 . | ||
| ``` | ||
|
|
||
| ### Image Naming Convention | ||
|
|
||
| Follow this pattern for function images: | ||
|
|
||
| ``` | ||
| ghcr.io/kptdev/krm-functions-catalog/{function-name}:{version} | ||
| ``` | ||
|
|
||
| Examples: | ||
| - `ghcr.io/kptdev/krm-functions-catalog/set-labels:v0.1` | ||
| - `ghcr.io/kptdev/krm-functions-catalog/enforce-namespace:v1.0` | ||
| - `ghcr.io/kptdev/krm-functions-catalog/generate-configmap:v0.3` | ||
|
|
||
| Use semantic versioning for tags. Avoid `latest` in production pipelines. | ||
|
|
||
| ## Running | ||
|
|
||
| KRM functions read from STDIN and write to STDOUT: | ||
|
|
||
| ```bash | ||
| docker run --rm -i ghcr.io/kptdev/krm-functions-catalog/my-function:v0.1 < input.yaml > output.yaml | ||
| ``` | ||
|
|
||
| ### With file mode | ||
|
|
||
| ```bash | ||
| docker run --rm -v $(pwd):/data ghcr.io/kptdev/krm-functions-catalog/my-function:v0.1 /data/deployment.yaml | ||
| ``` | ||
|
|
||
| Note: file mode assembles the given files into a ResourceList with an **empty | ||
| functionConfig**. Functions that require configuration should be run via STDIN | ||
| (or a `kpt` pipeline) so the functionConfig is provided. | ||
|
|
||
| ### Help and doc flags | ||
|
|
||
| ```bash | ||
| docker run --rm ghcr.io/kptdev/krm-functions-catalog/my-function:v0.1 --help | ||
| docker run --rm ghcr.io/kptdev/krm-functions-catalog/my-function:v0.1 --doc | ||
| ``` | ||
|
|
||
| ## Using with kpt | ||
|
|
||
| In a `Kptfile` pipeline, `kpt fn render` will pull the image from the registry | ||
| and run it against your package resources: | ||
|
|
||
| ```yaml | ||
| apiVersion: kpt.dev/v1 | ||
| kind: Kptfile | ||
| metadata: | ||
| name: my-package | ||
| pipeline: | ||
| mutators: | ||
| - image: ghcr.io/kptdev/krm-functions-catalog/set-labels:v0.1 | ||
| configMap: | ||
| app: my-app | ||
| validators: | ||
| - image: ghcr.io/kptdev/krm-functions-catalog/enforce-namespace:v1.0 | ||
| configMap: | ||
| namespace: production | ||
| ``` | ||
|
|
||
| Note: the image must be published and accessible from the machine running | ||
| `kpt fn render`. For local development, build the image locally first. It | ||
| will be used from the local Docker cache without pulling. | ||
|
|
||
| ## Tips | ||
|
|
||
| - Keep images small — a typical Go KRM function image is 5–15 MB with `scratch`. | ||
| - Pin dependency versions in `go.mod` for reproducible builds. | ||
| - Use `.dockerignore` to exclude test data, docs, and other non-build files. | ||
| - Test the container locally before publishing: | ||
| ```bash | ||
| echo '{"apiVersion":"config.kubernetes.io/v1","kind":"ResourceList","items":[]}' | \ | ||
| docker run --rm -i ghcr.io/kptdev/krm-functions-catalog/my-function:v0.1 | ||
| ``` | ||
|
|
||
| ## Publishing | ||
|
|
||
| Publishing function images to a registry is handled by the | ||
| [krm-functions-catalog](https://github.com/kptdev/krm-functions-catalog) | ||
| CI pipeline. See the catalog's | ||
| [CONTRIBUTING.md](https://github.com/kptdev/krm-functions-catalog/blob/main/CONTRIBUTING.md) | ||
| for the release workflow. |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.