Documentation
¶
Overview ¶
Package lifecycle provides cluster lifecycle command helpers.
This package contains utilities for building and executing standard cluster lifecycle commands (start, stop, delete, etc.) with consistent messaging, timing, and error handling patterns.
Index ¶
- Variables
- func ExtractClusterNameFromContext(context string, distribution v1alpha1.Distribution) string
- func GetClusterNameFromConfig(clusterCfg *v1alpha1.Cluster, factory clusterprovisioner.Factory) (string, error)
- func HandleRunE(cmd *cobra.Command, cfgManager *ksailconfigmanager.ConfigManager, deps Deps, ...) error
- func NewStandardRunE(runtimeContainer *runtime.Runtime, ...) func(*cobra.Command, []string) error
- func RunWithConfig(cmd *cobra.Command, deps Deps, config Config, clusterCfg *v1alpha1.Cluster) error
- func WrapHandler(runtimeContainer *runtime.Runtime, ...) func(*cobra.Command, []string) error
- type Action
- type Config
- type Deps
Constants ¶
This section is empty.
Variables ¶
var ErrClusterConfigRequired = errors.New("cluster configuration is required")
ErrClusterConfigRequired indicates that a nil cluster configuration was provided.
var ErrMissingClusterProvisionerDependency = errors.New("missing cluster provisioner dependency")
ErrMissingClusterProvisionerDependency indicates that a lifecycle command resolved a nil provisioner.
Functions ¶
func ExtractClusterNameFromContext ¶
func ExtractClusterNameFromContext(context string, distribution v1alpha1.Distribution) string
ExtractClusterNameFromContext extracts the cluster name from a context string. For kind clusters, contexts follow the pattern "kind-<cluster-name>". For k3d clusters, contexts follow the pattern "k3d-<cluster-name>". Returns empty string if the context doesn't match the expected pattern.
func GetClusterNameFromConfig ¶
func GetClusterNameFromConfig( clusterCfg *v1alpha1.Cluster, factory clusterprovisioner.Factory, ) (string, error)
GetClusterNameFromConfig extracts the cluster name from the KSail cluster configuration. When a context is explicitly set, it derives the cluster name from it. Otherwise, it loads the distribution config and extracts the name from there. This function is exported for use in command handlers that need the cluster name for operations beyond the standard lifecycle actions.
func HandleRunE ¶
func HandleRunE( cmd *cobra.Command, cfgManager *ksailconfigmanager.ConfigManager, deps Deps, config Config, ) error
HandleRunE orchestrates the standard lifecycle workflow. It performs the following steps in order:
- Create a new timer stage (config was already loaded in WrapHandler)
- Execute the lifecycle action via RunWithConfig
Note: The cluster configuration is already loaded by WrapHandler, so this function uses the cached config from cfgManager.Config.
func NewStandardRunE ¶
func NewStandardRunE( runtimeContainer *runtime.Runtime, cfgManager *ksailconfigmanager.ConfigManager, config Config, ) func(*cobra.Command, []string) error
NewStandardRunE creates a standard RunE handler for simple lifecycle commands. It handles dependency injection from the runtime container and delegates to HandleRunE with the provided lifecycle configuration.
This is the recommended way to create lifecycle command handlers for standard operations like start, stop, and delete. The returned function can be assigned directly to a cobra.Command's RunE field.
func RunWithConfig ¶
func RunWithConfig( cmd *cobra.Command, deps Deps, config Config, clusterCfg *v1alpha1.Cluster, ) error
RunWithConfig executes a lifecycle command using a pre-loaded cluster configuration. This function is useful when the cluster configuration has already been loaded, avoiding the need to reload it.
It performs the following steps:
- Create the cluster provisioner using the factory
- Extract the cluster name from the distribution config or context
- Execute the lifecycle action
- Display success message with timing information
Returns an error if provisioner creation, cluster name extraction, or the action itself fails.
func WrapHandler ¶
func WrapHandler( runtimeContainer *runtime.Runtime, cfgManager *ksailconfigmanager.ConfigManager, handler func(*cobra.Command, *ksailconfigmanager.ConfigManager, Deps) error, ) func(*cobra.Command, []string) error
WrapHandler resolves lifecycle dependencies from the runtime container and invokes the provided handler function with those dependencies.
This function loads the cluster configuration first, then creates a factory using the cached distribution config from the config manager. This ensures the factory has the proper distribution-specific configuration.
This function is used internally by NewStandardRunE but can also be used directly for custom lifecycle handlers that need dependency injection but require custom logic beyond the standard HandleRunE flow.
Types ¶
type Action ¶
type Action func( ctx context.Context, provisioner clusterprovisioner.ClusterProvisioner, clusterName string, ) error
Action represents a lifecycle operation executed against a cluster provisioner. The action receives a context for cancellation, the provisioner instance, and the cluster name. It returns an error if the lifecycle operation fails.
type Config ¶
type Config struct {
TitleEmoji string
TitleContent string
ActivityContent string
SuccessContent string
ErrorMessagePrefix string
Action Action
}
Config describes the messaging and action behavior for a lifecycle command. It configures the user-facing messages displayed during command execution and specifies the action to perform on the cluster provisioner.