From 8bed5a0de43c760c194f251b155130a494c7b946 Mon Sep 17 00:00:00 2001 From: Michael Burman Date: Wed, 21 Feb 2024 19:29:47 +0200 Subject: [PATCH] Remove hardcoded dirSuffix of k8ssandra and use repoName instead (#29) --- pkg/helmutil/crds.go | 4 ++-- pkg/helmutil/crds_test.go | 8 ++++---- pkg/helmutil/fetch.go | 9 ++++----- pkg/helmutil/release.go | 2 +- pkg/util/fs.go | 12 ++++-------- 5 files changed, 15 insertions(+), 20 deletions(-) diff --git a/pkg/helmutil/crds.go b/pkg/helmutil/crds.go index 9ef3469..1028602 100644 --- a/pkg/helmutil/crds.go +++ b/pkg/helmutil/crds.go @@ -38,7 +38,7 @@ func NewUpgrader(c client.Client, repoName, repoURL, chartName string) (*Upgrade // Upgrade installs the missing CRDs or updates them if they exists already func (u *Upgrader) Upgrade(ctx context.Context, chartVersion string) ([]unstructured.Unstructured, error) { - chartDir, err := GetChartTargetDir(u.chartName) + chartDir, err := GetChartTargetDir(u.repoName, u.chartName) if err != nil { return nil, err } @@ -49,7 +49,7 @@ func (u *Upgrader) Upgrade(ctx context.Context, chartVersion string) ([]unstruct return nil, err } - extractDir, err := ExtractChartRelease(downloadDir, u.chartName, chartVersion) + extractDir, err := ExtractChartRelease(downloadDir, u.repoName, u.chartName, chartVersion) if err != nil { return nil, err } diff --git a/pkg/helmutil/crds_test.go b/pkg/helmutil/crds_test.go index e74f713..9c554b6 100644 --- a/pkg/helmutil/crds_test.go +++ b/pkg/helmutil/crds_test.go @@ -21,7 +21,7 @@ func TestUpgradingCRDs(t *testing.T) { for _, chartName := range chartNames { namespace := env.CreateNamespace(t) kubeClient := env.Client(namespace) - require.NoError(cleanCache(chartName)) + require.NoError(cleanCache("k8ssandra", chartName)) // creating new upgrader u, err := helmutil.NewUpgrader(kubeClient, helmutil.K8ssandraRepoName, helmutil.StableK8ssandraRepoURL, chartName) @@ -55,7 +55,7 @@ func TestUpgradingCRDs(t *testing.T) { require.False(strings.HasPrefix(descRunsAsCassandra, "DEPRECATED")) // Upgrading to 0.46.1 - require.NoError(cleanCache(chartName)) + require.NoError(cleanCache("k8ssandra", chartName)) _, err = u.Upgrade(context.TODO(), "0.46.1") require.NoError(err) @@ -73,8 +73,8 @@ func TestUpgradingCRDs(t *testing.T) { } } -func cleanCache(chartName string) error { - chartDir, err := helmutil.GetChartTargetDir(chartName) +func cleanCache(repoName, chartName string) error { + chartDir, err := helmutil.GetChartTargetDir(repoName, chartName) if err != nil { return err } diff --git a/pkg/helmutil/fetch.go b/pkg/helmutil/fetch.go index 0e92dac..815839e 100644 --- a/pkg/helmutil/fetch.go +++ b/pkg/helmutil/fetch.go @@ -86,10 +86,10 @@ func DownloadChartRelease(repoName, repoURL, chartName, chartVersion string) (st return saved, nil } -func ExtractChartRelease(saved, chartName, chartVersion string) (string, error) { +func ExtractChartRelease(saved, repoName, chartName, chartVersion string) (string, error) { // Extract the files subDir := filepath.Join(chartName, chartVersion) - extractDir, err := util.GetCacheDir(subDir) + extractDir, err := util.GetCacheDir(repoName, subDir) if err != nil { return "", err } @@ -107,9 +107,8 @@ func ExtractChartRelease(saved, chartName, chartVersion string) (string, error) return extractDir, nil } -func GetChartTargetDir(chartName string) (string, error) { - subDir := filepath.Join(chartName) - extractDir, err := util.GetCacheDir(subDir) +func GetChartTargetDir(repoName, chartName string) (string, error) { + extractDir, err := util.GetCacheDir(repoName, chartName) if err != nil { return "", err } diff --git a/pkg/helmutil/release.go b/pkg/helmutil/release.go index 6404f87..8213dcf 100644 --- a/pkg/helmutil/release.go +++ b/pkg/helmutil/release.go @@ -86,7 +86,7 @@ func MergeValuesFile(cfg *action.Configuration, settings *cli.EnvSettings, chart defer file.Close() - cacheDir, err := util.GetCacheDir("helm") + cacheDir, err := util.GetCacheDir("helm", "values") if err != nil { return nil, err } diff --git a/pkg/util/fs.go b/pkg/util/fs.go index 6e75da4..e3adf67 100644 --- a/pkg/util/fs.go +++ b/pkg/util/fs.go @@ -5,29 +5,25 @@ import ( "path/filepath" ) -const ( - dirSuffix = "k8ssandra" -) - // GetCacheDir returns the caching directory for module -func GetCacheDir(module string) (string, error) { +func GetCacheDir(repo, chart string) (string, error) { userCacheDir, err := os.UserCacheDir() if err != nil { return "", err } - targetDir := filepath.Join(userCacheDir, dirSuffix, module) + targetDir := filepath.Join(userCacheDir, repo, chart) return targetDir, nil } // GetConfigDir returns the config directory for k8ssandra and creates it if it does not exists -func GetConfigDir(module string) (string, error) { +func GetConfigDir(repo, chart string) (string, error) { userConfigDir, err := os.UserConfigDir() if err != nil { return "", err } - targetDir := filepath.Join(userConfigDir, dirSuffix, module) + targetDir := filepath.Join(userConfigDir, repo, chart) return CreateIfNotExistsDir(targetDir) }