Documentation
¶
Overview ¶
Package runtime holds per-command Kubernetes and Helm client state.
Runtime lazily constructs Helm and Kubernetes clients from global CLI options, caches the loaded manifest, and travels through context.Context so every command shares one configured environment per invocation.
Construct a runtime with New, attach it via WithContext, and retrieve it in handlers through FromContext.
Index ¶
- Constants
- func GetValidStorageDrivers() []string
- func ValidateStorageDriver(driver string) bool
- func ValidateTimeout(timeout time.Duration) bool
- func WithContext(ctx context.Context, rt *Runtime) context.Context
- type HelmClient
- type KubernetesClient
- type LoggerProvider
- type ManifestLoader
- type Option
- func WithDebug(keep bool) Option
- func WithExtraKubeconfigPaths(paths ...string) Option
- func WithHelmFactory(factory func(*Runtime) (HelmClient, error)) Option
- func WithKubeContext(kubeContext string) Option
- func WithKubeconfig(kubeconfig string) Option
- func WithKubernetesFactory(factory func(*Runtime) (KubernetesClient, error)) Option
- func WithLogger(logger LoggerProvider) Option
- func WithManifestPath(manifestPath string) Option
- func WithNamespace(namespace string) Option
- func WithStorageDriver(driver string) Option
- func WithTimeout(timeout time.Duration) Option
- type Runtime
- func (r *Runtime) Close() error
- func (r *Runtime) DebugKeepTempChart() bool
- func (r *Runtime) Helm() (HelmClient, error)
- func (r *Runtime) Kubernetes() (KubernetesClient, error)
- func (r *Runtime) Manifest(ctx context.Context, environment string) (*manifest.Manifest, error)
- func (r *Runtime) Namespace() string
- func (r *Runtime) RESTConfig() (*rest.Config, error)
- func (r *Runtime) SetKubeContext(kubeContext string)
- func (r *Runtime) Timeout() time.Duration
- type RuntimeProvider
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" )
Runtime 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 Configuration
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
Variables ¶
This section is empty.
Functions ¶
func GetValidStorageDrivers ¶
func GetValidStorageDrivers() []string
GetValidStorageDrivers returns a list of valid storage drivers.
func ValidateStorageDriver ¶
ValidateStorageDriver ensures the storage driver is valid.
func ValidateTimeout ¶
ValidateTimeout ensures timeout is within acceptable bounds.
Types ¶
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 *manifest.Manifest, environment string, dryRun bool) error
// DeleteRelease uninstalls a Helm release
DeleteRelease(ctx context.Context, project, environment string) 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. This abstraction allows for easier testing and alternative implementations.
type KubernetesClient ¶
type KubernetesClient interface {
kubernetes.Interface
}
KubernetesClient defines the interface for Kubernetes operations. This abstraction enables testing with mock Kubernetes clients.
type LoggerProvider ¶
type LoggerProvider interface {
// Debug logs a debug-level message
Debug(msg string, keyvals ...any)
// Info logs an info-level message
Info(msg string, keyvals ...any)
// Warn logs a warning-level message
Warn(msg string, keyvals ...any)
// Error logs an error-level message
Error(msg string, keyvals ...any)
// Fatal logs a fatal-level message and exits
Fatal(msg string, keyvals ...any)
// With returns a new logger with the given key-value pairs
With(keyvals ...any) LoggerProvider
}
LoggerProvider defines the interface for logging operations. This enables structured logging with different implementations and levels.
type ManifestLoader ¶
type ManifestLoader interface {
// Load reads and parses a manifest file
Load(ctx context.Context, path, envName string) (*manifest.Manifest, error)
// Save writes a manifest to a file
Save(manifest *manifest.Manifest, path string) error
// Validate validates a manifest against its schema
Validate(manifest *manifest.Manifest) error
}
ManifestLoader defines the interface for manifest loading operations. This enables testing with mock loaders and different loading strategies.
type Option ¶
type Option func(*Runtime)
Option defines a functional option for configuring Runtime.
func WithExtraKubeconfigPaths ¶
WithExtraKubeconfigPaths appends additional kubeconfig file paths to the clientcmd loading-rules Precedence list. This makes 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(*Runtime) (HelmClient, error)) Option
WithHelmFactory sets a custom Helm client factory for testing.
func WithKubeContext ¶
WithKubeContext sets the Kubernetes context to use, 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(*Runtime) (KubernetesClient, error)) Option
WithKubernetesFactory sets a custom Kubernetes client factory for testing.
func WithManifestPath ¶
WithManifestPath sets the manifest file path.
func WithNamespace ¶
WithNamespace sets the Kubernetes namespace.
func WithStorageDriver ¶
WithStorageDriver sets the storage driver (default: "secret").
func WithTimeout ¶
WithTimeout sets the timeout for Helm operations.
type Runtime ¶
type Runtime struct {
// contains filtered or unexported fields
}
Runtime holds per-invocation state and lazily initialized clients/resources. It implements the RuntimeProvider interface for dependency injection.
func FromContext ¶
FromContext extracts a Runtime from the command context, or nil if absent.
func (*Runtime) Close ¶
Close performs cleanup of resources held by the runtime. It's safe to call multiple times.
func (*Runtime) DebugKeepTempChart ¶
DebugKeepTempChart returns whether temporary chart directories should be kept.
func (*Runtime) Helm ¶
func (r *Runtime) Helm() (HelmClient, error)
Helm returns a memoized Helm client configured for this runtime.
func (*Runtime) Kubernetes ¶
func (r *Runtime) Kubernetes() (KubernetesClient, error)
Kubernetes returns a memoized Kubernetes clientset configured for this runtime.
func (*Runtime) Manifest ¶
Manifest loads and memoizes the manifest for the configured path and environment.
func (*Runtime) Namespace ¶
Namespace returns the configured namespace, or "default" if none is set.
func (*Runtime) RESTConfig ¶
RESTConfig returns a Kubernetes REST config using the same logic as the runtime Kubernetes client.
func (*Runtime) SetKubeContext ¶
SetKubeContext sets the Kubernetes context to target and invalidates any memoized clients so they are rebuilt with the new context on next use.
It is intended to be called after the manifest is loaded (so an environment's "context" field can be applied) but before the Helm or Kubernetes clients are first built. A non-empty context set here takes effect; callers enforce precedence (flag over environment field) by only passing the resolved value.
type RuntimeProvider ¶
type RuntimeProvider interface {
// Helm returns a configured Helm client for Kubernetes operations
Helm() (HelmClient, error)
// Kubernetes returns a configured Kubernetes clientset
Kubernetes() (KubernetesClient, error)
// Manifest loads and returns the parsed manifest for the given environment
Manifest(ctx context.Context, environment string) (*manifest.Manifest, error)
// DebugKeepTempChart returns whether temporary chart directories should be kept
DebugKeepTempChart() bool
// Close performs cleanup of resources held by the runtime
Close() error
}
RuntimeProvider defines the interface for runtime dependency management. This interface enables better testability by allowing mock implementations and provides a clear contract for runtime services.