utils

package
v0.1.13 Latest Latest
Warning

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

Go to latest
Published: Jul 19, 2023 License: Apache-2.0 Imports: 31 Imported by: 0

Documentation

Index

Constants

View Source
const LabelSeparator = "_"

LabelSeparator to use instead of types.Separator.

Variables

View Source
var (
	ErrHoldFinalizer  = errors.New("hold finalizer")
	ErrSAHasNoSecrets = errors.New("service account has no secrets")
)

ErrHoldFinalizer indicates that the finalizer should be held.

Functions

func BuildLabelSelector

func BuildLabelSelector(requirements ...LabelSelectorRequirement) (labels.Selector, error)

BuildLabelSelector builds a label selector or returns an error if any of the requirements are invalid.

func ClientConfigFromServiceAccountKey

func ClientConfigFromServiceAccountKey(
	ctx context.Context,
	log logr.Logger,
	k8sClient client.Client,
	accountKey client.ObjectKey,
	externalKubernetesAddress string,
) (*clientcmdapi.Config, error)

ClientConfigFromServiceAccountKey fetches the service account secret and calls ClientConfigFromServiceAccountSecret.

func ClientConfigFromServiceAccountSecret

func ClientConfigFromServiceAccountSecret(secret *corev1.Secret, kubernetesAddress string) (*clientcmdapi.Config, error)

ClientConfigFromServiceAccountSecret uses the secret and address to construct a clientconfig that can be used to create a KUBECONFIG.

func ClientFromKubeconfig

func ClientFromKubeconfig(kubeconfig []byte, opts client.Options) (client.Client, error)

func CreateMetrics

func CreateMetrics(namespace string) *prometheus.HistogramVec

CreateMetrics creates a new Kubernetes Histogram metric and registers it in the given RegistererGatherer using given namespace. It is safe to call that method multiple times, the metric will be created only once. Subsequent calls returns the same metric.

func CreateOrUpdateContainer

func CreateOrUpdateContainer(containers []corev1.Container, name string, updateFn func(c *corev1.Container) error) ([]corev1.Container, error)

CreateOrUpdateContainer either creates or finds a container with the given name. The desired state of that container should be reconciled inside updateFn. The updated container slice is returned.

func CreateOrUpdateMonitor

func CreateOrUpdateMonitor(monitors []v1alpha1.Uptrends, name string, updateFn func(m *v1alpha1.Uptrends) error) ([]v1alpha1.Uptrends, error)

CreateOrUpdateMonitor creates or updates a monitor for the given object.

func DefaultNewClientWithMetrics

func DefaultNewClientWithMetrics(
	cache cache.Cache, config *rest.Config, options client.Options, uncachedObjects ...client.Object,
) (client.Client, error)

DefaultNewClientWithMetrics supports read and list caching and enables monitoring measured by ignored cache requests. This method is used by a k8s manager.

func EnqueueRequestFromNameLabel

func EnqueueRequestFromNameLabel(label string, namespace ...string) handler.EventHandler

EnqueueRequestFromNameLabel determines the reconcile request name based on the value of the specified label. If a namespace is specified, that namespace will be used instead of the objects namespace. This is useful if you want to reconcile based on changes without using an OwnerReference.

func EnsureFinalizer

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

EnsureFinalizer appends the given finalizer. If it did not exist before the object is updated using the client.

func EnsureNoFinalizer

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

EnsureNoFinalizer removes the given finalizer. If it existed the object is updated using the client.

func EnsureResourceDeletion

func EnsureResourceDeletion(ctx context.Context, clusterClient client.Client, resourcesToDelete ...client.Object) error

EnsureResourceDeletion deletes all resources and returns an error if not all could be deleted.

func EnsureResourceGone

func EnsureResourceGone(ctx context.Context, k8sClient GetterDeleter, obj client.Object) error

EnsureResourceGone calls Delete() only of the object still exists, so it is safe to use during reconciliation, as it will not cause additional load on the API Server.

func FetchObject

func FetchObject(ctx context.Context, client client.Client, namespace string, name string, obj client.Object) error

FetchObject ...

func FindContainer

func FindContainer(containers []corev1.Container, name string) (*corev1.Container, bool)

FindContainer finds a container by name. Second return value will be false, if no matching container was found.

func FindMonitor

func FindMonitor(monitors []v1alpha1.Uptrends, name string) (*v1alpha1.Uptrends, bool)

FindMonitor finds a monitor by name.

func GetK8sNamespace

func GetK8sNamespace(fallback string) (string, error)

GetK8sNamespace returns the namespace the process is running in, when actually running in-cluster. Returns fallback otherwise.

func GetKind

func GetKind(obj runtime.Object) string

GetKind returns the Kind of the kubernetes object. If the kind is not set (e.g. during tests without envtest), it returns the struct name, which should usually match the kind.

func HasFinalizer

func HasFinalizer(obj metav1.Object, finalizer string) bool

HasFinalizer returns true if the given finalizer is present on the object.

func IsControlledBy

func IsControlledBy(obj metav1.Object, owner metav1.Object) bool

IsControlledBy checks if the object has a controller ref set to the given owner.

func IsObjectFound

func IsObjectFound(ctx context.Context, client client.Client, namespace string, name string, obj client.Object) bool

IsObjectFound ...

func IsOwnedBy

func IsOwnedBy(obj metav1.Object, owner metav1.Object) bool

IsOwnedBy checks if the object has a owner ref set to the given owner.

func LabelToNamespacedName

func LabelToNamespacedName(l string) (types.NamespacedName, error)

LabelToNamespacedName reads from a label-encoded name ("namespace_resource") and parses the namespaced name.

func MergeEnvVars

func MergeEnvVars(oldEnvs []corev1.EnvVar, newEnvs ...corev1.EnvVar) []corev1.EnvVar

MergeEnvVars returns a new slice of EnvVar after merging all the new envvars. If a envvar with the same name already existed it will be overwritten, otherwise it will be appended.

func MergeTaints

func MergeTaints(oldTaints []corev1.Taint, newTaints ...corev1.Taint) []corev1.Taint

MergeTaints returns a new list of taints after merging all the new taints. If a taint with the same key already existed it will be overwritten, otherwise it will be appended.

func MetaObjectToObjectKey

func MetaObjectToObjectKey(meta metav1.Object) client.ObjectKey

MetaObjectToObjectKey returns a new client.ObjectKey for the meta object.

func NamespacedNameToLabel

func NamespacedNameToLabel(nn types.NamespacedName) string

NamespacedNameToLabel encodes a namespacedname into a label-compliant string ("namespace_resource").

func NewClient

func NewClient(client client.Client) client.Client

NewClient creates a new Kubernetes Client that monitors Kubernetes operations.

func NodeAffinityFromSelector

func NodeAffinityFromSelector(nodeSelector map[string]string) *corev1.NodeAffinity

NodeAffinityFromSelector converts a simple nodeSelector to a nodeAffinity, where the terms are ANDed.

func ObjectKeyToObjectMeta

func ObjectKeyToObjectMeta(key client.ObjectKey) metav1.ObjectMeta

ObjectKeyToObjectMeta returns a new metav1.ObjectMeta for the object key.

func ParseNamespacedNamed

func ParseNamespacedNamed(nn string) (types.NamespacedName, error)

ParseNamespacedNamed turns "namespace/resource" into a NamespacedName.

func RESTConfigFromServiceAccountSecret

func RESTConfigFromServiceAccountSecret(secret *corev1.Secret, kubernetesAddress string) (*rest.Config, error)

RESTConfigFromServiceAccountSecret constructs a rest config using the secret and host provided.

func RemoveOwnerReference

func RemoveOwnerReference(ctx context.Context, c client.Client, obj client.Object, owner metav1.Object) error

RemoveOwnerReference removes the given owner reference from the given object.

func SetAnnotation

func SetAnnotation(obj metav1.Object, key, value string)

SetAnnotation is a helper function that sets the given annotation and creates the annotation map if it does not exist yet.

func SetInheritedLabels

func SetInheritedLabels(obj metav1.Object, parent metav1.Object, keys ...string)

SetInheritedLabels sets the labels from the parent if they exist, overwriting labels that existed before.

func SetLabel

func SetLabel(obj metav1.Object, key, value string)

SetLabel is a helper function that sets the given label and creates the labels map if it does not exist yet.

func SkipReconcile

func SkipReconcile(obj client.Object) bool

SkipReconcile returns true if reconciliation should be temporarily skipped for this resource.

func SortTypedLocalObjectReference

func SortTypedLocalObjectReference(refs []corev1.TypedLocalObjectReference)

SortTypedLocalObjectReference into a stable order.

func UpdateSecretData

func UpdateSecretData(secret *corev1.Secret, data map[string]interface{})

UpdateSecretData writes the provided fields in the Data field of the struct. Provided values should either be []byte or string.

func UpdateStatus

func UpdateStatus(ctx context.Context, c client.Client, obj client.Object) error

UpdateStatus updates the status of the object, only if the status sub-resource has changed.

func UpdateStatusWithResult

func UpdateStatusWithResult(ctx context.Context, c client.Client, obj client.Object) (controllerutil.OperationResult, error)

UpdateStatusWithResult updates the status of the object, only if the status sub-resource has changed, and returns whether it was changed or not.

func WithFinalizer

func WithFinalizer(ctx context.Context, c client.Client, obj client.Object, finalizer string, reconcile, delete func() (ctrl.Result, error)) (ctrl.Result, error)

WithFinalizer ensures that finalizer is on obj until obj is deleted. Delete is called when obj is deleted. The finalizer is removed if delete return no error.

Types

type Deleter

type Deleter interface {
	Delete(ctx context.Context, obj client.Object, opts ...client.DeleteOption) error
}

Deleter can delete resources from kubernetes.

type GetterDeleter

type GetterDeleter interface {
	Deleter
	Get(ctx context.Context, key client.ObjectKey, obj client.Object, opts ...client.GetOption) error
}

GetterDeleter can get and delete resources from kubernetes.

type LabelSelectorRequirement

type LabelSelectorRequirement struct {
	Key  string
	Op   selection.Operator
	Vals []string
}

LabelSelectorRequirement is an unchecked representation of labels.Requirements.

func NewRequirement

func NewRequirement(key string, op selection.Operator, vals []string) LabelSelectorRequirement

NewRequirement creates a new requirement to be used for BuildLabelSelector.

Jump to

Keyboard shortcuts

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