clientutils

package
v0.9.5 Latest Latest
Warning

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

Go to latest
Published: Nov 18, 2024 License: Apache-2.0 Imports: 17 Imported by: 7

Documentation

Overview

Package clientutils provides utilities for working with the client package of controller-runtime.

Index

Constants

This section is empty.

Variables

View Source
var ApplyAll = apply{}

ApplyAll provides client.Apply for any given object.

Functions

func CreateMultiple

func CreateMultiple(ctx context.Context, c client.Client, objs []client.Object, opts ...client.CreateOption) error

CreateMultiple creates multiple objects using the given client and options.

func CreateMultipleFromFile

func CreateMultipleFromFile(ctx context.Context, c client.Client, filename string, opts ...client.CreateOption) ([]unstructured.Unstructured, error)

CreateMultipleFromFile creates multiple objects by reading the given file as unstructured objects and then creating the read objects using the given client and options.

func CreateOrUseAndPatch

func CreateOrUseAndPatch(
	ctx context.Context,
	c client.Client,
	objects []client.Object,
	obj client.Object,
	matchFunc func() (bool, error),
	lessFunc func(other client.Object) (bool, error),
	mutateFunc func() error,
) (controllerutil.OperationResult, []client.Object, error)

CreateOrUseAndPatch traverses through a slice of objects and tries to find a matching object using matchFunc. If it does, the matching object is set to the object, optionally patched and returned. If multiple objects match, the winning object is the oldest. If no object matches, initFunc is called and the new object is created. mutateFunc is optional, if none is specified no mutation will happen.

func DeleteIfExists

func DeleteIfExists(ctx context.Context, c client.Client, obj client.Object, opts ...client.DeleteOption) (existed bool, err error)

DeleteIfExists deletes the given object, if it exists. It returns any non apierrors.IsNotFound error and whether the object actually existed or not.

func DeleteMultiple

func DeleteMultiple(ctx context.Context, c client.Client, objs []client.Object, opts ...client.DeleteOption) error

DeleteMultiple deletes multiple given client.Object objects using the given client.DeleteOption options.

func DeleteMultipleFromFile

func DeleteMultipleFromFile(ctx context.Context, c client.Client, filename string, opts ...client.DeleteOption) error

DeleteMultipleFromFile deletes all client.Object objects from the given file with the given client.DeleteOption options.

func DeleteMultipleIfExist

func DeleteMultipleIfExist(ctx context.Context, c client.Client, objs []client.Object, opts ...client.DeleteOption) (existed []client.Object, err error)

DeleteMultipleIfExist deletes the given objects, if they exist. It returns any non apierrors.IsNotFound error and any object that existed before issuing the delete request.

func GetMultiple

func GetMultiple(ctx context.Context, c client.Client, reqs []GetRequest) error

GetMultiple gets multiple objects using the given client. The results are written back into the given GetRequest.

func GetMultipleFromFile

func GetMultipleFromFile(ctx context.Context, c client.Client, filename string) ([]unstructured.Unstructured, error)

GetMultipleFromFile creates multiple objects by reading the given file as unstructured objects and then creating the read objects using the given client and options.

func IgnoreAlreadyExists

func IgnoreAlreadyExists(err error) error

IgnoreAlreadyExists returns nil if the given error matches apierrors.IsAlreadyExists.

func IsOlderThan

func IsOlderThan(obj client.Object) func(other client.Object) (bool, error)

IsOlderThan returns a function that determines whether an object is older than another.

func ListAndFilter

func ListAndFilter(ctx context.Context, c client.Client, list client.ObjectList, filterFunc func(object client.Object) (bool, error), opts ...client.ListOption) error

ListAndFilter is a shorthand for doing a client.Client.List followed by filtering the list's elements with the given function.

func ListAndFilterControlledBy

func ListAndFilterControlledBy(ctx context.Context, c client.Client, owner client.Object, list client.ObjectList, opts ...client.ListOption) error

ListAndFilterControlledBy is a shorthand for doing a client.List followed by filtering the list's elements using metautils.IsControlledBy.

func ObjectRefSetReferencesGetRequest

func ObjectRefSetReferencesGetRequest(scheme *runtime.Scheme, s ObjectRefSet, req GetRequest) (bool, error)

ObjectRefSetReferencesGetRequest is a utility function to determine whether an ObjectRefSet contains a GetRequest.

func ObjectRefSetReferencesObject

func ObjectRefSetReferencesObject(scheme *runtime.Scheme, s ObjectRefSet, obj client.Object) (bool, error)

ObjectRefSetReferencesObject is a utility function to determine whether an ObjectRefSet contains a client.Object.

func ObjectsFromGetRequests

func ObjectsFromGetRequests(reqs []GetRequest) []client.Object

ObjectsFromGetRequests retrieves all client.Object objects from the given slice of GetRequest.

func ObjectsFromPatchRequests

func ObjectsFromPatchRequests(reqs []PatchRequest) []client.Object

ObjectsFromPatchRequests extracts all client.Object objects from the given slice of PatchRequest.

func PatchAddFinalizer

func PatchAddFinalizer(ctx context.Context, c client.Client, obj client.Object, finalizer string) error

PatchAddFinalizer issues a patch to add the given finalizer to the given object. The client.Patch method will be called regardless whether the finalizer was already present or not.

func PatchEnsureFinalizer

func PatchEnsureFinalizer(ctx context.Context, c client.Client, obj client.Object, finalizer string) (modified bool, err error)

PatchEnsureFinalizer checks if the given object has the given finalizer and, if not, issues a patch request to add it. The modified result reports whether the object had to be modified.

func PatchEnsureNoFinalizer

func PatchEnsureNoFinalizer(ctx context.Context, c client.Client, obj client.Object, finalizer string) (modified bool, err error)

PatchEnsureNoFinalizer checks if the given object has the given finalizer and, if yes, issues a patch request to remove it. The modified result reports whether the object had to be modified.

func PatchMultiple

func PatchMultiple(ctx context.Context, c client.Client, reqs []PatchRequest, opts ...client.PatchOption) error

PatchMultiple executes multiple PatchRequest with the given client.PatchOption.

func PatchMultipleFromFile

func PatchMultipleFromFile(
	ctx context.Context,
	c client.Client,
	filename string,
	patchProvider PatchProvider,
	opts ...client.PatchOption,
) ([]unstructured.Unstructured, error)

PatchMultipleFromFile patches all objects from the given filename using the patchFor function. The returned unstructured.Unstructured objects contain the result of applying them.

func PatchRemoveFinalizer

func PatchRemoveFinalizer(ctx context.Context, c client.Client, obj client.Object, finalizer string) error

PatchRemoveFinalizer issues a patch to remove the given finalizer from the given object. The client.Patch method will be called regardless whether the finalizer was already gone or not.

func ReaderClient

func ReaderClient(r client.Reader, c client.Client) client.Client

ReaderClient returns a client.Client that uses the given client.Reader for all read operations and the client.Client for the remaining operations.

Types

type GetRequest

type GetRequest struct {
	Key    client.ObjectKey
	Object client.Object
}

GetRequest is a request to get an object with the given key and object (that is later used to write the result into).

func GetRequestFromObject

func GetRequestFromObject(obj client.Object) GetRequest

GetRequestFromObject converts the given client.Object to a GetRequest. Namespace and name should be present on the object.

func GetRequestsFromObjects

func GetRequestsFromObjects(objs []client.Object) []GetRequest

GetRequestsFromObjects converts each client.Object into a GetRequest using GetRequestFromObject.

type GetRequestSet

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

GetRequestSet is a set of GetRequest.

Internally, the objects are differentiated by either being typed or unstructured. For unstructured objects, the group version kind they supply alongside their client.ObjectKey is used as identity. For typed objects, their element type (all typed objects *have* to be pointers to structs) alongside their client.ObjectKey is used as identity. If a typed object is *not* a pointer to a struct, a panic will happen.

func NewGetRequestSet

func NewGetRequestSet(items ...GetRequest) *GetRequestSet

NewGetRequestSet creates a new set of GetRequest.

Internally, the objects are differentiated by either being typed or unstructured. For unstructured objects, the group version kind they supply alongside their client.ObjectKey is used as identity. For typed objects, their element type (all typed objects *have* to be pointers to structs) alongside their client.ObjectKey is used as identity. If a typed object is *not* a pointer to a struct, a panic will happen.

func (*GetRequestSet) Delete

func (s *GetRequestSet) Delete(items ...GetRequest)

Delete deletes the given items from the set, if they were present.

func (*GetRequestSet) Has

func (s *GetRequestSet) Has(item GetRequest) bool

Has checks if the given item is present in the set.

func (*GetRequestSet) Insert

func (s *GetRequestSet) Insert(items ...GetRequest)

Insert inserts the given items into the set.

func (*GetRequestSet) Iterate

func (s *GetRequestSet) Iterate(f func(GetRequest) (cont bool))

Iterate iterates through the get requests of this set using the given function. If the function returns true (i.e. stop), the iteration is canceled.

func (*GetRequestSet) Len

func (s *GetRequestSet) Len() int

Len returns the length of the set.

func (*GetRequestSet) List

func (s *GetRequestSet) List() []GetRequest

List returns all GetRequests of this set.

type ObjectKeySet

type ObjectKeySet map[client.ObjectKey]struct{}

ObjectKeySet set is a set of client.ObjectKey.

func NewObjectKeySet

func NewObjectKeySet(items ...client.ObjectKey) ObjectKeySet

NewObjectKeySet creates a new ObjectKeySet and initializes it with the given items.

func (ObjectKeySet) Delete

func (s ObjectKeySet) Delete(items ...client.ObjectKey)

Delete removes the given items from the ObjectKeySet. The ObjectKeySet has to be non-nil for this operation.

func (ObjectKeySet) Has

func (s ObjectKeySet) Has(item client.ObjectKey) bool

Has checks if the given item is in the set.

func (ObjectKeySet) Insert

func (s ObjectKeySet) Insert(items ...client.ObjectKey)

Insert inserts the given items into the ObjectKeySet. The ObjectKeySet has to be non-nil for this operation.

func (ObjectKeySet) Len

func (s ObjectKeySet) Len() int

Len returns the length of the ObjectKeySet.

type ObjectRef

type ObjectRef struct {
	GroupKind schema.GroupKind
	Key       client.ObjectKey
}

ObjectRef references an object regardless of its version.

func ObjectRefFromGetRequest

func ObjectRefFromGetRequest(scheme *runtime.Scheme, req GetRequest) (ObjectRef, error)

ObjectRefFromGetRequest creates a new ObjectRef from the given GetRequest.

func ObjectRefFromObject

func ObjectRefFromObject(scheme *runtime.Scheme, obj client.Object) (ObjectRef, error)

ObjectRefFromObject creates a new ObjectRef from the given client.Object.

func ObjectRefsFromGetRequests

func ObjectRefsFromGetRequests(scheme *runtime.Scheme, reqs []GetRequest) ([]ObjectRef, error)

ObjectRefsFromGetRequests creates a list of ObjectRef from the given list of GetRequest.

func ObjectRefsFromObjects

func ObjectRefsFromObjects(scheme *runtime.Scheme, objs []client.Object) ([]ObjectRef, error)

ObjectRefsFromObjects creates a list of ObjectRef from a list of client.Object.

type ObjectRefSet

type ObjectRefSet map[ObjectRef]struct{}

ObjectRefSet is a set of ObjectRef references.

func NewObjectRefSet

func NewObjectRefSet(items ...ObjectRef) ObjectRefSet

NewObjectRefSet creates a new ObjectRefSet with the given set.

func ObjectRefSetFromGetRequestSet

func ObjectRefSetFromGetRequestSet(scheme *runtime.Scheme, s2 *GetRequestSet) (ObjectRefSet, error)

ObjectRefSetFromGetRequestSet creates a new ObjectRefSet from the given GetRequestSet.

func ObjectRefSetFromObjects

func ObjectRefSetFromObjects(scheme *runtime.Scheme, objs []client.Object) (ObjectRefSet, error)

ObjectRefSetFromObjects creates a new ObjectRefSet from the given list of client.Object.

func (ObjectRefSet) Delete

func (s ObjectRefSet) Delete(items ...ObjectRef)

Delete deletes the given items from the set, if present.

func (ObjectRefSet) Has

func (s ObjectRefSet) Has(item ObjectRef) bool

Has checks if the given item is present in the set.

func (ObjectRefSet) Insert

func (s ObjectRefSet) Insert(items ...ObjectRef)

Insert inserts the given items into the set.

func (ObjectRefSet) Len

func (s ObjectRefSet) Len() int

Len returns the length of the set.

type PatchProvider

type PatchProvider interface {
	PatchFor(obj client.Object) client.Patch
}

PatchProvider retrieves a patch for any given object.

type PatchRequest

type PatchRequest struct {
	Object client.Object
	Patch  client.Patch
}

PatchRequest is the request to patch an object with a patch.

func PatchRequestFromObjectAndProvider

func PatchRequestFromObjectAndProvider(obj client.Object, provider PatchProvider) PatchRequest

PatchRequestFromObjectAndProvider is a shorthand to create a PatchRequest using a client.Object and PatchProvider.

func PatchRequestsFromObjectsAndProvider

func PatchRequestsFromObjectsAndProvider(objs []client.Object, provider PatchProvider) []PatchRequest

PatchRequestsFromObjectsAndProvider converts all client.Object objects to PatchRequest using PatchRequestFromObjectAndProvider.

type SharedFieldIndexer

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

SharedFieldIndexer allows registering and calling field index functions shared by different users.

func NewSharedFieldIndexer

func NewSharedFieldIndexer(indexer client.FieldIndexer, scheme *runtime.Scheme) *SharedFieldIndexer

NewSharedFieldIndexer creates a new SharedFieldIndexer.

func (*SharedFieldIndexer) IndexField

func (s *SharedFieldIndexer) IndexField(ctx context.Context, obj client.Object, field string) error

IndexField calls a registered client.IndexerFunc for the given client.Object and field. If the object / field is unknown or its GVK could not be determined, it errors.

func (*SharedFieldIndexer) MustRegister

func (s *SharedFieldIndexer) MustRegister(obj client.Object, field string, extractValue client.IndexerFunc)

MustRegister registers the client.IndexerFunc for the given client.Object and field.

func (*SharedFieldIndexer) Register

func (s *SharedFieldIndexer) Register(obj client.Object, field string, extractValue client.IndexerFunc) error

Register registers the client.IndexerFunc for the given client.Object and field.

Jump to

Keyboard shortcuts

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