k8s

package
v5.36.4 Latest Latest
Warning

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

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

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

Constants

This section is empty.

Variables

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

ErrKubeconfigPathEmpty is returned when kubeconfig path is empty.

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 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

func GetRESTConfig() (*rest.Config, error)

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

func NewDynamicClient(kubeconfig, context string) (dynamic.Interface, error)

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

func SanitizeToDNSLabel(value string) string

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

func UniqueLabelValues[T any](items []T, key string, getLabels func(T) map[string]string) []string

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.

Directories

Path Synopsis
Package readiness provides Kubernetes resource readiness polling utilities.
Package readiness provides Kubernetes resource readiness polling utilities.

Jump to

Keyboard shortcuts

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