wait

package
v2.2.2 Latest Latest
Warning

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

Go to latest
Published: Dec 15, 2025 License: Apache-2.0 Imports: 20 Imported by: 4

Documentation

Overview

package wait provides functions to help with waiting for certain conditions to be true.

A `For` function is provided that can handle polling a given `WaitCondition` until it results in true or errors (either through a problem or a timeout condition).

A collection of conditions are also included that can be used with either the provided `For` function or or with the `Eventually` function from Gomega

Example using `For` with the `IsClusterReadyCondition` condition

err := wait.For(
	wait.IsClusterReadyCondition(ctx, f.MC(), clusterName, namespace, &clientPtr),
	wait.WithContext(ctx),
	wait.WithInterval(10*time.Second),
)
if err != nil {
	return nil, err
}

Example using Gomega's `Eventually` with the `AreNumNodesReady` condition

Eventually(
	wait.AreNumNodesReady(ctx, client, 3, &cr.MatchingLabels{"node-role.kubernetes.io/control-plane": ""}),
	20*time.Minute,
	30*time.Second,
).Should(BeTrue())

The WaitCondition functions return a success boolean and an error. The polling of the condition will continue until one of three things occurs:

  1. The success boolean is returned as `true`
  2. An error is returned from the WaitCondition function
  3. A timeout occurs, resulting in an error being returned

Additionally IsClusterReadyCondition accepts a pointer to a `client.Client` which will be set to a working workload cluster client once the condition is met. This allows the caller to use the client directly after the condition is met without needing to re-create the client.

Index

Constants

View Source
const (
	// DefaultTimeout is the default max time to wait before returning an error if a timeout is not provided
	DefaultTimeout = 30 * time.Minute
	// DefaultInterval is the polling interval to use if an interval is not provided
	DefaultInterval = 10 * time.Second
)

Variables

This section is empty.

Functions

func Consistent

func Consistent(action func() error, attempts int, pollInterval time.Duration) func() error

Consistent is a modifier for functions. It will return a function that will perform the provided action and return an error if that action doesn't consistently pass. You can configure the attempts and interval between attempts. This can be used in Ginkgo's Eventually to verify that something will eventually be consistent.

func ConsistentWaitCondition

func ConsistentWaitCondition(wc WaitCondition, attempts int, pollInterval time.Duration) func() error

ConsistentWaitCondition is like Consistent but takes in a WaitCondition

func For

func For(fn WaitCondition, opts ...Option) error

For continuously polls the provided WaitCondition function until either the timeout is reached or the function returns as done

func IsClusterAPIObjectConditionSet

func IsClusterAPIObjectConditionSet(obj clusterAPIObject, conditionType capi.ConditionType, expectedStatus corev1.ConditionStatus, expectedReason string) (bool, error)

IsClusterAPIObjectConditionSet checks if a cluster has the specified condition with the expected status.

func IsClusterApiObjectConditionSet

func IsClusterApiObjectConditionSet(obj clusterAPIObject, conditionType capi.ConditionType, expectedStatus corev1.ConditionStatus, expectedReason string) (bool, error)

IsClusterApiObjectConditionSet checks if a cluster has the specified condition with the expected status. Deprecated: Use IsClusterAPIObjectConditionSet instead. nolint // Keep old name for backward compatibility

func WithoutDone

func WithoutDone(wc WaitCondition) func() error

WithoutDone returns a WaitCondition that only returns an error (or nill if condition is met)

Types

type Option

type Option func(*Options)

Option is a function that can be optionally provided to override default options of a wait condition

func WithContext

func WithContext(context context.Context) Option

WithContext overrides the context used when waiting. This allows for using a context with a timeout / deadline already set.

func WithInterval

func WithInterval(interval time.Duration) Option

WithInterval overrides the default polling interval when waiting

func WithTimeout

func WithTimeout(timeout time.Duration) Option

WithTimeout overrides the default timeout when waiting

type Options

type Options struct {
	Context  context.Context
	Interval time.Duration
}

Options are the options available when waiting

type Range

type Range struct {
	Min int
	Max int
}

Range is a wrapper for min and max values

type WaitCondition

type WaitCondition func() (done bool, err error) // nolint

WaitCondition is a function performing a condition check for if we need to keep waiting

func AreAllDaemonSetsReady

func AreAllDaemonSetsReady(ctx context.Context, kubeClient *client.Client) WaitCondition

AreAllDaemonSetsReady returns a WaitCondition that checks if all DaemonSets found in the cluster have the expected number of replicas ready

func AreAllDeploymentsReady

func AreAllDeploymentsReady(ctx context.Context, kubeClient *client.Client) WaitCondition

AreAllDeploymentsReady returns a WaitCondition that checks if all Deployments found in the cluster have the expected number of replicas ready

func AreAllJobsSucceeded

func AreAllJobsSucceeded(ctx context.Context, kubeClient *client.Client) WaitCondition

AreAllJobsSucceeded returns a WaitCondition that checks if all Jobs found in the cluster have completed successfully

func AreAllPodsInSuccessfulPhase

func AreAllPodsInSuccessfulPhase(ctx context.Context, kubeClient *client.Client) WaitCondition

AreAllPodsInSuccessfulPhase returns a WaitCondition that checks if all Pods found in the cluster are in a successful phase (e.g. running or completed)

func AreAllStatefulSetsReady

func AreAllStatefulSetsReady(ctx context.Context, kubeClient *client.Client) WaitCondition

AreAllStatefulSetsReady returns a WaitCondition that checks if all StatefulSets found in the cluster have the expected number of replicas ready

func AreNoPodsCrashLooping

func AreNoPodsCrashLooping(ctx context.Context, kubeClient *client.Client, maxRestartCount int32) WaitCondition

AreNoPodsCrashLooping checks that all pods within the cluster have fewer than the provided number of restarts

func AreNoPodsCrashLoopingWithFilter

func AreNoPodsCrashLoopingWithFilter(ctx context.Context, kubeClient *client.Client, maxRestartCount int32, filterLabels []string) WaitCondition

AreNoPodsCrashLoopingWithFilter checks that all pods within the cluster have fewer than the provided number of restarts `filterLabels` is a list of label selector string to use to filter the pods to be checked. (e.g. `app.kubernetes.io/name!=cluster-autoscaler-app`)

func AreNumNodesReady

func AreNumNodesReady(ctx context.Context, kubeClient *client.Client, expectedNodes int, listOptions ...cr.ListOption) WaitCondition

AreNumNodesReady returns a WaitCondition that checks if the number of ready nodes equals or exceeds the expectedNodes value. It also receives a variadic arguments for list options

func AreNumNodesReadyWithinRange

func AreNumNodesReadyWithinRange(ctx context.Context, kubeClient *client.Client, expectedNodes Range, listOptions ...cr.ListOption) WaitCondition

AreNumNodesReadyWithinRange returns a WaitCondition that checks if the number of ready nodes are within the expected range. It also receives a variadic arguments for list options

func DoesResourceExist

func DoesResourceExist(ctx context.Context, kubeClient *client.Client, resource cr.Object) WaitCondition

DoesResourceExist returns a WaitCondition that checks if the given resource exists in the cluster

func IsAllAppDeployed

func IsAllAppDeployed(ctx context.Context, kubeClient *client.Client, appNamespacedNames []types.NamespacedName) WaitCondition

IsAllAppDeployed returns a WaitCondition that checks if all the apps provided have a deployed status

func IsAllAppStatus

func IsAllAppStatus(ctx context.Context, kubeClient *client.Client, appNamespacedNames []types.NamespacedName, expectedStatus string) WaitCondition

IsAllAppStatus returns a WaitCondition that checks if all the apps provided currently have the provided expected status

func IsAppDeployed

func IsAppDeployed(ctx context.Context, kubeClient *client.Client, appName string, appNamespace string) WaitCondition

IsAppDeployed returns a WaitCondition that checks if an app has a deployed status

func IsAppStatus

func IsAppStatus(ctx context.Context, kubeClient *client.Client, appName string, appNamespace string, expectedStatus string) WaitCondition

IsAppStatus returns a WaitCondition that checks if an app has the expected release status

func IsAppVersion

func IsAppVersion(ctx context.Context, kubeClient *client.Client, appName string, appNamespace string, expectedVersion string) WaitCondition

IsAppVersion returns a WaitCondition that checks if an app has the expected release status. This check ignores any `v` prefix on the version.

func IsClusterConditionSet

func IsClusterConditionSet(ctx context.Context, kubeClient *client.Client, clusterName string, clusterNamespace string, conditionType capi.ConditionType, expectedStatus corev1.ConditionStatus, expectedReason string) WaitCondition

IsClusterConditionSet returns a WaitCondition that checks if a Cluster resource has the specified condition with the expected status.

func IsClusterReadyCondition

func IsClusterReadyCondition(ctx context.Context, kubeClient *client.Client, clusterName string, namespace string, clientPtr **client.Client) WaitCondition

IsClusterReadyCondition returns a WaitCondition to check when a cluster is considered ready and accessible. Additionally IsClusterReadyCondition accepts a pointer to a `client.Client` which will be set to a working workload cluster client once the condition is met. This allows the caller to use the client directly after the condition is met without needing to re-create the client.

func IsKubeadmControlPlaneConditionSet

func IsKubeadmControlPlaneConditionSet(ctx context.Context, kubeClient *client.Client, clusterName string, clusterNamespace string, conditionType capi.ConditionType, expectedStatus corev1.ConditionStatus, expectedReason string) WaitCondition

IsKubeadmControlPlaneConditionSet returns a WaitCondition that checks if a KubeadmControlPlane resource has the specified condition with the expected status.

func IsResourceDeleted

func IsResourceDeleted(ctx context.Context, kubeClient *client.Client, resource cr.Object) WaitCondition

IsResourceDeleted returns a WaitCondition that checks if the given resource has been deleted from the cluster yet

type WaitConditionSlice

type WaitConditionSlice func() (result []any, err error) // nolint

WaitConditionSlice is a function performing a condition check for if we need to keep waiting and returns a slice to use as the check

func AreAllAppDeployedSlice

func AreAllAppDeployedSlice(ctx context.Context, kubeClient *client.Client, appNamespacedNames []types.NamespacedName) WaitConditionSlice

AreAllAppDeployedSlice returns a WaitConditionSlice that contains all the Apps not in a deployed state

func AreAllAppStatusSlice

func AreAllAppStatusSlice(ctx context.Context, kubeClient *client.Client, appNamespacedNames []types.NamespacedName, expectedStatus string) WaitConditionSlice

AreAllAppStatusSlice returns a WaitConditionSlice that contains all the resources not in the expected status

func AreAllDaemonSetsReadySlice

func AreAllDaemonSetsReadySlice(ctx context.Context, kubeClient *client.Client) WaitConditionSlice

AreAllDaemonSetsReadySlice returns a WaitConditionSlice that checks if all DaemonSets found in the cluster have the expected number of replicas ready

func AreAllDeploymentsReadySlice

func AreAllDeploymentsReadySlice(ctx context.Context, kubeClient *client.Client) WaitConditionSlice

AreAllDeploymentsReadySlice returns a WaitConditionSlice that checks if all Deployments found in the cluster have the expected number of replicas ready

func AreAllJobsSucceededSlice

func AreAllJobsSucceededSlice(ctx context.Context, kubeClient *client.Client) WaitConditionSlice

AreAllJobsSucceededSlice returns a WaitConditionSlice that checks if all Jobs found in the cluster have completed successfully

func AreAllPodsInSuccessfulPhaseSlice

func AreAllPodsInSuccessfulPhaseSlice(ctx context.Context, kubeClient *client.Client) WaitConditionSlice

AreAllPodsInSuccessfulPhaseSlice returns a WaitConditionSlice that checks if all Pods found in the cluster are in a successful phase (e.g. running or completed)

func AreAllStatefulSetsReadySlice

func AreAllStatefulSetsReadySlice(ctx context.Context, kubeClient *client.Client) WaitConditionSlice

AreAllStatefulSetsReadySlice returns a WaitConditionSlice that checks if all StatefulSets found in the cluster have the expected number of replicas ready

func ConsistentWaitConditionSlice

func ConsistentWaitConditionSlice(action WaitConditionSlice, attempts int, pollInterval time.Duration) WaitConditionSlice

ConsistentSlice is a modifier for functions. It will return a function that will perform the provided action and return an error if that action doesn't consistently pass. You can configure the attempts and interval between attempts. This can be used in Ginkgo's Eventually to verify that something will eventually be consistent.

Jump to

Keyboard shortcuts

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