Skip to content

Commit

Permalink
Fix download caching
Browse files Browse the repository at this point in the history
  • Loading branch information
burmanm committed Jan 26, 2024
1 parent 8838f76 commit 068dccb
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 21 deletions.
27 changes: 17 additions & 10 deletions pkg/helmutil/crds.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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())
}
}
Expand Down
23 changes: 16 additions & 7 deletions pkg/helmutil/fetch.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 {
Expand All @@ -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)
Expand Down
8 changes: 4 additions & 4 deletions pkg/util/fs.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@ 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 {
return "", err
}

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
Expand All @@ -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
Expand Down

0 comments on commit 068dccb

Please sign in to comment.