diff --git a/cmd/remote/longhornctl.go b/cmd/remote/longhornctl.go index 3c4750eb..279bf6f7 100644 --- a/cmd/remote/longhornctl.go +++ b/cmd/remote/longhornctl.go @@ -39,7 +39,7 @@ func newCmdLonghornctl() *cobra.Command { cmd.CompletionOptions.HiddenDefaultCmd = true cmd.PersistentFlags().StringVarP(&globalOpts.LogLevel, consts.CmdOptLogLevel, "l", "info", "log level (trace, debug, info, warn, error, fatal, panic)") - cmd.PersistentFlags().StringVar(&globalOpts.KubeConfigPath, consts.CmdOptKubeConfigPath, os.Getenv(consts.EnvKubeConfigPath), "Kubernetes config (kubeconfig) path") + cmd.PersistentFlags().StringVar(&globalOpts.KubeConfigPath, consts.CmdOptKubeConfigPath, "", "Kubernetes config (kubeconfig) path. If not set, $KUBECONFIG or ~/.kube/config is used") cmd.PersistentFlags().StringVar(&globalOpts.Image, consts.CmdOptImage, consts.ImageLonghornCli, "Image containing longhornctl-local") cmd.PersistentFlags().StringVar(&globalOpts.ImageRegistry, consts.CmdOptImageRegistry, "", "Registry to apply to all images (CLI, engine, pause, BCI, etc.), replacing any registry already specified in those images.") cmd.PersistentFlags().StringVar(&globalOpts.ImagePullSecret, consts.CmdOptImagePullSecret, "", "Secret with registry credentials for pulling images") diff --git a/cmd/remote/subcmd/diagnose.go b/cmd/remote/subcmd/diagnose.go index 8b81093f..7f95deb9 100644 --- a/cmd/remote/subcmd/diagnose.go +++ b/cmd/remote/subcmd/diagnose.go @@ -83,7 +83,7 @@ $ longhornctl diagnose -o json 2>/dev/null | jq '.[] | select(.status == "fail") } cmd.PersistentFlags().StringVarP(&globalOpts.LogLevel, consts.CmdOptLogLevel, "l", globalOpts.LogLevel, "Log level") - cmd.PersistentFlags().StringVar(&globalOpts.KubeConfigPath, consts.CmdOptKubeConfigPath, globalOpts.KubeConfigPath, "Kubernetes config (kubeconfig) path") + cmd.PersistentFlags().StringVar(&globalOpts.KubeConfigPath, consts.CmdOptKubeConfigPath, globalOpts.KubeConfigPath, "Kubernetes config (kubeconfig) path. If not set, $KUBECONFIG or ~/.kube/config is used") cmd.PersistentFlags().StringVar(&globalOpts.Namespace, consts.CmdOptNamespace, globalOpts.Namespace, "The namespace where Longhorn is installed.") cmd.Flags().StringVarP(&outputFormat, consts.CmdOptOutput, "o", diagnose.OutputFormatYAML, fmt.Sprintf("Output format of the results. One of: %v.", strings.Join(diagnose.OutputFormats, ", "))) diff --git a/docs/longhornctl.md b/docs/longhornctl.md index 64ff2a97..c79fb803 100644 --- a/docs/longhornctl.md +++ b/docs/longhornctl.md @@ -13,7 +13,7 @@ A CLI tool for troubleshooting and managing Longhorn operations. --image string Image containing longhornctl-local (default "longhornio/longhorn-cli:v1.13.0-dev") --image-pull-secret string Secret with registry credentials for pulling images --image-registry string Registry to apply to all images (CLI, engine, pause, BCI, etc.), replacing any registry already specified in those images. - --kubeconfig string Kubernetes config (kubeconfig) path + --kubeconfig string Kubernetes config (kubeconfig) path. If not set, $KUBECONFIG or ~/.kube/config is used -l, --log-level string log level (trace, debug, info, warn, error, fatal, panic) (default "info") --namespace string The namespace to run DaemonSet pods. (default "longhorn-system") --node-selector string Comma-separated list of key=value pairs to match against node labels, selecting the nodes the DaemonSet will run on (e.g. env=prod,zone=us-west). diff --git a/docs/longhornctl_check.md b/docs/longhornctl_check.md index d481efb5..3884e0a2 100644 --- a/docs/longhornctl_check.md +++ b/docs/longhornctl_check.md @@ -9,7 +9,7 @@ Longhorn checking operations --image string Image containing longhornctl-local (default "longhornio/longhorn-cli:v1.13.0-dev") --image-pull-secret string Secret with registry credentials for pulling images --image-registry string Registry to apply to all images (CLI, engine, pause, BCI, etc.), replacing any registry already specified in those images. - --kubeconfig string Kubernetes config (kubeconfig) path + --kubeconfig string Kubernetes config (kubeconfig) path. If not set, $KUBECONFIG or ~/.kube/config is used -l, --log-level string Log level (default "info") --namespace string The namespace to run DaemonSet pods. (default "longhorn-system") --node-selector string Comma-separated list of key=value pairs to match against node labels, selecting the nodes the DaemonSet will run on (e.g. env=prod,zone=us-west). diff --git a/docs/longhornctl_check_preflight.md b/docs/longhornctl_check_preflight.md index 7bd2d2d6..df4fecab 100644 --- a/docs/longhornctl_check_preflight.md +++ b/docs/longhornctl_check_preflight.md @@ -49,7 +49,7 @@ INFO[2024-07-16T17:17:42+08:00] Completed preflight checker --image string Image containing longhornctl-local (default "longhornio/longhorn-cli:v1.13.0-dev") --image-pull-secret string Secret with registry credentials for pulling images --image-registry string Registry to apply to all images (CLI, engine, pause, BCI, etc.), replacing any registry already specified in those images. - --kubeconfig string Kubernetes config (kubeconfig) path + --kubeconfig string Kubernetes config (kubeconfig) path. If not set, $KUBECONFIG or ~/.kube/config is used -l, --log-level string Log level (default "info") --namespace string The namespace to run DaemonSet pods. (default "longhorn-system") --node-selector string Comma-separated list of key=value pairs to match against node labels, selecting the nodes the DaemonSet will run on (e.g. env=prod,zone=us-west). diff --git a/docs/longhornctl_checksum.md b/docs/longhornctl_checksum.md index 109f8b37..5c68c6a6 100644 --- a/docs/longhornctl_checksum.md +++ b/docs/longhornctl_checksum.md @@ -9,7 +9,7 @@ Snapshot checksum operations --image string Image containing longhornctl-local (default "longhornio/longhorn-cli:v1.13.0-dev") --image-pull-secret string Secret with registry credentials for pulling images --image-registry string Registry to apply to all images (CLI, engine, pause, BCI, etc.), replacing any registry already specified in those images. - --kubeconfig string Kubernetes config (kubeconfig) path + --kubeconfig string Kubernetes config (kubeconfig) path. If not set, $KUBECONFIG or ~/.kube/config is used -l, --log-level string Log level (default "info") --namespace string The namespace to run DaemonSet pods. (default "longhorn-system") --node-selector string Comma-separated list of key=value pairs to match against node labels, selecting the nodes the DaemonSet will run on (e.g. env=prod,zone=us-west). diff --git a/docs/longhornctl_checksum_volume.md b/docs/longhornctl_checksum_volume.md index 531cc352..2ee7465d 100644 --- a/docs/longhornctl_checksum_volume.md +++ b/docs/longhornctl_checksum_volume.md @@ -30,7 +30,7 @@ $ longhornctl checksum volume --name=v1 --namespace=longhorn-system --image string Image containing longhornctl-local (default "longhornio/longhorn-cli:v1.13.0-dev") --image-pull-secret string Secret with registry credentials for pulling images --image-registry string Registry to apply to all images (CLI, engine, pause, BCI, etc.), replacing any registry already specified in those images. - --kubeconfig string Kubernetes config (kubeconfig) path + --kubeconfig string Kubernetes config (kubeconfig) path. If not set, $KUBECONFIG or ~/.kube/config is used -l, --log-level string Log level (default "info") --name string Name of the Longhorn volume --namespace string The namespace to run DaemonSet pods. (default "longhorn-system") diff --git a/docs/longhornctl_diagnose.md b/docs/longhornctl_diagnose.md index ddfc240b..9f61ad99 100644 --- a/docs/longhornctl_diagnose.md +++ b/docs/longhornctl_diagnose.md @@ -39,7 +39,7 @@ $ longhornctl diagnose -o json 2>/dev/null | jq '.[] | select(.status == "fail") ``` -h, --help help for diagnose - --kubeconfig string Kubernetes config (kubeconfig) path + --kubeconfig string Kubernetes config (kubeconfig) path. If not set, $KUBECONFIG or ~/.kube/config is used -l, --log-level string Log level (default "info") --namespace string The namespace where Longhorn is installed. (default "longhorn-system") -o, --output string Output format of the results. One of: yaml, json. (default "yaml") diff --git a/docs/longhornctl_doc.md b/docs/longhornctl_doc.md index 5ae9d6ba..85202ec1 100644 --- a/docs/longhornctl_doc.md +++ b/docs/longhornctl_doc.md @@ -22,7 +22,7 @@ longhornctl doc [output directory] [flags] --image string Image containing longhornctl-local (default "longhornio/longhorn-cli:v1.13.0-dev") --image-pull-secret string Secret with registry credentials for pulling images --image-registry string Registry to apply to all images (CLI, engine, pause, BCI, etc.), replacing any registry already specified in those images. - --kubeconfig string Kubernetes config (kubeconfig) path + --kubeconfig string Kubernetes config (kubeconfig) path. If not set, $KUBECONFIG or ~/.kube/config is used -l, --log-level string log level (trace, debug, info, warn, error, fatal, panic) (default "info") --namespace string The namespace to run DaemonSet pods. (default "longhorn-system") --node-selector string Comma-separated list of key=value pairs to match against node labels, selecting the nodes the DaemonSet will run on (e.g. env=prod,zone=us-west). diff --git a/docs/longhornctl_export.md b/docs/longhornctl_export.md index 43fd4bee..2f770b83 100644 --- a/docs/longhornctl_export.md +++ b/docs/longhornctl_export.md @@ -9,7 +9,7 @@ Export Longhorn resources --image string Image containing longhornctl-local (default "longhornio/longhorn-cli:v1.13.0-dev") --image-pull-secret string Secret with registry credentials for pulling images --image-registry string Registry to apply to all images (CLI, engine, pause, BCI, etc.), replacing any registry already specified in those images. - --kubeconfig string Kubernetes config (kubeconfig) path + --kubeconfig string Kubernetes config (kubeconfig) path. If not set, $KUBECONFIG or ~/.kube/config is used -l, --log-level string Log level (default "info") --namespace string The namespace to run DaemonSet pods. (default "longhorn-system") --node-selector string Comma-separated list of key=value pairs to match against node labels, selecting the nodes the DaemonSet will run on (e.g. env=prod,zone=us-west). diff --git a/docs/longhornctl_export_replica.md b/docs/longhornctl_export_replica.md index 8f155ee7..b8970ee9 100644 --- a/docs/longhornctl_export_replica.md +++ b/docs/longhornctl_export_replica.md @@ -53,7 +53,7 @@ lost+found --image string Image containing longhornctl-local (default "longhornio/longhorn-cli:v1.13.0-dev") --image-pull-secret string Secret with registry credentials for pulling images --image-registry string Registry to apply to all images (CLI, engine, pause, BCI, etc.), replacing any registry already specified in those images. - --kubeconfig string Kubernetes config (kubeconfig) path + --kubeconfig string Kubernetes config (kubeconfig) path. If not set, $KUBECONFIG or ~/.kube/config is used -l, --log-level string Log level (default "info") --name string Specify the replica directory name to export. The replica data directory name is not the same as the Kubernetes Replica custom resource (CR) object name. To retrieve the replica directory name, use 'longhornctl get replica'. --namespace string The namespace to run DaemonSet pods. (default "longhorn-system") diff --git a/docs/longhornctl_export_replica_stop.md b/docs/longhornctl_export_replica_stop.md index 0a4688f3..09c6c862 100644 --- a/docs/longhornctl_export_replica_stop.md +++ b/docs/longhornctl_export_replica_stop.md @@ -25,7 +25,7 @@ INFO[2024-07-16T17:29:14+08:00] Successfully stopped exporting replica --image string Image containing longhornctl-local (default "longhornio/longhorn-cli:v1.13.0-dev") --image-pull-secret string Secret with registry credentials for pulling images --image-registry string Registry to apply to all images (CLI, engine, pause, BCI, etc.), replacing any registry already specified in those images. - --kubeconfig string Kubernetes config (kubeconfig) path + --kubeconfig string Kubernetes config (kubeconfig) path. If not set, $KUBECONFIG or ~/.kube/config is used -l, --log-level string Log level (default "info") --namespace string The namespace to run DaemonSet pods. (default "longhorn-system") --node-selector string Comma-separated list of key=value pairs to match against node labels, selecting the nodes the DaemonSet will run on (e.g. env=prod,zone=us-west). diff --git a/docs/longhornctl_get.md b/docs/longhornctl_get.md index 4aaf8dce..1e10687d 100644 --- a/docs/longhornctl_get.md +++ b/docs/longhornctl_get.md @@ -9,7 +9,7 @@ Longhorn information gathering operations --image string Image containing longhornctl-local (default "longhornio/longhorn-cli:v1.13.0-dev") --image-pull-secret string Secret with registry credentials for pulling images --image-registry string Registry to apply to all images (CLI, engine, pause, BCI, etc.), replacing any registry already specified in those images. - --kubeconfig string Kubernetes config (kubeconfig) path + --kubeconfig string Kubernetes config (kubeconfig) path. If not set, $KUBECONFIG or ~/.kube/config is used -l, --log-level string Log level (default "info") --namespace string The namespace to run DaemonSet pods. (default "longhorn-system") --node-selector string Comma-separated list of key=value pairs to match against node labels, selecting the nodes the DaemonSet will run on (e.g. env=prod,zone=us-west). diff --git a/docs/longhornctl_get_replica.md b/docs/longhornctl_get_replica.md index ac36c15b..6f455a80 100644 --- a/docs/longhornctl_get_replica.md +++ b/docs/longhornctl_get_replica.md @@ -52,7 +52,7 @@ INFO[2024-07-16T17:23:51+08:00] Completed replica getter --image string Image containing longhornctl-local (default "longhornio/longhorn-cli:v1.13.0-dev") --image-pull-secret string Secret with registry credentials for pulling images --image-registry string Registry to apply to all images (CLI, engine, pause, BCI, etc.), replacing any registry already specified in those images. - --kubeconfig string Kubernetes config (kubeconfig) path + --kubeconfig string Kubernetes config (kubeconfig) path. If not set, $KUBECONFIG or ~/.kube/config is used -l, --log-level string Log level (default "info") --name string Specify the name of the replica to retrieve information. --namespace string The namespace to run DaemonSet pods. (default "longhorn-system") diff --git a/docs/longhornctl_global-options.md b/docs/longhornctl_global-options.md index 49c891b9..59fa0750 100644 --- a/docs/longhornctl_global-options.md +++ b/docs/longhornctl_global-options.md @@ -22,7 +22,7 @@ longhornctl global-options [flags] --image string Image containing longhornctl-local (default "longhornio/longhorn-cli:v1.13.0-dev") --image-pull-secret string Secret with registry credentials for pulling images --image-registry string Registry to apply to all images (CLI, engine, pause, BCI, etc.), replacing any registry already specified in those images. - --kubeconfig string Kubernetes config (kubeconfig) path + --kubeconfig string Kubernetes config (kubeconfig) path. If not set, $KUBECONFIG or ~/.kube/config is used -l, --log-level string log level (trace, debug, info, warn, error, fatal, panic) (default "info") --namespace string The namespace to run DaemonSet pods. (default "longhorn-system") --node-selector string Comma-separated list of key=value pairs to match against node labels, selecting the nodes the DaemonSet will run on (e.g. env=prod,zone=us-west). diff --git a/docs/longhornctl_install.md b/docs/longhornctl_install.md index e5792476..1fbc9ac7 100644 --- a/docs/longhornctl_install.md +++ b/docs/longhornctl_install.md @@ -9,7 +9,7 @@ Longhorn installation operations --image string Image containing longhornctl-local (default "longhornio/longhorn-cli:v1.13.0-dev") --image-pull-secret string Secret with registry credentials for pulling images --image-registry string Registry to apply to all images (CLI, engine, pause, BCI, etc.), replacing any registry already specified in those images. - --kubeconfig string Kubernetes config (kubeconfig) path + --kubeconfig string Kubernetes config (kubeconfig) path. If not set, $KUBECONFIG or ~/.kube/config is used -l, --log-level string Log level (default "info") --namespace string The namespace to run DaemonSet pods. (default "longhorn-system") --node-selector string Comma-separated list of key=value pairs to match against node labels, selecting the nodes the DaemonSet will run on (e.g. env=prod,zone=us-west). diff --git a/docs/longhornctl_install_preflight.md b/docs/longhornctl_install_preflight.md index 34a48841..8530c50f 100644 --- a/docs/longhornctl_install_preflight.md +++ b/docs/longhornctl_install_preflight.md @@ -54,7 +54,7 @@ If a reboot is required, the following message will be displayed: --image string Image containing longhornctl-local (default "longhornio/longhorn-cli:v1.13.0-dev") --image-pull-secret string Secret with registry credentials for pulling images --image-registry string Registry to apply to all images (CLI, engine, pause, BCI, etc.), replacing any registry already specified in those images. - --kubeconfig string Kubernetes config (kubeconfig) path + --kubeconfig string Kubernetes config (kubeconfig) path. If not set, $KUBECONFIG or ~/.kube/config is used -l, --log-level string Log level (default "info") --namespace string The namespace to run DaemonSet pods. (default "longhorn-system") --node-selector string Comma-separated list of key=value pairs to match against node labels, selecting the nodes the DaemonSet will run on (e.g. env=prod,zone=us-west). diff --git a/docs/longhornctl_install_preflight_stop.md b/docs/longhornctl_install_preflight_stop.md index 7bb5433d..552e0e52 100644 --- a/docs/longhornctl_install_preflight_stop.md +++ b/docs/longhornctl_install_preflight_stop.md @@ -25,7 +25,7 @@ INFO[2024-07-16T17:21:32+08:00] Successfully stopped preflight installer --image string Image containing longhornctl-local (default "longhornio/longhorn-cli:v1.13.0-dev") --image-pull-secret string Secret with registry credentials for pulling images --image-registry string Registry to apply to all images (CLI, engine, pause, BCI, etc.), replacing any registry already specified in those images. - --kubeconfig string Kubernetes config (kubeconfig) path + --kubeconfig string Kubernetes config (kubeconfig) path. If not set, $KUBECONFIG or ~/.kube/config is used -l, --log-level string Log level (default "info") --namespace string The namespace to run DaemonSet pods. (default "longhorn-system") --node-selector string Comma-separated list of key=value pairs to match against node labels, selecting the nodes the DaemonSet will run on (e.g. env=prod,zone=us-west). diff --git a/docs/longhornctl_trim.md b/docs/longhornctl_trim.md index acb1d614..07fa7054 100644 --- a/docs/longhornctl_trim.md +++ b/docs/longhornctl_trim.md @@ -9,7 +9,7 @@ Longhorn trimming operations --image string Image containing longhornctl-local (default "longhornio/longhorn-cli:v1.13.0-dev") --image-pull-secret string Secret with registry credentials for pulling images --image-registry string Registry to apply to all images (CLI, engine, pause, BCI, etc.), replacing any registry already specified in those images. - --kubeconfig string Kubernetes config (kubeconfig) path + --kubeconfig string Kubernetes config (kubeconfig) path. If not set, $KUBECONFIG or ~/.kube/config is used -l, --log-level string Log level (default "info") --namespace string The namespace to run DaemonSet pods. (default "longhorn-system") --node-selector string Comma-separated list of key=value pairs to match against node labels, selecting the nodes the DaemonSet will run on (e.g. env=prod,zone=us-west). diff --git a/docs/longhornctl_trim_volume.md b/docs/longhornctl_trim_volume.md index 0990cc38..14472967 100644 --- a/docs/longhornctl_trim_volume.md +++ b/docs/longhornctl_trim_volume.md @@ -34,7 +34,7 @@ INFO[2024-07-16T17:32:01+08:00] Completed volume trimmer vo --image string Image containing longhornctl-local (default "longhornio/longhorn-cli:v1.13.0-dev") --image-pull-secret string Secret with registry credentials for pulling images --image-registry string Registry to apply to all images (CLI, engine, pause, BCI, etc.), replacing any registry already specified in those images. - --kubeconfig string Kubernetes config (kubeconfig) path + --kubeconfig string Kubernetes config (kubeconfig) path. If not set, $KUBECONFIG or ~/.kube/config is used -l, --log-level string Log level (default "info") --name string Name of the Longhorn volum to be trimmed. --namespace string The namespace to run DaemonSet pods. (default "longhorn-system") diff --git a/docs/longhornctl_version.md b/docs/longhornctl_version.md index 4e97b864..fca9eeff 100644 --- a/docs/longhornctl_version.md +++ b/docs/longhornctl_version.md @@ -18,7 +18,7 @@ longhornctl version [flags] --image string Image containing longhornctl-local (default "longhornio/longhorn-cli:v1.13.0-dev") --image-pull-secret string Secret with registry credentials for pulling images --image-registry string Registry to apply to all images (CLI, engine, pause, BCI, etc.), replacing any registry already specified in those images. - --kubeconfig string Kubernetes config (kubeconfig) path + --kubeconfig string Kubernetes config (kubeconfig) path. If not set, $KUBECONFIG or ~/.kube/config is used -l, --log-level string log level (trace, debug, info, warn, error, fatal, panic) (default "info") --namespace string The namespace to run DaemonSet pods. (default "longhorn-system") --node-selector string Comma-separated list of key=value pairs to match against node labels, selecting the nodes the DaemonSet will run on (e.g. env=prod,zone=us-west). diff --git a/pkg/consts/environment.go b/pkg/consts/environment.go index b982f628..d1809fd4 100644 --- a/pkg/consts/environment.go +++ b/pkg/consts/environment.go @@ -8,7 +8,6 @@ const ( const ( EnvCurrentNodeID = "CURRENT_NODE_ID" - EnvKubeConfigPath = "KUBECONFIG" EnvLogLevel = "LOG_LEVEL" EnvOutputFilePath = "OUTPUT_FILE_PATH" diff --git a/pkg/utils/cmd.go b/pkg/utils/cmd.go index f51b1530..d85db939 100644 --- a/pkg/utils/cmd.go +++ b/pkg/utils/cmd.go @@ -26,7 +26,7 @@ func SetGlobalOptionsLocal(cmd *cobra.Command, globalOpts *types.GlobalCmdOption // SetGlobalOptionsRemote sets global options for remote commands. func SetGlobalOptionsRemote(cmd *cobra.Command, globalOpts *types.GlobalCmdOptions) { cmd.PersistentFlags().StringVarP(&globalOpts.LogLevel, consts.CmdOptLogLevel, "l", globalOpts.LogLevel, "Log level") - cmd.PersistentFlags().StringVar(&globalOpts.KubeConfigPath, consts.CmdOptKubeConfigPath, globalOpts.KubeConfigPath, "Kubernetes config (kubeconfig) path") + cmd.PersistentFlags().StringVar(&globalOpts.KubeConfigPath, consts.CmdOptKubeConfigPath, globalOpts.KubeConfigPath, "Kubernetes config (kubeconfig) path. If not set, $KUBECONFIG or ~/.kube/config is used") cmd.PersistentFlags().StringVar(&globalOpts.Image, consts.CmdOptImage, globalOpts.Image, "Image containing longhornctl-local") cmd.PersistentFlags().StringVar(&globalOpts.ImageRegistry, consts.CmdOptImageRegistry, globalOpts.ImageRegistry, "Registry to apply to all images (CLI, engine, pause, BCI, etc.), replacing any registry already specified in those images.") cmd.PersistentFlags().StringVar(&globalOpts.ImagePullSecret, consts.CmdOptImagePullSecret, globalOpts.ImagePullSecret, "Secret with registry credentials for pulling images") diff --git a/pkg/utils/kubernetes/client.go b/pkg/utils/kubernetes/client.go index 7d4e5422..f344c3da 100644 --- a/pkg/utils/kubernetes/client.go +++ b/pkg/utils/kubernetes/client.go @@ -3,25 +3,25 @@ package kubernetes import ( "fmt" "os" + "path/filepath" + "strings" "k8s.io/client-go/rest" "k8s.io/client-go/tools/clientcmd" kubeclient "k8s.io/client-go/kubernetes" + clientcmdapi "k8s.io/client-go/tools/clientcmd/api" ) const ( kubeConfigHint = `Make sure to either: + - Place a valid kubeconfig at ~/.kube/config - Set the environment variable: export KUBECONFIG=/path/to/config - Or use: --kubeconfig=/path/to/config` ) func NewKubeClient(masterUrl string, kubeconfigPath string) (kubeClient *kubeclient.Clientset, err error) { - if masterUrl == "" && kubeconfigPath == "" { - return nil, fmt.Errorf("no kubeconfig path provided.\n\n%s", kubeConfigHint) - } - - kubeconfig, err := GetKubeConfigFromPath(kubeconfigPath) + kubeconfig, err := getKubeConfig(masterUrl, kubeconfigPath) if err != nil { return nil, err } @@ -35,15 +35,40 @@ func NewKubeClient(masterUrl string, kubeconfigPath string) (kubeClient *kubecli } func GetKubeConfigFromPath(kubeconfigPath string) (*rest.Config, error) { + return getKubeConfig("", kubeconfigPath) +} + +func getKubeConfig(masterUrl string, kubeconfigPath string) (*rest.Config, error) { + loadingRules := clientcmd.NewDefaultClientConfigLoadingRules() if kubeconfigPath != "" { if _, err := os.Stat(kubeconfigPath); os.IsNotExist(err) { return nil, fmt.Errorf("provided kubeconfig path does not exist: %s\n\n%s", kubeconfigPath, kubeConfigHint) } + loadingRules.ExplicitPath = kubeconfigPath + } else if os.Getenv(clientcmd.RecommendedConfigPathEnvVar) == "" { + // Resolve ~/.kube/config at call time; clientcmd.RecommendedHomeFile is computed once at package init. + if homeDir, err := os.UserHomeDir(); err == nil && homeDir != "" { + loadingRules.Precedence = []string{ + filepath.Join(homeDir, clientcmd.RecommendedHomeDir, clientcmd.RecommendedFileName), + } + } } - kubeconfig, err := clientcmd.BuildConfigFromFlags("", kubeconfigPath) + overrides := &clientcmd.ConfigOverrides{} + if masterUrl != "" { + overrides.ClusterInfo = clientcmdapi.Cluster{Server: masterUrl} + } + + kubeconfig, err := clientcmd.NewNonInteractiveDeferredLoadingClientConfig(loadingRules, overrides).ClientConfig() if err != nil { - return nil, fmt.Errorf("failed to load kubeconfig from path '%s': %w\n\n%s", kubeconfigPath, err, kubeConfigHint) + if kubeconfigPath != "" { + return nil, fmt.Errorf("failed to load kubeconfig from path '%s': %w\n\n%s", kubeconfigPath, err, kubeConfigHint) + } + if clientcmd.IsEmptyConfig(err) { + return nil, fmt.Errorf("no kubeconfig found (checked: %s) and in-cluster configuration is unavailable\n\n%s", + strings.Join(loadingRules.GetLoadingPrecedence(), ", "), kubeConfigHint) + } + return nil, fmt.Errorf("failed to load kubeconfig: %w\n\n%s", err, kubeConfigHint) } return kubeconfig, nil diff --git a/pkg/utils/kubernetes/client_test.go b/pkg/utils/kubernetes/client_test.go new file mode 100644 index 00000000..bf8d72cc --- /dev/null +++ b/pkg/utils/kubernetes/client_test.go @@ -0,0 +1,92 @@ +package kubernetes + +import ( + "os" + "path/filepath" + "testing" + + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +func writeTestKubeconfig(t *testing.T, path, serverURL string) { + t.Helper() + require.NoError(t, os.MkdirAll(filepath.Dir(path), 0755)) + content := `apiVersion: v1 +kind: Config +clusters: +- name: test + cluster: + server: ` + serverURL + ` +contexts: +- name: test + context: + cluster: test + user: test +current-context: test +users: +- name: test + user: + token: dummy-token +` + require.NoError(t, os.WriteFile(path, []byte(content), 0600)) +} + +func TestGetKubeConfigPrecedence(t *testing.T) { + tmpHome := t.TempDir() + t.Setenv("HOME", tmpHome) + t.Setenv("KUBECONFIG", "") + t.Setenv("KUBERNETES_SERVICE_HOST", "") + t.Setenv("KUBERNETES_SERVICE_PORT", "") + + // 1. Missing default ~/.kube/config and no env/flag returns an actionable error listing the checked path. + homeCfg := filepath.Join(tmpHome, ".kube", "config") + _, err := NewKubeClient("", "") + require.Error(t, err) + assert.Contains(t, err.Error(), "no kubeconfig found (checked: "+homeCfg+")") + assert.NotContains(t, err.Error(), "KUBERNETES_MASTER") + + // 2. Falls back to ~/.kube/config when --kubeconfig and KUBECONFIG are omitted. + writeTestKubeconfig(t, homeCfg, "https://127.0.0.1:6443") + + cfg, err := GetKubeConfigFromPath("") + require.NoError(t, err) + assert.Equal(t, "https://127.0.0.1:6443", cfg.Host) + + client, err := NewKubeClient("", "") + require.NoError(t, err) + require.NotNil(t, client) + + // 3. KUBECONFIG path list overrides ~/.kube/config, and the first file wins when merging. + envCfg1 := filepath.Join(tmpHome, "env1.yaml") + envCfg2 := filepath.Join(tmpHome, "env2.yaml") + writeTestKubeconfig(t, envCfg1, "https://127.0.0.2:6443") + writeTestKubeconfig(t, envCfg2, "https://127.0.0.4:6443") + t.Setenv("KUBECONFIG", envCfg1+string(os.PathListSeparator)+envCfg2) + + cfg, err = GetKubeConfigFromPath("") + require.NoError(t, err) + assert.Equal(t, "https://127.0.0.2:6443", cfg.Host) + + // 4. KUBECONFIG pointing only to missing files reports every checked path instead of falling back to ~/.kube/config. + missing1 := filepath.Join(tmpHome, "missing1.yaml") + missing2 := filepath.Join(tmpHome, "missing2.yaml") + t.Setenv("KUBECONFIG", missing1+string(os.PathListSeparator)+missing2) + + _, err = GetKubeConfigFromPath("") + require.Error(t, err) + assert.Contains(t, err.Error(), "no kubeconfig found (checked: "+missing1+", "+missing2+")") + + // 5. Explicit --kubeconfig overrides both KUBECONFIG and ~/.kube/config. + explicitCfg := filepath.Join(tmpHome, "explicit.yaml") + writeTestKubeconfig(t, explicitCfg, "https://127.0.0.3:6443") + + cfg, err = GetKubeConfigFromPath(explicitCfg) + require.NoError(t, err) + assert.Equal(t, "https://127.0.0.3:6443", cfg.Host) + + // 6. Explicit --kubeconfig pointing to a missing file fails immediately. + _, err = GetKubeConfigFromPath(filepath.Join(tmpHome, "missing.yaml")) + require.Error(t, err) + assert.Contains(t, err.Error(), "provided kubeconfig path does not exist") +}