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 CommandPolicy
- type HelmClient
- type Option
- func WithCommandPolicy(policy CommandPolicy) 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 WithPlatformFile(path string) Option
- func WithSpecPath(specPath string) Option
- func WithStorageDriver(driver string) Option
- func WithTimeout(timeout time.Duration) Option
- type Session
- func (s *Session) Close() error
- func (s *Session) CommandPolicy() CommandPolicy
- func (s *Session) CurrentKubeContext() string
- func (s *Session) DebugKeepTempChart() bool
- func (s *Session) KubeContext() string
- func (s *Session) ParseManifest() (*spec.Spec, error)
- func (s *Session) Platform() (*spec.PlatformConfig, error)
- func (s *Session) Spec(ctx context.Context, environment string) (*spec.Spec, error)
- func (s *Session) SpecPath() string
- func (s *Session) Target(ctx context.Context, env string) (*Cluster, error)
- func (s *Session) Timeout() time.Duration
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) Context ¶ added in v0.4.0
Context returns the resolved Kubernetes context for this cluster target. An empty string means the default context from the active kubeconfig is used.
func (*Cluster) ContextFallback ¶ added in v0.4.0
ContextFallback reports whether the target follows the kubeconfig current-context, and that context's name.
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.
type CommandPolicy ¶ added in v0.4.0
type CommandPolicy int
CommandPolicy controls how the session handles missing platform files.
const ( // PolicyLenient allows proceeding with a warning when the platform file // is absent. Used for read-only commands (logs, status, list, shell). PolicyLenient CommandPolicy = iota // PolicyStrict requires a resolvable platform file for commands that // modify cluster state (deploy, delete). The caller must gate on this // before contacting the cluster. PolicyStrict )
type HelmClient ¶
type HelmClient interface {
// IsReachable checks whether the configured Kubernetes cluster is reachable.
IsReachable() error
// InstallApp installs or upgrades an application using Helm. When resolved
// is non-nil, TLS and hostname values are sourced from it rather than the
// raw spec. postRenderer, when non-nil, is applied to the rendered
// manifests before they are sent to the cluster.
InstallApp(ctx context.Context, manifest *spec.Spec, environment string, dryRun bool, resolved *spec.ResolvedSpec, postRenderer postrenderer.PostRenderer) error
// RenderManifests renders the chart for manifest/environment client-side,
// without mutating the cluster or Helm's release history. The caller must
// run the returned cleanup func once done with the result's ChartPath.
// postRenderer, when non-nil, is applied to the rendered manifests.
RenderManifests(ctx context.Context, manifest *spec.Spec, environment string, resolved *spec.ResolvedSpec, postRenderer postrenderer.PostRenderer) (*render.RenderResult, func(), error)
// RenderOffline renders the chart for manifest/environment as a fresh
// install, without any Kubernetes API access. The caller must run the
// returned cleanup func once done with the result's ChartPath.
// postRenderer, when non-nil, is applied to the rendered manifests.
RenderOffline(ctx context.Context, manifest *spec.Spec, environment string, resolved *spec.ResolvedSpec, postRenderer postrenderer.PostRenderer) (*render.RenderResult, func(), 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 is kept in this package so WithHelmFactory tests can inject a mock implementation without importing the concrete helm package. Render methods return render.RenderResult for the same reason.
type Option ¶
type Option func(*Session)
Option is a functional option for configuring a Session.
func WithCommandPolicy ¶ added in v0.4.0
func WithCommandPolicy(policy CommandPolicy) Option
WithCommandPolicy sets the platform-missing policy for this session. Destructive commands (deploy, delete) should use PolicyStrict; read-only commands (logs, status, list, shell) should use PolicyLenient.
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 WithPlatformFile ¶ added in v0.4.0
WithPlatformFile sets an explicit platform file path, overriding both the DEPLOYAH_PLATFORM_FILE environment variable and the same-directory default.
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) CommandPolicy ¶ added in v0.4.0
func (s *Session) CommandPolicy() CommandPolicy
CommandPolicy returns the configured platform-missing policy.
func (*Session) CurrentKubeContext ¶ added in v0.4.0
CurrentKubeContext returns the current-context name from the active kubeconfig resolution (explicit --kubeconfig path, deployah-managed extra paths, then KUBECONFIG/~/.kube/config), ignoring any --context override. Returns an empty string when no kubeconfig is readable.
func (*Session) DebugKeepTempChart ¶
DebugKeepTempChart reports whether temporary chart directories should be kept.
func (*Session) KubeContext ¶ added in v0.4.0
KubeContext returns the explicit kube context override, or empty string if none was set. An empty string means the cluster context comes from the platform file or kubeconfig default.
func (*Session) ParseManifest ¶ added in v0.4.0
ParseManifest reads and partially validates the spec (apiVersion + environments only, no envsubst, no defaults). It is intended for commands that need the raw manifest structure without environment-specific processing (e.g. validate manifest-only mode, resolve offline mode).
func (*Session) Platform ¶ added in v0.4.0
func (s *Session) Platform() (*spec.PlatformConfig, error)
Platform loads and memoizes the platform configuration. It resolves the platform file path from (in order of precedence):
- An explicit path set via WithPlatformFile.
- The DEPLOYAH_PLATFORM_FILE environment variable.
- The same directory as the spec file (deployah.platform.yaml).
When no platform file is found, Platform returns (nil, nil). Only when a path is found but fails to load does it return an error.
func (*Session) Spec ¶
Spec loads the spec for the configured path and environment. Each call loads from disk because the result depends on the environment argument (envsubst selects different env files per environment). The platform config, when present, supplies the environment registry.
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 platform file's context for env (via [PlatformEnvContext]).
- The default context from the active kubeconfig (empty string).