goGetterChart() downloads to user cache dir instead of working dir

This commit is contained in:
Quan TRAN 2022-02-11 00:24:02 +01:00 committed by Yusuke Kuoka
parent 766b03047c
commit cf02442591
3 changed files with 49 additions and 25 deletions

View File

@ -4,23 +4,37 @@ import (
"context" "context"
"encoding/json" "encoding/json"
"fmt" "fmt"
"os"
"path/filepath"
"strings"
"github.com/hashicorp/go-getter" "github.com/hashicorp/go-getter"
"github.com/hashicorp/go-getter/helper/url" "github.com/hashicorp/go-getter/helper/url"
"go.uber.org/multierr" "go.uber.org/multierr"
"go.uber.org/zap" "go.uber.org/zap"
"gopkg.in/yaml.v2" "gopkg.in/yaml.v2"
"os"
"path/filepath"
"strings"
) )
const DefaultCacheDir = ".helmfile/cache" const defaultCacheDir = "helmfile"
func cacheDir() string {
dir, err := os.UserCacheDir()
if err != nil {
// fall back to relative path with hidden directory
return relativeCacheDir()
}
return filepath.Join(dir, defaultCacheDir)
}
// TODO remove this function when rework on caching of remote helmfiles
func relativeCacheDir() string {
return fmt.Sprintf(".%s", defaultCacheDir)
}
type Remote struct { type Remote struct {
Logger *zap.SugaredLogger Logger *zap.SugaredLogger
// Home is the home directory for helmfile. Usually this points to $HOME of the user running helmfile. // Home is the directory in which remote downloads files. If empty, user cache directory is used
// Helmfile saves fetched remote files into .helmfile/cache under home
Home string Home string
// Getter is the underlying implementation of getter used for fetching remote files // Getter is the underlying implementation of getter used for fetching remote files
@ -166,7 +180,7 @@ func (r *Remote) Fetch(goGetterSrc string, cacheDirOpt ...string) (string, error
r.Logger.Debugf("file: %s", u.File) r.Logger.Debugf("file: %s", u.File)
// This should be shared across variant commands, so that they can share cache for the shared imports // This should be shared across variant commands, so that they can share cache for the shared imports
cacheBaseDir := DefaultCacheDir cacheBaseDir := ""
if len(cacheDirOpt) == 1 { if len(cacheDirOpt) == 1 {
cacheBaseDir = cacheDirOpt[0] cacheBaseDir = cacheDirOpt[0]
} else if len(cacheDirOpt) > 0 { } else if len(cacheDirOpt) > 0 {
@ -187,10 +201,10 @@ func (r *Remote) Fetch(goGetterSrc string, cacheDirOpt ...string) (string, error
cached := false cached := false
// e.g. .helmfile/cache/https_github_com_cloudposse_helmfiles_git.ref=0.xx.0 // e.g. https_github_com_cloudposse_helmfiles_git.ref=0.xx.0
getterDst := filepath.Join(cacheBaseDir, cacheKey) getterDst := filepath.Join(cacheBaseDir, cacheKey)
// e.g. $PWD/.helmfile/cache/https_github_com_cloudposse_helmfiles_git.ref=0.xx.0 // e.g. os.cacheDir()/helmfile/https_github_com_cloudposse_helmfiles_git.ref=0.xx.0
cacheDirPath := filepath.Join(r.Home, getterDst) cacheDirPath := filepath.Join(r.Home, getterDst)
r.Logger.Debugf("home: %s", r.Home) r.Logger.Debugf("home: %s", r.Home)
@ -275,5 +289,14 @@ func NewRemote(logger *zap.SugaredLogger, homeDir string, readFile func(string)
DirExists: dirExists, DirExists: dirExists,
FileExists: fileExists, FileExists: fileExists,
} }
if remote.Home == "" {
// Use for remote charts
remote.Home = cacheDir()
} else {
// Use for remote helmfiles, this case Home is relative to the processing file
remote.Home = filepath.Join(remote.Home, relativeCacheDir())
}
return remote return remote
} }

View File

@ -3,6 +3,7 @@ package remote
import ( import (
"fmt" "fmt"
"os" "os"
"path/filepath"
"testing" "testing"
"github.com/google/go-cmp/cmp" "github.com/google/go-cmp/cmp"
@ -12,10 +13,10 @@ import (
func TestRemote_HttpsGitHub(t *testing.T) { func TestRemote_HttpsGitHub(t *testing.T) {
cleanfs := map[string]string{ cleanfs := map[string]string{
"/path/to/home": "", cacheDir(): "",
} }
cachefs := map[string]string{ cachefs := map[string]string{
"/path/to/home/.helmfile/cache/https_github_com_cloudposse_helmfiles_git.ref=0.40.0/releases/kiam.yaml": "foo: bar", filepath.Join(cacheDir(), "https_github_com_cloudposse_helmfiles_git.ref=0.40.0/releases/kiam.yaml"): "foo: bar",
} }
type testcase struct { type testcase struct {
@ -37,7 +38,7 @@ func TestRemote_HttpsGitHub(t *testing.T) {
hit := true hit := true
get := func(wd, src, dst string) error { get := func(wd, src, dst string) error {
if wd != "/path/to/home" { if wd != cacheDir() {
return fmt.Errorf("unexpected wd: %s", wd) return fmt.Errorf("unexpected wd: %s", wd)
} }
if src != "git::https://github.com/cloudposse/helmfiles.git?ref=0.40.0" { if src != "git::https://github.com/cloudposse/helmfiles.git?ref=0.40.0" {
@ -54,7 +55,7 @@ func TestRemote_HttpsGitHub(t *testing.T) {
} }
remote := &Remote{ remote := &Remote{
Logger: helmexec.NewLogger(os.Stderr, "debug"), Logger: helmexec.NewLogger(os.Stderr, "debug"),
Home: "/path/to/home", Home: cacheDir(),
Getter: getter, Getter: getter,
ReadFile: testfs.ReadFile, ReadFile: testfs.ReadFile,
FileExists: testfs.FileExistsAt, FileExists: testfs.FileExistsAt,
@ -73,8 +74,9 @@ func TestRemote_HttpsGitHub(t *testing.T) {
t.Fatalf("unexpected error: %v", err) t.Fatalf("unexpected error: %v", err)
} }
if file != "/path/to/home/.helmfile/cache/https_github_com_cloudposse_helmfiles_git.ref=0.40.0/releases/kiam.yaml" { expectedFile := filepath.Join(cacheDir(), "https_github_com_cloudposse_helmfiles_git.ref=0.40.0/releases/kiam.yaml")
t.Errorf("unexpected file located: %s", file) if file != expectedFile {
t.Errorf("unexpected file located: %s vs expected: %s", file, expectedFile)
} }
if testcase.expectCacheHit && !hit { if testcase.expectCacheHit && !hit {
@ -89,10 +91,10 @@ func TestRemote_HttpsGitHub(t *testing.T) {
func TestRemote_SShGitHub(t *testing.T) { func TestRemote_SShGitHub(t *testing.T) {
cleanfs := map[string]string{ cleanfs := map[string]string{
"/path/to/home": "", cacheDir(): "",
} }
cachefs := map[string]string{ cachefs := map[string]string{
"/path/to/home/.helmfile/cache/ssh_github_com_cloudposse_helmfiles_git.ref=0.40.0/releases/kiam.yaml": "foo: bar", filepath.Join(cacheDir(), "ssh_github_com_cloudposse_helmfiles_git.ref=0.40.0/releases/kiam.yaml"): "foo: bar",
} }
type testcase struct { type testcase struct {
@ -114,7 +116,7 @@ func TestRemote_SShGitHub(t *testing.T) {
hit := true hit := true
get := func(wd, src, dst string) error { get := func(wd, src, dst string) error {
if wd != "/path/to/home" { if wd != cacheDir() {
return fmt.Errorf("unexpected wd: %s", wd) return fmt.Errorf("unexpected wd: %s", wd)
} }
if src != "git::ssh://git@github.com/cloudposse/helmfiles.git?ref=0.40.0" { if src != "git::ssh://git@github.com/cloudposse/helmfiles.git?ref=0.40.0" {
@ -131,7 +133,7 @@ func TestRemote_SShGitHub(t *testing.T) {
} }
remote := &Remote{ remote := &Remote{
Logger: helmexec.NewLogger(os.Stderr, "debug"), Logger: helmexec.NewLogger(os.Stderr, "debug"),
Home: "/path/to/home", Home: cacheDir(),
Getter: getter, Getter: getter,
ReadFile: testfs.ReadFile, ReadFile: testfs.ReadFile,
FileExists: testfs.FileExistsAt, FileExists: testfs.FileExistsAt,
@ -144,8 +146,9 @@ func TestRemote_SShGitHub(t *testing.T) {
t.Fatalf("unexpected error: %v", err) t.Fatalf("unexpected error: %v", err)
} }
if file != "/path/to/home/.helmfile/cache/ssh_github_com_cloudposse_helmfiles_git.ref=0.40.0/releases/kiam.yaml" { expectedFile := filepath.Join(cacheDir(), "ssh_github_com_cloudposse_helmfiles_git.ref=0.40.0/releases/kiam.yaml")
t.Errorf("unexpected file located: %s", file) if file != expectedFile {
t.Errorf("unexpected file located: %s vs expected: %s", file, expectedFile)
} }
if testcase.expectCacheHit && !hit { if testcase.expectCacheHit && !hit {

View File

@ -40,9 +40,7 @@ type Chartify struct {
} }
func (st *HelmState) downloadChartWithGoGetter(r *ReleaseSpec) (string, error) { func (st *HelmState) downloadChartWithGoGetter(r *ReleaseSpec) (string, error) {
pathElems := []string{ var pathElems []string
remote.DefaultCacheDir,
}
if r.Namespace != "" { if r.Namespace != "" {
pathElems = append(pathElems, r.Namespace) pathElems = append(pathElems, r.Namespace)
@ -70,7 +68,7 @@ func (st *HelmState) goGetterChart(chart, dir, cacheDir string, force bool) (str
return "", fmt.Errorf("Parsing url from dir failed due to error %q.\nContinuing the process assuming this is a regular Helm chart or a local dir.", err.Error()) return "", fmt.Errorf("Parsing url from dir failed due to error %q.\nContinuing the process assuming this is a regular Helm chart or a local dir.", err.Error())
} }
} else { } else {
r := remote.NewRemote(st.logger, st.basePath, st.readFile, directoryExistsAt, fileExistsAt) r := remote.NewRemote(st.logger, "", st.readFile, directoryExistsAt, fileExistsAt)
fetchedDir, err := r.Fetch(chart, cacheDir) fetchedDir, err := r.Fetch(chart, cacheDir)
if err != nil { if err != nil {