kubernetes

package
v2.12.0 Latest Latest
Warning

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

Go to latest
Published: Aug 6, 2026 License: GPL-3.0 Imports: 25 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrImageRequired             = errors.New("image is required")
	ErrInvalidCleanupPolicy      = errors.New("cleanup_policy must be either delete or keep")
	ErrInvalidImagePullPolicy    = errors.New("image_pull_policy must be one of Always, IfNotPresent, or Never")
	ErrNegativeActiveDeadline    = errors.New("active_deadline must be >= 0")
	ErrNegativeBackoffLimit      = errors.New("backoff_limit must be >= 0")
	ErrNegativeTTLAfterFinished  = errors.New("ttl_after_finished must be >= 0")
	ErrNegativeTerminationGrace  = errors.New("termination_grace_period_seconds must be >= 0")
	ErrNegativeQuantity          = errors.New("resource quantity must be >= 0")
	ErrInvalidVolumeSource       = errors.New("volume must define exactly one source")
	ErrUnsupportedFallbackPolicy = errors.New("fallback is not supported for kubernetes executor (use Always, IfNotPresent, or Never)")
)

Functions

This section is empty.

Types

type Affinity

type Affinity struct {
	NodeAffinity    *NodeAffinity `mapstructure:"node_affinity"`
	PodAffinity     *PodAffinity  `mapstructure:"pod_affinity"`
	PodAntiAffinity *PodAffinity  `mapstructure:"pod_anti_affinity"`
}

Affinity configures node and pod scheduling rules.

type Capabilities

type Capabilities struct {
	Add  []string `mapstructure:"add"`
	Drop []string `mapstructure:"drop"`
}

Capabilities configures Linux capabilities to add or drop.

type Client

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

Client wraps the Kubernetes clientset and manages Job lifecycle.

func NewClient

func NewClient(cfg *Config) (*Client, error)

NewClient creates a Kubernetes client using the given config. It discovers the kubeconfig in this order: 1. Explicit kubeconfig path from config 2. KUBECONFIG env / ~/.kube/config (default loading rules) 3. In-cluster config

func (*Client) CreateJob

func (c *Client) CreateJob(ctx context.Context, stepName string, command []string) error

CreateJob creates a Kubernetes Job from the config. The command parameter overrides the container command if non-empty.

func (*Client) DeleteJob

func (c *Client) DeleteJob(ctx context.Context) error

DeleteJob deletes the Job and its Pods using background propagation.

func (*Client) GetExitCode

func (c *Client) GetExitCode(ctx context.Context, podName string) (int, error)

GetExitCode retrieves the exit code from the terminated container in the pod.

func (*Client) GetJobName

func (c *Client) GetJobName() string

GetJobName returns the name of the created Job.

func (*Client) StreamLogs

func (c *Client) StreamLogs(ctx context.Context, podName string, stdout io.Writer) error

StreamLogs streams logs from the given pod to the stdout writer. Kubernetes merges stdout and stderr into a single log stream.

func (*Client) WaitForCompletion

func (c *Client) WaitForCompletion(ctx context.Context) error

WaitForCompletion polls the Job until it completes or fails.

func (*Client) WaitForPod

func (c *Client) WaitForPod(ctx context.Context) (string, error)

WaitForPod waits until a Pod created by the Job reaches Running, Succeeded, or Failed state and returns its name.

func (*Client) WaitForPodTermination

func (c *Client) WaitForPodTermination(ctx context.Context, podName string) error

WaitForPodTermination polls the pod until its container terminates. This is used after log streaming completes to ensure we can read the exit code.

type Config

type Config struct {
	// Kubeconfig is the path to the kubeconfig file.
	// If empty, uses default discovery (KUBECONFIG env, ~/.kube/config, in-cluster).
	Kubeconfig string `mapstructure:"kubeconfig"`
	// Context is the kubeconfig context to use. If empty, uses current-context.
	Context string `mapstructure:"context"`

	// Namespace is the Kubernetes namespace. Default: "default".
	Namespace string `mapstructure:"namespace"`
	// Image is the container image to use (required).
	Image string `mapstructure:"image"`
	// ImagePullPolicy is the image pull policy (Always, IfNotPresent, Never).
	ImagePullPolicy string `mapstructure:"image_pull_policy"`
	// ImagePullSecrets are the names of secrets for pulling images from private registries.
	ImagePullSecrets []string `mapstructure:"image_pull_secrets"`

	// WorkingDir is the working directory inside the container.
	WorkingDir string `mapstructure:"working_dir"`
	// Env specifies environment variables for the container.
	Env []EnvVar `mapstructure:"env"`
	// EnvFrom specifies sources to populate environment variables.
	EnvFrom []EnvFromSource `mapstructure:"env_from"`

	// Resources specifies CPU/memory requests and limits.
	Resources *ResourceRequirements `mapstructure:"resources"`
	// ServiceAccount is the service account to use for the pod.
	ServiceAccount string `mapstructure:"service_account"`
	// NodeSelector constrains which nodes the pod can run on.
	NodeSelector map[string]string `mapstructure:"node_selector"`
	// Tolerations allow the pod to schedule onto nodes with matching taints.
	Tolerations []Toleration `mapstructure:"tolerations"`
	// SecurityContext configures container-level Linux security settings.
	SecurityContext *SecurityContext `mapstructure:"security_context"`
	// PodSecurityContext configures Pod-level Linux security defaults.
	PodSecurityContext *PodSecurityContext `mapstructure:"pod_security_context"`
	// Affinity configures node and pod scheduling affinity.
	Affinity *Affinity `mapstructure:"affinity"`
	// TerminationGracePeriodSeconds controls graceful shutdown time for the Pod.
	TerminationGracePeriodSeconds *int64 `mapstructure:"termination_grace_period_seconds"`
	// PriorityClassName sets the Pod priority class.
	PriorityClassName string `mapstructure:"priority_class_name"`

	// Labels are applied to the Job and Pod.
	Labels map[string]string `mapstructure:"labels"`
	// Annotations are applied to the Job and Pod.
	Annotations map[string]string `mapstructure:"annotations"`

	// Volumes defines volumes available to the pod.
	Volumes []Volume `mapstructure:"volumes"`
	// VolumeMounts defines mount points for volumes in the container.
	VolumeMounts []VolumeMount `mapstructure:"volume_mounts"`

	// ActiveDeadlineSeconds is the Kubernetes-native timeout for the Job in seconds.
	ActiveDeadlineSeconds *int64 `mapstructure:"active_deadline"`
	// BackoffLimit is the number of retries before considering the Job failed. Default: 0.
	BackoffLimit *int32 `mapstructure:"backoff_limit"`
	// TTLSecondsAfterFinished controls automatic cleanup by Kubernetes.
	TTLSecondsAfterFinished *int32 `mapstructure:"ttl_after_finished"`

	// CleanupPolicy controls whether to delete the Job after completion.
	// "delete" (default) or "keep".
	CleanupPolicy string `mapstructure:"cleanup_policy"`
	// PodFailurePolicy configures Kubernetes-native Job failure handling.
	PodFailurePolicy *PodFailurePolicy `mapstructure:"pod_failure_policy"`
}

Config holds the configuration for creating a Kubernetes Job.

func LoadConfigFromMap

func LoadConfigFromMap(data map[string]any) (*Config, error)

LoadConfigFromMap decodes a config map into a Config struct and applies defaults.

type ConfigMap

type ConfigMap struct {
	Name string `mapstructure:"name"`
}

ConfigMap represents a configMap volume source.

type EmptyDir

type EmptyDir struct {
	Medium    string `mapstructure:"medium"`
	SizeLimit string `mapstructure:"size_limit"`
}

EmptyDir represents an emptyDir volume source.

type EnvFromRef

type EnvFromRef struct {
	Name string `mapstructure:"name"`
}

EnvFromRef references a ConfigMap or Secret for envFrom.

type EnvFromSource

type EnvFromSource struct {
	ConfigMapRef *EnvFromRef `mapstructure:"config_map_ref"`
	SecretRef    *EnvFromRef `mapstructure:"secret_ref"`
	Prefix       string      `mapstructure:"prefix"`
}

EnvFromSource represents a source to populate environment variables.

type EnvVar

type EnvVar struct {
	Name      string        `mapstructure:"name"`
	Value     string        `mapstructure:"value"`
	ValueFrom *EnvVarSource `mapstructure:"value_from"`
}

EnvVar represents a Kubernetes environment variable.

type EnvVarSource

type EnvVarSource struct {
	SecretKeyRef    *KeySelector `mapstructure:"secret_key_ref"`
	ConfigMapKeyRef *KeySelector `mapstructure:"config_map_key_ref"`
	FieldRef        *FieldRef    `mapstructure:"field_ref"`
}

EnvVarSource represents a source for an environment variable's value.

type FieldRef

type FieldRef struct {
	FieldPath string `mapstructure:"field_path"`
}

FieldRef selects a field of the pod.

type HostPath

type HostPath struct {
	Path string `mapstructure:"path"`
	Type string `mapstructure:"type"`
}

HostPath represents a hostPath volume source.

type KeySelector

type KeySelector struct {
	Name string `mapstructure:"name"`
	Key  string `mapstructure:"key"`
}

KeySelector selects a key from a ConfigMap or Secret.

type LabelSelector

type LabelSelector struct {
	MatchLabels      map[string]string          `mapstructure:"match_labels"`
	MatchExpressions []LabelSelectorRequirement `mapstructure:"match_expressions"`
}

LabelSelector is a typed subset of metav1.LabelSelector.

type LabelSelectorRequirement

type LabelSelectorRequirement struct {
	Key      string   `mapstructure:"key"`
	Operator string   `mapstructure:"operator"`
	Values   []string `mapstructure:"values"`
}

LabelSelectorRequirement matches Kubernetes label-selector expressions.

type NodeAffinity

type NodeAffinity struct {
	RequiredDuringSchedulingIgnoredDuringExecution  *NodeSelector             `mapstructure:"required_during_scheduling_ignored_during_execution"`
	PreferredDuringSchedulingIgnoredDuringExecution []PreferredSchedulingTerm `mapstructure:"preferred_during_scheduling_ignored_during_execution"`
}

NodeAffinity configures node selector affinity.

type NodeSelector

type NodeSelector struct {
	NodeSelectorTerms []NodeSelectorTerm `mapstructure:"node_selector_terms"`
}

NodeSelector is a disjunction of node selector terms.

type NodeSelectorRequirement

type NodeSelectorRequirement struct {
	Key      string   `mapstructure:"key"`
	Operator string   `mapstructure:"operator"`
	Values   []string `mapstructure:"values"`
}

NodeSelectorRequirement matches a node label requirement.

type NodeSelectorTerm

type NodeSelectorTerm struct {
	MatchExpressions []NodeSelectorRequirement `mapstructure:"match_expressions"`
}

NodeSelectorTerm matches nodes by expressions.

type PVCVol

type PVCVol struct {
	ClaimName string `mapstructure:"claim_name"`
	ReadOnly  bool   `mapstructure:"read_only"`
}

PVCVol represents a persistentVolumeClaim volume source.

type PodAffinity

type PodAffinity struct {
	RequiredDuringSchedulingIgnoredDuringExecution  []PodAffinityTerm         `mapstructure:"required_during_scheduling_ignored_during_execution"`
	PreferredDuringSchedulingIgnoredDuringExecution []WeightedPodAffinityTerm `mapstructure:"preferred_during_scheduling_ignored_during_execution"`
}

PodAffinity configures pod affinity or anti-affinity rules.

type PodAffinityTerm

type PodAffinityTerm struct {
	LabelSelector     *LabelSelector `mapstructure:"label_selector"`
	Namespaces        []string       `mapstructure:"namespaces"`
	NamespaceSelector *LabelSelector `mapstructure:"namespace_selector"`
	TopologyKey       string         `mapstructure:"topology_key"`
}

PodAffinityTerm selects pods relative to topology.

type PodFailurePolicy

type PodFailurePolicy struct {
	Rules []PodFailurePolicyRule `mapstructure:"rules"`
}

PodFailurePolicy configures Kubernetes-native Job failure handling.

type PodFailurePolicyOnExitCodesRequirement

type PodFailurePolicyOnExitCodesRequirement struct {
	ContainerName string  `mapstructure:"container_name"`
	Operator      string  `mapstructure:"operator"`
	Values        []int32 `mapstructure:"values"`
}

PodFailurePolicyOnExitCodesRequirement matches failed container exit codes.

type PodFailurePolicyOnPodConditionsPattern

type PodFailurePolicyOnPodConditionsPattern struct {
	Type   string `mapstructure:"type"`
	Status string `mapstructure:"status"`
}

PodFailurePolicyOnPodConditionsPattern matches pod conditions.

type PodFailurePolicyRule

type PodFailurePolicyRule struct {
	Action          string                                   `mapstructure:"action"`
	OnExitCodes     *PodFailurePolicyOnExitCodesRequirement  `mapstructure:"on_exit_codes"`
	OnPodConditions []PodFailurePolicyOnPodConditionsPattern `mapstructure:"on_pod_conditions"`
}

PodFailurePolicyRule matches either exit codes or pod conditions.

type PodSecurityContext

type PodSecurityContext struct {
	RunAsUser           *int64          `mapstructure:"run_as_user"`
	RunAsGroup          *int64          `mapstructure:"run_as_group"`
	RunAsNonRoot        *bool           `mapstructure:"run_as_non_root"`
	FSGroup             *int64          `mapstructure:"fs_group"`
	FSGroupChangePolicy string          `mapstructure:"fs_group_change_policy"`
	SupplementalGroups  []int64         `mapstructure:"supplemental_groups"`
	Sysctls             []Sysctl        `mapstructure:"sysctls"`
	SeccompProfile      *SeccompProfile `mapstructure:"seccomp_profile"`
}

PodSecurityContext configures Pod-level Linux security defaults.

type PreferredSchedulingTerm

type PreferredSchedulingTerm struct {
	Weight     int32            `mapstructure:"weight"`
	Preference NodeSelectorTerm `mapstructure:"preference"`
}

PreferredSchedulingTerm gives a node selector preference a weight.

type ResourceRequirements

type ResourceRequirements struct {
	Requests map[string]string `mapstructure:"requests"`
	Limits   map[string]string `mapstructure:"limits"`
}

ResourceRequirements specifies CPU and memory requests and limits.

type SeccompProfile

type SeccompProfile struct {
	Type             string `mapstructure:"type"`
	LocalhostProfile string `mapstructure:"localhost_profile"`
}

SeccompProfile configures Linux seccomp behavior.

type SecretVol

type SecretVol struct {
	SecretName string `mapstructure:"secret_name"`
}

SecretVol represents a secret volume source.

type SecurityContext

type SecurityContext struct {
	RunAsUser                *int64          `mapstructure:"run_as_user"`
	RunAsGroup               *int64          `mapstructure:"run_as_group"`
	RunAsNonRoot             *bool           `mapstructure:"run_as_non_root"`
	Privileged               *bool           `mapstructure:"privileged"`
	ReadOnlyRootFilesystem   *bool           `mapstructure:"read_only_root_filesystem"`
	AllowPrivilegeEscalation *bool           `mapstructure:"allow_privilege_escalation"`
	Capabilities             *Capabilities   `mapstructure:"capabilities"`
	SeccompProfile           *SeccompProfile `mapstructure:"seccomp_profile"`
}

SecurityContext configures container-level Linux security settings.

type Sysctl

type Sysctl struct {
	Name  string `mapstructure:"name"`
	Value string `mapstructure:"value"`
}

Sysctl configures a namespaced Linux sysctl for the Pod.

type Toleration

type Toleration struct {
	Key      string `mapstructure:"key"`
	Operator string `mapstructure:"operator"`
	Value    string `mapstructure:"value"`
	Effect   string `mapstructure:"effect"`
}

Toleration represents a Kubernetes toleration.

type Volume

type Volume struct {
	Name                  string     `mapstructure:"name"`
	EmptyDir              *EmptyDir  `mapstructure:"empty_dir"`
	HostPath              *HostPath  `mapstructure:"host_path"`
	ConfigMap             *ConfigMap `mapstructure:"config_map"`
	Secret                *SecretVol `mapstructure:"secret"`
	PersistentVolumeClaim *PVCVol    `mapstructure:"persistent_volume_claim"`
}

Volume defines a volume available to the pod.

type VolumeMount

type VolumeMount struct {
	Name      string `mapstructure:"name"`
	MountPath string `mapstructure:"mount_path"`
	SubPath   string `mapstructure:"sub_path"`
	ReadOnly  bool   `mapstructure:"read_only"`
}

VolumeMount defines a mount point for a volume in a container.

type WeightedPodAffinityTerm

type WeightedPodAffinityTerm struct {
	Weight          int32           `mapstructure:"weight"`
	PodAffinityTerm PodAffinityTerm `mapstructure:"pod_affinity_term"`
}

WeightedPodAffinityTerm gives a pod affinity term a weight.

Jump to

Keyboard shortcuts

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