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 ¶
- Variables
- func BuildRESTConfig(kubeconfig, context string) (*rest.Config, error)
- func PollForReadiness(ctx context.Context, deadline time.Duration, ...) error
- func WaitForDaemonSetReady(ctx context.Context, clientset kubernetes.Interface, namespace, name string, ...) error
- func WaitForDeploymentReady(ctx context.Context, clientset kubernetes.Interface, namespace, name string, ...) error
- func WaitForMultipleResources(ctx context.Context, clientset kubernetes.Interface, checks []ReadinessCheck, ...) error
- type ReadinessCheck
Constants ¶
This section is empty.
Variables ¶
var ErrKubeconfigPathEmpty = errors.New("kubeconfig path is empty")
ErrKubeconfigPathEmpty is returned when kubeconfig path is empty.
var ErrTimeoutExceeded = errors.New("timeout exceeded")
ErrTimeoutExceeded is returned when a timeout is exceeded.
Functions ¶
func BuildRESTConfig ¶
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 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 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.