controller

package
v0.0.17 Latest Latest
Warning

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

Go to latest
Published: Jan 9, 2026 License: Apache-2.0 Imports: 26 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	DeploymentGVK  = appsv1.SchemeGroupVersion.WithKind("Deployment")
	StatefulSetGVK = appsv1.SchemeGroupVersion.WithKind("StatefulSet")
	DaemonSetGVK   = appsv1.SchemeGroupVersion.WithKind("DaemonSet")
)

Functions

func RenderVPAName

func RenderVPAName(tmpl string, data utils.NameTemplateData) (string, error)

RenderVPAName renders and validates the VPA name using the provided template and data.

Types

type BaseReconciler

type BaseReconciler struct {
	KubeClient client.Client
	Logger     *logr.Logger
	Recorder   record.EventRecorder
	Meta       MetaConfig
	Profiles   ProfileConfig
}

BaseReconciler contains the shared logic for Deployment/StatefulSet/DaemonSet reconcilers.

func (*BaseReconciler) DeleteManagedVPAsForGoneWorkload added in v0.0.15

func (b *BaseReconciler) DeleteManagedVPAsForGoneWorkload(
	ctx context.Context,
	owner client.Object,
	workloadKind string,
) error

DeleteManagedVPAsForGoneWorkload deletes managed VPAs when the workload was removed.

func (*BaseReconciler) DeleteManagedVPAsForOptOut added in v0.0.15

func (b *BaseReconciler) DeleteManagedVPAsForOptOut(
	ctx context.Context,
	owner client.Object,
	workloadKind string,
) error

DeleteManagedVPAsForOptOut deletes managed VPAs when a workload opts out.

func (*BaseReconciler) DeleteObsoleteManagedVPAs added in v0.0.12

func (b *BaseReconciler) DeleteObsoleteManagedVPAs(
	ctx context.Context,
	owner client.Object,
	workloadKind string,
	keepName string,
) error

DeleteObsoleteManagedVPAs deletes all managed VPAs owned by `owner` except the one named keepName. This handles profile/name-template changes.

func (*BaseReconciler) ReconcileWorkload

func (b *BaseReconciler) ReconcileWorkload(
	ctx context.Context,
	obj client.Object,
	targetGVK schema.GroupVersionKind,
) (ctrl.Result, error)

ReconcileWorkload executes the full VPA lifecycle state machine for a workload.

Algorithm overview:

  1. Determine whether the workload opts into VPA management (profile annotation).
  2. If not opted-in → delete all managed VPAs for this workload.
  3. Resolve the profile to use.
  4. Render the desired VPA name, labels, and spec.
  5. Delete obsolete VPAs (e.g. profile/name-template change).
  6. Create the desired VPA if missing.
  7. If it exists, merge and apply changes via server-side apply.

This function NEVER requeues on configuration errors (e.g. profile missing) to avoid thrashing. It only returns a non-nil error when an API call fails.

type DaemonSetReconciler

type DaemonSetReconciler struct {
	BaseReconciler
}

DaemonSetReconciler reconciles DaemonSets and manages their VPAs.

Its responsibilities are:

  • Drive the desired VPA state for DaemonSets by delegating to BaseReconciler.ReconcileWorkload.
  • Perform cleanup of managed VPAs when a DaemonSet is deleted.

Actual VPA content (name, labels, spec) is resolved by the shared BaseReconciler logic; this controller only wires that logic to the DaemonSet API type.

func (*DaemonSetReconciler) Reconcile

func (r *DaemonSetReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error)

Reconcile ensures that the DaemonSet's opted-in state (profile annotation) is reflected in its managed VPAs.

High-level flow:

  1. Try to load the DaemonSet. - If it does not exist anymore, proactively delete any managed VPAs that still point at this DaemonSet (best-effort cleanup).
  2. If it exists, delegate to ReconcileWorkload to create/update/delete the associated VPA based on the selected profile.

func (*DaemonSetReconciler) SetupWithManager

func (r *DaemonSetReconciler) SetupWithManager(mgr ctrl.Manager) error

SetupWithManager wires the DaemonSet controller into the manager.

  • DaemonSet events are filtered by the profile annotation lifecycle.
  • Owned VPA events are filtered by ManagedVPALifecycle, so spec/label drift requeues the owning DaemonSet ("snap back" behavior) while still ignoring status churn.

type DeploymentReconciler

type DeploymentReconciler struct {
	BaseReconciler
}

DeploymentReconciler reconciles Deployments and manages their VPAs.

Its responsibilities are:

  • Drive the desired VPA state for Deployments by delegating to BaseReconciler.ReconcileWorkload.
  • Perform cleanup of managed VPAs when a Deployment is deleted.

Actual VPA content (name, labels, spec) is resolved by the shared BaseReconciler logic; this controller only wires that logic to the Deployment API type.

func (*DeploymentReconciler) Reconcile

func (r *DeploymentReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error)

Reconcile ensures that the Deployment's opted-in state (profile annotation) is reflected in its managed VPAs.

High-level flow:

  1. Try to load the Deployment. - If it does not exist anymore, proactively delete any managed VPAs that still point at this Deployment (best-effort cleanup).
  2. If it exists, delegate to ReconcileWorkload to create/update/delete the associated VPA based on the selected profile.

func (*DeploymentReconciler) SetupWithManager

func (r *DeploymentReconciler) SetupWithManager(mgr ctrl.Manager) error

SetupWithManager wires the Deployment controller into the manager.

  • Deployment events are filtered by the profile annotation lifecycle.
  • Owned VPA events are filtered by ManagedVPALifecycle, so spec/label drift requeues the owning Deployment ("snap back" behavior) while still ignoring status churn.

type MetaConfig

type MetaConfig struct {
	ProfileKey   string // Workload annotation key used to pick a VPA profile.
	ManagedLabel string // Label key applied to VPAs managed by this operator.
}

MetaConfig holds annotation/label settings shared across reconcilers. It controls how workloads opt into profiles and how managed VPAs are marked.

type ProfileConfig

type ProfileConfig struct {
	NameTemplate string                    // Default VPA name template when a profile does not override.
	Default      string                    // Default profile name to use when annotation selects "default".
	Entries      map[string]config.Profile // All available profiles keyed by name.
}

ProfileConfig wraps profile data shared across reconcilers. It supplies the available profiles, default profile, and default name template.

type StatefulSetReconciler

type StatefulSetReconciler struct {
	BaseReconciler
}

StatefulSetReconciler reconciles StatefulSets and manages their VPAs.

Its responsibilities are:

  • Drive the desired VPA state for StatefulSets by delegating to BaseReconciler.ReconcileWorkload.
  • Perform cleanup of managed VPAs when a StatefulSet is deleted.

Actual VPA content (name, labels, spec) is resolved by the shared BaseReconciler logic; this controller only wires that logic to the StatefulSet API type.

func (*StatefulSetReconciler) Reconcile

func (r *StatefulSetReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error)

Reconcile ensures that the StatefulSet's opted-in state (profile annotation) is reflected in its managed VPAs.

High-level flow:

  1. Try to load the StatefulSet. - If it does not exist anymore, proactively delete any managed VPAs that still point at this StatefulSet (best-effort cleanup).
  2. If it exists, delegate to ReconcileWorkload to create/update/delete the associated VPA based on the selected profile.

func (*StatefulSetReconciler) SetupWithManager

func (r *StatefulSetReconciler) SetupWithManager(mgr ctrl.Manager) error

SetupWithManager wires the StatefulSet controller into the manager.

  • StatefulSet events are filtered by the profile annotation lifecycle.
  • Owned VPA events are filtered by ManagedVPALifecycle, so spec/label drift requeues the owning StatefulSet ("snap back" behavior) while still ignoring status churn.

type VPAReconciler added in v0.0.12

type VPAReconciler struct {
	// KubeClient is the Kubernetes API client used for reads and deletes.
	KubeClient client.Client

	// Logger is used for structured reconciliation logging.
	Logger *logr.Logger

	// Recorder emits Kubernetes events for visibility.
	Recorder record.EventRecorder

	// Meta contains operator metadata such as label keys.
	Meta MetaConfig
}

VPAReconciler enforces the *structural correctness* of managed VPAs.

This controller is intentionally lightweight and defensive. It does NOT manage desired state (spec, labels, naming).

Responsibilities:

  1. Delete managed VPAs that have no valid controller ownerRef (orphans).
  2. Delete managed VPAs whose referenced owner object no longer exists.

All desired-state reconciliation (create/update/snap-back) is handled exclusively by workload reconcilers (DeploymentReconciler, etc.).

The VPAReconciler acts purely as a *safety net* to prevent resource leaks and stale VPAs caused by user tampering or partial cleanup.

func (*VPAReconciler) Reconcile added in v0.0.12

func (r *VPAReconciler) Reconcile(
	ctx context.Context,
	req ctrl.Request,
) (ctrl.Result, error)

Reconcile validates a managed VPA’s ownership and deletes invalid VPAs.

A VPA is deleted when:

  • it carries the managed label, AND
  • it has no controller ownerRef, OR
  • its controller ownerRef points to a non-existent workload.

The reconciler never creates or updates VPAs. It only deletes invalid ones.

Errors are returned only for failed API operations; “not found” conditions are treated as terminal and non-fatal.

func (*VPAReconciler) SetupWithManager added in v0.0.12

func (r *VPAReconciler) SetupWithManager(mgr ctrl.Manager) error

SetupWithManager wires the VPAReconciler into the controller manager.

The reconciler watches only VPAs and uses a structural predicate to ensure it is triggered exclusively by meaningful lifecycle or ownership changes.

Jump to

Keyboard shortcuts

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