Documentation
¶
Overview ¶
Package k8s provides Kubernetes client configuration and general-purpose utilities.
This package offers reusable utilities for working with Kubernetes clusters, including REST client configuration, kubeconfig management, DNS label sanitization, and label extraction.
For resource readiness polling, see the [readiness] sub-package.
Key features:
- REST config building from kubeconfig files (BuildRESTConfig, GetRESTConfig)
- Clientset creation (NewClientset)
- Kubeconfig cleanup (CleanupKubeconfig)
- DNS label sanitization (SanitizeToDNSLabel)
- Label value extraction (UniqueLabelValues)
Index ¶
- Variables
- func BuildRESTConfig(kubeconfig, context string) (*rest.Config, error)
- func CleanupKubeconfig(kubeconfigPath string, clusterName string, contextName string, userName string, ...) error
- func DefaultKubeconfigPath() string
- func EnsurePrivilegedNamespace(ctx context.Context, clientset kubernetes.Interface, name string) error
- func GetRESTConfig() (*rest.Config, error)
- func NewClientset(kubeconfig, context string) (*kubernetes.Clientset, error)
- func NewDynamicClient(kubeconfig, context string) (dynamic.Interface, error)
- func SanitizeToDNSLabel(value string) string
- func UniqueLabelValues[T any](items []T, key string, getLabels func(T) map[string]string) []string
Constants ¶
This section is empty.
Variables ¶
var ErrKubeconfigPathEmpty = errors.New("kubeconfig path is empty")
ErrKubeconfigPathEmpty is returned when kubeconfig path is empty.
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 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 DefaultKubeconfigPath ¶ added in v5.30.0
func DefaultKubeconfigPath() string
DefaultKubeconfigPath returns the default kubeconfig path for the current user. The path is constructed as ~/.kube/config using the user's home directory.
func EnsurePrivilegedNamespace ¶ added in v5.31.3
func EnsurePrivilegedNamespace( ctx context.Context, clientset kubernetes.Interface, name string, ) error
EnsurePrivilegedNamespace creates the given namespace with PodSecurity Standard "privileged" labels, or updates an existing namespace to add them.
func GetRESTConfig ¶ added in v5.30.0
GetRESTConfig loads the kubeconfig using default loading rules and returns a REST config. This is a convenience function that uses the standard client-go loading rules (KUBECONFIG env var, default kubeconfig path) without requiring explicit paths.
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 NewDynamicClient ¶ added in v5.31.3
NewDynamicClient creates a Kubernetes dynamic client from kubeconfig path and context. This is a convenience function that combines BuildRESTConfig and dynamic client creation. Use this when working with unstructured resources or custom resource types.
func SanitizeToDNSLabel ¶ added in v5.30.0
SanitizeToDNSLabel converts an arbitrary string to a lowercase alphanumeric string with hyphens as the only separator. Consecutive hyphens are collapsed and leading/trailing hyphens are trimmed.
This is the shared sanitization kernel used by OCI repository segment normalisation and DNS-1123 repo name construction.
func UniqueLabelValues ¶ added in v5.30.0
UniqueLabelValues extracts unique non-empty values for a given label key from a slice of labeled items. The getLabels function extracts the label map from each item.
Types ¶
This section is empty.