k8s

package
v0.13.3 Latest Latest
Warning

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

Go to latest
Published: May 20, 2024 License: Apache-2.0 Imports: 30 Imported by: 0

Documentation

Overview

Package k8s provides utility functions for working with Kubernetes clusters.

Package k8s provides utility functions for working with Kubernetes clusters.

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrKnuuNotInitialized            = &Error{Code: "KnuuNotInitialized", Message: "knuu is not initialized"}
	ErrGettingConfigmap              = &Error{Code: "ErrorGettingConfigmap", Message: "error getting configmap %s"}
	ErrConfigmapAlreadyExists        = &Error{Code: "ConfigmapAlreadyExists", Message: "configmap %s already exists"}
	ErrCreatingConfigmap             = &Error{Code: "ErrorCreatingConfigmap", Message: "error creating configmap %s"}
	ErrConfigmapDoesNotExist         = &Error{Code: "ConfigmapDoesNotExist", Message: "configmap %s does not exist"}
	ErrDeletingConfigmap             = &Error{Code: "ErrorDeletingConfigmap", Message: "error deleting configmap %s"}
	ErrGettingDaemonset              = &Error{Code: "ErrorGettingDaemonset", Message: "error getting daemonset %s"}
	ErrCreatingDaemonset             = &Error{Code: "ErrorCreatingDaemonset", Message: "error creating daemonset %s"}
	ErrUpdatingDaemonset             = &Error{Code: "ErrorUpdatingDaemonset", Message: "error updating daemonset %s"}
	ErrDeletingDaemonset             = &Error{Code: "ErrorDeletingDaemonset", Message: "error deleting daemonset %s"}
	ErrCreatingNamespace             = &Error{Code: "ErrorCreatingNamespace", Message: "error creating namespace %s"}
	ErrDeletingNamespace             = &Error{Code: "ErrorDeletingNamespace", Message: "error deleting namespace %s"}
	ErrGettingNamespace              = &Error{Code: "ErrorGettingNamespace", Message: "error getting namespace %s"}
	ErrCreatingNetworkPolicy         = &Error{Code: "ErrorCreatingNetworkPolicy", Message: "error creating network policy %s"}
	ErrDeletingNetworkPolicy         = &Error{Code: "ErrorDeletingNetworkPolicy", Message: "error deleting network policy %s"}
	ErrGettingNetworkPolicy          = &Error{Code: "ErrorGettingNetworkPolicy", Message: "error getting network policy %s"}
	ErrGettingPod                    = &Error{Code: "ErrorGettingPod", Message: "failed to get pod %s"}
	ErrPreparingPod                  = &Error{Code: "ErrorPreparingPod", Message: "error preparing pod"}
	ErrCreatingPod                   = &Error{Code: "ErrorCreatingPod", Message: "failed to create pod"}
	ErrDeletingPod                   = &Error{Code: "ErrorDeletingPod", Message: "failed to delete pod"}
	ErrDeployingPod                  = &Error{Code: "ErrorDeployingPod", Message: "failed to deploy pod"}
	ErrGettingK8sConfig              = &Error{Code: "ErrorGettingK8sConfig", Message: "failed to get k8s config"}
	ErrCreatingExecutor              = &Error{Code: "ErrorCreatingExecutor", Message: "failed to create Executor"}
	ErrExecutingCommand              = &Error{Code: "ErrorExecutingCommand", Message: "failed to execute command"}
	ErrCommandExecution              = &Error{Code: "ErrorCommandExecution", Message: "error while executing command"}
	ErrDeletingPodFailed             = &Error{Code: "ErrorDeletingPodFailed", Message: "failed to delete pod %s"}
	ErrParsingMemoryRequest          = &Error{Code: "ErrorParsingMemoryRequest", Message: "failed to parse memory request quantity '%s'"}
	ErrParsingMemoryLimit            = &Error{Code: "ErrorParsingMemoryLimit", Message: "failed to parse memory limit quantity '%s'"}
	ErrParsingCPURequest             = &Error{Code: "ErrorParsingCPURequest", Message: "failed to parse CPU request quantity '%s'"}
	ErrBuildingContainerVolumes      = &Error{Code: "ErrorBuildingContainerVolumes", Message: "failed to build container volumes"}
	ErrBuildingResources             = &Error{Code: "ErrorBuildingResources", Message: "failed to build resources"}
	ErrBuildingInitContainerVolumes  = &Error{Code: "ErrorBuildingInitContainerVolumes", Message: "failed to build init container volumes"}
	ErrBuildingInitContainerCommand  = &Error{Code: "ErrorBuildingInitContainerCommand", Message: "failed to build init container command"}
	ErrBuildingPodVolumes            = &Error{Code: "ErrorBuildingPodVolumes", Message: "failed to build pod volumes"}
	ErrPreparingMainContainer        = &Error{Code: "ErrorPreparingMainContainer", Message: "failed to prepare main container"}
	ErrPreparingInitContainer        = &Error{Code: "ErrorPreparingInitContainer", Message: "failed to prepare init container"}
	ErrPreparingPodVolumes           = &Error{Code: "ErrorPreparingPodVolumes", Message: "failed to prepare pod volumes"}
	ErrPreparingSidecarContainer     = &Error{Code: "ErrorPreparingSidecarContainer", Message: "failed to prepare sidecar container"}
	ErrPreparingSidecarVolumes       = &Error{Code: "ErrorPreparingSidecarVolumes", Message: "failed to prepare sidecar volumes"}
	ErrCreatingPodSpec               = &Error{Code: "ErrorCreatingPodSpec", Message: "failed to create pod spec"}
	ErrGettingClusterConfig          = &Error{Code: "ErrorGettingClusterConfig", Message: "failed to get cluster config"}
	ErrCreatingRoundTripper          = &Error{Code: "ErrorCreatingRoundTripper", Message: "failed to create round tripper"}
	ErrCreatingPortForwarder         = &Error{Code: "ErrorCreatingPortForwarder", Message: "failed to create port forwarder"}
	ErrPortForwarding                = &Error{Code: "ErrorPortForwarding", Message: "failed to port forward: %v"}
	ErrForwardingPorts               = &Error{Code: "ErrorForwardingPorts", Message: "error forwarding ports"}
	ErrPortForwardingTimeout         = &Error{Code: "ErrorPortForwardingTimeout", Message: "timed out waiting for port forwarding to be ready"}
	ErrDeletingPersistentVolumeClaim = &Error{Code: "ErrorDeletingPersistentVolumeClaim", Message: "error deleting PersistentVolumeClaim %s"}
	ErrCreatingPersistentVolumeClaim = &Error{Code: "ErrorCreatingPersistentVolumeClaim", Message: "error creating PersistentVolumeClaim"}
	ErrGettingReplicaSet             = &Error{Code: "ErrorGettingReplicaSet", Message: "failed to get ReplicaSet %s"}
	ErrCreatingReplicaSet            = &Error{Code: "ErrorCreatingReplicaSet", Message: "failed to create ReplicaSet"}
	ErrDeletingReplicaSet            = &Error{Code: "ErrorDeletingReplicaSet", Message: "failed to delete ReplicaSet %s"}
	ErrCheckingReplicaSetExists      = &Error{Code: "ErrorCheckingReplicaSetExists", Message: "failed to check if ReplicaSet %s exists"}
	ErrWaitingForReplicaSet          = &Error{Code: "ErrorWaitingForReplicaSet", Message: "error waiting for ReplicaSet to delete"}
	ErrDeployingReplicaSet           = &Error{Code: "ErrorDeployingReplicaSet", Message: "failed to deploy ReplicaSet"}
	ErrPreparingPodSpec              = &Error{Code: "ErrorPreparingPodSpec", Message: "failed to prepare pod spec"}
	ErrListingPodsForReplicaSet      = &Error{Code: "ErrorListingPodsForReplicaSet", Message: "failed to list pods for ReplicaSet %s"}
	ErrNoPodsForReplicaSet           = &Error{Code: "NoPodsForReplicaSet", Message: "no pods found for ReplicaSet %s"}
	ErrGettingService                = &Error{Code: "ErrorGettingService", Message: "error getting service %s"}
	ErrPreparingService              = &Error{Code: "ErrorPreparingService", Message: "error preparing service %s"}
	ErrCreatingService               = &Error{Code: "ErrorCreatingService", Message: "error creating service %s"}
	ErrPatchingService               = &Error{Code: "ErrorPatchingService", Message: "error patching service %s"}
	ErrDeletingService               = &Error{Code: "ErrorDeletingService", Message: "error deleting service %s"}
	ErrNamespaceRequired             = &Error{Code: "NamespaceRequired", Message: "namespace is required"}
	ErrServiceNameRequired           = &Error{Code: "ServiceNameRequired", Message: "service name is required"}
	ErrNoPortsSpecified              = &Error{Code: "NoPortsSpecified", Message: "no ports specified for service %s"}
	ErrRetrievingKubernetesConfig    = &Error{Code: "RetrievingKubernetesConfig", Message: "retrieving the Kubernetes config"}
	ErrCreatingClientset             = &Error{Code: "CreatingClientset", Message: "creating clientset for Kubernetes"}
	ErrCreatingDiscoveryClient       = &Error{Code: "CreatingDiscoveryClient", Message: "creating discovery client for Kubernetes"}
	ErrCreatingDynamicClient         = &Error{Code: "CreatingDynamicClient", Message: "creating dynamic client for Kubernetes"}
	ErrGettingResourceList           = &Error{Code: "GettingResourceList", Message: "getting resource list for group version %s"}
	ErrResourceDoesNotExist          = &Error{Code: "ResourceDoesNotExist", Message: "resource %s does not exist in group version %s"}
	ErrCreatingCustomResource        = &Error{Code: "CreatingCustomResource", Message: "creating custom resource %s"}
)

Functions

func SanitizeName added in v0.13.2

func SanitizeName(name string) string

SanitizeName ensures compliance with Kubernetes DNS-1123 subdomain names. It:

  1. Converts the input string to lowercase.
  2. Replaces underscores and any non-DNS-1123 compliant characters with hyphens.
  3. Trims leading and trailing hyphens.
  4. Ensures the name does not exceed 63 characters, trimming excess characters if necessary and ensuring it does not end with a hyphen after trimming.

Use this function to sanitize strings to be used as Kubernetes names for resources.

Types

type Client added in v0.13.3

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

func New added in v0.13.3

func New(ctx context.Context, namespace string) (*Client, error)

func (*Client) Clientset added in v0.13.3

func (c *Client) Clientset() *kubernetes.Clientset

func (*Client) ConfigMapExists added in v0.13.3

func (c *Client) ConfigMapExists(ctx context.Context, name string) (bool, error)

func (*Client) CreateConfigMap added in v0.13.3

func (c *Client) CreateConfigMap(
	ctx context.Context,
	name string,
	labels, data map[string]string,
) (*v1.ConfigMap, error)

func (*Client) CreateCustomResource added in v0.13.3

func (c *Client) CreateCustomResource(
	ctx context.Context,
	name string,
	gvr *schema.GroupVersionResource,
	obj *map[string]interface{},
) error

func (*Client) CreateDaemonSet added in v0.13.3

func (c *Client) CreateDaemonSet(
	ctx context.Context,
	name string,
	labels map[string]string,
	initContainers []v1.Container,
	containers []v1.Container,
) (*appv1.DaemonSet, error)

func (*Client) CreateNamespace added in v0.13.3

func (c *Client) CreateNamespace(ctx context.Context, name string) error

func (*Client) CreateNetworkPolicy added in v0.13.3

func (c *Client) CreateNetworkPolicy(
	ctx context.Context,
	name string,
	selectorMap,
	ingressSelectorMap,
	egressSelectorMap map[string]string,
) error

func (*Client) CreatePersistentVolumeClaim added in v0.13.3

func (c *Client) CreatePersistentVolumeClaim(
	ctx context.Context,
	name string,
	labels map[string]string,
	size resource.Quantity,
) error

CreatePersistentVolumeClaim deploys a PersistentVolumeClaim if it does not exist.

func (*Client) CreateReplicaSet added in v0.13.3

func (c *Client) CreateReplicaSet(ctx context.Context, rsConfig ReplicaSetConfig, init bool) (*appv1.ReplicaSet, error)

CreateReplicaSet creates a new replicaSet in namespace that k8s is initialized with if it doesn't already exist.

func (*Client) CreateRole added in v0.13.3

func (c *Client) CreateRole(
	ctx context.Context,
	name string,
	labels map[string]string,
	policyRules []rbacv1.PolicyRule,
) error

func (*Client) CreateRoleBinding added in v0.13.3

func (c *Client) CreateRoleBinding(
	ctx context.Context,
	name string,
	labels map[string]string,
	role, serviceAccount string,
) error

func (*Client) CreateService added in v0.13.3

func (c *Client) CreateService(
	ctx context.Context,
	name string,
	labels,
	selectorMap map[string]string,
	portsTCP,
	portsUDP []int,
) (*v1.Service, error)

func (*Client) CreateServiceAccount added in v0.13.3

func (c *Client) CreateServiceAccount(ctx context.Context, name string, labels map[string]string) error

func (*Client) CustomResourceDefinitionExists added in v0.13.3

func (c *Client) CustomResourceDefinitionExists(ctx context.Context, gvr *schema.GroupVersionResource) bool

func (*Client) DaemonSetExists added in v0.13.3

func (c *Client) DaemonSetExists(ctx context.Context, name string) (bool, error)

func (*Client) DeleteConfigMap added in v0.13.3

func (c *Client) DeleteConfigMap(ctx context.Context, name string) error

func (*Client) DeleteDaemonSet added in v0.13.3

func (c *Client) DeleteDaemonSet(ctx context.Context, name string) error

func (*Client) DeleteNamespace added in v0.13.3

func (c *Client) DeleteNamespace(ctx context.Context, name string) error

func (*Client) DeleteNetworkPolicy added in v0.13.3

func (c *Client) DeleteNetworkPolicy(ctx context.Context, name string) error

func (*Client) DeletePersistentVolumeClaim added in v0.13.3

func (c *Client) DeletePersistentVolumeClaim(ctx context.Context, name string) error

func (*Client) DeletePod added in v0.13.3

func (c *Client) DeletePod(ctx context.Context, name string) error

func (*Client) DeletePodWithGracePeriod added in v0.13.3

func (c *Client) DeletePodWithGracePeriod(ctx context.Context, name string, gracePeriodSeconds *int64) error

func (*Client) DeleteReplicaSet added in v0.13.3

func (c *Client) DeleteReplicaSet(ctx context.Context, name string) error

func (*Client) DeleteReplicaSetWithGracePeriod added in v0.13.3

func (c *Client) DeleteReplicaSetWithGracePeriod(ctx context.Context, name string, gracePeriodSeconds *int64) error

func (*Client) DeleteRole added in v0.13.3

func (c *Client) DeleteRole(ctx context.Context, name string) error

func (*Client) DeleteRoleBinding added in v0.13.3

func (c *Client) DeleteRoleBinding(ctx context.Context, name string) error

func (*Client) DeleteService added in v0.13.3

func (c *Client) DeleteService(ctx context.Context, name string) error

func (*Client) DeleteServiceAccount added in v0.13.3

func (c *Client) DeleteServiceAccount(ctx context.Context, name string) error

func (*Client) DeployPod added in v0.13.3

func (c *Client) DeployPod(ctx context.Context, podConfig PodConfig, init bool) (*v1.Pod, error)

DeployPod creates a new pod in the namespace that k8s client is initiate with if it doesn't already exist.

func (*Client) GetConfigMap added in v0.13.3

func (c *Client) GetConfigMap(ctx context.Context, name string) (*v1.ConfigMap, error)

func (*Client) GetDaemonSet added in v0.13.3

func (c *Client) GetDaemonSet(ctx context.Context, name string) (*appv1.DaemonSet, error)

func (*Client) GetFirstPodFromReplicaSet added in v0.13.3

func (c *Client) GetFirstPodFromReplicaSet(ctx context.Context, name string) (*v1.Pod, error)

func (*Client) GetNamespace added in v0.13.3

func (c *Client) GetNamespace(ctx context.Context, name string) (*corev1.Namespace, error)

func (*Client) GetNetworkPolicy added in v0.13.3

func (c *Client) GetNetworkPolicy(ctx context.Context, name string) (*v1.NetworkPolicy, error)

func (*Client) GetService added in v0.13.3

func (c *Client) GetService(ctx context.Context, name string) (*v1.Service, error)

func (*Client) GetServiceIP added in v0.13.3

func (c *Client) GetServiceIP(ctx context.Context, name string) (string, error)

func (*Client) IsPodRunning added in v0.13.3

func (c *Client) IsPodRunning(ctx context.Context, name string) (bool, error)

IsPodRunning returns true if all containers in the pod are running.

func (*Client) IsReplicaSetRunning added in v0.13.3

func (c *Client) IsReplicaSetRunning(ctx context.Context, name string) (bool, error)

func (*Client) Namespace added in v0.13.3

func (c *Client) Namespace() string

func (*Client) NamespaceExists added in v0.13.3

func (c *Client) NamespaceExists(ctx context.Context, name string) bool

func (*Client) NetworkPolicyExists added in v0.13.3

func (c *Client) NetworkPolicyExists(ctx context.Context, name string) bool

func (*Client) NewFile added in v0.13.3

func (c *Client) NewFile(source, dest string) *File

func (*Client) NewVolume added in v0.13.3

func (c *Client) NewVolume(path, size string, owner int64) *Volume

func (*Client) PatchService added in v0.13.3

func (c *Client) PatchService(
	ctx context.Context,
	name string,
	labels,
	selectorMap map[string]string,
	portsTCP,
	portsUDP []int,
) (*v1.Service, error)

func (*Client) PortForwardPod added in v0.13.3

func (c *Client) PortForwardPod(
	ctx context.Context,
	podName string,
	localPort,
	remotePort int,
) error

PortForwardPod forwards a local port to a port on a pod.

func (*Client) ReplacePod added in v0.13.3

func (c *Client) ReplacePod(ctx context.Context, podConfig PodConfig) (*v1.Pod, error)

ReplacePod replaces a pod and returns the new Pod object.

func (*Client) ReplacePodWithGracePeriod added in v0.13.3

func (c *Client) ReplacePodWithGracePeriod(ctx context.Context, podConfig PodConfig, gracePeriod *int64) (*v1.Pod, error)

func (*Client) ReplaceReplicaSet added in v0.13.3

func (c *Client) ReplaceReplicaSet(ctx context.Context, ReplicaSetConfig ReplicaSetConfig) (*appv1.ReplicaSet, error)

func (*Client) ReplaceReplicaSetWithGracePeriod added in v0.13.3

func (c *Client) ReplaceReplicaSetWithGracePeriod(ctx context.Context, ReplicaSetConfig ReplicaSetConfig, gracePeriod *int64) (*appv1.ReplicaSet, error)

func (*Client) ReplicaSetExists added in v0.13.3

func (c *Client) ReplicaSetExists(ctx context.Context, name string) (bool, error)

ReplicaSetExists checks if a ReplicaSet exists in the namespace that k8s is initialized with.

func (*Client) RunCommandInPod added in v0.13.3

func (c *Client) RunCommandInPod(
	ctx context.Context,
	podName,
	containerName string,
	cmd []string,
) (string, error)

RunCommandInPod runs a command in a container within a pod with a context.

func (*Client) UpdateDaemonSet added in v0.13.3

func (c *Client) UpdateDaemonSet(ctx context.Context,
	name string,
	labels map[string]string,
	initContainers []v1.Container,
	containers []v1.Container,
) (*appv1.DaemonSet, error)

type ContainerConfig added in v0.9.0

type ContainerConfig struct {
	Name            string              // Name to assign to the Container
	Image           string              // Name of the container image to use for the container
	Command         []string            // Command to run in the container
	Args            []string            // Arguments to pass to the command in the container
	Env             map[string]string   // Environment variables to set in the container
	Volumes         []*Volume           // Volumes to mount in the Pod
	MemoryRequest   string              // Memory request for the container
	MemoryLimit     string              // Memory limit for the container
	CPURequest      string              // CPU request for the container
	LivenessProbe   *v1.Probe           // Liveness probe for the container
	ReadinessProbe  *v1.Probe           // Readiness probe for the container
	StartupProbe    *v1.Probe           // Startup probe for the container
	Files           []*File             // Files to add to the Pod
	SecurityContext *v1.SecurityContext // Security context for the container
}

type Error added in v0.13.3

type Error struct {
	Code    string
	Message string
	Err     error
	Params  []interface{}
}

func (*Error) Error added in v0.13.3

func (e *Error) Error() string

func (*Error) WithParams added in v0.13.3

func (e *Error) WithParams(params ...interface{}) *Error

func (*Error) Wrap added in v0.13.3

func (e *Error) Wrap(err error) error

type File added in v0.9.0

type File struct {
	Source string
	Dest   string
}

type PodConfig

type PodConfig struct {
	Namespace          string            // Kubernetes namespace of the Pod
	Name               string            // Name to assign to the Pod
	Labels             map[string]string // Labels to apply to the Pod
	ServiceAccountName string            // ServiceAccount to assign to Pod
	FsGroup            int64             // FSGroup to apply to the Pod
	ContainerConfig    ContainerConfig   // ContainerConfig for the Pod
	SidecarConfigs     []ContainerConfig // SideCarConfigs for the Pod
}

type ReplicaSetConfig added in v0.12.0

type ReplicaSetConfig struct {
	Name      string            // Name of the ReplicaSet
	Namespace string            // Namespace of the ReplicaSet
	Labels    map[string]string // Labels to apply to the ReplicaSet, key/value represents the name/value of the label
	Replicas  int32             // Replicas is the number of replicas
	PodConfig PodConfig         // PodConfig represents the pod configuration
}

type Volume added in v0.7.2

type Volume struct {
	Path  string
	Size  string
	Owner int64
}

Jump to

Keyboard shortcuts

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