MCPcopy
hub / github.com/helm/helm / DownloadToCache

Method DownloadToCache

pkg/downloader/chart_downloader.go:217–346  ·  view source on GitHub ↗

DownloadToCache retrieves resources while using a content based cache.

(ref, version string)

Source from the content-addressed store, hash-verified

215
216// DownloadToCache retrieves resources while using a content based cache.
217func (c *ChartDownloader) DownloadToCache(ref, version string) (string, *provenance.Verification, error) {
218 if c.Cache == nil {
219 if c.ContentCache == "" {
220 return "", nil, errors.New("content cache must be set")
221 }
222 c.Cache = &DiskCache{Root: c.ContentCache}
223 slog.Debug("set up default downloader cache")
224 }
225
226 digestString, u, err := c.ResolveChartVersion(ref, version)
227 if err != nil {
228 return "", nil, err
229 }
230
231 g, err := c.Getters.ByScheme(u.Scheme)
232 if err != nil {
233 return "", nil, err
234 }
235
236 c.Options = append(c.Options, getter.WithAcceptHeader("application/gzip,application/octet-stream"))
237
238 // Check the cache for the file
239 // Strip the algorithm prefix (e.g., "sha256:") if present
240 digest, err := hex.DecodeString(stripDigestAlgorithm(digestString))
241 if err != nil {
242 return "", nil, fmt.Errorf("unable to decode digest: %w", err)
243 }
244 if digestString != "" && len(digest) != 32 {
245 return "", nil, fmt.Errorf("invalid digest length: %d", len(digest))
246 }
247 var digest32 [32]byte
248 copy(digest32[:], digest)
249
250 var pth string
251 // only fetch from the cache if we have a digest
252 if len(digest) > 0 {
253 pth, err = c.Cache.Get(digest32, CacheChart)
254 if err == nil {
255 slog.Debug("found chart in cache", "id", digestString)
256 }
257 }
258 if len(digest) == 0 || err != nil {
259 slog.Debug("attempting to download chart", "ref", ref, "version", version)
260 if err != nil && !os.IsNotExist(err) {
261 return "", nil, err
262 }
263
264 // Get file not in the cache
265 data, gerr := g.Get(u.String(), c.Options...)
266 if gerr != nil {
267 return "", nil, gerr
268 }
269
270 // Generate the digest
271 if len(digest) == 0 {
272 digest32 = sha256.Sum256(data.Bytes())
273 }
274

Callers 2

TestDownloadToCacheFunction · 0.95
LocateChartMethod · 0.95

Calls 9

ResolveChartVersionMethod · 0.95
WithAcceptHeaderFunction · 0.92
stripDigestAlgorithmFunction · 0.85
VerifyChartFunction · 0.85
GetMethod · 0.65
PutMethod · 0.65
DirMethod · 0.65
BySchemeMethod · 0.45
StringMethod · 0.45

Tested by 1

TestDownloadToCacheFunction · 0.76