runtime

package
v0.1.1 Latest Latest
Warning

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

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

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

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"
)

Runtime 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 Configuration

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

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 ensures the storage driver is valid.

func ValidateTimeout

func ValidateTimeout(timeout time.Duration) bool

ValidateTimeout ensures timeout is within acceptable bounds.

func WithContext

func WithContext(ctx context.Context, rt *Runtime) context.Context

WithContext returns a new context carrying the provided runtime.

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 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. 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

func WithKubeContext(kubeContext string) Option

WithKubeContext sets the Kubernetes context to use, 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(*Runtime) (KubernetesClient, error)) Option

WithKubernetesFactory sets a custom Kubernetes client factory for testing.

func WithLogger

func WithLogger(logger LoggerProvider) Option

WithLogger sets a custom logger.

func WithManifestPath

func WithManifestPath(manifestPath string) Option

WithManifestPath sets the manifest file path.

func WithNamespace

func WithNamespace(namespace string) Option

WithNamespace sets the Kubernetes namespace.

func WithStorageDriver

func WithStorageDriver(driver string) Option

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

func WithTimeout

func WithTimeout(timeout time.Duration) Option

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

func FromContext(ctx context.Context) *Runtime

FromContext extracts a Runtime from the command context, or nil if absent.

func New

func New(options ...Option) *Runtime

New constructs a Runtime with functional options.

func (*Runtime) Close

func (r *Runtime) Close() error

Close performs cleanup of resources held by the runtime. It's safe to call multiple times.

func (*Runtime) DebugKeepTempChart

func (r *Runtime) DebugKeepTempChart() bool

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

func (r *Runtime) Manifest(ctx context.Context, environment string) (*manifest.Manifest, error)

Manifest loads and memoizes the manifest for the configured path and environment.

func (*Runtime) Namespace

func (r *Runtime) Namespace() string

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

func (*Runtime) RESTConfig

func (r *Runtime) RESTConfig() (*rest.Config, error)

RESTConfig returns a Kubernetes REST config using the same logic as the runtime Kubernetes client.

func (*Runtime) SetKubeContext

func (r *Runtime) SetKubeContext(kubeContext string)

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.

func (*Runtime) Timeout

func (r *Runtime) Timeout() time.Duration

Timeout returns the configured timeout for Helm operations.

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.

Jump to

Keyboard shortcuts

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