KRM Functions SDK
May 29, 2026 · View on GitHub
An opinionated Go SDK for implementing KRM functions.
Quick Start
A KRM function is a program that reads Kubernetes resources from STDIN, transforms or validates them, and writes the result to STDOUT. The SDK handles the I/O — you write the logic.
package main
import (
"context"
_ "embed"
"os"
"github.com/kptdev/krm-functions-sdk/go/fn"
)
//go:embed README.md
var readme []byte
//go:embed metadata.yaml
var metadata []byte
type SetLabels struct {
Labels map[string]string `json:"labels,omitempty"`
}
func (r *SetLabels) Run(ctx *fn.Context, functionConfig *fn.KubeObject, items fn.KubeObjects, results *fn.Results) bool {
for _, obj := range items {
for k, v := range r.Labels {
obj.SetLabel(k, v)
}
}
return true
}
func main() {
runner := fn.WithContext(context.Background(), &SetLabels{})
if err := fn.AsMain(runner, fn.WithDocs(readme, metadata)); err != nil {
os.Exit(1)
}
}
A starter template is available at go/get-started/. For the full walkthrough, see the Tutorial.
How It Works
fn.AsMain is the single entrypoint. It handles:
- STDIN/STDOUT (default) — reads a ResourceList, processes it, writes the result
- File mode — pass file paths as positional args for local debugging
--help— prints human-readable docs from embedded README markers--doc— outputs machine-readable JSON (consumed bykpt fn docand catalog pipelines)
Register embedded documentation with fn.WithDocs:
fn.AsMain(runner, fn.WithDocs(readme, metadata))
The SDK provides two interfaces for implementing functions:
| Interface | Use for | Can add/remove items? | Auto-parses config? |
|---|---|---|---|
fn.Runner | Transformers, validators | No | Yes |
fn.ResourceListProcessor | Generators, complex functions | Yes | No |
See Interfaces for details and code examples.
Documentation
- API Reference — Go API docs
- Tutorial — end-to-end function development
- Interfaces — Runner vs ResourceListProcessor
- Testing — golden test patterns
- Containerizing — building and running function images
Contributing
See CONTRIBUTING.md for guidelines on DCO sign-off, copyright headers, and code review process.
Issues
Please open issues at kptdev/kpt.
License
Code is under the Apache License 2.0, documentation is CC BY 4.0.
Governance
The governance of the kpt project is described in the governance repo.
Code of Conduct
The kpt project follows the CNCF Code of Conduct. More information is here.
CNCF
The kpt project is a CNCF Sandbox project.