wait

package
v1.7.0 Latest Latest
Warning

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

Go to latest
Published: Jul 3, 2026 License: BSD-3-Clause, MIT Imports: 11 Imported by: 0

Documentation

Overview

Package wait provides reusable polling infrastructure shared by the `--wait` flag (block until a resource reaches a target state) and the `events list --follow` stream. It owns three concerns: a context-aware polling loop with bounded backoff, the `--wait`/`--timeout` flag pair, and a signal-derived context so Ctrl+C exits cleanly (no traceback).

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrTimeout  = errors.New("timed out waiting for the resource to reach its target state")
	ErrCanceled = errors.New("wait canceled")
)

Sentinel errors returned by Poll and its callers. Commands map ErrTimeout to a non-zero exit code; ErrCanceled is the clean Ctrl+C path.

View Source
var ErrClusterFailed = errors.New("kubernetes cluster entered a failed phase")

ErrClusterFailed is returned when a cluster enters its Failed phase instead of reaching a target phase.

View Source
var ErrFailedState = errors.New("server entered a failed state")

ErrFailedState is returned when a server reaches a state the caller declared as terminal-failure (e.g. failed_deployment) instead of its target state.

Functions

func AddFlags

func AddFlags(cmd *cobra.Command)

AddFlags registers the --wait/--timeout pair on a state-changing command.

func ForKubernetesClusterPhase added in v1.7.0

func ForKubernetesClusterPhase(
	ctx context.Context,
	client *sdk.Latitudesh,
	clusterID string,
	want, fail []components.KubernetesClusterDataPhase,
	o Options,
	opts ...operations.Option,
) (components.KubernetesClusterDataPhase, error)

ForKubernetesClusterPhase polls GET /kubernetes_clusters/{id} until the cluster's phase is one of want (success), one of fail (ErrClusterFailed), o.Timeout elapses (ErrTimeout), or the user cancels (ErrCanceled). It returns the last phase observed.

Transient API errors are swallowed and retried until the timeout, at which point the last error is attached to ErrTimeout so the failure is diagnosable.

func ForServerState

func ForServerState(
	ctx context.Context,
	client *sdk.Latitudesh,
	serverID string,
	want, fail []components.ServerDataStatus,
	requireTransition bool,
	o Options,
	onStatus func(components.ServerDataStatus),
	opts ...operations.Option,
) (components.ServerDataStatus, error)

ForServerState polls GET /servers/{id} until the server's status is one of want (success), one of fail (returns ErrFailedState), o.Timeout elapses (ErrTimeout), or the user cancels (ErrCanceled). It returns the last status observed and invokes onStatus (when non-nil) on every successful poll.

When requireTransition is true, a terminal state is only accepted after the server has first been observed in some other (transition) state. This guards operations that act on a server already sitting in a target state — e.g. a reinstall on a powered-on server, or a create whose POST response optimistically reports "on" — from returning before the operation has actually begun.

Transient errors from the API (the platform occasionally 5xxs mid-provision) are not fatal: they are swallowed and retried until the timeout, at which point the last error is attached to ErrTimeout so the failure is diagnosable.

func ForVirtualMachineState added in v1.7.0

ForVirtualMachineState polls GET /virtual_machines/{id} until the VM's status is one of want (success), o.Timeout elapses (ErrTimeout), or the user cancels (ErrCanceled). It returns the last status observed.

The virtual machine schema does not expose a distinct failure status, so there is no fail set: a stuck provision surfaces as an ErrTimeout instead. Transient API errors are swallowed and retried until the timeout, at which point the last error is attached to ErrTimeout so the failure is diagnosable.

func Poll

func Poll(ctx context.Context, b Backoff, probe func(context.Context) (done bool, err error)) error

Poll calls probe immediately, then repeatedly with a backoff delay between attempts, until probe reports done, probe returns an error, or ctx is cancelled/expired. A (false, nil) result means "not there yet — keep waiting". Context cancellation is normalized to ErrCanceled/ErrTimeout so callers get a stable, friendly error regardless of where it was observed.

func SignalContext

func SignalContext(parent context.Context) (context.Context, context.CancelFunc)

SignalContext derives a context that is cancelled on SIGINT (Ctrl+C) or SIGTERM, so long-running waits and the --follow stream stop cleanly. The returned cancel func must be called to release the signal handler.

Types

type Backoff

type Backoff struct {
	Initial time.Duration
	Max     time.Duration
	Factor  float64
}

Backoff controls the delay between polls. The delay grows geometrically from Initial by Factor, capped at Max, and is never shorter than pollFloor.

func DefaultBackoff

func DefaultBackoff() Backoff

DefaultBackoff is tuned for resource-state waiters: a quick first re-check that eases off so a long provision doesn't hammer the API.

type Options

type Options struct {
	Enabled bool
	Timeout time.Duration
}

Options is the parsed form of the --wait/--timeout flags.

func OptionsFrom

func OptionsFrom(cmd *cobra.Command) Options

OptionsFrom reads the --wait/--timeout flags registered by AddFlags.

Jump to

Keyboard shortcuts

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