Skip to content

Commit f00aeae

Browse files
committed
controllers: use TransformLegacyRevision helper
Signed-off-by: Hidde Beydals <[email protected]>
1 parent eaa4a4f commit f00aeae

8 files changed

+39
-83
lines changed

controllers/artifact.go

Lines changed: 2 additions & 14 deletions
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

Lines changed: 1 addition & 1 deletion
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

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

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

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

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

controllers/gitrepository_controller.go

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -516,7 +516,7 @@ func (r *GitRepositoryReconciler) reconcileSource(ctx context.Context, sp *patch
516516
}
517517

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

595595
// Mark observations about the revision on the object
596-
if curArtifact := obj.Status.Artifact; curArtifact == nil ||
597-
git.TransformRevision(curArtifact.Revision) != commit.String() {
596+
if !obj.GetArtifact().HasRevision(commit.String()) {
598597
message := fmt.Sprintf("new upstream revision '%s'", commit.String())
599598
if obj.GetArtifact() != nil {
600599
conditions.MarkTrue(obj, sourcev1.ArtifactOutdatedCondition, "NewRevision", message)
@@ -627,9 +626,8 @@ func (r *GitRepositoryReconciler) reconcileArtifact(ctx context.Context, sp *pat
627626

628627
// Set the ArtifactInStorageCondition if there's no drift.
629628
defer func() {
630-
if curArtifact := obj.GetArtifact(); curArtifact != nil &&
631-
git.TransformRevision(curArtifact.Revision) == artifact.Revision &&
632-
!includes.Diff(obj.Status.IncludedArtifacts, gitArtifactRevisionEqual) &&
629+
if curArtifact := obj.GetArtifact(); curArtifact.HasRevision(artifact.Revision) &&
630+
!includes.Diff(obj.Status.IncludedArtifacts) &&
633631
!gitContentConfigChanged(obj, includes) {
634632
conditions.Delete(obj, sourcev1.ArtifactOutdatedCondition)
635633
conditions.MarkTrue(obj, sourcev1.ArtifactInStorageCondition, meta.SucceededReason,
@@ -638,9 +636,8 @@ func (r *GitRepositoryReconciler) reconcileArtifact(ctx context.Context, sp *pat
638636
}()
639637

640638
// The artifact is up-to-date
641-
if curArtifact := obj.GetArtifact(); curArtifact != nil &&
642-
git.TransformRevision(curArtifact.Revision) == artifact.Revision &&
643-
!includes.Diff(obj.Status.IncludedArtifacts, gitArtifactRevisionEqual) &&
639+
if curArtifact := obj.GetArtifact(); curArtifact.HasRevision(artifact.Revision) &&
640+
!includes.Diff(obj.Status.IncludedArtifacts) &&
644641
!gitContentConfigChanged(obj, includes) {
645642
r.eventLogf(ctx, obj, eventv1.EventTypeTrace, sourcev1.ArtifactUpToDateReason, "artifact up-to-date with remote revision: '%s'", curArtifact.Revision)
646643
return sreconcile.ResultSuccess, nil
@@ -1027,7 +1024,7 @@ func gitContentConfigChanged(obj *sourcev1.GitRepository, includes *artifactSet)
10271024
}
10281025

10291026
// Check if the included repositories are still the same.
1030-
if git.TransformRevision(observedInclArtifact.Revision) != git.TransformRevision(currentIncl.Revision) {
1027+
if !observedInclArtifact.HasRevision(currentIncl.Revision) {
10311028
return true
10321029
}
10331030
if observedInclArtifact.Checksum != currentIncl.Checksum {
@@ -1050,10 +1047,3 @@ func gitRepositoryIncludeEqual(a, b sourcev1.GitRepositoryInclude) bool {
10501047
}
10511048
return true
10521049
}
1053-
1054-
func gitArtifactRevisionEqual(x, y *sourcev1.Artifact) bool {
1055-
if x == nil || y == nil {
1056-
return false
1057-
}
1058-
return git.TransformRevision(x.Revision) == git.TransformRevision(y.Revision)
1059-
}

controllers/gitrepository_controller_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2268,7 +2268,7 @@ func TestGitRepositoryReconciler_fetchIncludes(t *testing.T) {
22682268
g.Expect(err != nil).To(Equal(tt.wantErr))
22692269
g.Expect(obj.GetConditions()).To(conditions.MatchConditions(tt.assertConditions))
22702270
if !tt.wantErr && gotArtifactSet != nil {
2271-
g.Expect(gotArtifactSet.Diff(tt.wantArtifactSet, gitArtifactRevisionEqual)).To(BeFalse())
2271+
g.Expect(gotArtifactSet.Diff(tt.wantArtifactSet)).To(BeFalse())
22722272
}
22732273
})
22742274
}

controllers/helmchart_controller.go

Lines changed: 6 additions & 5 deletions
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"
@@ -793,7 +794,9 @@ func (r *HelmChartReconciler) buildFromTarballArtifact(ctx context.Context, obj
793794
rev = git.ExtractHashFromRevision(rev).String()
794795
}
795796
if obj.Spec.SourceRef.Kind == sourcev1.BucketKind {
796-
rev = backwardsCompatibleDigest(rev).Hex()
797+
if dig := digest.Digest(sourcev1.TransformLegacyRevision(rev)); dig.Validate() == nil {
798+
rev = dig.Hex()
799+
}
797800
}
798801
if kind := obj.Spec.SourceRef.Kind; kind == sourcev1.GitRepositoryKind || kind == sourcev1.BucketKind {
799802
// The SemVer from the metadata is at times used in e.g. the label metadata for a resource
@@ -1244,10 +1247,9 @@ func (r *HelmChartReconciler) requestsForGitRepositoryChange(o client.Object) []
12441247
return nil
12451248
}
12461249

1247-
revision := git.TransformRevision(repo.GetArtifact().Revision)
12481250
var reqs []reconcile.Request
12491251
for _, i := range list.Items {
1250-
if git.TransformRevision(i.Status.ObservedSourceArtifactRevision) != revision {
1252+
if !repo.GetArtifact().HasRevision(i.Status.ObservedSourceArtifactRevision) {
12511253
reqs = append(reqs, reconcile.Request{NamespacedName: client.ObjectKeyFromObject(&i)})
12521254
}
12531255
}
@@ -1272,10 +1274,9 @@ func (r *HelmChartReconciler) requestsForBucketChange(o client.Object) []reconci
12721274
return nil
12731275
}
12741276

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

controllers/ocirepository_controller.go

Lines changed: 13 additions & 18 deletions
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"
@@ -391,7 +390,7 @@ func (r *OCIRepositoryReconciler) reconcileSource(ctx context.Context, sp *patch
391390
return sreconcile.ResultEmpty, e
392391
}
393392

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

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

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

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

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

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

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

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

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

controllers/ocirepository_controller_test.go

Lines changed: 3 additions & 14 deletions
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)