apply

package
v1.0.6 Latest Latest
Warning

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

Go to latest
Published: Jun 24, 2025 License: Apache-2.0 Imports: 33 Imported by: 0

Documentation

Overview

Package apply implements an "atomic apply" algorithm for Kubernetes resources.

The public entry‑point is RunApply, which mimics the behaviour of

kubectl apply -f FILE ...

but guarantees **all‑or‑nothing** semantics:

  1. Each manifest is applied server‑side (SSA).
  2. If any step fails or any resource does not reach the desired status before the configured timeout, the helper rolls everything back to the state observed *before* the invocation began.

A high‑level flow looks like this:

  • readDocs() -> YAML/JSON -> []*unstructured.Unstructured
  • prepareApplyPlan() -> []applyItem (backup & CRUD plan)
  • applyPlanned() -> Patch/Update/Delete via dynamic client
  • waitStatus() -> poll until Current or timeout
  • rollbackAndExit() on any error

The package is meant to be used from a kubectl‑style CLI, therefore it relies on the same cli‑runtime helpers and accepts genericclioptions flags.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func RunApply

func RunApply(ctx context.Context, runOpts *AtomicApplyRunOptions) error

RunApply is the public entry‑point. It orchestrates the full lifecycle: parsing manifests, creating a plan, applying, waiting for readiness and rolling back on failure.

ctx     - context that can enforce an overall deadline/cancelation
runOpts - fully populated AtomicApplyRunOptions

It returns nil on success or an error if *any* step failed. When the function returns an error, it is guaranteed that rollbackAndExit has *already* been invoked or rollback itself failed (the latter is included in the returned error chain).

Types

type AtomicApplyOptions

type AtomicApplyOptions struct {
	Filenames []string
	Timeout   time.Duration
	Recursive bool
}

AtomicApplyOptions groups the user‑visible flags of the CLI layer. It purposely contains **only plain data** so it can be embedded in higher level option structs or reused in tests.

Filenames - list of paths or "-" for stdin
Timeout   - maximum time to wait for resources to become Current
Recursive - whether to walk directories recursively when expanding -f
            arguments.

type AtomicApplyRunOptions

type AtomicApplyRunOptions struct {
	ConfigFlags *genericclioptions.ConfigFlags
	Streams     genericiooptions.IOStreams
	ApplyOpts   AtomicApplyOptions
}

AtomicApplyRunOptions wires together everything required to run the high level algorithm (config flags, IO streams and the user flags above).

This mirrors the pattern used by upstream kubectl commands so the calling code can build the struct in the same way it would for a builtin command.

ConfigFlags - kubectl connection flags (kubeconfig, context, namespace …)
Streams     - stdin/stdout/stderr (allows unit‑testing with fake streams)
ApplyOpts   - parsed AtomicApplyOptions

The struct is passed *by pointer* to avoid large copies.

Jump to

Keyboard shortcuts

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