kindkit

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Apr 3, 2026 License: Apache-2.0 Imports: 23 Imported by: 0

README

kindkit

Go Reference CI

Go library for managing Kind clusters in test workflows.

Why kindkit?

Go test suites that run against Kind clusters often shell out to the kind CLI for cluster lifecycle, making failure handling, cleanup, and debugging harder to keep consistent.

Even when using Kind's Go packages directly, projects tend to reimplement the same helpers: readiness checks, kubeconfig handling, reuse logic, and teardown.

kindkit provides a small, focused API for these lifecycle operations, so you can manage clusters directly from Go test code without rebuilding the same plumbing each time.

Prerequisites

The kind CLI is not required. kindkit manages clusters through Kind's Go packages.

Install

go get github.com/IrvingMg/kindkit

Quick example

Create a Kind cluster, get a REST config, and load images — all from Go:

ctx := context.Background()

cluster, err := kindkit.Create(ctx, "my-test-cluster",
    kindkit.WithWaitForReady(3*time.Minute),
)
if err != nil {
    // Partial failure: creation failed but a cluster was returned.
    // Export logs for debugging, then clean up.
    if cluster != nil {
        _ = cluster.ExportLogs(ctx, "./test-logs")
        _ = cluster.Delete(ctx)
    }
    log.Fatal(err)
}
defer cluster.Delete(ctx)

restCfg, err := cluster.RESTConfig()
if err != nil {
    log.Fatal(err)
}

if err := cluster.LoadImages(ctx, "my-app:latest"); err != nil {
    log.Fatal(err)
}

API

The public API covers cluster creation, configuration access, and common operations:

Function Description
Create(ctx, name, opts...) Create a cluster. Returns both *Cluster and error on partial failure.
CreateOrReuse(ctx, name, opts...) Reuse an existing cluster if reachable, otherwise create a new one.
Cluster.Delete(ctx) Delete the cluster. Idempotent.
Cluster.RESTConfig() Get a *rest.Config for the cluster.
Cluster.KubeconfigPath() Write kubeconfig to a temp file and return its path.
Cluster.LoadImages(ctx, images...) Load local Docker images into all cluster nodes.
Cluster.ExportLogs(ctx, dir) Export cluster logs to a directory for debugging.
Cluster.ApplyManifests(ctx, yaml) Apply multi-document Kubernetes YAML via server-side apply.
WithNodeImage(image) Option: set the Kind node image.
WithWaitForReady(d) Option: set the readiness timeout.
WithRawConfig(raw) Option: pass raw Kind cluster config YAML for full control over cluster topology. Mutually exclusive with WithConfigFile.
WithConfigFile(path) Option: load Kind cluster config from a file path. Mutually exclusive with WithRawConfig.

Examples

Complete runnable examples are available in the examples/ directory:

Run all examples with:

make test-examples # Requires Docker

Compatibility

kindkit is tested against the Kind versions that target currently-supported Kubernetes releases. Each Kind release supports multiple Kubernetes versions — see the Kind releases page for details.

The exact Kind versions tested are defined in the CI workflow.

Running tests

The project includes unit and end-to-end tests. E2e tests create real Kind clusters and require Docker.

make test          # unit + e2e tests
make test-unit     # unit tests only
make test-e2e      # e2e tests only (requires Docker)

Contributing

Feedback and contributions are welcome — open an issue, start a discussion, or submit a pull request.

License

This project is licensed under the Apache License 2.0.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Cluster

type Cluster struct {
	// contains filtered or unexported fields
}

Cluster represents a Kind cluster managed by kindkit.

A Cluster is obtained by calling Create. Use Delete to tear it down.

func Create

func Create(ctx context.Context, name string, opts ...Option) (*Cluster, error)

Create creates a new Kind cluster. On partial failure, both a non-nil *Cluster and an error may be returned so the caller can still inspect or clean up. ctx is reserved for future use; Kind's API does not support cancellation.

func CreateOrReuse

func CreateOrReuse(ctx context.Context, name string, opts ...Option) (*Cluster, error)

CreateOrReuse returns an existing cluster if its API server is reachable, otherwise creates a new one. Options only apply on create. Like Create, both a non-nil *Cluster and an error may be returned. ctx is reserved for future use; Kind's API does not support cancellation.

func (*Cluster) ApplyManifests

func (c *Cluster) ApplyManifests(ctx context.Context, manifests []byte) error

ApplyManifests applies multi-document Kubernetes YAML to the cluster using server-side apply.

func (*Cluster) Delete

func (c *Cluster) Delete(ctx context.Context) error

Delete deletes the cluster. It is safe to call on an already-deleted cluster. ctx is reserved for future use; Kind's API does not support cancellation.

func (*Cluster) ExportLogs

func (c *Cluster) ExportLogs(ctx context.Context, dir string) error

ExportLogs exports cluster logs to the given directory for debugging. ctx is reserved for future use; Kind's API does not support cancellation.

func (*Cluster) KubeconfigPath

func (c *Cluster) KubeconfigPath() (string, error)

KubeconfigPath writes the kubeconfig to a temporary file and returns its path. The caller is responsible for removing the file.

func (*Cluster) LoadImages

func (c *Cluster) LoadImages(ctx context.Context, images ...string) error

LoadImages loads images from the local Docker daemon into all cluster nodes. The images must already exist locally.

func (*Cluster) Name

func (c *Cluster) Name() string

Name returns the cluster name.

func (*Cluster) RESTConfig

func (c *Cluster) RESTConfig() (*rest.Config, error)

RESTConfig returns a *rest.Config for the cluster.

type Option

type Option func(*options)

Option configures cluster creation.

func WithConfigFile

func WithConfigFile(path string) Option

WithConfigFile loads a Kind cluster configuration from a file path. Mutually exclusive with WithRawConfig.

func WithNodeImage

func WithNodeImage(image string) Option

WithNodeImage sets the node Docker image (e.g. "kindest/node:v1.31.0").

func WithRawConfig

func WithRawConfig(raw []byte) Option

WithRawConfig passes a raw Kind cluster configuration YAML (kind: Cluster, apiVersion: kind.x-k8s.io/v1alpha4) to the provider. Mutually exclusive with WithConfigFile. WithNodeImage and WithWaitForReady layer on top.

func WithWaitForReady

func WithWaitForReady(d time.Duration) Option

WithWaitForReady sets the timeout for waiting for the cluster to be ready.

Directories

Path Synopsis
test

Jump to

Keyboard shortcuts

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