diff --git a/pkg/helmutil/crds.go b/pkg/helmutil/crds.go index c64dc24..516eeb9 100644 --- a/pkg/helmutil/crds.go +++ b/pkg/helmutil/crds.go @@ -37,23 +37,30 @@ 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, targetVersion string) ([]unstructured.Unstructured, error) { - downloadDir, err := DownloadChartRelease(u.repoName, u.repoURL, u.chartName, targetVersion) + chartDir, err := GetChartTargetDir(u.chartName, targetVersion) if err != nil { return nil, err } - extractDir, err := ExtractChartRelease(downloadDir, targetVersion) - if err != nil { - return nil, err + if _, err := os.Stat(chartDir); os.IsNotExist(err) { + downloadDir, err := DownloadChartRelease(u.repoName, u.repoURL, u.chartName, targetVersion) + if err != nil { + return nil, err + } + + extractDir, err := ExtractChartRelease(downloadDir, u.chartName, targetVersion) + if err != nil { + return nil, err + } + chartDir = extractDir } - chartPath := filepath.Join(extractDir, u.chartName) - defer os.RemoveAll(downloadDir) + // defer os.RemoveAll(downloadDir) crds := make([]unstructured.Unstructured, 0) // For each dir under the charts subdir, check the "crds/" - paths, _ := findCRDDirs(chartPath) + paths, _ := findCRDDirs(chartDir) for _, path := range paths { err = parseChartCRDs(&crds, path) @@ -64,16 +71,16 @@ func (u *Upgrader) Upgrade(ctx context.Context, targetVersion string) ([]unstruc for _, obj := range crds { existingCrd := obj.DeepCopy() - err = u.client.Get(context.TODO(), client.ObjectKey{Name: obj.GetName()}, existingCrd) + err = u.client.Get(ctx, client.ObjectKey{Name: obj.GetName()}, existingCrd) if apierrors.IsNotFound(err) { - if err = u.client.Create(context.TODO(), &obj); err != nil { + if err = u.client.Create(ctx, &obj); err != nil { return nil, errors.Wrapf(err, "failed to create CRD %s", obj.GetName()) } } else if err != nil { return nil, errors.Wrapf(err, "failed to fetch state of %s", obj.GetName()) } else { obj.SetResourceVersion(existingCrd.GetResourceVersion()) - if err = u.client.Update(context.TODO(), &obj); err != nil { + if err = u.client.Update(ctx, &obj); err != nil { return nil, errors.Wrapf(err, "failed to update CRD %s", obj.GetName()) } } diff --git a/pkg/helmutil/fetch.go b/pkg/helmutil/fetch.go index 3c57114..2beab78 100644 --- a/pkg/helmutil/fetch.go +++ b/pkg/helmutil/fetch.go @@ -77,9 +77,6 @@ func DownloadChartRelease(repoName, repoURL, chartName, targetVersion string) (s return "", err } - // TODO We can't do removeAll here.. - // defer os.RemoveAll(dir) - // _ is ProvenanceVerify (TODO we might want to verify the release) saved, _, err := c.DownloadTo(url, targetVersion, dir) if err != nil { @@ -89,16 +86,18 @@ func DownloadChartRelease(repoName, repoURL, chartName, targetVersion string) (s return saved, nil } -func ExtractChartRelease(saved, targetVersion string) (string, error) { - // TODO We need saved for the install process, clip from here to another function.. - +func ExtractChartRelease(saved, chartName, targetVersion string) (string, error) { // Extract the files - subDir := filepath.Join("helm", targetVersion) + subDir := filepath.Join(chartName, targetVersion) extractDir, err := util.GetCacheDir(subDir) if err != nil { return "", err } + if _, err := util.CreateIfNotExistsDir(extractDir); err != nil { + return "", err + } + // extractDir is a target directory err = chartutil.ExpandFile(extractDir, saved) if err != nil { @@ -108,6 +107,16 @@ func ExtractChartRelease(saved, targetVersion string) (string, error) { return extractDir, nil } +func GetChartTargetDir(chartName, targetVersion string) (string, error) { + subDir := filepath.Join(chartName, targetVersion) + extractDir, err := util.GetCacheDir(subDir) + if err != nil { + return "", err + } + + return extractDir, err +} + func Release(cfg *action.Configuration, releaseName string) (*release.Release, error) { getAction := action.NewGet(cfg) return getAction.Run(releaseName) diff --git a/pkg/util/fs.go b/pkg/util/fs.go index 15b6274..6e75da4 100644 --- a/pkg/util/fs.go +++ b/pkg/util/fs.go @@ -9,7 +9,7 @@ const ( dirSuffix = "k8ssandra" ) -// GetCacheDir returns the caching directory for k8ssandra and creates it if it does not exists +// GetCacheDir returns the caching directory for module func GetCacheDir(module string) (string, error) { userCacheDir, err := os.UserCacheDir() if err != nil { @@ -17,7 +17,7 @@ func GetCacheDir(module string) (string, error) { } targetDir := filepath.Join(userCacheDir, dirSuffix, module) - return createIfNotExistsDir(targetDir) + return targetDir, nil } // GetConfigDir returns the config directory for k8ssandra and creates it if it does not exists @@ -28,10 +28,10 @@ func GetConfigDir(module string) (string, error) { } targetDir := filepath.Join(userConfigDir, dirSuffix, module) - return createIfNotExistsDir(targetDir) + return CreateIfNotExistsDir(targetDir) } -func createIfNotExistsDir(targetDir string) (string, error) { +func CreateIfNotExistsDir(targetDir string) (string, error) { if _, err := os.Stat(targetDir); os.IsNotExist(err) { if err := os.MkdirAll(targetDir, 0755); err != nil { return "", err