k8s

package
v0.13.2 Latest Latest
Warning

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

Go to latest
Published: Apr 18, 2024 License: Apache-2.0 Imports: 27 Imported by: 0

Documentation

Overview

Package k8s provides utility functions for working with Kubernetes clusters.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Clientset

func Clientset() *kubernetes.Clientset

Clientset returns the Kubernetes clientset.

func ConfigMapExists added in v0.9.0

func ConfigMapExists(namespace, name string) (bool, error)

ConfigMapExists checks if a configmap exists

func CreateConfigMap added in v0.9.0

func CreateConfigMap(
	namespace,
	name string,
	labels,
	data map[string]string,
) (*v1.ConfigMap, error)

CreateConfigMap creates a configmap

func CreateDaemonSet added in v0.7.0

func CreateDaemonSet(
	namespace,
	name string,
	labels map[string]string,
	initContainers []v1.Container,
	containers []v1.Container,
) (*appv1.DaemonSet, error)

CreateDaemonSet creates a new daemonset.

func CreateNamespace added in v0.13.2

func CreateNamespace(name string) error

CreateNamespace creates a new namespace if it does not exist

func CreateNetworkPolicy added in v0.7.0

func CreateNetworkPolicy(
	namespace,
	name string,
	selectorMap,
	ingressSelectorMap,
	egressSelectorMap map[string]string,
) error

CreateNetworkPolicy creates a new NetworkPolicy resource.

func CreateRole added in v0.8.2

func CreateRole(
	namespace,
	name string,
	labels map[string]string,
	policyRules []rbacv1.PolicyRule,
) error

CreateRole creates a role

func CreateRoleBinding added in v0.8.2

func CreateRoleBinding(
	namespace,
	name string,
	labels map[string]string,
	role,
	serviceAccount string,
) error

CreateRoleBinding creates a roleBinding

func CreateServiceAccount added in v0.8.2

func CreateServiceAccount(namespace, name string, labels map[string]string) error

CreateServiceAccount creates a service account

func DaemonSetExists added in v0.7.0

func DaemonSetExists(namespace, name string) (bool, error)

DaemonSetExists checks if a daemonset exists.

func DeleteConfigMap added in v0.9.0

func DeleteConfigMap(namespace, name string) error

DeleteConfigMap deletes a configmap

func DeleteDaemonSet added in v0.7.0

func DeleteDaemonSet(namespace, name string) error

DeleteDaemonSet deletes an existing daemonset.

func DeleteNamespace added in v0.13.2

func DeleteNamespace(name string) error

DeleteNamespace deletes an existing namespace

func DeleteNetworkPolicy added in v0.7.0

func DeleteNetworkPolicy(namespace string, name string) error

DeleteNetworkPolicy removes a NetworkPolicy resource.

func DeletePersistentVolumeClaim

func DeletePersistentVolumeClaim(namespace, name string)

DeletePersistentVolumeClaim deletes the PersistentVolumeClaim with the specified name in the specified namespace.

func DeletePod

func DeletePod(namespace, name string) error

DeletePod deletes a pod with the given name in the specified namespace.

func DeletePodWithGracePeriod added in v0.7.0

func DeletePodWithGracePeriod(namespace, name string, gracePeriodSeconds *int64) error

DeletePodWithGracePeriod deletes a pod with the given name in the specified namespace.

func DeleteReplicaSet added in v0.12.0

func DeleteReplicaSet(namespace, name string) error

DeleteReplicaSet deletes a ReplicaSet with the given name in the specified namespace.

func DeleteReplicaSetWithGracePeriod added in v0.12.0

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

DeleteReplicaSetWithGracePeriod deletes a ReplicaSet with the given name in the specified namespace.

func DeleteRole added in v0.8.2

func DeleteRole(namespace, name string) error

DeleteRole deletes a role

func DeleteRoleBinding added in v0.8.2

func DeleteRoleBinding(namespace, name string) error

DeleteRoleBinding deletes a roleBinding

func DeleteService

func DeleteService(namespace, name string) error

DeleteService deletes a service if it exists.

func DeleteServiceAccount added in v0.8.2

func DeleteServiceAccount(namespace, name string) error

DeleteServiceAccount deletes a service account

func DeployPersistentVolumeClaim

func DeployPersistentVolumeClaim(namespace, name string, labels map[string]string, size resource.Quantity)

DeployPersistentVolumeClaim creates a new PersistentVolumeClaim in the specified namespace.

func DeployPod

func DeployPod(podConfig PodConfig, init bool) (*v1.Pod, error)

DeployPod creates a new pod in the given namespace if it doesn't already exist.

func DeployReplicaSet added in v0.12.0

func DeployReplicaSet(ctx context.Context, replicaSetConfig ReplicaSetConfig, init bool) (*appv1.ReplicaSet, error)

DeployReplicaSet creates a new replicaSet in the given namespace if it doesn't already exist.

func DeployService

func DeployService(
	namespace,
	name string,
	labels,
	selectorMap map[string]string,
	portsTCP,
	portsUDP []int,
) (*v1.Service, error)

DeployService deploys a service if it does not exist.

func GetConfigMap added in v0.9.0

func GetConfigMap(namespace, name string) (*v1.ConfigMap, error)

GetConfigMap retrieves a configmap

func GetDaemonSet added in v0.7.0

func GetDaemonSet(namespace, name string) (*appv1.DaemonSet, error)

GetDaemonSet retrieves a daemonset.

func GetFirstPodFromReplicaSet added in v0.12.0

func GetFirstPodFromReplicaSet(namespace, name string) (*v1.Pod, error)

GetFirstPodFromReplicaSet returns the first pod of a Replicaset.

func GetNamespace added in v0.13.2

func GetNamespace(name string) (*corev1.Namespace, error)

GetNamespace retrieves an existing namespace

func GetNetworkPolicy added in v0.9.0

func GetNetworkPolicy(namespace string, name string) (*v1.NetworkPolicy, error)

GetNetworkPolicy retrieves a NetworkPolicy resource.

func GetService

func GetService(namespace, name string) (*v1.Service, error)

GetService retrieves a service.

func GetServiceIP

func GetServiceIP(namespace, name string) (string, error)

GetServiceIP retrieves the IP address of a service.

func Initialize

func Initialize() error

Initialize sets up the Kubernetes client.

func IsInitialized added in v0.7.0

func IsInitialized() bool

IsInitialized checks if the Kubernetes clientset has been initialized.

func IsPodRunning added in v0.7.0

func IsPodRunning(namespace, name string) (bool, error)

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

func IsReplicaSetRunning added in v0.12.0

func IsReplicaSetRunning(namespace, name string) (bool, error)

IsReplicaSetRunning returns true if the ReplicaSet is running.

func Namespace

func Namespace() string

Namespace returns the current namespace in use.

func NamespaceExists added in v0.13.2

func NamespaceExists(name string) bool

NamespaceExists checks if a namespace exists

func NetworkPolicyExists added in v0.9.0

func NetworkPolicyExists(namespace string, name string) bool

NetworkPolicyExists checks if a NetworkPolicy resource exists.

func PatchService

func PatchService(
	namespace,
	name string,
	labels,
	selectorMap map[string]string,
	portsTCP,
	portsUDP []int,
) error

PatchService patches an existing service.

func PortForwardPod added in v0.5.0

func PortForwardPod(
	namespace,
	podName string,
	localPort,
	remotePort int,
) error

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

func ReplacePod

func ReplacePod(podConfig PodConfig) (*v1.Pod, error)

ReplacePod replaces a pod in the given namespace and returns the new Pod object.

func ReplacePodWithGracePeriod added in v0.7.0

func ReplacePodWithGracePeriod(podConfig PodConfig, gracePeriod *int64) (*v1.Pod, error)

ReplacePodWithGracePeriod replaces a pod in the given namespace and returns the new Pod object with a grace period.

func ReplaceReplicaSet added in v0.12.0

func ReplaceReplicaSet(ReplicaSetConfig ReplicaSetConfig) (*appv1.ReplicaSet, error)

ReplaceReplicaSet replaces a ReplicaSet in the given namespace and returns the new ReplicaSet object.

func ReplaceReplicaSetWithGracePeriod added in v0.12.0

func ReplaceReplicaSetWithGracePeriod(ReplicaSetConfig ReplicaSetConfig, gracePeriod *int64) (*appv1.ReplicaSet, error)

ReplaceReplicaSetWithGracePeriod replaces a ReplicaSet in the given namespace and returns the new ReplicaSet object with a grace period.

func RunCommandInPod

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

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

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.

func SetNamespace added in v0.13.2

func SetNamespace(newNamespace string)

SetNamespace sets the used namespace to the provided string.

func Timeout added in v0.11.0

func Timeout() time.Duration

func UpdateDaemonSet added in v0.7.0

func UpdateDaemonSet(
	namespace,
	name string,
	labels map[string]string,
	initContainers []v1.Container,
	containers []v1.Container,
) (*appv1.DaemonSet, error)

UpdateDaemonSet updates an existing daemonset.

Types

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
}

ContainerConfig contains the specifications for creating a new Container object

type File added in v0.9.0

type File struct {
	Source string
	Dest   string
}

File represents a file.

func NewFile added in v0.9.0

func NewFile(source, dest string) *File

NewFile creates a new file with the given source and destination.

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
}

PodConfig contains the specifications for creating a new Pod object

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
}

ReplicaSetConfig contains the specifications for creating a new ReplicaSet object

type Volume added in v0.7.2

type Volume struct {
	Path  string
	Size  string
	Owner int64
}

Volume represents a volume.

func NewVolume added in v0.7.2

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

NewVolume creates a new volume with the given path, size and owner.

Jump to

Keyboard shortcuts

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