fn

package module
v1.2.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 27, 2026 License: Apache-2.0 Imports: 16 Imported by: 30 modules, 72 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package fn provides the SDK to write KRM functions.

Before you start

This fn SDK requires some basic KRM function Specification knowledge. To make the best usage of your time, we recommend you to be familiar with "ResourceList" before moving forward.

The KRM Function Specification, or "ResourceList", defines the standards of the inter-process communication between
the orchestrator (i.e. kpt CLI) and functions.

See KRM Function Specification reference in https://github.com/kubernetes-sigs/kustomize/blob/master/cmd/config/docs/api-conventions/functions-spec.md

KRM Function

A KRM function can mutate and/or validate Kubernetes resources in a ResourceList.

KubeObject, SubObject, and ResourceList live in github.com/kptdev/kpt/api/kubeobject. The ResourceList type maps to the ResourceList in the function spec.

Read more about how to use KRM functions in https://kpt.dev/book/04-using-functions/

Read more about how to develop a KRM function in https://kpt.dev/book/05-developing-functions/

A general workflow is:

  1. Reads the "ResourceList" object from STDIN.
  2. Gets the function configs from the "ResourceList.FunctionConfig".
  3. Mutate or validate the Kubernetes YAML resources from the "ResourceList.Items" field with the function configs.
  4. Writes the modified "ResourceList" to STDOUT.
  5. Write function message to "ResourceList.Results" with severity "Info", "Warning" or "Error"

KubeObject

KubeObject is the basic unit for operating on KRM resources. It, along with SubObject and the helpers for reading and writing resources, is provided by github.com/kptdev/kpt/api/kubeobject.

In "AsMain", both "Items" and "FunctionConfig" are converted to KubeObject values.

AsMain

"AsMain" is the main entrypoint. In most cases, you only need to provide the mutator or validation logic and have AsMain handles the ResourceList parsing, KRM resource field type detection, read from STDIN and write to STDOUT.

"AsMain" accepts a kubeobject.ResourceListProcessor or a Runner.

See github.com/kptdev/krm-functions-sdk/go/fn/examples for detailed usage.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AsMain

func AsMain(input any, opts ...Option) error

AsMain evaluates a KRM function. By default it reads a ResourceList from STDIN, processes it, and writes the result to STDOUT.

`input` can be - a `kubeobject.ResourceListProcessor` which implements `Process` method - a function `Runner` which implements `Run` method

Invocation modes (checked in this order):

  • --help: prints human-readable documentation to STDOUT and returns nil.
  • --doc: prints machine-readable JSON documentation to STDOUT and returns nil.
  • positional file args: reads KRM resources from files instead of STDIN.
  • no args: reads ResourceList from STDIN (default behavior).

Options configure additional behavior such as documentation support via WithDocs. Existing callers with no options continue to work unchanged.

func EmptyFunctionConfig

func EmptyFunctionConfig(o *kubeobject.KubeObject) bool

EmptyFunctionConfig is a workaround solution to handle the case where kpt passes in a functionConfig placeholder (Configmap with empty `data`) if user does not provide the actual FunctionConfig. Ideally, kpt should pass in an empty FunctionConfig object.

func Log

func Log(in ...any)

func Logf

func Logf(format string, in ...any)

func Run

func Run(p kubeobject.ResourceListProcessor, input []byte) ([]byte, error)

Run evaluates the function. input must be a resourceList in yaml format. An updated resourceList will be returned.

func WithContext

func WithContext(ctx context.Context, runner Runner) kubeobject.ResourceListProcessor

Types

type Context

type Context struct {
	context.Context
}

TODO: Have Context implement `context.Context`.

type Option added in v1.0.4

type Option func(*mainConfig)

Option configures fn.AsMain behavior.

func WithDocs added in v1.0.4

func WithDocs(readme []byte, meta []byte) Option

WithDocs registers embedded README and metadata content for --help and --doc.

func WithFlags added in v1.2.1

func WithFlags(register func(fs *flag.FlagSet)) Option

WithFlags registers function specific command line flags. register is called with a flag.FlagSet before the arguments are parsed. Flags and their values are not treated as file paths; "--" ends flag parsing. --help and --doc are reserved and handled first, and -h and -help act like --help. Flag values are only valid once AsMain has parsed them.

func WithInput added in v1.2.1

func WithInput(read func() ([]byte, error)) Option

WithInput replaces reading the ResourceList from STDIN. It is not used in file mode.

func WithOutput added in v1.2.1

func WithOutput(write func(out []byte) error) Option

WithOutput replaces writing the ResourceList to STDOUT. It is also called when the function fails, and is not called when there is no output.

type Runner

type Runner interface {
	// Run provides the entrypoint to allow you make changes to input `resourcelist.Items`
	// Args:
	//    items: The KRM resources in the form of a slice of KubeObject.
	//       Note: You can only modify the existing items but not add or delete items.
	//       We intentionally design the method this way to make the Runner be used as a Transformer or Validator, but not a Generator.
	//    results: You can use `ErrorE` `Errorf` `Infof` `Warningf` `WarningE` to add user message to `Results`.
	// Returns:
	//    return a boolean to tell whether the execution should be considered as PASS or FAIL. CLI like kpt will
	// display the corresponding message.
	Run(context *Context, functionConfig *kubeobject.KubeObject, items kubeobject.KubeObjects, results *kubeobject.Results) bool
}

Directories

Path Synopsis
examples module
internal

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL