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
- func GetValidStorageDrivers() []string
- func ValidateStorageDriver(driver string) bool
- func ValidateTimeout(timeout time.Duration) bool
- func WithContext(ctx context.Context, sess *Session) context.Context
- type Cluster
- type HelmClient
- type Option
- func WithDebug(keep bool) Option
- func WithExtraKubeconfigPaths(paths ...string) Option
- func WithHelmFactory(factory func(*Session) (HelmClient, error)) Option
- func WithKubeContext(kubeContext string) Option
- func WithKubeconfig(kubeconfig string) Option
- func WithKubernetesFactory(factory func(*Session) (kubernetes.Interface, error)) Option
- func WithNamespace(namespace string) Option
- func WithSpecPath(specPath string) Option
- func WithStorageDriver(driver string) Option
- func WithTimeout(timeout time.Duration) Option
- type Session
Constants ¶
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.
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.
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 ¶
ValidateStorageDriver reports whether the storage driver is valid.
func ValidateTimeout ¶
ValidateTimeout reports whether timeout is within acceptable bounds.
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 ¶
Namespace returns the configured namespace, or "default" if none is set.
func (*Cluster) RESTConfig ¶
RESTConfig returns a Kubernetes REST config for the resolved cluster.
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 WithExtraKubeconfigPaths ¶
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 ¶
WithKubeContext sets the Kubernetes context, overriding the kubeconfig's current context. An empty value leaves the current context in effect.
func WithKubeconfig ¶
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 ¶
WithNamespace sets the Kubernetes namespace.
func WithStorageDriver ¶
WithStorageDriver sets the Helm storage driver (default: "secret").
func WithTimeout ¶
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 ¶
FromContext extracts the Session from ctx, or nil if absent.
func (*Session) DebugKeepTempChart ¶
DebugKeepTempChart reports whether temporary chart directories should be kept.
func (*Session) Target ¶
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:
- The global --context flag (already stored in s.kubeContext).
- The environment's "context" field in the spec (loaded when env is non-empty and the global flag is absent).
- 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.