kube

package
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: Dec 12, 2025 License: Apache-2.0 Imports: 28 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func IsCRDReady

func IsCRDReady(obj *unstructured.Unstructured, err error) (bool, error)

IsCRDReady checks if a CustomResourceDefinition is established

func IsDeleted

func IsDeleted(obj *unstructured.Unstructured, err error) (bool, error)

IsDeleted checks if a resource has been deleted (returns true if obj is nil or error is NotFound)

func IsDeploymentReady

func IsDeploymentReady(obj *unstructured.Unstructured, err error) (bool, error)

IsDeploymentReady checks if a Deployment has all desired replicas ready

func IsJobComplete

func IsJobComplete(obj *unstructured.Unstructured, err error) (bool, error)

IsJobComplete checks if a Job has completed successfully

func IsNodeReady

func IsNodeReady(obj *unstructured.Unstructured, err error) (bool, error)

IsNodeReady checks if a Node has Ready condition == True

func IsPVCBound

func IsPVCBound(obj *unstructured.Unstructured, err error) (bool, error)

IsPVCBound checks if a PersistentVolumeClaim is in Bound phase

func IsPhase

func IsPhase(desired Phase) func(obj *unstructured.Unstructured, err error) (bool, error)

IsPhase returns a check function that verifies if the resource is in the desired phase

func IsPodReady

func IsPodReady(obj *unstructured.Unstructured, err error) (bool, error)

IsPodReady checks if a Pod is in Ready condition

func IsPresent

func IsPresent(obj *unstructured.Unstructured, err error) (bool, error)

IsPresent checks if the item exists

func RegisterKind

func RegisterKind(kind ResourceKind, gvr schema.GroupVersionResource)

func ToGroupVersionResource

func ToGroupVersionResource(kind ResourceKind) (schema.GroupVersionResource, error)

func WithKubeClient

func WithKubeClient(ctx context.Context, kc *Client) context.Context

Types

type CheckFunc

type CheckFunc func(obj *unstructured.Unstructured, err error) (bool, error)

CheckFunc defines a function type for checking resource conditions Notes: when err != nil, obj may be nil. CheckFunc should handle API errors like IsNotFound, IsForbidden.

func IsContainerReady

func IsContainerReady(containerName string) CheckFunc

IsContainerReady returns a CheckFunc that succeeds when the named container reports Ready==true. This is meant to be used for WaitForContainer function.

func IsContainerTerminated

func IsContainerTerminated(containerName string, wantCode int64) CheckFunc

IsContainerTerminated returns a CheckFunc that succeeds when the named container terminated with the specified exit code. If terminated with a different exit code it returns an error. This is meant to be used for WaitForContainer function.

type Client

type Client struct {
	Dyn    dynamic.Interface
	Mapper *restmapper.DeferredDiscoveryRESTMapper
}

Client wraps Kubernetes dynamic client and REST mapper It is intended to be a replacement of invoking kubectl commands directly

func ClientFromContext

func ClientFromContext(ctx context.Context) (*Client, error)

func NewClient

func NewClient() (*Client, error)

NewClient creates a Kubernetes client that automatically detects whether it is running inside a cluster or using a kubeconfig file. It returns a fully prepared dynamic client + discovery mapper.

func (*Client) ApplyManifest

func (c *Client) ApplyManifest(ctx context.Context, manifestPath string) error

ApplyManifest applies resources defined in the given manifest file

func (*Client) DeleteManifest

func (c *Client) DeleteManifest(ctx context.Context, manifestPath string) error

DeleteManifest deletes resources defined in the given manifest file

func (*Client) List

func (c *Client) List(ctx context.Context, kind ResourceKind, namespace string, opts WaitOptions) (*unstructured.UnstructuredList, error)

List lists resources of the given kind in the specified namespace with optional filtering. If opts.NamePrefix is set, client-side filtering is applied to return only resources whose names start with the specified prefix.

func (*Client) WaitForContainer

func (c *Client) WaitForContainer(ctx context.Context, namespace string, checkFn CheckFunc, timeout time.Duration, opts WaitOptions) error

WaitForContainer waits until the specified container in the given Pod is ready or has terminated successfully within the timeout. It returns an error when the container terminates with a non-zero exit code or on other failures.

func (*Client) WaitForResource

func (c *Client) WaitForResource(
	ctx context.Context,
	kind ResourceKind,
	namespace, name string,
	checkFn CheckFunc,
	timeout time.Duration,
) error

WaitForResource waits until the specified resource of the given kind in the specified namespace satisfies the condition defined by checkFn within the timeout. It returns an error if the timeout is reached or if any fatal API errors occur.

func (*Client) WaitForResources

func (c *Client) WaitForResources(
	ctx context.Context,
	kind ResourceKind,
	namespace string,
	checkFn CheckFunc,
	timeout time.Duration,
	opts WaitOptions,
) error

WaitForResources waits until all resources of the given kind in the specified namespace satisfy the condition defined by checkFn within the timeout. It returns an error if the timeout is reached or if any fatal API errors occur.

type ClientProvider

type ClientProvider func() (*Client, error)

ClientProvider is a function that provides a kube client NewClient can be used for this provider type

type ClientProviderFromContext

type ClientProviderFromContext func(ctx context.Context) (*Client, error)

ClientProviderFromContext is a function that provides a kube client instance from context. ClientFromContext can be used for this provider type

type KubeConfigManager

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

KubeConfigManager manages kubeconfig file operations with injected dependencies.

func NewKubeConfigManager

func NewKubeConfigManager() (*KubeConfigManager, error)

NewKubeConfigManager creates a new KubeConfigManager with the default dependencies.

func (*KubeConfigManager) Configure

func (m *KubeConfigManager) Configure() error

Configure copies the kubeconfig file to the user's home directory, to /root/.kube, and to the current user's directory. This allows kubectl to be used without requiring root privileges and ensures the config is available for all relevant users.

func (*KubeConfigManager) SetKubeDir

func (m *KubeConfigManager) SetKubeDir(dir string)

SetKubeDir sets a custom kubeconfig directory path. If not set, the default ~/.kube will be used.

type Phase

type Phase string
const (
	PhasePending   Phase = "Pending"
	PhaseRunning   Phase = "Running"
	PhaseSucceeded Phase = "Succeeded"
	PhaseFailed    Phase = "Failed"
	PhaseUnknown   Phase = "Unknown"
)

func RegisterPhase

func RegisterPhase(name string) Phase

RegisterPhase allows registering a new Phase value

func ToPhase

func ToPhase(s string) Phase

ToPhase converts a string to a Phase, normalizing the case

func (Phase) String

func (p Phase) String() string

type ResourceKind

type ResourceKind string
const (
	KindNode       ResourceKind = "Node"
	KindService    ResourceKind = "Service"
	KindNamespace  ResourceKind = "Namespace"
	KindConfigMap  ResourceKind = "ConfigMap"
	KindPod        ResourceKind = "Pod"
	KindDeployment ResourceKind = "Deployment"
	KindJob        ResourceKind = "Job"
	KindPVC        ResourceKind = "PersistentVolumeClaim"
	KindCRD        ResourceKind = "CustomResourceDefinition"
)

func ToResourceKind

func ToResourceKind(gvr schema.GroupVersionResource) ResourceKind

func (ResourceKind) String

func (k ResourceKind) String() string

type WaitOptions

type WaitOptions struct {
	// NamePrefix restricts the list of returned objects to those whose names start with the given prefix.
	// +optional
	NamePrefix string `json:"namePrefix,omitempty"`
	// A selector to restrict the list of returned objects by their labels.
	// Defaults to everything.
	// +optional
	LabelSelector string `json:"labelSelector,omitempty"`
	// A selector to restrict the list of returned objects by their fields.
	// Defaults to everything.
	// +optional
	FieldSelector string `json:"fieldSelector,omitempty"`
}

func (WaitOptions) AsListOptions

func (w WaitOptions) AsListOptions() metav1.ListOptions

func (WaitOptions) String

func (w WaitOptions) String() string

Jump to

Keyboard shortcuts

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