Skip to content

Commit bdd0717

Browse files
committed
controllers: use TransformLegacyRevision helper
Signed-off-by: Hidde Beydals <hello@hidde.co>
1 parent 082bae2 commit bdd0717

8 files changed

+39
-83
lines changed

controllers/artifact.go

+2-14
Original file line numberDiff line numberDiff line change
@@ -21,31 +21,19 @@ import sourcev1 "github.com/fluxcd/source-controller/api/v1beta2"
2121
type artifactSet []*sourcev1.Artifact
2222

2323
// Diff returns true if any of the revisions in the artifactSet does not match any of the given artifacts.
24-
func (s artifactSet) Diff(set artifactSet, comp func(x, y *sourcev1.Artifact) bool) bool {
24+
func (s artifactSet) Diff(set artifactSet) bool {
2525
if len(s) != len(set) {
2626
return true
2727
}
2828

29-
if comp == nil {
30-
comp = defaultCompare
31-
}
32-
3329
outer:
3430
for _, j := range s {
3531
for _, k := range set {
36-
if comp(j, k) {
32+
if k.HasRevision(j.Revision) {
3733
continue outer
3834
}
3935
}
4036
return true
4137
}
4238
return false
4339
}
44-
45-
func defaultCompare(x, y *sourcev1.Artifact) bool {
46-
if y == nil {
47-
return false
48-
}
49-
return x.HasRevision(y.Revision)
50-
}
51-

controllers/artifact_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ func Test_artifactSet_Diff(t *testing.T) {
115115
}
116116
for _, tt := range tests {
117117
t.Run(tt.name, func(t *testing.T) {
118-
result := tt.current.Diff(tt.updated, nil)
118+
result := tt.current.Diff(tt.updated)
119119
if result != tt.expected {
120120
t.Errorf("Archive() result = %v, wantResult %v", result, tt.expected)
121121
}

controllers/bucket_controller.go

+6-13
Original file line numberDiff line numberDiff line change
@@ -465,8 +465,8 @@ func (r *BucketReconciler) reconcileSource(ctx context.Context, sp *patch.Serial
465465
// Check if index has changed compared to current Artifact revision.
466466
var changed bool
467467
if artifact := obj.Status.Artifact; artifact != nil && artifact.Revision != "" {
468-
curRev := backwardsCompatibleDigest(artifact.Revision)
469-
changed = curRev != index.Digest(curRev.Algorithm())
468+
curRev := digest.Digest(sourcev1.TransformLegacyRevision(artifact.Revision))
469+
changed = curRev.Validate() != nil || curRev != index.Digest(curRev.Algorithm())
470470
}
471471

472472
// Fetch the bucket objects if required to.
@@ -518,8 +518,8 @@ func (r *BucketReconciler) reconcileArtifact(ctx context.Context, sp *patch.Seri
518518
// Set the ArtifactInStorageCondition if there's no drift.
519519
defer func() {
520520
if curArtifact := obj.GetArtifact(); curArtifact != nil && curArtifact.Revision != "" {
521-
curRev := backwardsCompatibleDigest(curArtifact.Revision)
522-
if index.Digest(curRev.Algorithm()) == curRev {
521+
curRev := digest.Digest(sourcev1.TransformLegacyRevision(curArtifact.Revision))
522+
if curRev.Validate() == nil && index.Digest(curRev.Algorithm()) == curRev {
523523
conditions.Delete(obj, sourcev1.ArtifactOutdatedCondition)
524524
conditions.MarkTrue(obj, sourcev1.ArtifactInStorageCondition, meta.SucceededReason,
525525
"stored artifact: revision '%s'", artifact.Revision)
@@ -529,8 +529,8 @@ func (r *BucketReconciler) reconcileArtifact(ctx context.Context, sp *patch.Seri
529529

530530
// The artifact is up-to-date
531531
if curArtifact := obj.GetArtifact(); curArtifact != nil && curArtifact.Revision != "" {
532-
curRev := backwardsCompatibleDigest(curArtifact.Revision)
533-
if index.Digest(curRev.Algorithm()) == curRev {
532+
curRev := digest.Digest(sourcev1.TransformLegacyRevision(curArtifact.Revision))
533+
if curRev.Validate() == nil && index.Digest(curRev.Algorithm()) == curRev {
534534
r.eventLogf(ctx, obj, eventv1.EventTypeTrace, sourcev1.ArtifactUpToDateReason, "artifact up-to-date with remote revision: '%s'", artifact.Revision)
535535
return sreconcile.ResultSuccess, nil
536536
}
@@ -796,10 +796,3 @@ func fetchIndexFiles(ctx context.Context, provider BucketProvider, obj *sourcev1
796796

797797
return nil
798798
}
799-
800-
func backwardsCompatibleDigest(d string) digest.Digest {
801-
if !strings.Contains(d, ":") {
802-
d = digest.SHA256.String() + ":" + d
803-
}
804-
return digest.Digest(d)
805-
}

controllers/gitrepository_controller.go

+7-17
Original file line numberDiff line numberDiff line change
@@ -515,7 +515,7 @@ func (r *GitRepositoryReconciler) reconcileSource(ctx context.Context, sp *patch
515515
}
516516

517517
// Observe if the artifacts still match the previous included ones
518-
if artifacts.Diff(obj.Status.IncludedArtifacts, gitArtifactRevisionEqual) {
518+
if artifacts.Diff(obj.Status.IncludedArtifacts) {
519519
message := fmt.Sprintf("included artifacts differ from last observed includes")
520520
if obj.Status.IncludedArtifacts != nil {
521521
conditions.MarkTrue(obj, sourcev1.ArtifactOutdatedCondition, "IncludeChange", message)
@@ -584,8 +584,7 @@ func (r *GitRepositoryReconciler) reconcileSource(ctx context.Context, sp *patch
584584
}
585585

586586
// Mark observations about the revision on the object
587-
if curArtifact := obj.Status.Artifact; curArtifact == nil ||
588-
git.TransformRevision(curArtifact.Revision) != commit.String() {
587+
if !obj.GetArtifact().HasRevision(commit.String()) {
589588
message := fmt.Sprintf("new upstream revision '%s'", commit.String())
590589
if obj.GetArtifact() != nil {
591590
conditions.MarkTrue(obj, sourcev1.ArtifactOutdatedCondition, "NewRevision", message)
@@ -618,9 +617,8 @@ func (r *GitRepositoryReconciler) reconcileArtifact(ctx context.Context, sp *pat
618617

619618
// Set the ArtifactInStorageCondition if there's no drift.
620619
defer func() {
621-
if curArtifact := obj.GetArtifact(); curArtifact != nil &&
622-
git.TransformRevision(curArtifact.Revision) == artifact.Revision &&
623-
!includes.Diff(obj.Status.IncludedArtifacts, gitArtifactRevisionEqual) &&
620+
if curArtifact := obj.GetArtifact(); curArtifact.HasRevision(artifact.Revision) &&
621+
!includes.Diff(obj.Status.IncludedArtifacts) &&
624622
!gitContentConfigChanged(obj, includes) {
625623
conditions.Delete(obj, sourcev1.ArtifactOutdatedCondition)
626624
conditions.MarkTrue(obj, sourcev1.ArtifactInStorageCondition, meta.SucceededReason,
@@ -629,9 +627,8 @@ func (r *GitRepositoryReconciler) reconcileArtifact(ctx context.Context, sp *pat
629627
}()
630628

631629
// The artifact is up-to-date
632-
if curArtifact := obj.GetArtifact(); curArtifact != nil &&
633-
git.TransformRevision(curArtifact.Revision) == artifact.Revision &&
634-
!includes.Diff(obj.Status.IncludedArtifacts, gitArtifactRevisionEqual) &&
630+
if curArtifact := obj.GetArtifact(); curArtifact.HasRevision(artifact.Revision) &&
631+
!includes.Diff(obj.Status.IncludedArtifacts) &&
635632
!gitContentConfigChanged(obj, includes) {
636633
r.eventLogf(ctx, obj, eventv1.EventTypeTrace, sourcev1.ArtifactUpToDateReason, "artifact up-to-date with remote revision: '%s'", curArtifact.Revision)
637634
return sreconcile.ResultSuccess, nil
@@ -1018,7 +1015,7 @@ func gitContentConfigChanged(obj *sourcev1.GitRepository, includes *artifactSet)
10181015
}
10191016

10201017
// Check if the included repositories are still the same.
1021-
if git.TransformRevision(observedInclArtifact.Revision) != git.TransformRevision(currentIncl.Revision) {
1018+
if !observedInclArtifact.HasRevision(currentIncl.Revision) {
10221019
return true
10231020
}
10241021
if observedInclArtifact.Checksum != currentIncl.Checksum {
@@ -1041,10 +1038,3 @@ func gitRepositoryIncludeEqual(a, b sourcev1.GitRepositoryInclude) bool {
10411038
}
10421039
return true
10431040
}
1044-
1045-
func gitArtifactRevisionEqual(x, y *sourcev1.Artifact) bool {
1046-
if x == nil || y == nil {
1047-
return false
1048-
}
1049-
return git.TransformRevision(x.Revision) == git.TransformRevision(y.Revision)
1050-
}

controllers/gitrepository_controller_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -2215,7 +2215,7 @@ func TestGitRepositoryReconciler_fetchIncludes(t *testing.T) {
22152215
g.Expect(err != nil).To(Equal(tt.wantErr))
22162216
g.Expect(obj.GetConditions()).To(conditions.MatchConditions(tt.assertConditions))
22172217
if !tt.wantErr && gotArtifactSet != nil {
2218-
g.Expect(gotArtifactSet.Diff(tt.wantArtifactSet, gitArtifactRevisionEqual)).To(BeFalse())
2218+
g.Expect(gotArtifactSet.Diff(tt.wantArtifactSet)).To(BeFalse())
22192219
}
22202220
})
22212221
}

controllers/helmchart_controller.go

+6-5
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
"errors"
2323
"fmt"
2424
"github.com/fluxcd/pkg/git"
25+
"github.com/opencontainers/go-digest"
2526
"net/url"
2627
"os"
2728
"path/filepath"
@@ -792,7 +793,9 @@ func (r *HelmChartReconciler) buildFromTarballArtifact(ctx context.Context, obj
792793
rev = git.ExtractHashFromRevision(rev).String()
793794
}
794795
if obj.Spec.SourceRef.Kind == sourcev1.BucketKind {
795-
rev = backwardsCompatibleDigest(rev).Hex()
796+
if dig := digest.Digest(sourcev1.TransformLegacyRevision(rev)); dig.Validate() == nil {
797+
rev = dig.Hex()
798+
}
796799
}
797800
if kind := obj.Spec.SourceRef.Kind; kind == sourcev1.GitRepositoryKind || kind == sourcev1.BucketKind {
798801
// The SemVer from the metadata is at times used in e.g. the label metadata for a resource
@@ -1243,10 +1246,9 @@ func (r *HelmChartReconciler) requestsForGitRepositoryChange(o client.Object) []
12431246
return nil
12441247
}
12451248

1246-
revision := git.TransformRevision(repo.GetArtifact().Revision)
12471249
var reqs []reconcile.Request
12481250
for _, i := range list.Items {
1249-
if git.TransformRevision(i.Status.ObservedSourceArtifactRevision) != revision {
1251+
if !repo.GetArtifact().HasRevision(i.Status.ObservedSourceArtifactRevision) {
12501252
reqs = append(reqs, reconcile.Request{NamespacedName: client.ObjectKeyFromObject(&i)})
12511253
}
12521254
}
@@ -1271,10 +1273,9 @@ func (r *HelmChartReconciler) requestsForBucketChange(o client.Object) []reconci
12711273
return nil
12721274
}
12731275

1274-
revision := backwardsCompatibleDigest(bucket.GetArtifact().Revision)
12751276
var reqs []reconcile.Request
12761277
for _, i := range list.Items {
1277-
if backwardsCompatibleDigest(i.Status.ObservedSourceArtifactRevision) != revision {
1278+
if !bucket.GetArtifact().HasRevision(i.Status.ObservedSourceArtifactRevision) {
12781279
reqs = append(reqs, reconcile.Request{NamespacedName: client.ObjectKeyFromObject(&i)})
12791280
}
12801281
}

controllers/ocirepository_controller.go

+13-18
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ import (
2222
"crypto/x509"
2323
"errors"
2424
"fmt"
25-
"github.com/fluxcd/pkg/git"
2625
"io"
2726
"net/http"
2827
"os"
@@ -390,7 +389,7 @@ func (r *OCIRepositoryReconciler) reconcileSource(ctx context.Context, sp *patch
390389
return sreconcile.ResultEmpty, e
391390
}
392391

393-
// Get the upstream revision from the artifact revision
392+
// Get the upstream revision from the artifact digest
394393
revision, err := r.getRevision(url, opts.craneOpts)
395394
if err != nil {
396395
e := serror.NewGeneric(
@@ -405,7 +404,7 @@ func (r *OCIRepositoryReconciler) reconcileSource(ctx context.Context, sp *patch
405404

406405
// Mark observations about the revision on the object
407406
defer func() {
408-
if obj.GetArtifact() == nil || git.TransformRevision(obj.GetArtifact().Revision) != git.TransformRevision(revision) {
407+
if !obj.GetArtifact().HasRevision(revision) {
409408
message := fmt.Sprintf("new revision '%s' for '%s'", revision, url)
410409
if obj.GetArtifact() != nil {
411410
conditions.MarkTrue(obj, sourcev1.ArtifactOutdatedCondition, "NewRevision", message)
@@ -425,7 +424,7 @@ func (r *OCIRepositoryReconciler) reconcileSource(ctx context.Context, sp *patch
425424
if obj.Spec.Verify == nil {
426425
// Remove old observations if verification was disabled
427426
conditions.Delete(obj, sourcev1.SourceVerifiedCondition)
428-
} else if (obj.GetArtifact() == nil || git.TransformRevision(obj.GetArtifact().Revision) != git.TransformRevision(revision)) ||
427+
} else if !obj.GetArtifact().HasRevision(revision) ||
429428
conditions.GetObservedGeneration(obj, sourcev1.SourceVerifiedCondition) != obj.Generation ||
430429
conditions.IsFalse(obj, sourcev1.SourceVerifiedCondition) {
431430

@@ -458,9 +457,7 @@ func (r *OCIRepositoryReconciler) reconcileSource(ctx context.Context, sp *patch
458457

459458
// Skip pulling if the artifact revision and the source configuration has
460459
// not changed.
461-
if (obj.GetArtifact() != nil &&
462-
git.TransformRevision(obj.GetArtifact().Revision) == git.TransformRevision(revision)) &&
463-
!ociContentConfigChanged(obj) {
460+
if obj.GetArtifact().HasRevision(revision) && !ociContentConfigChanged(obj) {
464461
conditions.Delete(obj, sourcev1.FetchFailedCondition)
465462
return sreconcile.ResultSuccess, nil
466463
}
@@ -584,7 +581,8 @@ func (r *OCIRepositoryReconciler) selectLayer(obj *sourcev1.OCIRepository, image
584581
return blob, nil
585582
}
586583

587-
// getRevision fetches the upstream revision and returns the revision in the format `<tag>/<revision>`
584+
// getRevision fetches the upstream digest, returning the revision in the
585+
// format '<tag>@<digest>'.
588586
func (r *OCIRepositoryReconciler) getRevision(url string, options []crane.Option) (string, error) {
589587
ref, err := name.ParseReference(url)
590588
if err != nil {
@@ -618,14 +616,15 @@ func (r *OCIRepositoryReconciler) getRevision(url string, options []crane.Option
618616
return revision, nil
619617
}
620618

621-
// digestFromRevision extract the revision from the revision string
619+
// digestFromRevision extracts the digest from the revision string.
622620
func (r *OCIRepositoryReconciler) digestFromRevision(revision string) string {
623621
parts := strings.Split(revision, "@")
624622
return parts[len(parts)-1]
625623
}
626624

627-
// verifySignature verifies the authenticity of the given image reference url. First, it tries using a key
628-
// if a secret with a valid public key is provided. If not, it falls back to a keyless approach for verification.
625+
// verifySignature verifies the authenticity of the given image reference URL.
626+
// First, it tries to use a key if a Secret with a valid public key is provided.
627+
// If not, it falls back to a keyless approach for verification.
629628
func (r *OCIRepositoryReconciler) verifySignature(ctx context.Context, obj *sourcev1.OCIRepository, url string, opt ...remote.Option) error {
630629
ctxTimeout, cancel := context.WithTimeout(ctx, obj.Spec.Timeout.Duration)
631630
defer cancel()
@@ -953,11 +952,9 @@ func (r *OCIRepositoryReconciler) reconcileStorage(ctx context.Context, sp *patc
953952
// and the symlink in the Storage is updated to its path.
954953
func (r *OCIRepositoryReconciler) reconcileArtifact(ctx context.Context, sp *patch.SerialPatcher,
955954
obj *sourcev1.OCIRepository, metadata *sourcev1.Artifact, dir string) (sreconcile.Result, error) {
956-
revision := metadata.Revision
957-
958955
// Create artifact
959-
artifact := r.Storage.NewArtifactFor(obj.Kind, obj, revision,
960-
fmt.Sprintf("%s.tar.gz", r.digestFromRevision(revision)))
956+
artifact := r.Storage.NewArtifactFor(obj.Kind, obj, metadata.Revision,
957+
fmt.Sprintf("%s.tar.gz", r.digestFromRevision(metadata.Revision)))
961958

962959
// Set the ArtifactInStorageCondition if there's no drift.
963960
defer func() {
@@ -969,9 +966,7 @@ func (r *OCIRepositoryReconciler) reconcileArtifact(ctx context.Context, sp *pat
969966
}()
970967

971968
// The artifact is up-to-date
972-
if (obj.GetArtifact() != nil &&
973-
git.TransformRevision(obj.GetArtifact().Revision) == git.TransformRevision(revision)) &&
974-
!ociContentConfigChanged(obj) {
969+
if obj.GetArtifact().HasRevision(artifact.Revision) && !ociContentConfigChanged(obj) {
975970
r.eventLogf(ctx, obj, eventv1.EventTypeTrace, sourcev1.ArtifactUpToDateReason,
976971
"artifact up-to-date with remote revision: '%s'", artifact.Revision)
977972
return sreconcile.ResultSuccess, nil

controllers/ocirepository_controller_test.go

+3-14
Original file line numberDiff line numberDiff line change
@@ -1281,7 +1281,7 @@ func TestOCIRepository_reconcileSource_verifyOCISourceSignature(t *testing.T) {
12811281
func TestOCIRepository_reconcileSource_noop(t *testing.T) {
12821282
g := NewWithT(t)
12831283

1284-
testRevision := "6.1.5@sha256:8a0eed109e056ab1f7e70e8fb47e00cf6f560ca5cd910c83451882e07edb77fa"
1284+
testRevision := "6.1.5@sha256:8e4057c22d531d40e12b065443cb0d80394b7257c4dc557cb1fbd4dce892b86d"
12851285

12861286
tmpDir := t.TempDir()
12871287
server, err := setupRegistryServer(ctx, tmpDir, registryOptions{})
@@ -1319,18 +1319,7 @@ func TestOCIRepository_reconcileSource_noop(t *testing.T) {
13191319
name: "noop - artifact revisions match (legacy)",
13201320
beforeFunc: func(obj *sourcev1.OCIRepository) {
13211321
obj.Status.Artifact = &sourcev1.Artifact{
1322-
Revision: "6.1.5/8a0eed109e056ab1f7e70e8fb47e00cf6f560ca5cd910c83451882e07edb77fa",
1323-
}
1324-
},
1325-
afterFunc: func(g *WithT, artifact *sourcev1.Artifact) {
1326-
g.Expect(artifact.Metadata).To(BeEmpty())
1327-
},
1328-
},
1329-
{
1330-
name: "noop - artifact revisions match (legacy: digest)",
1331-
beforeFunc: func(obj *sourcev1.OCIRepository) {
1332-
obj.Status.Artifact = &sourcev1.Artifact{
1333-
Revision: "8a0eed109e056ab1f7e70e8fb47e00cf6f560ca5cd910c83451882e07edb77fa",
1322+
Revision: "6.1.5/8e4057c22d531d40e12b065443cb0d80394b7257c4dc557cb1fbd4dce892b86d",
13341323
}
13351324
},
13361325
afterFunc: func(g *WithT, artifact *sourcev1.Artifact) {
@@ -2257,7 +2246,7 @@ func pushMultiplePodinfoImages(serverURL string, versions ...string) (map[string
22572246
func setPodinfoImageAnnotations(img gcrv1.Image, tag string) gcrv1.Image {
22582247
metadata := map[string]string{
22592248
oci.SourceAnnotation: "https://github.com/stefanprodan/podinfo",
2260-
oci.RevisionAnnotation: fmt.Sprintf("%s@sha256:8a0eed109e056ab1f7e70e8fb47e00cf6f560ca5cd910c83451882e07edb77fa", tag),
2249+
oci.RevisionAnnotation: fmt.Sprintf("%s@sha1:b3b00fe35424a45d373bf4c7214178bc36fd7872", tag),
22612250
}
22622251
return mutate.Annotations(img, metadata).(gcrv1.Image)
22632252
}

0 commit comments

Comments
 (0)