util

package
v0.19.0 Latest Latest
Warning

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

Go to latest
Published: Oct 25, 2024 License: Apache-2.0 Imports: 23 Imported by: 34

Documentation

Overview

Package util provides various utility functions for Kubernetes resources.

Index

Constants

View Source
const (
	MetadataField    = "metadata"
	LabelsField      = "labels"
	AnnotationsField = "annotations"
	StatusField      = "status"
)

Variables

This section is empty.

Functions

func AddCertificateErrorHandler added in v0.16.1

func AddCertificateErrorHandler(fatal bool)

func BuildRestMapper

func BuildRestMapper(restConfig *rest.Config) (meta.RESTMapper, error)

func ConditionsFromUnstructured added in v0.10.1

func ConditionsFromUnstructured(from *unstructured.Unstructured, fields ...string) []metav1.Condition

func ConditionsToUnstructured added in v0.10.1

func ConditionsToUnstructured(conditions []metav1.Condition, to *unstructured.Unstructured, fields ...string)

func CopyImmutableMetadata added in v0.12.0

func CopyImmutableMetadata(from, to *unstructured.Unstructured) *unstructured.Unstructured

CopyImmutableMetadata copies the static metadata fields (except Labels and Annotations) from one resource to another.

func CreateAnew added in v0.9.0

func CreateAnew[T runtime.Object](ctx context.Context, client resource.Interface[T], obj T,
	createOptions metav1.CreateOptions,
	deleteOptions metav1.DeleteOptions,
) (T, error)

CreateAnew creates a resource, first deleting an existing instance if one exists. If the delete options specify that deletion should be propagated in the foreground, this will wait for the deletion to be complete before creating the new object: with foreground propagation, Get will continue to return the object being deleted and Create will fail with “already exists” until deletion is complete.

func DeeplyEmpty added in v0.17.0

func DeeplyEmpty(m map[string]interface{}) bool

func FindGroupVersionResource

func FindGroupVersionResource(from *unstructured.Unstructured, restMapper meta.RESTMapper) (*schema.GroupVersionResource, error)

func GetMetadata

func GetMetadata(from *unstructured.Unstructured) map[string]interface{}

func GetNestedField added in v0.6.0

func GetNestedField(obj *unstructured.Unstructured, fields ...string) interface{}

func GetSpec added in v0.6.0

func GetSpec(obj *unstructured.Unstructured) interface{}

func MustUpdate added in v0.14.0

func MustUpdate[T runtime.Object](ctx context.Context, client resource.Interface[T], obj T, mutate MutateFn[T]) error

Update tries to obtain an existing resource and, if found, updates it. If not found, a NotFound error is returned.

func SetBackoff added in v0.9.0

func SetBackoff(b wait.Backoff) wait.Backoff

func SetNestedField added in v0.12.0

func SetNestedField(to map[string]interface{}, value interface{}, fields ...string)

func ToUnstructuredResource

func ToUnstructuredResource(from runtime.Object, restMapper meta.RESTMapper,
) (*unstructured.Unstructured, *schema.GroupVersionResource, error)

func Update added in v0.10.1

func Update[T runtime.Object](ctx context.Context, client resource.Interface[T], obj T, mutate MutateFn[T]) error

Update tries to obtain an existing resource and, if found, updates it. If not found, no error is returned.

Types

type CreateOrUpdateOptions added in v0.17.3

type CreateOrUpdateOptions[T runtime.Object] struct {
	Client         resource.Interface[T]
	Obj            T
	MutateOnUpdate MutateFn[T]
	MutateOnCreate MutateFn[T]
	// IdentifyingLabels is used to find an existing resource if GenerateName is set in the target resource.
	IdentifyingLabels map[string]string
}

type MutateFn

type MutateFn[T runtime.Object] func(existing T) (T, error)

func Replace added in v0.9.0

func Replace[T runtime.Object](with T) MutateFn[T]

type OperationResult

type OperationResult string
const (
	OperationResultNone    OperationResult = "unchanged"
	OperationResultCreated OperationResult = "created"
	OperationResultUpdated OperationResult = "updated"
)

func CreateOrUpdate

func CreateOrUpdate[T runtime.Object](ctx context.Context, client resource.Interface[T], obj T, mutate MutateFn[T],
) (OperationResult, error)

CreateOrUpdate tries to obtain an existing resource and, if not found, creates 'obj' otherwise updates it. The existing resource is normally retrieved via 'obj's Name field but if it's empty and the GenerateName field is non-empty, it will try to retrieve it via the List method using 'obj's Labels. This assumes that the labels uniquely identify the resource. If more than one resource is found, an error is returned.

func CreateOrUpdateWithOptions added in v0.17.3

func CreateOrUpdateWithOptions[T runtime.Object](ctx context.Context, options CreateOrUpdateOptions[T]) (OperationResult, T, error)

Jump to

Keyboard shortcuts

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