Documentation
¶
Overview ¶
Package helpers provides common CLI utilities for command handling.
This package consolidates various CLI helper functions that were previously scattered across multiple small packages (docker, editor, flags, kubeconfig). By grouping these related utilities together, we achieve better package cohesion and reduce fragmentation.
Key functionality:
- Docker client lifecycle management (WithClient, WithClientInstance)
- Editor configuration resolution with proper precedence
- Flag handling utilities including timing detection
- Kubeconfig path resolution with home directory expansion
Index ¶
- Constants
- Variables
- func DetectGitOpsEngine(ctx context.Context) (v1alpha1.GitOpsEngine, error)
- func DetectLocalRegistryPort(ctx context.Context) (int32, error)
- func FormatRegistryURL(host string, port int32, repository string) string
- func GetDefaultKubeconfigPath() string
- func GetKubeconfigPathFromConfig(cfg *v1alpha1.Cluster) (string, error)
- func GetKubeconfigPathSilently() string
- func GetKubeconfigRESTConfig() (*rest.Config, error)
- func IsTimingEnabled(cmd *cobra.Command) (bool, error)
- func MaybeTimer(cmd *cobra.Command, tmr timer.Timer) timer.Timer
- func NewStandardIOStreams() genericiooptions.IOStreams
- func SetupEditorEnv(editorFlag, forCommand string) func()
- func WithDockerClient(cmd *cobra.Command, operation func(client.APIClient) error) error
- func WithDockerClientInstance(cmd *cobra.Command, dockerClient client.APIClient, ...) error
- type ClusterEnvironment
- type DockerResources
- type EditorResolver
- type RegistryInfo
- func DetectRegistryFromArgoCD(ctx context.Context) (*RegistryInfo, error)
- func DetectRegistryFromConfig(cfg *v1alpha1.Cluster) (*RegistryInfo, error)
- func DetectRegistryFromDocker(ctx context.Context, clusterName string) (*RegistryInfo, error)
- func DetectRegistryFromFlux(ctx context.Context) (*RegistryInfo, error)
- func DetectRegistryFromViper(v *viper.Viper) (*RegistryInfo, error)
- func ResolveRegistry(ctx context.Context, opts ResolveRegistryOptions) (*RegistryInfo, error)
- type ResolveRegistryOptions
Constants ¶
const TimingFlagName = "timing"
TimingFlagName is the global/root persistent flag that enables timing output.
const (
// ViperRegistryKey is the viper key for the registry flag/env var.
ViperRegistryKey = "registry"
)
Registry secret constants for GitOps engines.
Variables ¶
var ( // ErrNoRegistryFound is returned when no registry can be detected from any source. ErrNoRegistryFound = errors.New( "unable to detect registry; provide --registry flag, set KSAIL_REGISTRY, or configure local-registry", ) // ErrViperNil is returned when a nil viper instance is provided. ErrViperNil = errors.New("viper instance is nil") // ErrRegistryNotSet is returned when registry is not set via flag or environment. ErrRegistryNotSet = errors.New("registry not set via flag or environment variable") // ErrLocalRegistryNotConfigured is returned when local registry is not in config. ErrLocalRegistryNotConfigured = errors.New("local registry not configured in ksail.yaml") // ErrFluxNoSyncURL is returned when FluxInstance has no sync.url. ErrFluxNoSyncURL = errors.New("FluxInstance has no sync.url configured") // ErrArgoCDNoRepoURL is returned when ArgoCD Application has no source.repoURL. ErrArgoCDNoRepoURL = errors.New("ArgoCD Application has no source.repoURL configured") // ErrEmptyOCIURL is returned when an empty OCI URL is provided. ErrEmptyOCIURL = errors.New("empty OCI URL") )
Static errors for registry detection.
var ErrNoGitOpsEngine = errors.New(
"no GitOps engine detected in cluster; " +
"create a cluster with '--gitops-engine Flux|ArgoCD' during cluster init",
)
ErrNoGitOpsEngine is returned when no GitOps engine is detected.
var ErrNoLocalRegistry = errors.New(
"no running local registry found; " +
"create a cluster with '--local-registry' during cluster init",
)
ErrNoLocalRegistry is returned when no local registry container is found.
Functions ¶
func DetectGitOpsEngine ¶ added in v5.13.0
func DetectGitOpsEngine(ctx context.Context) (v1alpha1.GitOpsEngine, error)
DetectGitOpsEngine checks if Flux or ArgoCD is deployed in the cluster.
func DetectLocalRegistryPort ¶ added in v5.13.0
DetectLocalRegistryPort finds the host port of a running local-registry container. It checks for cluster-prefixed registries (e.g., "kind-local-registry", "k3d-default-local-registry") by searching for containers matching the "*-local-registry" pattern. Both KSail-managed registries (with labels) and K3d-managed registries are detected.
func FormatRegistryURL ¶ added in v5.19.0
FormatRegistryURL formats a registry URL using net.JoinHostPort for proper host:port handling.
func GetDefaultKubeconfigPath ¶
func GetDefaultKubeconfigPath() string
GetDefaultKubeconfigPath returns the default kubeconfig path for the current user. The path is constructed as ~/.kube/config using the user's home directory.
func GetKubeconfigPathFromConfig ¶
GetKubeconfigPathFromConfig extracts and expands the kubeconfig path from a loaded cluster config. If the config doesn't specify a kubeconfig path, it returns the default path from GetDefaultKubeconfigPath.
The function always expands tilde (~) characters in the path to the user's home directory, regardless of whether the path came from the config or is the default.
Returns an error if path expansion fails.
func GetKubeconfigPathSilently ¶
func GetKubeconfigPathSilently() string
GetKubeconfigPathSilently attempts to load the KSail config and extract the kubeconfig path without producing any output. All config loading output is suppressed using io.Discard.
If config loading fails for any reason, this function returns the default kubeconfig path rather than propagating the error. This makes it suitable for scenarios where a best-effort path is acceptable.
func GetKubeconfigRESTConfig ¶ added in v5.19.0
GetKubeconfigRESTConfig loads the kubeconfig and returns a REST config for Kubernetes clients. This is used by both kubernetes.Clientset and dynamic.Client creation.
func IsTimingEnabled ¶
IsTimingEnabled reports whether the current command invocation has timing enabled.
The flag is defined as a root persistent flag and inherited by subcommands.
func MaybeTimer ¶
MaybeTimer returns the provided timer when timing output is enabled.
When timing is disabled (or the flag is unavailable), it returns nil.
func NewStandardIOStreams ¶ added in v5.13.0
func NewStandardIOStreams() genericiooptions.IOStreams
NewStandardIOStreams creates IOStreams for standard input/output/error. This is used by kubectl-based commands across the CLI.
func SetupEditorEnv ¶
func SetupEditorEnv(editorFlag, forCommand string) func()
SetupEditorEnv sets up the editor environment variables based on flag and config. It returns a cleanup function that should be called to restore the original environment.
func WithDockerClient ¶
WithDockerClient creates a Docker client, executes the given operation function, and ensures cleanup. The Docker client is automatically closed after the operation completes, regardless of success or failure.
This function is suitable for production use. For testing with mock clients, use WithDockerClientInstance instead.
Returns an error if client creation fails or if the operation function returns an error.
func WithDockerClientInstance ¶
func WithDockerClientInstance( cmd *cobra.Command, dockerClient client.APIClient, operation func(client.APIClient) error, ) error
WithDockerClientInstance executes an operation with a provided Docker client and handles cleanup. The client will be closed after the operation completes, even if the operation returns an error.
This function is particularly useful for testing with mock clients, as it allows you to provide a pre-configured client instance. Any error during client cleanup is logged but does not cause the function to return an error if the operation itself succeeded.
Types ¶
type ClusterEnvironment ¶ added in v5.13.0
type ClusterEnvironment struct {
RegistryPort int32
GitOpsEngine v1alpha1.GitOpsEngine
}
ClusterEnvironment holds auto-detected cluster configuration from the running environment.
func DetectClusterEnvironment ¶ added in v5.13.0
func DetectClusterEnvironment(ctx context.Context) (*ClusterEnvironment, error)
DetectClusterEnvironment auto-detects the registry port and GitOps engine from the running Docker containers and Kubernetes cluster.
type DockerResources ¶ added in v5.19.0
type DockerResources struct {
Client client.APIClient
RegistryManager *dockerclient.RegistryManager
}
DockerResources holds Docker client and registry manager for cleanup.
func NewDockerRegistryManager ¶ added in v5.19.0
func NewDockerRegistryManager() (*DockerResources, error)
NewDockerRegistryManager creates a Docker client and registry manager. The caller is responsible for calling Close() on the returned resources.
func (*DockerResources) Close ¶ added in v5.19.0
func (r *DockerResources) Close()
Close releases the Docker client resources.
type EditorResolver ¶
type EditorResolver struct {
// contains filtered or unexported fields
}
EditorResolver handles editor configuration resolution with proper precedence.
func NewEditorResolver ¶
func NewEditorResolver(flagEditor string, cfg *v1alpha1.Cluster) *EditorResolver
NewEditorResolver creates a new editor resolver.
func (*EditorResolver) Resolve ¶
func (r *EditorResolver) Resolve() string
Resolve resolves the editor command based on precedence: 1. --editor flag 2. spec.editor from config 3. Environment variables (SOPS_EDITOR, KUBE_EDITOR, EDITOR, VISUAL) 4. Fallback to vim, nano, vi.
func (*EditorResolver) SetEnvVars ¶
func (r *EditorResolver) SetEnvVars(editorCmd string, forCommand string) func()
SetEnvVars sets the appropriate environment variables for the resolved editor. It returns a cleanup function that restores the original environment.
type RegistryInfo ¶ added in v5.19.0
type RegistryInfo struct {
Host string
Port int32
Repository string
Tag string
Username string
Password string
// IsExternal is true if the registry is external (e.g., ghcr.io) vs local Docker registry
IsExternal bool
// Source describes where the registry info was detected from
Source string
}
RegistryInfo contains detected registry information.
func DetectRegistryFromArgoCD ¶ added in v5.19.0
func DetectRegistryFromArgoCD(ctx context.Context) (*RegistryInfo, error)
DetectRegistryFromArgoCD tries to get registry URL from ArgoCD Application source.
func DetectRegistryFromConfig ¶ added in v5.19.0
func DetectRegistryFromConfig(cfg *v1alpha1.Cluster) (*RegistryInfo, error)
DetectRegistryFromConfig extracts registry info from ksail cluster configuration.
func DetectRegistryFromDocker ¶ added in v5.19.0
func DetectRegistryFromDocker(ctx context.Context, clusterName string) (*RegistryInfo, error)
DetectRegistryFromDocker tries to find a local registry Docker container.
func DetectRegistryFromFlux ¶ added in v5.19.0
func DetectRegistryFromFlux(ctx context.Context) (*RegistryInfo, error)
DetectRegistryFromFlux tries to get registry URL from FluxInstance sync configuration.
func DetectRegistryFromViper ¶ added in v5.19.0
func DetectRegistryFromViper(v *viper.Viper) (*RegistryInfo, error)
DetectRegistryFromViper checks for registry configuration from a Viper instance. This handles both --registry flag and KSAIL_REGISTRY environment variable since Viper binds them together.
func ResolveRegistry ¶ added in v5.19.0
func ResolveRegistry(ctx context.Context, opts ResolveRegistryOptions) (*RegistryInfo, error)
ResolveRegistry resolves registry configuration using a priority-based approach. Priority order: 1. CLI flag or env var via Viper (--registry / KSAIL_REGISTRY). 2. Config file (ksail.yaml localRegistry). 3. Cluster GitOps resources (FluxInstance or ArgoCD Application). 4. Docker containers (matching cluster name). 5. Error (no registry found).
type ResolveRegistryOptions ¶ added in v5.19.0
type ResolveRegistryOptions struct {
// Viper is the viper instance with bound flags and env vars.
// If provided, it's used to resolve registry from --registry flag or KSAIL_REGISTRY env var.
Viper *viper.Viper
// ClusterConfig is the parsed ksail.yaml configuration
ClusterConfig *v1alpha1.Cluster
// ClusterName is the name of the cluster (used for Docker container lookup)
ClusterName string
}
ResolveRegistryOptions contains configuration for registry resolution.