Skip to content

Commit

Permalink
Remove hardcoded dirSuffix of k8ssandra and use repoName instead (#29)
Browse files Browse the repository at this point in the history
  • Loading branch information
burmanm authored Feb 21, 2024
1 parent 851c544 commit 8bed5a0
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 20 deletions.
4 changes: 2 additions & 2 deletions pkg/helmutil/crds.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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
}
Expand Down
8 changes: 4 additions & 4 deletions pkg/helmutil/crds_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)

Expand All @@ -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
}
Expand Down
9 changes: 4 additions & 5 deletions pkg/helmutil/fetch.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/helmutil/release.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
12 changes: 4 additions & 8 deletions pkg/util/fs.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down

0 comments on commit 8bed5a0

Please sign in to comment.