From a93c2d420f41fdeb13f5a809ec9c6da0e53a260c Mon Sep 17 00:00:00 2001 From: Hidde Beydals Date: Thu, 21 May 2020 00:38:35 +0200 Subject: [PATCH] helm/v3: garbage collect anonymous index files The Helm 3 dependency update generates anonymous index files on every run without garbage collecting them, resulting in spurious disk usage. Configuring a temporary cache directory and garbage collecting it after the dependency update is not possible as the download manager seems to ignore the configured repository cache path and always create the index file in the XDG cache directory. Instead we detect the any anonymous index filex by their "=" suffix and delete if they are older than 2m. --- pkg/helm/v3/dependency.go | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/pkg/helm/v3/dependency.go b/pkg/helm/v3/dependency.go index 423f00f05..4a13a501a 100644 --- a/pkg/helm/v3/dependency.go +++ b/pkg/helm/v3/dependency.go @@ -1,12 +1,22 @@ package v3 import ( + "os" + "path/filepath" + "strings" + "time" + "helm.sh/helm/v3/pkg/downloader" "github.com/fluxcd/helm-operator/pkg/utils" ) func (h *HelmV3) DependencyUpdate(chartPath string) error { + // Garbage collect before the dependency update so that + // anonymous files from previous runs are cleared, with + // a safe guard time offset to not touch any files in + // use. + garbageCollect(repositoryCache, time.Second * 120) out := utils.NewLogWriter(h.logger) man := &downloader.Manager{ Out: out, @@ -17,3 +27,21 @@ func (h *HelmV3) DependencyUpdate(chartPath string) error { } return man.Update() } + +// garbageCollect walks over the files in the given path and deletes +// any anonymous index file with a mod time older than the given +// duration. +func garbageCollect(path string, olderThan time.Duration) { + now := time.Now() + filepath.Walk(path, func(p string, f os.FileInfo, err error) error { + if err != nil || f.IsDir() { + return nil + } + if strings.HasSuffix(f.Name(), "=-index.yaml") || strings.HasSuffix(f.Name(), "=-charts.txt") { + if now.Sub(f.ModTime()) > olderThan { + return os.Remove(p) + } + } + return nil + }) +}