Skip to content
This repository has been archived by the owner on Nov 1, 2022. It is now read-only.

Commit

Permalink
helm/v2: use download manager for dep updates
Browse files Browse the repository at this point in the history
This avoids having to shell out to the `helm2` binary while still
maintaining the same functionality. It aligns with the approach
used in `helm/v3`.
  • Loading branch information
hiddeco committed Dec 11, 2019
1 parent 9752271 commit 691aecc
Showing 1 changed file with 25 additions and 56 deletions.
81 changes: 25 additions & 56 deletions pkg/helm/v2/dependency.go
Original file line number Diff line number Diff line change
@@ -1,68 +1,37 @@
package v2

import (
"errors"
"fmt"
"os"
"os/exec"
"path/filepath"
)

func (h *HelmV2) DependencyUpdate(chartPath string) error {
var hasLockFile bool

// sanity check: does the chart directory exist
if chartPath == "" {
return errors.New("empty path to chart supplied")
}
chartInfo, err := os.Stat(chartPath)
switch {
case os.IsNotExist(err):
return fmt.Errorf("chart path %s does not exist", chartPath)
case err != nil:
return err
case !chartInfo.IsDir():
return fmt.Errorf("chart path %s is not a directory", chartPath)
}

// check if the requirements file exists
reqFilePath := filepath.Join(chartPath, "requirements.yaml")
reqInfo, err := os.Stat(reqFilePath)
if err != nil || reqInfo.IsDir() {
return nil
}
"github.com/go-kit/kit/log"

// We are going to use `helm dep build`, which tries to update the
// dependencies in charts/ by looking at the file
// `requirements.lock` in the chart directory. If the lockfile
// does not match what is specified in requirements.yaml, it will
// error out.
//
// If that file doesn't exist, `helm dep build` will fall back on
// `helm dep update`, which populates the charts/ directory _and_
// creates the lockfile. So that it will have the same behaviour
// the next time it attempts a release, remove the lockfile if it
// was created by helm.
lockfilePath := filepath.Join(chartPath, "requirements.lock")
info, err := os.Stat(lockfilePath)
hasLockFile = (err == nil && !info.IsDir())
if !hasLockFile {
defer os.Remove(lockfilePath)
}
"k8s.io/helm/pkg/downloader"
)

cmd := exec.Command("helm2", "repo", "update")
out, err := cmd.CombinedOutput()
if err != nil {
return fmt.Errorf("could not update repo: %s", string(out))
func (h *HelmV2) DependencyUpdate(chartPath string) error {
repositoryConfigLock.RLock()
defer repositoryConfigLock.RUnlock()

out := &logWriter{h.logger}
man := downloader.Manager{
Out: out,
ChartPath: chartPath,
HelmHome: helmHome(),
Getters: getters,
}
return man.Update()
}

cmd = exec.Command("helm2", "dep", "build", ".")
cmd.Dir = chartPath
// logWriter wraps a `log.Logger` so it can be used as an `io.Writer`
type logWriter struct {
log.Logger
}

out, err = cmd.CombinedOutput()
if err != nil {
return fmt.Errorf("could not update dependencies in %s: %s", chartPath, string(out))
func (l *logWriter) Write(p []byte) (n int, err error) {
origLen := len(p)
if len(p) > 0 && p[len(p)-1] == '\n' {
p = p[:len(p)-1] // Cut terminating newline
}

return nil
l.Log("info", fmt.Sprintf("%s", p))
return origLen, nil
}

0 comments on commit 691aecc

Please sign in to comment.