Skip to content

Commit

Permalink
propagate default env vars to helm binary (#169)
Browse files Browse the repository at this point in the history
Co-authored-by: Dmitriy Kalinin <[email protected]>
  • Loading branch information
cppforlife and Dmitriy Kalinin authored Jun 29, 2022
1 parent aa2ac34 commit bcb9c64
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 26 deletions.
27 changes: 3 additions & 24 deletions pkg/vendir/fetch/helmchart/http_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"fmt"
"os"
"os/exec"
"regexp"
"strings"

ctlconf "github.com/vmware-tanzu/carvel-vendir/pkg/vendir/config"
Expand Down Expand Up @@ -43,32 +42,13 @@ func (t *HTTPSource) Fetch(dstPath string, tempArea ctlfetch.TempArea) error {
return t.fetch(helmHomeDir, dstPath)
}

func setEnv(helmHomeDir string) []string {
re := regexp.MustCompile(`(?i)\A(https?|no)_proxy=.*`)
env := []string{
"HOME=" + helmHomeDir,
"TEMP=" + helmHomeDir,
"HELM_CACHE_HOME=" + helmHomeDir,
"HELM_CONFIG_HOME=" + helmHomeDir,
"HELM_DATA_HOME=" + helmHomeDir,
}
for _, e := range os.Environ() {
if re.MatchString(e) {
env = append(env, e)
}
}
// TODO: what about all those other variables ?
// https://helm.sh/docs/helm/helm/
return env
}

func (t *HTTPSource) init(helmHomeDir string) error {
args := []string{"init", "--client-only", "--stable-repo-url", "https://charts.helm.sh/stable"}

var stdoutBs, stderrBs bytes.Buffer

cmd := exec.Command(t.helmBinary, args...)
cmd.Env = setEnv(helmHomeDir)
cmd.Env = helmEnv(helmHomeDir)
cmd.Stdout = &stdoutBs
cmd.Stderr = &stderrBs

Expand Down Expand Up @@ -126,7 +106,7 @@ func (t *HTTPSource) fetch(helmHomeDir, chartsPath string) error {
var stdoutBs, stderrBs bytes.Buffer

cmd := exec.Command(t.helmBinary, repoAddArgs...)
cmd.Env = setEnv(helmHomeDir)
cmd.Env = helmEnv(helmHomeDir)
cmd.Stdout = &stdoutBs
cmd.Stderr = &stderrBs

Expand All @@ -149,8 +129,7 @@ func (t *HTTPSource) fetch(helmHomeDir, chartsPath string) error {
var stdoutBs, stderrBs bytes.Buffer

cmd := exec.Command(t.helmBinary, fetchArgs...)
cmd.Env = setEnv(helmHomeDir)

cmd.Env = helmEnv(helmHomeDir)
cmd.Stdout = &stdoutBs
cmd.Stderr = &stderrBs

Expand Down
4 changes: 2 additions & 2 deletions pkg/vendir/fetch/helmchart/oci_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ func (t *OCISource) login(repo, helmHomeDir string) error {
var stdoutBs, stderrBs bytes.Buffer

cmd := exec.Command(t.helmBinary, args...)
cmd.Env = []string{"HOME=" + helmHomeDir}
cmd.Env = helmEnv(helmHomeDir)
cmd.Stdin = cmdStdin
cmd.Stdout = &stdoutBs
cmd.Stderr = &stderrBs
Expand All @@ -129,7 +129,7 @@ func (t *OCISource) pull(ref, helmHomeDir, dstPath string) error {
var stdoutBs, stderrBs bytes.Buffer

cmd := exec.Command(t.helmBinary, args...)
cmd.Env = []string{"HOME=" + helmHomeDir}
cmd.Env = helmEnv(helmHomeDir)
cmd.Stdout = &stdoutBs
cmd.Stderr = &stderrBs

Expand Down
13 changes: 13 additions & 0 deletions pkg/vendir/fetch/helmchart/sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,3 +137,16 @@ func (t *Sync) findChartDir(chartsPath string) (string, error) {
}
return filepath.Join(chartsPath, dirNames[0]), nil
}

func helmEnv(helmHomeDir string) []string {
// Previous discussion around env vars propagation:
// https://github.com/vmware-tanzu/carvel-vendir/issues/164
// Example: without propagating few env vars, asdf pkg mgr cannot execute helm binary
return append(os.Environ(), []string{
"HOME=" + helmHomeDir,
"TEMP=" + helmHomeDir,
"HELM_CACHE_HOME=" + helmHomeDir,
"HELM_CONFIG_HOME=" + helmHomeDir,
"HELM_DATA_HOME=" + helmHomeDir,
}...)
}

0 comments on commit bcb9c64

Please sign in to comment.