k8sutil

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Apr 22, 2021 License: Apache-2.0 Imports: 19 Imported by: 0

Documentation

Overview

Package k8sutil for Kubernetes helpers.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func BaseKubernetesDeleteOptions

func BaseKubernetesDeleteOptions() *metav1.DeleteOptions

BaseKubernetesDeleteOptions returns the base set of Kubernetes delete options which most delete operations should use.

func CreateDeployment

func CreateDeployment(clientset kubernetes.Interface, name, namespace string, dep *apps.Deployment) error

func CreateReplaceableConfigmap

func CreateReplaceableConfigmap(clientset kubernetes.Interface, configmap *corev1.ConfigMap) error

func DeleteBatchJob

func DeleteBatchJob(clientset kubernetes.Interface, namespace, name string, wait bool) error

DeleteBatchJob deletes a Kubernetes job.

func DeleteConfigMap

func DeleteConfigMap(clientset kubernetes.Interface, cmName, namespace string, opts *DeleteOptions) error

func DeleteDeployment

func DeleteDeployment(clientset kubernetes.Interface, namespace, name string) error

DeleteDeployment makes a best effort at deleting a deployment and its pods, then waits for them to be deleted

func DeleteResource

func DeleteResource(
	delete func() error,
	verify func() error,
	resource string,
	opts *DeleteOptions,
	defaultWaitOptions *WaitOptions,
) error

DeleteResource implements the DeleteOptions logic around deletion of a Kubernetes resource.

The delete and verify functions used as parameters should implement and return the error from the Kubernetes `Delete` and `Get` commands respectively.

The resource string will be used to report the resource in log and error messages. A good pattern would be to set this string to the resource type (e.g., Deployment) and the name of the resource (e.g., my-deployment).

The default wait options should specify sane defaults for retry count and retry interval for deletion of the specific resource type. Only retry count and interval will be used from this parameter.

func GetContainerImage

func GetContainerImage(pod *v1.Pod, name string) (string, error)

GetContainerImage returns the container image matching the given name for a pod. If the pod only has a single container, the name argument is ignored.

func GetMatchingContainer

func GetMatchingContainer(containers []v1.Container, name string) (v1.Container, error)

GetMatchingContainer takes a list of containers and a name, and returns the first container in the list matching the name. If the list contains a single container it is always returned, even if the name does not match.

func GetRunningPod

func GetRunningPod(clientset kubernetes.Interface) (*v1.Pod, error)

GetRunningPod reads the name and namespace of a pod from the environment, and returns the pod (if it exists).

func GetSpecContainerImage

func GetSpecContainerImage(spec v1.PodSpec, name string, initContainer bool) (string, error)

GetSpecContainerImage returns the container image for a podspec, given a container name. The name is ignored if the podspec has a single container, in which case the image for that container is returned.

func Hash

func Hash(s string) string

Hash stableName computes a stable pseudorandom string suitable for inclusion in a Kubernetes object name from the given seed string. Do **NOT** edit this function in a way that would change its output as it needs to

func RunReplaceableJob

func RunReplaceableJob(clientset kubernetes.Interface, job *batch.Job, deleteIfFound bool) error

RunReplaceableJob runs a Kubernetes job with the intention that the job can be replaced by another call to this function with the same job name. For example, if a storage operator is restarted/updated before the job can complete, the operator's next run of the job should replace the previous job if deleteIfFound is set to true.

func TruncateNodeName

func TruncateNodeName(format, nodeName string) string

func UpdateStatus

func UpdateStatus(client client.Client, obj client.Object) error

UpdateStatus updates an object with a given status

func WaitForJobCompletion

func WaitForJobCompletion(clientset kubernetes.Interface, job *batch.Job, timeout time.Duration) error

WaitForJobCompletion waits for a job to reach the completed state. Assumes that only one pod needs to complete.

Types

type DeleteOptions

type DeleteOptions struct {
	// MustDelete controls the idempotency of the delete operation. If MustDelete is true and the
	// resource being deleted does not exist, the delete operation is considered a failure. If
	// MustDelete is false and the resource being deleted does not exist, the delete operation is
	// considered a success.
	MustDelete bool

	// DeleteOptions is a superset of WaitOptions.
	WaitOptions
}

DeleteOptions are a common set of options controlling the behavior of k8sutil delete operations. DeleteOptions is a superset of WaitOptions.

type WaitOptions

type WaitOptions struct {
	// Wait defines whether the operation should wait in a loop and verify that the operation was
	// successful before returning.
	Wait bool

	// RetryCount defines how many times the operation should retry verification in the wait loop
	// before giving up. If RetryCount is zero, the operation should default to a sane value based
	// on the operation.
	RetryCount uint

	// RetryInterval defines the time the operation will wait before retrying verification. If
	// RetryInterval is zero, the operation should default to a sane value based on the operation.
	RetryInterval time.Duration

	// ErrorOnTimeout defines whether the operation should time out with an error. If ErrorOnTimeout
	// is true and the operation times out, the operation is considered a failure. If ErrorOnTimeout
	// is false and the operation times out, the operation should log a warning but not not report
	// failure.
	ErrorOnTimeout bool
}

WaitOptions are a common set of options controlling the behavior of k8sutil operations. If WaitOptions are specified, the operation should wait in a loop and verify that the operation being performed was successful.

Jump to

Keyboard shortcuts

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