Skip to content

kptdev/krm-functions-sdk

KRM Functions SDK

FOSSA Status OpenSSF Best Practices

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 by kpt fn doc and 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

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.

FOSSA Status

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.

About

Catalog of Kubernetes Resource Model (KRM) functions that can be used to validate or mutate kpt packages

Resources

License

Apache-2.0, CC-BY-4.0 licenses found

Licenses found

Apache-2.0
LICENSE
CC-BY-4.0
LICENSE-documentation

Code of conduct

Contributing

Stars

Watchers

Forks

Packages

 
 
 

Contributors

Languages