Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

nokia/kpt-functions-sdk

Open more actions menu
 
 

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.

Releases

Packages

Used by

Contributors

Languages

Morty Proxy This is a proxified and sanitized view of the page, visit original site.