Skip to content

Commit 469c938

Browse files
committed
controllers: make OCIRepository compat with RFC-0005
Signed-off-by: Hidde Beydals <[email protected]>
1 parent 909ece4 commit 469c938

File tree

2 files changed

+90
-64
lines changed

2 files changed

+90
-64
lines changed

controllers/ocirepository_controller.go

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
"crypto/x509"
2323
"errors"
2424
"fmt"
25+
"github.com/fluxcd/pkg/git"
2526
"io"
2627
"net/http"
2728
"os"
@@ -390,7 +391,7 @@ func (r *OCIRepositoryReconciler) reconcileSource(ctx context.Context, sp *patch
390391
return sreconcile.ResultEmpty, e
391392
}
392393

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

406407
// Mark observations about the revision on the object
407408
defer func() {
408-
if !obj.GetArtifact().HasRevision(revision) {
409+
if obj.GetArtifact() == nil || git.TransformRevision(obj.GetArtifact().Revision) != git.TransformRevision(revision) {
409410
message := fmt.Sprintf("new revision '%s' for '%s'", revision, url)
410411
if obj.GetArtifact() != nil {
411412
conditions.MarkTrue(obj, sourcev1.ArtifactOutdatedCondition, "NewRevision", message)
@@ -425,7 +426,7 @@ func (r *OCIRepositoryReconciler) reconcileSource(ctx context.Context, sp *patch
425426
if obj.Spec.Verify == nil {
426427
// Remove old observations if verification was disabled
427428
conditions.Delete(obj, sourcev1.SourceVerifiedCondition)
428-
} else if !obj.GetArtifact().HasRevision(revision) ||
429+
} else if (obj.GetArtifact() == nil || git.TransformRevision(obj.GetArtifact().Revision) != git.TransformRevision(revision)) ||
429430
conditions.GetObservedGeneration(obj, sourcev1.SourceVerifiedCondition) != obj.Generation ||
430431
conditions.IsFalse(obj, sourcev1.SourceVerifiedCondition) {
431432

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

459460
// Skip pulling if the artifact revision and the source configuration has
460461
// not changed.
461-
if obj.GetArtifact().HasRevision(revision) && !ociContentConfigChanged(obj) {
462+
if (obj.GetArtifact() != nil &&
463+
git.TransformRevision(obj.GetArtifact().Revision) == git.TransformRevision(revision)) &&
464+
!ociContentConfigChanged(obj) {
462465
conditions.Delete(obj, sourcev1.FetchFailedCondition)
463466
return sreconcile.ResultSuccess, nil
464467
}
@@ -582,7 +585,7 @@ func (r *OCIRepositoryReconciler) selectLayer(obj *sourcev1.OCIRepository, image
582585
return blob, nil
583586
}
584587

585-
// getRevision fetches the upstream digest and returns the revision in the format `<tag>/<digest>`
588+
// getRevision fetches the upstream revision and returns the revision in the format `<tag>/<revision>`
586589
func (r *OCIRepositoryReconciler) getRevision(url string, options []crane.Option) (string, error) {
587590
ref, err := name.ParseReference(url)
588591
if err != nil {
@@ -609,16 +612,16 @@ func (r *OCIRepositoryReconciler) getRevision(url string, options []crane.Option
609612
return "", err
610613
}
611614

612-
revision := digestHash.Hex
615+
revision := digestHash.String()
613616
if repoTag != "" {
614-
revision = fmt.Sprintf("%s/%s", repoTag, digestHash.Hex)
617+
revision = fmt.Sprintf("%s@%s", repoTag, revision)
615618
}
616619
return revision, nil
617620
}
618621

619-
// digestFromRevision extract the digest from the revision string
622+
// digestFromRevision extract the revision from the revision string
620623
func (r *OCIRepositoryReconciler) digestFromRevision(revision string) string {
621-
parts := strings.Split(revision, "/")
624+
parts := strings.Split(revision, "@")
622625
return parts[len(parts)-1]
623626
}
624627

@@ -722,7 +725,7 @@ func (r *OCIRepositoryReconciler) parseRepositoryURL(obj *sourcev1.OCIRepository
722725
return ref.Context().Name(), nil
723726
}
724727

725-
// getArtifactURL determines which tag or digest should be used and returns the OCI artifact FQN.
728+
// getArtifactURL determines which tag or revision should be used and returns the OCI artifact FQN.
726729
func (r *OCIRepositoryReconciler) getArtifactURL(obj *sourcev1.OCIRepository, options []crane.Option) (string, error) {
727730
url, err := r.parseRepositoryURL(obj)
728731
if err != nil {
@@ -967,7 +970,9 @@ func (r *OCIRepositoryReconciler) reconcileArtifact(ctx context.Context, sp *pat
967970
}()
968971

969972
// The artifact is up-to-date
970-
if obj.GetArtifact().HasRevision(artifact.Revision) && !ociContentConfigChanged(obj) {
973+
if (obj.GetArtifact() != nil &&
974+
git.TransformRevision(obj.GetArtifact().Revision) == git.TransformRevision(revision)) &&
975+
!ociContentConfigChanged(obj) {
971976
r.eventLogf(ctx, obj, eventv1.EventTypeTrace, sourcev1.ArtifactUpToDateReason,
972977
"artifact up-to-date with remote revision: '%s'", artifact.Revision)
973978
return sreconcile.ResultSuccess, nil
@@ -1141,7 +1146,7 @@ func (r *OCIRepositoryReconciler) notify(ctx context.Context, oldObj, newObj *so
11411146
fmt.Sprintf("%s/%s", sourcev1.GroupVersion.Group, eventv1.MetaChecksumKey): newObj.Status.Artifact.Checksum,
11421147
}
11431148
if newObj.Status.Artifact.Digest != "" {
1144-
annotations[sourcev1.GroupVersion.Group+"/digest"] = newObj.Status.Artifact.Digest
1149+
annotations[sourcev1.GroupVersion.Group+"/revision"] = newObj.Status.Artifact.Digest
11451150
}
11461151

11471152
var oldChecksum string

0 commit comments

Comments
 (0)