From fcbe2c30ccde8bb1472bebbe543796756e64cfd6 Mon Sep 17 00:00:00 2001 From: Maxym Fugol <110974066+mfuhol-weka@users.noreply.github.com> Date: Fri, 1 Dec 2023 16:35:15 +0100 Subject: [PATCH] feat: add helper kubeconfig loader function (#17) --- internal/chart/install.go | 14 +++++++------- internal/chart/kube.go | 31 ++++++++++++++++++++++++++++++ internal/cli/chart/install.go | 36 ++++++++++++----------------------- 3 files changed, 50 insertions(+), 31 deletions(-) create mode 100644 internal/chart/kube.go diff --git a/internal/chart/install.go b/internal/chart/install.go index 4d529ae..02dbcd4 100644 --- a/internal/chart/install.go +++ b/internal/chart/install.go @@ -26,16 +26,16 @@ var ErrUnableToFindChart = fmt.Errorf("unable to determine chart location") var logger = utils.GetLogger("HelmChart") type LocationOverride struct { - Path string - RemoteDownload bool - Version string + Path string // path to chart package + RemoteDownload bool // download from remote repository + Version string // version of the chart to download from remote repository } type HelmOptions struct { - KubeConfig []byte - Override *LocationOverride - KubeContext string - NamespaceOverride string + KubeConfig []byte // path or content of kubeconfig file + Override *LocationOverride // override chart package location + KubeContext string // kubeconfig context to use + NamespaceOverride string // override namespace for release } func InstallOrUpgrade( diff --git a/internal/chart/kube.go b/internal/chart/kube.go new file mode 100644 index 0000000..43460f7 --- /dev/null +++ b/internal/chart/kube.go @@ -0,0 +1,31 @@ +package chart + +import ( + "fmt" + "os" + "path/filepath" +) + +// ReadKubeConfig reads the kubeconfig from the given path with fallback to ~/.kube/config +func ReadKubeConfig(kubeConfigPath string) ([]byte, error) { + if kubeConfigPath == "" { + kubeConfigPath = os.Getenv("KUBECONFIG") + if kubeConfigPath == "" { + homeDir, err := os.UserHomeDir() + if err != nil { + return nil, fmt.Errorf("unable to read kubeconfig: %w", err) + } + + kubeConfigPath = filepath.Join(homeDir, ".kube", "config") + } + } + + logger.Debug().Str("kubeConfigPath", kubeConfigPath).Msg("Reading kubeconfig") + kubeConfig, err := os.ReadFile(kubeConfigPath) + if err != nil { + logger.Error().Err(err).Msg("Failed to read kubeconfig") + return nil, fmt.Errorf("failed to read kubeconfig: %w", err) + } + + return kubeConfig, nil +} diff --git a/internal/cli/chart/install.go b/internal/cli/chart/install.go index 0b149e9..4847bc9 100644 --- a/internal/cli/chart/install.go +++ b/internal/cli/chart/install.go @@ -14,37 +14,17 @@ import ( var logger = utils.GetLogger("HelmChart") -func normPath(path string) string { +func normPath(path string) (string, error) { if strings.HasPrefix(path, "~/") { homeDir, err := os.UserHomeDir() if err != nil { - logger.Error().Err(err).Msg("Failed to get user home directory") - os.Exit(255) + return "", fmt.Errorf("unable to expand home directory: %w", err) } path = filepath.Join(homeDir, path[2:]) } - return filepath.Clean(path) -} - -func readKubeConfig(kubeConfigPath string) ([]byte, error) { - if kubeConfigPath == "" { - kubeConfigPath = os.Getenv("KUBECONFIG") - if kubeConfigPath == "" { - kubeConfigPath = "~/.kube/config" - } - } - kubeConfigPath = normPath(kubeConfigPath) - - logger.Debug().Str("kubeConfigPath", kubeConfigPath).Msg("Reading kubeconfig") - kubeConfig, err := os.ReadFile(kubeConfigPath) - if err != nil { - logger.Error().Err(err).Msg("Failed to read kubeconfig") - return nil, fmt.Errorf("failed to read kubeconfig: %w", err) - } - - return kubeConfig, nil + return filepath.Clean(path), nil } func readConfiguration(jsonConfig string) (*chart.Configuration, error) { @@ -81,7 +61,15 @@ func runInstallOrUpgrade(cmd *cobra.Command, args []string) error { return fmt.Errorf("%w: --remote-version can only be used with --remote-download", utils.ErrValidationFailed) } - kubeConfig, err := readKubeConfig(installCmdOpts.kubeConfigPath) + var err error + if installCmdOpts.kubeConfigPath != "" { + installCmdOpts.kubeConfigPath, err = normPath(installCmdOpts.kubeConfigPath) + if err != nil { + return err + } + } + + kubeConfig, err := chart.ReadKubeConfig(installCmdOpts.kubeConfigPath) if err != nil { return err }