k8s

package
v5.20.4 Latest Latest
Warning

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

Go to latest
Published: Jan 18, 2026 License: Apache-2.0 Imports: 12 Imported by: 0

Documentation

Overview

Package k8s provides Kubernetes utilities for resource management and readiness checking.

This package offers reusable utilities for working with Kubernetes resources, including REST client configuration, resource readiness polling, and multi-resource coordination. It is designed to be used across different parts of the application that interact with Kubernetes clusters.

Key features:

  • REST config building from kubeconfig files (BuildRESTConfig)
  • Deployment readiness polling (WaitForDeploymentReady)
  • DaemonSet readiness polling (WaitForDaemonSetReady)
  • Multi-resource coordination (WaitForMultipleResources)
  • Flexible polling mechanism (PollForReadiness)

Index

Constants

This section is empty.

Variables

View Source
var ErrKubeconfigPathEmpty = errors.New("kubeconfig path is empty")

ErrKubeconfigPathEmpty is returned when kubeconfig path is empty.

View Source
var ErrTimeoutExceeded = errors.New("timeout exceeded")

ErrTimeoutExceeded is returned when a timeout is exceeded.

Functions

func BuildRESTConfig

func BuildRESTConfig(kubeconfig, context string) (*rest.Config, error)

BuildRESTConfig builds a Kubernetes REST config from kubeconfig path and optional context.

The kubeconfig parameter must be a non-empty path to a valid kubeconfig file. The context parameter is optional and specifies which context to use from the kubeconfig. If context is empty, the default context from the kubeconfig is used.

Returns ErrKubeconfigPathEmpty if kubeconfig path is empty. Returns an error if the kubeconfig cannot be loaded or parsed.

func CheckAPIServerConnectivity added in v5.9.0

func CheckAPIServerConnectivity(
	clientset kubernetes.Interface,
) error

CheckAPIServerConnectivity performs a single API server connectivity check.

This function attempts to get the server version from the API server. It returns nil if the API server is reachable, or an error describing the connectivity issue.

Parameters:

  • clientset: Kubernetes client interface

Returns nil if connected, or an error with connectivity details.

func CleanupKubeconfig added in v5.9.0

func CleanupKubeconfig(
	kubeconfigPath string,
	clusterName string,
	contextName string,
	userName string,
	logWriter io.Writer,
) error

CleanupKubeconfig removes the cluster, context, and user entries for a cluster from the kubeconfig file. This only removes entries matching the provided names, leaving other cluster configurations intact.

Parameters:

  • kubeconfigPath: absolute path to the kubeconfig file
  • clusterName: the cluster entry name to remove
  • contextName: the context entry name to remove
  • userName: the user/authinfo entry name to remove
  • logWriter: writer for log output (can be io.Discard)

func NewClientset added in v5.13.0

func NewClientset(kubeconfig, context string) (*kubernetes.Clientset, error)

NewClientset creates a Kubernetes clientset from kubeconfig path and context. This is a convenience function that combines BuildRESTConfig and client creation.

func PollForReadiness

func PollForReadiness(
	ctx context.Context,
	deadline time.Duration,
	poll func(context.Context) (bool, error),
) error

PollForReadiness polls a check function until ready or timeout.

This function repeatedly calls the provided poll function at regular intervals until either:

  • The poll function returns (true, nil) indicating readiness
  • The deadline is exceeded
  • The poll function returns an error

The poll function should return (false, nil) to continue polling, (true, nil) when the resource is ready, or (false, error) on errors.

Returns an error if polling times out or if the poll function returns an error.

func WaitForAPIServerReady added in v5.9.0

func WaitForAPIServerReady(
	ctx context.Context,
	clientset kubernetes.Interface,
	deadline time.Duration,
) error

WaitForAPIServerReady waits for the Kubernetes API server to be ready and stable.

This function polls the API server by performing a ServerVersion request until it responds consistently without errors. This is useful after cluster bootstrap when the API server may be unstable due to initial startup.

The function uses the configured polling interval and will timeout after the specified deadline.

Parameters:

  • ctx: Context for cancellation
  • clientset: Kubernetes client interface
  • deadline: Maximum time to wait for API server readiness

Returns an error if the API server is not ready within the deadline.

func WaitForAPIServerStable added in v5.9.0

func WaitForAPIServerStable(
	ctx context.Context,
	clientset kubernetes.Interface,
	deadline time.Duration,
	requiredSuccesses int,
) error

WaitForAPIServerStable waits for the Kubernetes API server to respond consistently.

Unlike WaitForAPIServerReady which returns on the first successful response, this function requires multiple consecutive successful responses to ensure the API server is truly stable. This is particularly useful for Talos where the API server may respond once but then fail with connection resets.

Parameters:

  • ctx: Context for cancellation
  • clientset: Kubernetes client interface
  • deadline: Maximum time to wait for API server stability
  • requiredSuccesses: Number of consecutive successful responses required

Returns an error if the API server is not stable within the deadline.

func WaitForDaemonSetReady

func WaitForDaemonSetReady(
	ctx context.Context,
	clientset kubernetes.Interface,
	namespace, name string,
	deadline time.Duration,
) error

WaitForDaemonSetReady waits for a DaemonSet to be ready.

This function polls the specified DaemonSet until it is ready or the deadline is reached. A DaemonSet is considered ready when:

  • At least one pod is scheduled
  • No pods are unavailable
  • All pods have been updated to the current specification

The function tolerates NotFound errors and continues polling. Other API errors are returned immediately.

Returns an error if the DaemonSet is not ready within the deadline or if an API error occurs.

func WaitForDeploymentReady

func WaitForDeploymentReady(
	ctx context.Context,
	clientset kubernetes.Interface,
	namespace, name string,
	deadline time.Duration,
) error

WaitForDeploymentReady waits for a Deployment to be ready.

This function polls the specified Deployment until it is ready or the deadline is reached. A Deployment is considered ready when:

  • It has at least one replica
  • All replicas have been updated
  • All replicas are available

The function tolerates NotFound errors and continues polling. Other API errors are returned immediately.

Returns an error if the Deployment is not ready within the deadline or if an API error occurs.

func WaitForMultipleResources

func WaitForMultipleResources(
	ctx context.Context,
	clientset kubernetes.Interface,
	checks []ReadinessCheck,
	timeout time.Duration,
) error

WaitForMultipleResources waits for multiple Kubernetes resources to be ready.

This function checks each resource in sequence, allocating remaining timeout proportionally. If any resource fails to become ready within the allocated time, the function returns an error.

The timeout parameter is shared across all resources, so each subsequent resource gets less time to become ready. Resources are checked in the order they appear in the checks slice.

Returns ErrTimeoutExceeded if the timeout is reached before a resource is checked. Returns an error if any resource fails to become ready.

Types

type ReadinessCheck

type ReadinessCheck struct {
	// Type specifies the kind of Kubernetes resource to check for readiness.
	// Valid values are "deployment" or "daemonset".
	Type string
	// Namespace is the Kubernetes namespace where the resource resides.
	Namespace string
	// Name is the name of the resource to check for readiness.
	Name string
}

ReadinessCheck defines a check to perform for a Kubernetes resource.

Jump to

Keyboard shortcuts

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