Skip to content

Commit fe8bc43

Browse files
committed
controllers: use RFC-0005 format for Git revision
Signed-off-by: Hidde Beydals <[email protected]>
1 parent 83b6fdc commit fe8bc43

File tree

6 files changed

+180
-113
lines changed

6 files changed

+180
-113
lines changed

controllers/artifact.go

Lines changed: 11 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +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) bool {
24+
func (s artifactSet) Diff(set artifactSet, comp func(x, y *sourcev1.Artifact) bool) bool {
2525
if len(s) != len(set) {
2626
return true
2727
}
2828

29+
if comp == nil {
30+
comp = defaultCompare
31+
}
32+
2933
outer:
3034
for _, j := range s {
3135
for _, k := range set {
32-
if k.HasRevision(j.Revision) {
36+
if comp(j, k) {
3337
continue outer
3438
}
3539
}
@@ -38,24 +42,10 @@ outer:
3842
return false
3943
}
4044

41-
// hasArtifactUpdated returns true if any of the revisions in the current artifacts
42-
// does not match any of the artifacts in the updated artifacts
43-
// NOTE: artifactSet is a replacement for this. Remove this once it's not used
44-
// anywhere.
45-
func hasArtifactUpdated(current []*sourcev1.Artifact, updated []*sourcev1.Artifact) bool {
46-
if len(current) != len(updated) {
47-
return true
45+
func defaultCompare(x, y *sourcev1.Artifact) bool {
46+
if y == nil {
47+
return false
4848
}
49-
50-
OUTER:
51-
for _, c := range current {
52-
for _, u := range updated {
53-
if u.HasRevision(c.Revision) {
54-
continue OUTER
55-
}
56-
}
57-
return true
58-
}
59-
60-
return false
49+
return x.HasRevision(y.Revision)
6150
}
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)
118+
result := tt.current.Diff(tt.updated, nil)
119119
if result != tt.expected {
120120
t.Errorf("Archive() result = %v, wantResult %v", result, tt.expected)
121121
}

controllers/gitrepository_controller.go

Lines changed: 19 additions & 9 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) {
519+
if artifacts.Diff(obj.Status.IncludedArtifacts, gitArtifactRevisionEqual) {
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,7 +593,8 @@ func (r *GitRepositoryReconciler) reconcileSource(ctx context.Context, sp *patch
593593
}
594594

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

627628
// Set the ArtifactInStorageCondition if there's no drift.
628629
defer func() {
629-
if obj.GetArtifact().HasRevision(artifact.Revision) &&
630-
!includes.Diff(obj.Status.IncludedArtifacts) &&
630+
if curArtifact := obj.GetArtifact(); curArtifact != nil &&
631+
git.TransformRevision(curArtifact.Revision) == artifact.Revision &&
632+
!includes.Diff(obj.Status.IncludedArtifacts, gitArtifactRevisionEqual) &&
631633
!gitContentConfigChanged(obj, includes) {
632634
conditions.Delete(obj, sourcev1.ArtifactOutdatedCondition)
633635
conditions.MarkTrue(obj, sourcev1.ArtifactInStorageCondition, meta.SucceededReason,
634-
"stored artifact for revision '%s'", artifact.Revision)
636+
"stored artifact for revision '%s'", curArtifact.Revision)
635637
}
636638
}()
637639

638640
// The artifact is up-to-date
639-
if obj.GetArtifact().HasRevision(artifact.Revision) &&
640-
!includes.Diff(obj.Status.IncludedArtifacts) &&
641+
if curArtifact := obj.GetArtifact(); curArtifact != nil &&
642+
git.TransformRevision(curArtifact.Revision) == artifact.Revision &&
643+
!includes.Diff(obj.Status.IncludedArtifacts, gitArtifactRevisionEqual) &&
641644
!gitContentConfigChanged(obj, includes) {
642-
r.eventLogf(ctx, obj, eventv1.EventTypeTrace, sourcev1.ArtifactUpToDateReason, "artifact up-to-date with remote revision: '%s'", artifact.Revision)
645+
r.eventLogf(ctx, obj, eventv1.EventTypeTrace, sourcev1.ArtifactUpToDateReason, "artifact up-to-date with remote revision: '%s'", curArtifact.Revision)
643646
return sreconcile.ResultSuccess, nil
644647
}
645648

@@ -1024,7 +1027,7 @@ func gitContentConfigChanged(obj *sourcev1.GitRepository, includes *artifactSet)
10241027
}
10251028

10261029
// Check if the included repositories are still the same.
1027-
if observedInclArtifact.Revision != currentIncl.Revision {
1030+
if git.TransformRevision(observedInclArtifact.Revision) != git.TransformRevision(currentIncl.Revision) {
10281031
return true
10291032
}
10301033
if observedInclArtifact.Checksum != currentIncl.Checksum {
@@ -1047,3 +1050,10 @@ func gitRepositoryIncludeEqual(a, b sourcev1.GitRepositoryInclude) bool {
10471050
}
10481051
return true
10491052
}
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+
}

0 commit comments

Comments
 (0)