session

package
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Jun 27, 2026 License: Apache-2.0 Imports: 11 Imported by: 0

Documentation

Overview

Package session holds per-command Kubernetes and Helm client state.

Session carries the global CLI options (kubeconfig, namespace, spec path, etc.) and travels through context.Context so every command shares one configured environment per invocation. Construct one with New, attach it via WithContext, and retrieve it in handlers through FromContext.

To access cluster resources, call Session.Target with the target environment name. It eagerly resolves the Kubernetes context from the spec's environment field and returns a Cluster that lazily constructs the Helm and Kubernetes clients. This two-phase model makes it a compile-time error to request a cluster client without first resolving which cluster to use.

Index

Constants

View Source
const (
	// DefaultTimeout is the default timeout for Helm operations.
	DefaultTimeout = 10 * time.Minute

	// DefaultStorageDriver is the default Helm storage driver.
	DefaultStorageDriver = "secret"

	// DefaultNamespace is used when no namespace is specified.
	DefaultNamespace = "default"
)

Session defaults.

View Source
const (
	// HelmStorageDriverSecret uses Kubernetes secrets for Helm storage.
	HelmStorageDriverSecret = "secret"

	// HelmStorageDriverConfigMap uses Kubernetes ConfigMaps for Helm storage.
	HelmStorageDriverConfigMap = "configmap"

	// HelmStorageDriverMemory uses in-memory storage for Helm (testing only).
	HelmStorageDriverMemory = "memory"

	// HelmTimeoutMin is the minimum allowed timeout for Helm operations.
	HelmTimeoutMin = 30 * time.Second

	// HelmTimeoutMax is the maximum allowed timeout for Helm operations.
	HelmTimeoutMax = 60 * time.Minute
)

Helm storage driver constants.

View Source
const (
	// KubeConfigEnvVar is the environment variable for kubeconfig path.
	KubeConfigEnvVar = "KUBECONFIG"

	// NamespaceEnvVar is the environment variable for default namespace.
	NamespaceEnvVar = "DPY_NAMESPACE"

	// DebugEnvVar is the environment variable for enabling debug mode.
	DebugEnvVar = "DPY_DEBUG"
)

Environment variables consulted by the session package.

Variables

This section is empty.

Functions

func GetValidStorageDrivers

func GetValidStorageDrivers() []string

GetValidStorageDrivers returns a list of valid storage drivers.

func ValidateStorageDriver

func ValidateStorageDriver(driver string) bool

ValidateStorageDriver reports whether the storage driver is valid.

func ValidateTimeout

func ValidateTimeout(timeout time.Duration) bool

ValidateTimeout reports whether timeout is within acceptable bounds.

func WithContext

func WithContext(ctx context.Context, sess *Session) context.Context

WithContext returns a new context carrying sess.

Types

type Cluster

type Cluster struct {
	*Session
	// contains filtered or unexported fields
}

Cluster is a resolved target: it embeds the base Session and adds a confirmed Kubernetes context plus lazily-initialized Helm and Kubernetes clients. Obtain one via Session.Target.

func (*Cluster) Helm

func (cl *Cluster) Helm() (HelmClient, error)

Helm returns a memoized Helm client targeted at the resolved cluster.

func (*Cluster) Kubernetes

func (cl *Cluster) Kubernetes() (kubernetes.Interface, error)

Kubernetes returns a memoized Kubernetes clientset targeted at the resolved cluster.

func (*Cluster) Namespace

func (cl *Cluster) Namespace() string

Namespace returns the configured namespace, or "default" if none is set.

func (*Cluster) RESTConfig

func (cl *Cluster) RESTConfig() (*rest.Config, error)

RESTConfig returns a Kubernetes REST config for the resolved cluster.

func (*Cluster) Spec

func (cl *Cluster) Spec() *spec.Spec

Spec returns the spec that was loaded during Session.Target, or nil if Target was called without an environment or the spec could not be loaded.

type HelmClient

type HelmClient interface {
	// IsReachable checks whether the configured Kubernetes cluster is reachable.
	IsReachable() error

	// InstallApp installs or upgrades an application using Helm.
	InstallApp(ctx context.Context, manifest *spec.Spec, environment string, dryRun bool) error

	// DeleteRelease uninstalls a Helm release. When wait is true the call
	// blocks until all resources are fully removed using the legacy polling
	// strategy with foreground cascade deletion.
	DeleteRelease(ctx context.Context, project, environment string, wait bool) error

	// GetRelease retrieves information about a specific release.
	GetRelease(ctx context.Context, project, environment string) (*v1.Release, error)

	// ListReleases returns a list of releases matching the given selector.
	ListReleases(ctx context.Context, selector labels.Selector) ([]*v1.Release, error)

	// GetReleaseHistory returns the history of a specific release.
	GetReleaseHistory(ctx context.Context, project, environment string) ([]*v1.Release, error)

	// RollbackRelease rolls back a release to a previous revision.
	RollbackRelease(ctx context.Context, releaseName string, revision int, timeout time.Duration) error
}

HelmClient defines the interface for Helm operations. It is kept in this package so WithHelmFactory tests can inject a mock without importing the concrete helm package.

type Option

type Option func(*Session)

Option is a functional option for configuring a Session.

func WithDebug

func WithDebug(keep bool) Option

WithDebug controls whether to keep temporary chart directories.

func WithExtraKubeconfigPaths

func WithExtraKubeconfigPaths(paths ...string) Option

WithExtraKubeconfigPaths appends additional kubeconfig file paths to the clientcmd loading-rules Precedence list, making contexts from those files available without polluting the user's default kubeconfig. Missing files are silently skipped by client-go. An explicit --kubeconfig flag still takes priority because it sets ExplicitPath, which causes Precedence to be ignored.

func WithHelmFactory

func WithHelmFactory(factory func(*Session) (HelmClient, error)) Option

WithHelmFactory sets a custom Helm client factory, primarily for testing.

func WithKubeContext

func WithKubeContext(kubeContext string) Option

WithKubeContext sets the Kubernetes context, overriding the kubeconfig's current context. An empty value leaves the current context in effect.

func WithKubeconfig

func WithKubeconfig(kubeconfig string) Option

WithKubeconfig sets the kubeconfig file path.

func WithKubernetesFactory

func WithKubernetesFactory(factory func(*Session) (kubernetes.Interface, error)) Option

WithKubernetesFactory sets a custom Kubernetes client factory, primarily for testing.

func WithNamespace

func WithNamespace(namespace string) Option

WithNamespace sets the Kubernetes namespace.

func WithSpecPath

func WithSpecPath(specPath string) Option

WithSpecPath sets the spec file path.

func WithStorageDriver

func WithStorageDriver(driver string) Option

WithStorageDriver sets the Helm storage driver (default: "secret").

func WithTimeout

func WithTimeout(timeout time.Duration) Option

WithTimeout sets the timeout for Helm operations.

type Session

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

Session holds per-invocation configuration and lazily loads the spec. It is created once in the root pre-run hook and travels through context.Context so every command shares one configured environment.

To access Helm or Kubernetes clients, call Session.Target first.

func FromContext

func FromContext(ctx context.Context) *Session

FromContext extracts the Session from ctx, or nil if absent.

func New

func New(options ...Option) *Session

New constructs a Session with the given functional options.

func (*Session) Close

func (s *Session) Close() error

Close releases memoized resources held by the session.

func (*Session) DebugKeepTempChart

func (s *Session) DebugKeepTempChart() bool

DebugKeepTempChart reports whether temporary chart directories should be kept.

func (*Session) Spec

func (s *Session) Spec(ctx context.Context, environment string) (*spec.Spec, error)

Spec loads and memoizes the spec for the configured path and environment.

func (*Session) Target

func (s *Session) Target(ctx context.Context, env string) (*Cluster, error)

Target resolves the Kubernetes context for env and returns a Cluster from which Helm and Kubernetes clients can be obtained.

Precedence for the kubeContext used by the returned Cluster:

  1. The global --context flag (already stored in s.kubeContext).
  2. The environment's "context" field in the spec (loaded when env is non-empty and the global flag is absent).
  3. The default context from the active kubeconfig (empty string).

If the spec file cannot be loaded when env is non-empty, Target falls back silently to the default context rather than returning an error. Commands that require the spec (e.g. deploy) load it explicitly afterwards and will surface a proper error to the user.

func (*Session) Timeout

func (s *Session) Timeout() time.Duration

Timeout returns the configured timeout for Helm operations.

Jump to

Keyboard shortcuts

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