util

package
v0.0.0-...-7b107d7 Latest Latest
Warning

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

Go to latest
Published: Apr 26, 2024 License: Apache-2.0 Imports: 35 Imported by: 1

Documentation

Index

Examples

Constants

This section is empty.

Variables

View Source
var NopNotifier = nopNotifier{}

NopNotifier takes no action when notified.

Functions

func AppendLogToError

func AppendLogToError(err error, log string) error

func Contains

func Contains[T comparable](s []T, x T) bool

Contains performs a linear search on `s` looking for `x`.

func CopyMap

func CopyMap[K comparable, V any](m map[K]V) map[K]V

CopyMap creates a new map copying all key/values from its argument

func CreateOrRestartPod

func CreateOrRestartPod(ctx context.Context, podClient ctrlruntimeclient.Client, pod *corev1.Pod) (*corev1.Pod, error)

func CreateRBACs

func CreateRBACs(ctx context.Context, sa *coreapi.ServiceAccount, role *rbacapi.Role, roleBindings []rbacapi.RoleBinding, client ctrlruntimeclient.Client, retryDuration, timeout time.Duration) error

CreateRBACs creates the given service account, role, and role bindings. In addition, it waits until the service account will be updated with an image pull secret. Because the DockerCfgController controller needs some time to create the corresponding secrets we need to wait until this happens, otherwise, the pod that will use this service account will fail to start since there are no credentials to pull any images.

func IsBitSet

func IsBitSet[T constraints.Integer](x, b T) bool

IsBitSet tests if a single specific bit is set in a value

func IsSpecialInformingJobOnTestGrid

func IsSpecialInformingJobOnTestGrid(jobName string) bool

func Keys

func Keys[K comparable, V any](m map[K]V) (ret []K)

Keys returns a slice with the keys in `m` in unspecified order

func LoadClusterConfig

func LoadClusterConfig() (*rest.Config, error)

func LoadKubeConfig

func LoadKubeConfig(path string) (*rest.Config, error)

LoadKubeConfig loads a kubeconfig from the file and uses the default context

func ParseImageStreamTagReference

func ParseImageStreamTagReference(s string) (api.ImageStreamTagReference, error)

ParseImageStreamTagReference creates a reference from an "is:tag" string.

func PendingBuildError

func PendingBuildError(ctx context.Context, client kubernetes.PodClient, build *buildapi.Build) (ret error)

PendingBuildError fetches scheduling errors from the build pod's events

func PopCount

func PopCount[T comparable](xs ...T) (ret uint)

PopCount returns the number of "true" argument values.

func ProduceMap

func ProduceMap(
	n int,
	produce WorkerFn,
	map_ WorkerFn,
	errCh chan error,
) error

ProduceMap is similar to ProduceMapReduce but without a reduce step.

func ProduceMapReduce

func ProduceMapReduce(
	n int,
	produce WorkerFn,
	map_ WorkerFn,
	reduce WorkerFn,
	done func(),
	errCh chan error,
) error

ProduceMapReduce handles the execution of a triphasic pipeline. The pipeline is constituted of a single producer, a single reducer, and `n` mapper workers, i.e.:

  producer
 / /   \ \
m m  …  m m ---> done
 \ \   / /        |
  reducer <-------'

All processes are executed to completion even if an error occurs in any of them (as opposed to, for example, `x/sync/errorgroup`). Errors sent to `errCh` are aggregated in the return value. A process which encounters an error can either return it to stop itself or send it to `errCh` and continue processing its input.

For convenience, if `n` is zero, it is treated as `runtime.GOMAXPROCS(0)`.

`done` is called when all mapping workers finish. It usually closes a channel used by the reduce step.

Example
input := []int{0, 1, 2, 3}
inputCh := make(chan int)
errCh := make(chan error)
produce := func() error {
	defer close(inputCh)
	for _, x := range input {
		inputCh <- x
	}
	return nil
}
outputCh := make(chan int)
map_ := func() error {
	for x := range inputCh {
		if x == 2 {
			errCh <- fmt.Errorf("%d", x)
		} else {
			outputCh <- x
		}
	}
	return nil
}
done := func() { close(outputCh) }
reduce := func() error {
	r := 0
	for x := range outputCh {
		r += x
	}
	fmt.Println(r)
	return nil
}
if err := ProduceMapReduce(0, produce, map_, reduce, done, errCh); err != nil {
	fmt.Println("error:", err)
}
Output:

4
error: 2

func RemoveIf

func RemoveIf[T any](s []T, p func(T) bool) []T

RemoveIf retains only elements which are not selected by a predicate The slice is modified in place and (potentially a subset) is returned.

func ResolvePullSpec

func ResolvePullSpec(is *imageapi.ImageStream, tag string, requireExact bool) (string, bool, imageapi.TagEventCondition)

ResolvePullSpec if a tag of an imagestream is resolved

func SecretFromDir

func SecretFromDir(path string) (*coreapi.Secret, error)

SecretFromDir creates a secret with the contents of files in a directory.

func SortSlice

func SortSlice[T constraints.Ordered](s []T)

SortSlice is a generic version of sort.Slice

func SortedKeys

func SortedKeys[K constraints.Ordered, V any](m map[K]V) (ret []K)

SortedKeys returns a slice with the keys in `m` in sorted order

func UpsertImmutableSecret

func UpsertImmutableSecret(ctx context.Context, client ctrlruntimeclient.Client, secret *coreapi.Secret) (created bool, err error)

UpsertImmutableSecret adds new values to an existing secret. New values are added, existing values are overwritten. The secret will be created if it doesn't already exist. Updating an existing secret happens by re-creating it.

func WaitForPodCompletion

func WaitForPodCompletion(ctx context.Context, podClient kubernetes.PodClient, namespace, name string, notifier ContainerNotifier, flags WaitForPodFlag) (*corev1.Pod, error)

func WaitForPodDeletion

func WaitForPodDeletion(ctx context.Context, podClient ctrlruntimeclient.Client, namespace, name string, uid types.UID) error

func Zero

func Zero[T any]() (_ T)

Zero returns the zero value of a type

Types

type ContainerNotifier

type ContainerNotifier interface {
	// Notify indicates that the provided container name has transitioned to an appropriate state and
	// any per container actions should be taken.
	Notify(pod *corev1.Pod, containerName string)
	// Complete indicates the specified pod has completed execution, been deleted, or that no further
	// Notify() calls can be made.
	Complete(podName string)
	// Done returns a channel that can be used to wait for the specified pod name to complete the work it has pending.
	Done(podName string) <-chan struct{}
}

ContainerNotifier receives updates about the status of a poll action on a pod. The caller is required to define what notifications are made.

type WaitForPodFlag

type WaitForPodFlag uint8

WaitForPodFlag changes the behavior of the functions which monitor pods

const (
	// SkipLogs omits informational logs, such as when the pod is part of a
	// larger step like release creation where displaying pod specific info is
	// confusing to an end user. Failure logs are still printed.
	SkipLogs WaitForPodFlag = 1 << iota
	// Interruptible indicates this pod is expected to potentially be cancelled
	// Used for observer pods so that their cancellation is not reported as
	// abnormal.
	Interruptible
)

type WorkerFn

type WorkerFn func() error

Directories

Path Synopsis
Package imagestreamtagwrapper implements a wrapper for a ctrlruntimeclient that assembles imagestreamtags client-side by fetching the corresponding imagestream and image(s).
Package imagestreamtagwrapper implements a wrapper for a ctrlruntimeclient that assembles imagestreamtags client-side by fetching the corresponding imagestream and image(s).

Jump to

Keyboard shortcuts

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