Documentation
¶
Overview ¶
Package session holds per-command Kubernetes and Helm client state.
Session is the invocation handle for one CLI run. It carries global options and travels through context.Context so every command shares one configured environment. Construct one with New, attach it via WithContext, and retrieve it in handlers through FromContext.
Spec and platform source loading is owned by workspace.Workspace. Call Session.Workspace to load those sources. Kubernetes destination resolution is delegated to target.Resolver. Call Session.Target with the environment name to obtain a Cluster that holds the resolved target.Target, a HelmConfig snapshot, client factories, and lazily constructed Helm and Kubernetes clients. Cluster does not embed Session. Cluster REST, Kubernetes, and Helm construction use the Target kubeconfig destination.
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 HelmConfig
- type HelmFactory
- type Option
- func WithDebug(keep bool) Option
- func WithExtraKubeconfigPaths(paths ...string) Option
- func WithHelmFactory(factory HelmFactory) Option
- func WithKubeContext(kubeContext string) Option
- func WithKubeconfig(kubeconfig string) Option
- func WithKubernetesFactory(factory func(*target.Target) (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
Constants ¶
const ( // DefaultTimeout is the default timeout for Helm operations. // It is [spec.DefaultDeployTimeout]. DefaultTimeout = spec.DefaultDeployTimeout // 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 {
// contains filtered or unexported fields
}
Cluster is a resolved destination plus lazily-initialized Helm and Kubernetes clients. Obtain one via Session.Target.
Destination metadata is owned by target.Target. Helm construction uses target.Target.ClientConfig, HelmConfig, and HelmFactory. Kubernetes construction and Cluster.RESTConfig use target.Target.RESTConfig.
Cluster does not embed or depend on Session. Concurrent Cluster.Helm and Cluster.Kubernetes calls are safe.
func (*Cluster) Context ¶ added in v0.4.0
Context returns the effective Kubernetes context for this destination.
func (*Cluster) ContextSource ¶ added in v0.9.0
func (cl *Cluster) ContextSource() target.ContextSource
ContextSource returns which rule selected Cluster.Context.
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) RESTConfig ¶
RESTConfig returns a Kubernetes REST config for the resolved target.Target kubeconfig destination.
type HelmClient ¶
type HelmClient interface {
// IsReachable checks whether the configured Kubernetes cluster is reachable.
IsReachable() error
// InstallApp installs or upgrades an application from [spec.ResolvedSpec].
// Chart content, release name, and labels all come from resolved.
// crds are written into the per-invocation chart copy under crds/.
// skipCRDs maps to Helm Install.SkipCRDs on a fresh install only.
// postRenderer, when non-nil, is applied to the rendered manifests
// before they are sent to the cluster.
InstallApp(ctx context.Context, dryRun bool, resolved *spec.ResolvedSpec, postRenderer postrenderer.PostRenderer, crds []extras.RawFile, skipCRDs bool) error
// RenderManifests renders the chart from [spec.ResolvedSpec] 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.
// crds are written into the per-invocation chart copy under crds/.
// postRenderer, when non-nil, is applied to the rendered manifests.
RenderManifests(ctx context.Context, resolved *spec.ResolvedSpec, postRenderer postrenderer.PostRenderer, crds []extras.RawFile) (*render.RenderResult, func(), error)
// RenderManifestsWithPrep is [HelmClient.RenderManifests] and the
// [helm.ReleasePrep] that picked install or upgrade.
// Cleanup is nil on error. On success the caller runs it once.
RenderManifestsWithPrep(ctx context.Context, resolved *spec.ResolvedSpec, postRenderer postrenderer.PostRenderer, crds []extras.RawFile) (*render.RenderResult, helm.ReleasePrep, 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. Render methods return render.RenderResult.
type HelmConfig ¶ added in v0.9.0
type HelmConfig struct {
// KubeconfigPath is the explicit kubeconfig path snapshotted from
// Session. Destination selection uses [target.Target], not this field.
KubeconfigPath string
// ExtraKubeconfigPaths is the extra kubeconfig path list snapshotted
// from Session. Destination selection uses [target.Target], not this
// field.
ExtraKubeconfigPaths []string
// StorageDriver is the Helm storage driver (secret, configmap, or memory).
// Empty lets [helm.NewClient] use its default ("secret").
StorageDriver string
// Debug reports whether temporary chart directories should be kept.
Debug bool
// Timeout is the Helm operation timeout. Zero lets [helm.NewClient] use
// its default (5 minutes). [Session.New] sets [DefaultTimeout] (10
// minutes) unless [WithTimeout] overrides it.
Timeout time.Duration
}
HelmConfig holds Helm client-construction inputs that are not destination metadata. Context and namespace come from target.Target.
Session.Target snapshots these values onto Cluster. Session accessors such as Session.Timeout keep reading the Session fields.
type HelmFactory ¶ added in v0.9.0
type HelmFactory func(*target.Target, HelmConfig) (HelmClient, error)
HelmFactory constructs a Helm client for a resolved destination. It must not depend on Session.
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 HelmFactory) 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 ¶
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 for one CLI run. It is created once in the root pre-run hook and travels through context.Context so every command shares one configured environment.
Spec and platform source loading is owned by workspace.Workspace. Call Session.Workspace to load sources. Kubernetes destination resolution is delegated to target.Resolver. To access Helm or Kubernetes clients, call Session.Target first.
func FromContext ¶
FromContext extracts the Session from ctx, or nil if absent.
func New ¶
New constructs a Session with the given functional options. The composed workspace.Workspace is created after all options are applied.
func (*Session) CurrentKubeContext ¶ added in v0.4.0
CurrentKubeContext returns the current-context name from 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) 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) Target ¶
Target resolves the Kubernetes destination for env and returns a Cluster from which Helm and Kubernetes clients can be obtained.
Destination resolution is delegated to target.Resolver. The platform file, when present, supplies only a context name via spec.PlatformEnvContext. Platform load errors are ignored so Target can still resolve from kubeconfig. ctx is unused.