logo

Cheatsheet - kpt

Kpt is an open-source command-line tool by Google for managing Kubernetes configurations. It focuses on packaging, customizing, validating, and applying Kubernetes resource configurations, emphasizing a "Configuration as Data" approach. Kpt integrates with Git for version control and uses containerized functions for transformations and validations.

Kpt commands generally follow the structure: kpt <group> <command> <positional args> [PKG_PATH] [flags].

Core Concepts

  • Package: A collection of Kubernetes configuration files (YAML) along with a Kptfile.
  • Kptfile: A special file in the root of a Kpt package that contains metadata and configuration for the package, including upstream information, functions, and inventory.
  • Functions: Containerized programs that take Kubernetes resources as YAML input, transform or validate them, and output modified YAML.
  • Setters: Allow programmatic setting of field values within configurations.

1. Package Management (kpt pkg)

Commands for managing packages of Kubernetes resources, including fetching, updating, and describing them.

  • kpt pkg get <GIT_REPO_URL> <LOCAL_DIR>
    • Fetches a remote package from a Git repository and writes it to a new local directory.
    • Example: kpt pkg get https://github.com/kptdev/kpt/package-examples/[email protected] my-nginx
  • kpt pkg update <LOCAL_DIR>
    • Pulls in upstream changes from the remote repository and merges them into a local package.
    • Example: kpt pkg update my-nginx
  • kpt pkg init [PKG_PATH]
    • Initializes an existing empty directory as a Kpt package by adding a Kptfile.
    • Example: kpt pkg init my-new-package
  • kpt pkg tree [PKG_PATH]
    • Displays resources, files, and packages in a tree structure.
    • Example: kpt pkg tree my-nginx
  • kpt pkg diff <LOCAL_DIR>
    • Displays differences between upstream and local packages.
    • Example: kpt pkg diff my-nginx

2. Functions (kpt fn)

Commands for generating, transforming, and validating packages using containerized functions.

  • kpt fn eval <DIR> --image <FUNCTION_IMAGE> [flags]
    • Executes a single function on resources in a directory, applying transformations or validations.
    • Example: kpt fn eval my-nginx --image gcr.io/kpt-fn/set-labels:v0.1 -- 'label_name=app,label_value=my-app' (Note: Specific function arguments vary)
  • kpt fn render <DIR>
    • Executes the pipeline of functions defined in the Kptfile on resources in the package and writes the output to the local filesystem.
    • Example: kpt fn render my-nginx
  • kpt fn run <DIR>
    • An older command, typically replaced by kpt fn eval or kpt fn render for more explicit control over single functions vs. pipelines.
  • kpt fn search [KEYWORD]
    • Searches for available Kpt functions.

3. Live State Management (kpt live)

Commands for deploying local configuration packages to a Kubernetes cluster.

  • kpt live init <LOCAL_DIR>
    • Initializes the package for live deployment by adding an inventory object (often a ConfigMap) to keep track of applied resources, enabling features like pruning.
    • Example: kpt live init my-nginx
  • kpt live apply <LOCAL_DIR>
    • Applies the configuration from the local package to the Kubernetes cluster. This command also provides status reporting and pruning of deleted resources.
    • Example: kpt live apply my-nginx
  • kpt live diff <LOCAL_DIR>
    • Compares the local configuration with the live state on the cluster.
    • Example: kpt live diff my-nginx
  • kpt live destroy <LOCAL_DIR>
    • Deletes all resources managed by the package from the Kubernetes cluster.
    • Example: kpt live destroy my-nginx
  • kpt live status <LOCAL_DIR>
    • Checks the reconciliation status of resources on the cluster.
    • Example: kpt live status my-nginx

4. Alpha Commands (kpt alpha)

Commands currently in alpha and subject to change.

  • kpt alpha repo register <REPOSITORY_URL>
    • Registers a Git or OCI repository with a Porch server, which is a Kpt package orchestrator.
    • Example: kpt alpha repo register https://github.com/my-org/blueprints.git

General Kpt Commands

  • kpt version
    • Displays the Kpt CLI version.
  • kpt help [COMMAND]
    • Provides help for a specific Kpt command or group.