From 5e9c2268e6754ce5beac6b360cc3d7574bc29629 Mon Sep 17 00:00:00 2001 From: Nikolai Prokoschenko Date: Sun, 25 Aug 2024 08:59:13 +0200 Subject: [PATCH] Re-use cache when downloading charts Previously, when a chart has been downloaded, it had been put in the cache. However, this has happened unconditionally, so that the cache hasn't actually been used for anything. This patch attempts to fix this and checks if the target file exists in the cache before downloading. Fixes: #12607, #12602 Signed-off-by: Nikolai Prokoschenko --- pkg/downloader/chart_downloader.go | 27 +++++++++++++++------------ 1 file changed, 15 insertions(+), 12 deletions(-) diff --git a/pkg/downloader/chart_downloader.go b/pkg/downloader/chart_downloader.go index a95894e00..bb4756efc 100644 --- a/pkg/downloader/chart_downloader.go +++ b/pkg/downloader/chart_downloader.go @@ -92,25 +92,28 @@ func (c *ChartDownloader) DownloadTo(ref, version, dest string) (string, *proven return "", nil, err } - g, err := c.Getters.ByScheme(u.Scheme) - if err != nil { - return "", nil, err - } - - data, err := g.Get(u.String(), c.Options...) - if err != nil { - return "", nil, err - } - name := filepath.Base(u.Path) if u.Scheme == registry.OCIScheme { idx := strings.LastIndexByte(name, ':') name = fmt.Sprintf("%s-%s.tgz", name[:idx], name[idx+1:]) } + g, err := c.Getters.ByScheme(u.Scheme) + if err != nil { + return "", nil, err + } destfile := filepath.Join(dest, name) - if err := fileutil.AtomicWriteFile(destfile, data, 0644); err != nil { - return destfile, nil, err + + _, err = os.Stat(destfile) + if err != nil { + data, err := g.Get(u.String(), c.Options...) + if err != nil { + return "", nil, err + } + + if err := fileutil.AtomicWriteFile(destfile, data, 0644); err != nil { + return destfile, nil, err + } } // If provenance is requested, verify it.