From 664530d33f6d3ca1e45072571b108f68e442b1fa Mon Sep 17 00:00:00 2001 From: Nino Kodabande Date: Fri, 28 Jun 2024 10:59:24 -0700 Subject: [PATCH] Introduces a new flag to support diagnostics. The 'verify' flag tests the symlinked kubeconfig for non-Rancher Desktop configuration. If it encounters such a configuration, the process exits with exit code 1; otherwise, exit code 0 is returned. Signed-off-by: Nino Kodabande --- src/go/wsl-helper/cmd/kubeconfig.go | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/src/go/wsl-helper/cmd/kubeconfig.go b/src/go/wsl-helper/cmd/kubeconfig.go index 1c2e8e80a94..2ed84f48e12 100644 --- a/src/go/wsl-helper/cmd/kubeconfig.go +++ b/src/go/wsl-helper/cmd/kubeconfig.go @@ -46,8 +46,21 @@ var kubeconfigCmd = &cobra.Command{ Long: `This command configures the Kubernetes configuration inside a WSL2 distribution.`, Args: cobra.ExactArgs(0), RunE: func(cmd *cobra.Command, args []string) error { + cmd.SilenceUsage = true + configPath := kubeconfigViper.GetString("kubeconfig") enable := kubeconfigViper.GetBool("enable") + verify := kubeconfigViper.GetBool("verify") + + configDir := path.Join(homedir.HomeDir(), ".kube") + linkPath := path.Join(configDir, "config") + unsupportedConfig, symlinkErr := requireManualSymlink(linkPath) + if verify { + if unsupportedConfig { + os.Exit(1) + } + os.Exit(0) + } if configPath == "" { return errors.New("Windows kubeconfig not supplied") @@ -57,11 +70,7 @@ var kubeconfigCmd = &cobra.Command{ if err != nil { return fmt.Errorf("could not open Windows kubeconfig: %w", err) } - cmd.SilenceUsage = true - configDir := path.Join(homedir.HomeDir(), ".kube") - linkPath := path.Join(configDir, "config") - unsupportedConfig, symlinkErr := requireManualSymlink(linkPath) if !unsupportedConfig && symlinkErr != nil { return symlinkErr } @@ -149,6 +158,7 @@ func readKubeConfig(configPath string) (kubeConfig, error) { } func init() { + kubeconfigCmd.PersistentFlags().Bool("verify", true, "Checks whether the symlinked config contains non-Rancher Desktop configuration.") kubeconfigCmd.PersistentFlags().Bool("enable", true, "Set up config file") kubeconfigCmd.PersistentFlags().String("kubeconfig", "", "Path to Windows kubeconfig, in /mnt/... form.") kubeconfigViper.AutomaticEnv()