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:
- Reads the "ResourceList" object from STDIN.
- Gets the function configs from the "ResourceList.FunctionConfig".
- Mutate or validate the Kubernetes YAML resources from the "ResourceList.Items" field with the function configs.
- Writes the modified "ResourceList" to STDOUT.
- 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 ¶
- func AsMain(input any, opts ...Option) error
- func EmptyFunctionConfig(o *kubeobject.KubeObject) bool
- func Execute(p kubeobject.ResourceListProcessor, r io.Reader, w io.Writer) error
- func Log(in ...any)
- func Logf(format string, in ...any)
- func Run(p kubeobject.ResourceListProcessor, input []byte) ([]byte, error)
- func WithContext(ctx context.Context, runner Runner) kubeobject.ResourceListProcessor
- type Context
- type Option
- type Runner
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func AsMain ¶
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 Execute ¶
func Execute(p kubeobject.ResourceListProcessor, r io.Reader, w io.Writer) error
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 Option ¶ added in v1.0.4
type Option func(*mainConfig)
Option configures fn.AsMain behavior.
func WithDocs ¶ added in v1.0.4
WithDocs registers embedded README and metadata content for --help and --doc.
func WithFlags ¶ added in v1.2.1
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
WithInput replaces reading the ResourceList from STDIN. It is not used in file mode.
func WithOutput ¶ added in v1.2.1
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
}