Skip to content

Commit f53bfd1

Browse files
committed
Use Artifact.Path for HelmRepository index cache
Resolving it to a local path does not make it more unique, while resulting in longer keys and a lot of safejoin calls. Signed-off-by: Hidde Beydals <[email protected]>
1 parent d62f4dc commit f53bfd1

File tree

4 files changed

+16
-19
lines changed

4 files changed

+16
-19
lines changed

controllers/helmchart_controller.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -667,16 +667,16 @@ func (r *HelmChartReconciler) buildFromHelmRepository(ctx context.Context, obj *
667667

668668
// Attempt to load the index from the cache.
669669
if r.Cache != nil {
670-
if index, ok := r.Cache.Get(httpChartRepo.Path); ok {
670+
if index, ok := r.Cache.Get(repo.GetArtifact().Path); ok {
671671
r.IncCacheEvents(cache.CacheEventTypeHit, repo.Name, repo.Namespace)
672-
r.Cache.SetExpiration(httpChartRepo.Path, r.TTL)
672+
r.Cache.SetExpiration(repo.GetArtifact().Path, r.TTL)
673673
httpChartRepo.Index = index.(*helmrepo.IndexFile)
674674
} else {
675675
r.IncCacheEvents(cache.CacheEventTypeMiss, repo.Name, repo.Namespace)
676676
defer func() {
677677
// If we succeed in loading the index, cache it.
678678
if httpChartRepo.Index != nil {
679-
if err = r.Cache.Set(httpChartRepo.Path, httpChartRepo.Index, r.TTL); err != nil {
679+
if err = r.Cache.Set(repo.GetArtifact().Path, httpChartRepo.Index, r.TTL); err != nil {
680680
r.eventLogf(ctx, obj, eventv1.EventTypeTrace, sourcev1.CacheOperationFailedReason, "failed to cache index: %s", err)
681681
}
682682
}
@@ -1123,21 +1123,21 @@ func (r *HelmChartReconciler) namespacedChartRepositoryCallback(ctx context.Cont
11231123
return nil, err
11241124
}
11251125

1126-
if obj.Status.Artifact != nil {
1126+
if artifact := obj.GetArtifact(); artifact != nil {
1127+
httpChartRepo.Path = r.Storage.LocalPath(*artifact)
1128+
11271129
// Attempt to load the index from the cache.
1128-
httpChartRepo.Path = r.Storage.LocalPath(*obj.GetArtifact())
11291130
if r.Cache != nil {
1130-
if index, ok := r.Cache.Get(httpChartRepo.Path); ok {
1131+
if index, ok := r.Cache.Get(artifact.Path); ok {
11311132
r.IncCacheEvents(cache.CacheEventTypeHit, name, namespace)
1132-
r.Cache.SetExpiration(httpChartRepo.Path, r.TTL)
1133-
1133+
r.Cache.SetExpiration(artifact.Path, r.TTL)
11341134
httpChartRepo.Index = index.(*helmrepo.IndexFile)
11351135
} else {
11361136
r.IncCacheEvents(cache.CacheEventTypeMiss, name, namespace)
11371137
if err := httpChartRepo.LoadFromPath(); err != nil {
11381138
return nil, err
11391139
}
1140-
r.Cache.Set(httpChartRepo.Path, httpChartRepo.Index, r.TTL)
1140+
r.Cache.Set(artifact.Path, httpChartRepo.Index, r.TTL)
11411141
}
11421142
}
11431143
}

controllers/helmchart_controller_test.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -137,8 +137,7 @@ func TestHelmChartReconciler_Reconcile(t *testing.T) {
137137
repoKey := client.ObjectKey{Name: repository.Name, Namespace: repository.Namespace}
138138
err = testEnv.Get(ctx, repoKey, repository)
139139
g.Expect(err).ToNot(HaveOccurred())
140-
localPath := testStorage.LocalPath(*repository.GetArtifact())
141-
_, found := testCache.Get(localPath)
140+
_, found := testCache.Get(repository.GetArtifact().Path)
142141
g.Expect(found).To(BeTrue())
143142

144143
g.Expect(testEnv.Delete(ctx, obj)).To(Succeed())

controllers/helmrepository_controller.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -563,7 +563,7 @@ func (r *HelmRepositoryReconciler) reconcileArtifact(ctx context.Context, sp *pa
563563
if obj.GetArtifact().HasRevision(artifact.Revision) && obj.GetArtifact().HasChecksum(artifact.Checksum) {
564564
// Extend TTL of the Index in the cache (if present).
565565
if r.Cache != nil {
566-
r.Cache.SetExpiration(r.Storage.LocalPath(*artifact), r.TTL)
566+
r.Cache.SetExpiration(artifact.Path, r.TTL)
567567
}
568568

569569
r.eventLogf(ctx, obj, eventv1.EventTypeTrace, sourcev1.ArtifactUpToDateReason, "artifact up-to-date with remote revision: '%s'", artifact.Revision)
@@ -607,10 +607,9 @@ func (r *HelmRepositoryReconciler) reconcileArtifact(ctx context.Context, sp *pa
607607
if r.Cache != nil && chartRepo.Index != nil {
608608
// The cache keys have to be safe in multi-tenancy environments, as
609609
// otherwise it could be used as a vector to bypass the repository's
610-
// authentication. Using r.Storage.LocalPath(*repo.GetArtifact())
611-
// is safe as the path is in the format of:
612-
// /<repository-name>/<chart-name>/<filename>.
613-
if err := r.Cache.Set(r.Storage.LocalPath(*artifact), chartRepo.Index, r.TTL); err != nil {
610+
// authentication. Using the Artifact.Path is safe as the path is in
611+
// the format of: /<repository-name>/<chart-name>/<filename>.
612+
if err := r.Cache.Set(artifact.Path, chartRepo.Index, r.TTL); err != nil {
614613
r.eventLogf(ctx, obj, eventv1.EventTypeTrace, sourcev1.CacheOperationFailedReason, "failed to cache index: %s", err)
615614
}
616615
}

controllers/helmrepository_controller_test.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -848,7 +848,7 @@ func TestHelmRepositoryReconciler_reconcileArtifact(t *testing.T) {
848848
},
849849
want: sreconcile.ResultSuccess,
850850
afterFunc: func(t *WithT, obj *sourcev1.HelmRepository, cache *cache.Cache) {
851-
i, ok := cache.Get(testStorage.LocalPath(*obj.GetArtifact()))
851+
i, ok := cache.Get(obj.GetArtifact().Path)
852852
t.Expect(ok).To(BeTrue())
853853
t.Expect(i).To(BeAssignableToTypeOf(&repo.IndexFile{}))
854854
},
@@ -1581,7 +1581,6 @@ func TestHelmRepositoryReconciler_InMemoryCaching(t *testing.T) {
15811581

15821582
err = testEnv.Get(ctx, key, helmRepo)
15831583
g.Expect(err).ToNot(HaveOccurred())
1584-
localPath := testStorage.LocalPath(*helmRepo.GetArtifact())
1585-
_, cacheHit := testCache.Get(localPath)
1584+
_, cacheHit := testCache.Get(helmRepo.GetArtifact().Path)
15861585
g.Expect(cacheHit).To(BeTrue())
15871586
}

0 commit comments

Comments
 (0)