util

package
v1.2.3 Latest Latest
Warning

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

Go to latest
Published: Apr 22, 2024 License: Apache-2.0, BSD-2-Clause, MIT, + 1 more Imports: 25 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AlwaysRetry

func AlwaysRetry(_ error) bool

AlwaysRetry always returns true irrespective of the error passed.

func CreateClientFromKubeConfigBytes

func CreateClientFromKubeConfigBytes(kubeConfigBytes []byte, connectionTimeout time.Duration) (kubernetes.Interface, error)

CreateClientFromKubeConfigBytes creates a client to connect to the Kube ApiServer using the kubeConfigBytes passed as a parameter It will also set a connection timeout and will disable KeepAlive.

func CreateClientSetFromRestConfig

func CreateClientSetFromRestConfig(config *rest.Config) (*kubernetes.Clientset, error)

CreateClientSetFromRestConfig creates a kubernetes.Clientset from rest.Config.

func CreateScalesGetter

func CreateScalesGetter(config *rest.Config) (scale.ScalesGetter, error)

CreateScalesGetter Creates a new ScalesGetter given the config

func EqualOrBeforeNow added in v1.2.0

func EqualOrBeforeNow(expiryTime time.Time) bool

EqualOrBeforeNow returns false if the argument passed is after the current time.

func GetKubeConfigFromSecret

func GetKubeConfigFromSecret(ctx context.Context, namespace, secretName string, client client.Client, logger logr.Logger) ([]byte, error)

GetKubeConfigFromSecret extracts kubeconfig from a k8s secret with name secretName in namespace

func GetResourceAnnotations

func GetResourceAnnotations(ctx context.Context, client client.Client, namespace string, resourceRef *autoscalingv1.CrossVersionObjectReference) (map[string]string, error)

GetResourceAnnotations gets the annotations for a resource identified by resourceRef withing the given namespace.

func GetResourceReadyReplicas

func GetResourceReadyReplicas(ctx context.Context, cli client.Client, namespace string, resourceRef *autoscalingv1.CrossVersionObjectReference) (int32, error)

GetResourceReadyReplicas gets spec.replicas for any resource identified via resourceRef withing the given namespace. It is an error if there is no spec.replicas or if there is an error fetching the resource.

func GetScaleResource

GetScaleResource returns a kubernetes scale subresource.

func GetValOrDefault added in v1.2.0

func GetValOrDefault[T any](val *T, defaultVal T) *T

GetValOrDefault assigns the default value if the pointer is nil

func PatchResourceAnnotations

func PatchResourceAnnotations(ctx context.Context, cl client.Client, namespace string, resourceRef *autoscalingv1.CrossVersionObjectReference, patchBytes []byte) error

PatchResourceAnnotations patches the resource annotation with patchBytes. It uses StrategicMergePatchType strategy so the consumers should only provide changes to the annotations.

func ReadAndUnmarshall

func ReadAndUnmarshall[T any](filename string) (*T, error)

ReadAndUnmarshall reads file and Unmarshall the contents in a generic type

func RetryOnError

func RetryOnError(ctx context.Context, logger logr.Logger, operation string, retriableFn func() error, interval time.Duration)

RetryOnError retries invoking a function till either the invocation of the function does not return an error or the context has timed-out or has been cancelled. The consumers should ensure that the context passed to it has a proper finite timeout set as there is no other timeout taken as a function argument.

func RetryUntilPredicate

func RetryUntilPredicate(ctx context.Context, logger logr.Logger, operation string, predicateFn func() bool, timeout time.Duration, interval time.Duration) bool

RetryUntilPredicate retries an operation with a given `interval` until one of the following condition is met: 1. `predicateFn` returns true. 2. `timeout` expires. 3. `ctx` (context) is cancelled or expires. Returns true if the invocation of the `predicateFn` was successful and false otherwise.

func SleepWithContext

func SleepWithContext(ctx context.Context, sleepFor time.Duration) error

SleepWithContext sleeps until sleepFor duration has expired or the context has been cancelled.

Types

type RetryResult

type RetryResult[T any] struct {
	Value T
	Err   error
}

RetryResult captures the result of a retriable operation.

func Retry

func Retry[T any](ctx context.Context, logger logr.Logger, operation string, fn func() (T, error), numAttempts int, backOff time.Duration, canRetry func(error) bool) RetryResult[T]

Retry retries an operation `fn`, `numAttempts` number of times with a given `backOff` until one of the conditions is met: 1. Invocation of `fn` succeeds. 2. `canRetry` returns false. 3. `numAttempts` have exhausted. 4. `ctx` (context) has either been cancelled or it has expired. The result is captured eventually in `RetryResult`.

type Validator

type Validator struct {
	Error error
}

Validator is a struct to store all validation errors.

func (*Validator) MustNotBeEmpty

func (v *Validator) MustNotBeEmpty(key string, value interface{}) bool

MustNotBeEmpty checks whether the given value is empty. It returns false if it is empty or nil.

func (*Validator) MustNotBeNil

func (v *Validator) MustNotBeNil(key string, value interface{}) bool

MustNotBeNil checks whether the given value is nil and returns false if it is nil.

func (*Validator) MustNotBeZeroDuration added in v1.2.0

func (v *Validator) MustNotBeZeroDuration(key string, duration metav1.Duration) bool

MustNotBeZeroDuration checks whether the given duration is zero. It returns false if it is zero.

func (*Validator) ResourceRefMustBeValid

func (v *Validator) ResourceRefMustBeValid(resourceRef *autoscalingv1.CrossVersionObjectReference, scheme *runtime.Scheme) bool

ResourceRefMustBeValid validates the given resourceRef by parsing the apiVersion.

Jump to

Keyboard shortcuts

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