Skip to content

Commit

Permalink
feat: add helper kubeconfig loader function (#17)
Browse files Browse the repository at this point in the history
  • Loading branch information
mfuhol-weka authored Dec 1, 2023
1 parent e9e9c05 commit fcbe2c3
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 31 deletions.
14 changes: 7 additions & 7 deletions internal/chart/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
31 changes: 31 additions & 0 deletions internal/chart/kube.go
Original file line number Diff line number Diff line change
@@ -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
}
36 changes: 12 additions & 24 deletions internal/cli/chart/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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
}
Expand Down

0 comments on commit fcbe2c3

Please sign in to comment.