This repository has been archived by the owner on Nov 1, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 262
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chartsync: use target helm version for download (#145)
chartsync: use target helm version for download
- Loading branch information
Showing
9 changed files
with
206 additions
and
193 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package helm | ||
|
||
import ( | ||
"fmt" | ||
"io" | ||
|
||
"github.com/go-kit/kit/log" | ||
) | ||
|
||
// logWriter wraps a `log.Logger` so it can be used as an `io.Writer` | ||
type logWriter struct { | ||
log.Logger | ||
} | ||
|
||
func NewLogWriter(logger log.Logger) io.Writer { | ||
return &logWriter{logger} | ||
} | ||
|
||
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 | ||
} | ||
l.Log("info", fmt.Sprintf("%s", p)) | ||
return origLen, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,68 +1,21 @@ | ||
package v2 | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"os" | ||
"os/exec" | ||
"path/filepath" | ||
"k8s.io/helm/pkg/downloader" | ||
|
||
"github.com/fluxcd/helm-operator/pkg/helm" | ||
) | ||
|
||
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 | ||
} | ||
|
||
// 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) | ||
repositoryConfigLock.RLock() | ||
defer repositoryConfigLock.RUnlock() | ||
|
||
out := helm.NewLogWriter(h.logger) | ||
man := downloader.Manager{ | ||
Out: out, | ||
ChartPath: chartPath, | ||
HelmHome: helmHome(), | ||
Getters: getters, | ||
} | ||
|
||
cmd := exec.Command("helm2", "repo", "update") | ||
out, err := cmd.CombinedOutput() | ||
if err != nil { | ||
return fmt.Errorf("could not update repo: %s", string(out)) | ||
} | ||
|
||
cmd = exec.Command("helm2", "dep", "build", ".") | ||
cmd.Dir = chartPath | ||
|
||
out, err = cmd.CombinedOutput() | ||
if err != nil { | ||
return fmt.Errorf("could not update dependencies in %s: %s", chartPath, string(out)) | ||
} | ||
|
||
return nil | ||
return man.Update() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package v2 | ||
|
||
import ( | ||
"k8s.io/helm/pkg/downloader" | ||
"k8s.io/helm/pkg/repo" | ||
"k8s.io/helm/pkg/urlutil" | ||
|
||
"github.com/fluxcd/helm-operator/pkg/helm" | ||
) | ||
|
||
func (h *HelmV2) Pull(ref, version, dest string) (string, error) { | ||
repositoryConfigLock.RLock() | ||
defer repositoryConfigLock.RUnlock() | ||
|
||
out := helm.NewLogWriter(h.logger) | ||
c := downloader.ChartDownloader{ | ||
Out: out, | ||
HelmHome: helmHome(), | ||
Verify: downloader.VerifyNever, | ||
Getters: getters, | ||
} | ||
d, _, err := c.DownloadTo(ref, version, dest) | ||
return d, err | ||
} | ||
|
||
func (h *HelmV2) PullWithRepoURL(repoURL, name, version, dest string) (string, error) { | ||
// This resolves the repo URL, chart name and chart version to a | ||
// URL for the chart. To be able to resolve the chart name and | ||
// version to a URL, we have to have the index file; and to have | ||
// that, we may need to authenticate. The credentials will be in | ||
// the repository config. | ||
repositoryConfigLock.RLock() | ||
repoFile, err := loadRepositoryConfig() | ||
repositoryConfigLock.RUnlock() | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
// Now find the entry for the repository, if there is one. If not, | ||
// we'll assume there's no auth needed. | ||
repoEntry := &repo.Entry{} | ||
repoEntry.URL = repoURL | ||
for _, entry := range repoFile.Repositories { | ||
if urlutil.Equal(repoEntry.URL, entry.URL) { | ||
repoEntry = entry | ||
// Ensure we have the repository index as this is | ||
// later used by Helm. | ||
if r, err := repo.NewChartRepository(repoEntry, getters); err == nil { | ||
r.DownloadIndexFile(repositoryCache) | ||
} | ||
break | ||
} | ||
} | ||
|
||
// Look up the full URL of the chart with the collected credentials | ||
// and given chart name and version. | ||
chartURL, err := repo.FindChartInAuthRepoURL(repoEntry.URL, repoEntry.Username, repoEntry.Password, name, version, | ||
repoEntry.CertFile, repoEntry.KeyFile, repoEntry.CAFile, getters) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
return h.Pull(chartURL, version, dest) | ||
} |
Oops, something went wrong.