Skip to content

Commit b951cbb

Browse files
authored
Merge pull request #1035 from fluxcd/helm-index-digest-rev
helm: only use Digest to calculcate index revision
2 parents 568b932 + c712fed commit b951cbb

File tree

4 files changed

+10
-81
lines changed

4 files changed

+10
-81
lines changed

controllers/helmrepository_controller.go

+4-5
Original file line numberDiff line numberDiff line change
@@ -318,8 +318,8 @@ func (r *HelmRepositoryReconciler) notify(ctx context.Context, oldObj, newObj *s
318318
if sreconcile.FailureRecovery(oldObj, newObj, helmRepositoryFailConditions) {
319319
r.AnnotatedEventf(newObj, annotations, corev1.EventTypeNormal,
320320
meta.SucceededReason, message)
321+
ctrl.LoggerFrom(ctx).Info(message)
321322
}
322-
ctrl.LoggerFrom(ctx).Info(message)
323323
}
324324
}
325325
}
@@ -475,8 +475,7 @@ func (r *HelmRepositoryReconciler) reconcileSource(ctx context.Context, sp *patc
475475
}
476476
if curDig.Validate() == nil {
477477
// Short-circuit based on the fetched index being an exact match to the
478-
// stored Artifact. This prevents having to unmarshal the YAML to calculate
479-
// the (stable) revision, which is a memory expensive operation.
478+
// stored Artifact.
480479
if newDig := chartRepo.Digest(curDig.Algorithm()); newDig.Validate() == nil && (newDig == curDig) {
481480
*artifact = *curArtifact
482481
conditions.Delete(obj, sourcev1.FetchFailedCondition)
@@ -501,11 +500,11 @@ func (r *HelmRepositoryReconciler) reconcileSource(ctx context.Context, sp *patc
501500
var changed bool
502501
if artifact := obj.Status.Artifact; artifact != nil {
503502
curRev := digest.Digest(sourcev1.TransformLegacyRevision(artifact.Revision))
504-
changed = curRev.Validate() != nil || curRev != chartRepo.Revision(curRev.Algorithm())
503+
changed = curRev.Validate() != nil || curRev != chartRepo.Digest(curRev.Algorithm())
505504
}
506505

507506
// Calculate revision.
508-
revision := chartRepo.Revision(intdigest.Canonical)
507+
revision := chartRepo.Digest(intdigest.Canonical)
509508
if revision.Validate() != nil {
510509
e := &serror.Event{
511510
Err: fmt.Errorf("failed to calculate revision: %w", err),

controllers/helmrepository_controller_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -775,7 +775,7 @@ func TestHelmRepositoryReconciler_reconcileSource(t *testing.T) {
775775
checksum = newChartRepo.Digest(intdigest.Canonical)
776776

777777
g.Expect(newChartRepo.LoadFromPath()).To(Succeed())
778-
revision = newChartRepo.Revision(intdigest.Canonical)
778+
revision = newChartRepo.Digest(intdigest.Canonical)
779779
}
780780
if tt.beforeFunc != nil {
781781
tt.beforeFunc(g, obj, revision, checksum)

internal/helm/repository/chart_repository.go

+5-27
Original file line numberDiff line numberDiff line change
@@ -122,9 +122,8 @@ type ChartRepository struct {
122122

123123
tlsConfig *tls.Config
124124

125-
cached bool
126-
revisions map[digest.Algorithm]digest.Digest
127-
digests map[digest.Algorithm]digest.Digest
125+
cached bool
126+
digests map[digest.Algorithm]digest.Digest
128127

129128
*sync.RWMutex
130129
}
@@ -155,9 +154,8 @@ func NewChartRepository(URL, path string, providers getter.Providers, tlsConfig
155154

156155
func newChartRepository() *ChartRepository {
157156
return &ChartRepository{
158-
revisions: make(map[digest.Algorithm]digest.Digest, 0),
159-
digests: make(map[digest.Algorithm]digest.Digest, 0),
160-
RWMutex: &sync.RWMutex{},
157+
digests: make(map[digest.Algorithm]digest.Digest, 0),
158+
RWMutex: &sync.RWMutex{},
161159
}
162160
}
163161

@@ -351,7 +349,6 @@ func (r *ChartRepository) LoadFromPath() error {
351349
}
352350

353351
r.Index = i
354-
r.revisions = make(map[digest.Algorithm]digest.Digest, 0)
355352
return nil
356353
}
357354

@@ -384,24 +381,6 @@ func (r *ChartRepository) DownloadIndex(w io.Writer) (err error) {
384381
return nil
385382
}
386383

387-
// Revision returns the revision of the ChartRepository's Index. It assumes
388-
// the Index is stable sorted.
389-
func (r *ChartRepository) Revision(algorithm digest.Algorithm) digest.Digest {
390-
if !r.HasIndex() {
391-
return ""
392-
}
393-
394-
r.Lock()
395-
defer r.Unlock()
396-
397-
if _, ok := r.revisions[algorithm]; !ok {
398-
if b, _ := yaml.Marshal(r.Index); len(b) > 0 {
399-
r.revisions[algorithm] = algorithm.FromBytes(b)
400-
}
401-
}
402-
return r.revisions[algorithm]
403-
}
404-
405384
// Digest returns the digest of the file at the ChartRepository's Path.
406385
func (r *ChartRepository) Digest(algorithm digest.Algorithm) digest.Digest {
407386
if !r.HasFile() {
@@ -463,7 +442,7 @@ func (r *ChartRepository) Clear() error {
463442
return nil
464443
}
465444

466-
// Invalidate clears any cached digests and revisions.
445+
// Invalidate clears any cached digests.
467446
func (r *ChartRepository) Invalidate() {
468447
r.Lock()
469448
defer r.Unlock()
@@ -473,7 +452,6 @@ func (r *ChartRepository) Invalidate() {
473452

474453
func (r *ChartRepository) invalidate() {
475454
r.digests = make(map[digest.Algorithm]digest.Digest, 0)
476-
r.revisions = make(map[digest.Algorithm]digest.Digest, 0)
477455
}
478456

479457
// VerifyChart verifies the chart against a signature.

internal/helm/repository/chart_repository_test.go

-48
Original file line numberDiff line numberDiff line change
@@ -392,7 +392,6 @@ func TestChartRepository_CacheIndex(t *testing.T) {
392392
r := newChartRepository()
393393
r.URL = "https://example.com"
394394
r.Client = &mg
395-
r.revisions["key"] = "value"
396395
r.digests["key"] = "value"
397396

398397
err := r.CacheIndex()
@@ -405,7 +404,6 @@ func TestChartRepository_CacheIndex(t *testing.T) {
405404
b, _ := os.ReadFile(r.Path)
406405
g.Expect(b).To(Equal(mg.Response))
407406

408-
g.Expect(r.revisions).To(BeEmpty())
409407
g.Expect(r.digests).To(BeEmpty())
410408
}
411409

@@ -480,11 +478,9 @@ func TestChartRepository_LoadFromPath(t *testing.T) {
480478

481479
r := newChartRepository()
482480
r.Path = i
483-
r.revisions["key"] = "value"
484481

485482
g.Expect(r.LoadFromPath()).To(Succeed())
486483
g.Expect(r.Index).ToNot(BeNil())
487-
g.Expect(r.revisions).To(BeEmpty())
488484
})
489485

490486
t.Run("no cache path", func(t *testing.T) {
@@ -507,44 +503,6 @@ func TestChartRepository_LoadFromPath(t *testing.T) {
507503
})
508504
}
509505

510-
func TestChartRepository_Revision(t *testing.T) {
511-
t.Run("with algorithm", func(t *testing.T) {
512-
r := newChartRepository()
513-
r.Index = repo.NewIndexFile()
514-
515-
for _, algo := range []digest.Algorithm{digest.SHA256, digest.SHA512} {
516-
t.Run(algo.String(), func(t *testing.T) {
517-
g := NewWithT(t)
518-
519-
d := r.Revision(algo)
520-
g.Expect(d).ToNot(BeEmpty())
521-
g.Expect(d.Algorithm()).To(Equal(algo))
522-
g.Expect(r.revisions[algo]).To(Equal(d))
523-
})
524-
}
525-
})
526-
527-
t.Run("without index", func(t *testing.T) {
528-
g := NewWithT(t)
529-
530-
r := newChartRepository()
531-
g.Expect(r.Revision(digest.SHA256)).To(BeEmpty())
532-
})
533-
534-
t.Run("from cache", func(t *testing.T) {
535-
g := NewWithT(t)
536-
537-
algo := digest.SHA256
538-
expect := digest.Digest("sha256:fake")
539-
540-
r := newChartRepository()
541-
r.Index = repo.NewIndexFile()
542-
r.revisions[algo] = expect
543-
544-
g.Expect(r.Revision(algo)).To(Equal(expect))
545-
})
546-
}
547-
548506
func TestChartRepository_Digest(t *testing.T) {
549507
t.Run("with algorithm", func(t *testing.T) {
550508
g := NewWithT(t)
@@ -625,11 +583,9 @@ func TestChartRepository_Clear(t *testing.T) {
625583

626584
r := newChartRepository()
627585
r.Index = repo.NewIndexFile()
628-
r.revisions["key"] = "value"
629586

630587
g.Expect(r.Clear()).To(Succeed())
631588
g.Expect(r.Index).To(BeNil())
632-
g.Expect(r.revisions).To(BeEmpty())
633589
})
634590

635591
t.Run("with index and cached path", func(t *testing.T) {
@@ -643,14 +599,12 @@ func TestChartRepository_Clear(t *testing.T) {
643599
r.Path = f.Name()
644600
r.Index = repo.NewIndexFile()
645601
r.digests["key"] = "value"
646-
r.revisions["key"] = "value"
647602
r.cached = true
648603

649604
g.Expect(r.Clear()).To(Succeed())
650605
g.Expect(r.Index).To(BeNil())
651606
g.Expect(r.Path).To(BeEmpty())
652607
g.Expect(r.digests).To(BeEmpty())
653-
g.Expect(r.revisions).To(BeEmpty())
654608
g.Expect(r.cached).To(BeFalse())
655609
})
656610

@@ -677,11 +631,9 @@ func TestChartRepository_Invalidate(t *testing.T) {
677631

678632
r := newChartRepository()
679633
r.digests["key"] = "value"
680-
r.revisions["key"] = "value"
681634

682635
r.Invalidate()
683636
g.Expect(r.digests).To(BeEmpty())
684-
g.Expect(r.revisions).To(BeEmpty())
685637
}
686638

687639
func verifyLocalIndex(t *testing.T, i *repo.IndexFile) {

0 commit comments

Comments
 (0)