util

package
v1.5.5 Latest Latest
Warning

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

Go to latest
Published: Sep 19, 2024 License: Apache-2.0 Imports: 32 Imported by: 9

Documentation

Index

Constants

View Source
const (
	ProtectionEventPub                = "PodUnavailableBudget"
	ProtectionEventDeletionProtection = "DeletionProtection"
)

Variables

View Source
var GlobalCache = cache.NewStore(func(obj interface{}) (string, error) {
	metaObj, ok := obj.(metav1.Object)
	if !ok {
		return "", fmt.Errorf("failed to convert obj to metav1.Object")
	}
	namespacedName := fmt.Sprintf("%s/%s", metaObj.GetNamespace(), metaObj.GetName())

	runtimeObj, ok := obj.(runtime.Object)
	if !ok {
		return "", fmt.Errorf("failed to convert obj to runtime.Object")
	}
	key := fmt.Sprintf("%v/%s", runtimeObj.GetObjectKind().GroupVersionKind(), namespacedName)
	return key, nil
})

GlobalCache using GVK/namespace/name as key

View Source
var GlobalKeyedMutex = &KeyedMutex{}

Functions

func CalculatePartitionReplicas added in v1.2.0

func CalculatePartitionReplicas(partition *intstrutil.IntOrString, replicasPointer *int32) (int, error)

CalculatePartitionReplicas returns absolute value of partition for workload. This func can solve some corner cases about percentage-type partition, such as: - if partition > "0%" and replicas > 0, we will ensure at least 1 old pod is reserved. - if partition < "100%" and replicas > 1, we will ensure at least 1 pod is upgraded.

func CheckDuplicate added in v0.3.1

func CheckDuplicate(list []string) []string

CheckDuplicate finds if there are duplicated items in a list.

func ContainsObjectRef added in v1.0.1

func ContainsObjectRef(slice []v1.ObjectReference, obj v1.ObjectReference) bool

func DiffPods added in v0.9.0

func DiffPods(pods1, pods2 []*v1.Pod) (ret []*v1.Pod)

DiffPods returns pods in pods1 but not in pods2

func DumpJSON

func DumpJSON(o interface{}) string

DumpJSON returns the JSON encoding

func GetCondition added in v1.2.0

func GetCondition(pod *v1.Pod, cType v1.PodConditionType) *v1.PodCondition

func GetContainer added in v0.8.0

func GetContainer(name string, pod *v1.Pod) *v1.Container

func GetContainerEnvValue added in v0.8.0

func GetContainerEnvValue(container *v1.Container, key string) string

func GetContainerEnvVar added in v0.8.0

func GetContainerEnvVar(container *v1.Container, key string) *v1.EnvVar

func GetContainerStatus added in v0.9.0

func GetContainerStatus(name string, pod *v1.Pod) *v1.ContainerStatus

func GetContainerVolumeMount added in v0.8.0

func GetContainerVolumeMount(container *v1.Container, key string) *v1.VolumeMount

func GetControllerCacheSyncTimeout added in v1.5.0

func GetControllerCacheSyncTimeout() time.Duration

func GetIntOrStrPointer added in v0.7.0

func GetIntOrStrPointer(i intstrutil.IntOrString) *intstrutil.IntOrString

func GetKruiseDaemonConfigNamespace added in v1.4.2

func GetKruiseDaemonConfigNamespace() string

func GetKruiseNamespace added in v1.3.0

func GetKruiseNamespace() string

func GetPodContainerImageIDs added in v1.0.1

func GetPodContainerImageIDs(pod *v1.Pod) map[string]string

func GetPodNames added in v0.6.1

func GetPodNames(pods []*v1.Pod) sets.String

GetPodNames returns names of the given Pods array

func GetPodVolume added in v0.8.0

func GetPodVolume(pod *v1.Pod, volumeName string) *v1.Volume

func GetScaledValueFromIntOrPercent added in v1.4.0

func GetScaledValueFromIntOrPercent(intOrPercent *intstrutil.IntOrString, total int, roundUp bool) (int, error)

func HasOwnerRef added in v1.4.0

func HasOwnerRef(target, owner metav1.Object) bool

func InitProtectionLogger added in v1.5.1

func InitProtectionLogger() error

func InjectReadinessGateToPod added in v0.9.0

func InjectReadinessGateToPod(pod *v1.Pod, conditionType v1.PodConditionType)

func IntAbs added in v0.9.0

func IntAbs(i int) int

IntAbs returns the abs number of the given int number

func IsContainerImageEqual added in v0.8.0

func IsContainerImageEqual(image1, image2 string) bool

IsContainerImageEqual indicates whether container images are equal 1. image1, image2 are digest image, compare repo+digest 2. image1, image2 are normal image, compare repo+tag 3. image1, image2 are digest+normal image, don't support compare it, return false

func IsImageDigest added in v0.8.0

func IsImageDigest(image string) bool

IsImageDigest indicates whether image is digest format, for example: docker.io/busybox@sha256:a9286defaba7b3a519d585ba0e37d0b2cbee74ebfe590960b0b1d6a5e97d1e1d

func IsIntPlusAndMinus added in v0.9.0

func IsIntPlusAndMinus(i, j int) bool

func IsJSONObjectEqual added in v0.9.0

func IsJSONObjectEqual(o1, o2 interface{}) bool

IsJSONObjectEqual checks if two objects are equal after encoding json

func IsPodContainerDigestEqual added in v0.8.0

func IsPodContainerDigestEqual(containers sets.String, pod *v1.Pod) bool

func IsPodOwnedByKruise added in v0.9.0

func IsPodOwnedByKruise(pod *v1.Pod) bool

func IsReferenceEqual added in v1.2.0

func IsReferenceEqual(ref1, ref2 appsv1alpha1.TargetReference) bool

IsReferenceEqual checks APIVersion, Kind, Name

func IsRunningAndReady added in v0.8.0

func IsRunningAndReady(pod *v1.Pod) bool

func IsSelectorLooseOverlap added in v0.10.0

func IsSelectorLooseOverlap(selector1, selector2 *metav1.LabelSelector) bool

IsSelectorLooseOverlap indicates whether selectors overlap (indicates that selector1, selector2 have same key, and there is an certain intersection) 1. when selector1、selector2 don't have same key, it is considered non-overlap, e.g. selector1(a=b) and selector2(c=d) 2. when selector1、selector2 have same key, and matchLabels & matchExps are intersection, it is considered overlap. For examples:

a In [b,c]    And a Exist
                  a In [b,...] [c,...] [Include any b,c,...]
                  a NotIn [a,...] [b,....] [c,....] [All other cases are allowed except for the inclusion of both b,c...] [b,c,e]
a Exist       And a Exist
                  a In [x,y,Any,...]
                  a NotIn [a,b,Any...]
a NotIn [b,c] And a Exist
                  a NotExist
                  a NotIn [a,b,Any...]
                  a In [a,b] [a,c] [e,f] [Any,...] other than [b],[c],[b,c]
a NotExist    And a NotExist
                  a NotIn [Any,...]
When selector1 and selector2 contain the same key, except for the above case, they are considered non-overlap

func IsSelectorOverlapping added in v0.8.0

func IsSelectorOverlapping(selector1, selector2 *metav1.LabelSelector) bool

IsSelectorOverlapping indicates whether selector overlaps, the criteria: if exist one same key has different value and not overlap, then it is judged non-overlap, for examples:

  • a=b and a=c
  • a in [b,c] and a not in [b,c...]
  • a not in [b] and a not exist
  • a=b,c=d,e=f and a=x,c=d,e=f

then others is overlap:

  • a=b and c=d

func LoggerProtectionInfo added in v1.5.1

func LoggerProtectionInfo(event, kind, ns, name, userAgent string)

func MergeEnvVar added in v0.8.0

func MergeEnvVar(original []v1.EnvVar, additional []v1.EnvVar) []v1.EnvVar

func MergePods added in v0.6.1

func MergePods(pods1, pods2 []*v1.Pod) []*v1.Pod

MergePods merges two pods arrays

func MergeVolumeMounts added in v0.8.0

func MergeVolumeMounts(original, additional []v1.VolumeMount) []v1.VolumeMount

func MergeVolumeMountsInContainer added in v0.8.0

func MergeVolumeMountsInContainer(origin *v1.Container, other v1.Container)

func MergeVolumes added in v0.8.0

func MergeVolumes(original []v1.Volume, additional []v1.Volume) []v1.Volume

func ParseImage added in v0.8.0

func ParseImage(image string) (repo, tag, digest string, err error)

parse container images, 1. docker.io/busybox@sha256:a9286defaba7b3a519d585ba0e37d0b2cbee74ebfe590960b0b1d6a5e97d1e1d repo=docker.io/busybox, tag="", digest=sha256:a9286defaba7b3a519d585ba0e37d0b2cbee74ebfe590960b0b1d6a5e97d1e1d 2. docker.io/busybox:latest repo=docker.io/busybox, tag=latest, digest=""

func RemoveOwnerRef added in v1.4.0

func RemoveOwnerRef(target, owner metav1.Object) bool

func SetControllerCacheSyncTimeout added in v1.5.0

func SetControllerCacheSyncTimeout(t time.Duration)

func SetOwnerRef added in v1.4.0

func SetOwnerRef(target, owner metav1.Object, gvk schema.GroupVersionKind) bool

func SetPodCondition added in v1.2.0

func SetPodCondition(pod *v1.Pod, condition v1.PodCondition)

func SetPodConditionIfMsgChanged added in v1.5.2

func SetPodConditionIfMsgChanged(pod *v1.Pod, condition v1.PodCondition)

func SetPodReadyCondition added in v1.2.0

func SetPodReadyCondition(pod *v1.Pod)

func SlowStartBatch added in v0.3.0

func SlowStartBatch(count int, initialBatchSize int, fn func(index int) error) (int, error)

SlowStartBatch tries to call the provided function a total of 'count' times, starting slow to check for errors, then speeding up if calls succeed.

It groups the calls into batches, starting with a group of initialBatchSize. Within each batch, it may call the function multiple times concurrently with its index.

If a whole batch succeeds, the next batch may get exponentially larger. If there are any failures in a batch, all remaining batches are skipped after waiting for the current batch to complete.

It returns the number of successful calls to the function.

func UpdateFinalizer added in v1.3.0

func UpdateFinalizer(c client.Client, object client.Object, op FinalizerOpType, finalizer string) error

func ValidatedLabelSelectorAsSelector added in v1.3.0

func ValidatedLabelSelectorAsSelector(ps *metav1.LabelSelector) (labels.Selector, error)

Types

type FinalizerOpType added in v1.3.0

type FinalizerOpType string
const (
	AddFinalizerOpType    FinalizerOpType = "Add"
	RemoveFinalizerOpType FinalizerOpType = "Remove"
)

type KeyedMutex added in v0.10.1

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

func (*KeyedMutex) Lock added in v0.10.1

func (m *KeyedMutex) Lock(key string) func()

func (*KeyedMutex) Unlock added in v0.10.1

func (m *KeyedMutex) Unlock(key string)

type ProtectionLoggerInfo added in v1.5.1

type ProtectionLoggerInfo struct {
	// PUB, ProtectionDeletion
	Event     string
	Kind      string
	Namespace string
	Name      string
	UserAgent string
}

Jump to

Keyboard shortcuts

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