Skip to content

Commit

Permalink
controllers: make OCIRepository compat with RFC-0005
Browse files Browse the repository at this point in the history
Signed-off-by: Hidde Beydals <hello@hidde.co>
  • Loading branch information
hiddeco committed Jan 18, 2023
1 parent acce861 commit 85c5fc8
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 64 deletions.
29 changes: 17 additions & 12 deletions controllers/ocirepository_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"crypto/x509"
"errors"
"fmt"
"github.com/fluxcd/pkg/git"
"io"
"net/http"
"os"
Expand Down Expand Up @@ -389,7 +390,7 @@ func (r *OCIRepositoryReconciler) reconcileSource(ctx context.Context, sp *patch
return sreconcile.ResultEmpty, e
}

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

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

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

// Skip pulling if the artifact revision and the source configuration has
// not changed.
if obj.GetArtifact().HasRevision(revision) && !ociContentConfigChanged(obj) {
if (obj.GetArtifact() != nil &&
git.TransformRevision(obj.GetArtifact().Revision) == git.TransformRevision(revision)) &&
!ociContentConfigChanged(obj) {
conditions.Delete(obj, sourcev1.FetchFailedCondition)
return sreconcile.ResultSuccess, nil
}
Expand Down Expand Up @@ -581,7 +584,7 @@ func (r *OCIRepositoryReconciler) selectLayer(obj *sourcev1.OCIRepository, image
return blob, nil
}

// getRevision fetches the upstream digest and returns the revision in the format `<tag>/<digest>`
// getRevision fetches the upstream revision and returns the revision in the format `<tag>/<revision>`
func (r *OCIRepositoryReconciler) getRevision(url string, options []crane.Option) (string, error) {
ref, err := name.ParseReference(url)
if err != nil {
Expand All @@ -608,16 +611,16 @@ func (r *OCIRepositoryReconciler) getRevision(url string, options []crane.Option
return "", err
}

revision := digestHash.Hex
revision := digestHash.String()
if repoTag != "" {
revision = fmt.Sprintf("%s/%s", repoTag, digestHash.Hex)
revision = fmt.Sprintf("%s@%s", repoTag, revision)
}
return revision, nil
}

// digestFromRevision extract the digest from the revision string
// digestFromRevision extract the revision from the revision string
func (r *OCIRepositoryReconciler) digestFromRevision(revision string) string {
parts := strings.Split(revision, "/")
parts := strings.Split(revision, "@")
return parts[len(parts)-1]
}

Expand Down Expand Up @@ -721,7 +724,7 @@ func (r *OCIRepositoryReconciler) parseRepositoryURL(obj *sourcev1.OCIRepository
return ref.Context().Name(), nil
}

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

// The artifact is up-to-date
if obj.GetArtifact().HasRevision(artifact.Revision) && !ociContentConfigChanged(obj) {
if (obj.GetArtifact() != nil &&
git.TransformRevision(obj.GetArtifact().Revision) == git.TransformRevision(revision)) &&
!ociContentConfigChanged(obj) {
r.eventLogf(ctx, obj, eventv1.EventTypeTrace, sourcev1.ArtifactUpToDateReason,
"artifact up-to-date with remote revision: '%s'", artifact.Revision)
return sreconcile.ResultSuccess, nil
Expand Down Expand Up @@ -1140,7 +1145,7 @@ func (r *OCIRepositoryReconciler) notify(ctx context.Context, oldObj, newObj *so
fmt.Sprintf("%s/%s", sourcev1.GroupVersion.Group, eventv1.MetaChecksumKey): newObj.Status.Artifact.Checksum,
}
if newObj.Status.Artifact.Digest != "" {
annotations[sourcev1.GroupVersion.Group+"/digest"] = newObj.Status.Artifact.Digest
annotations[sourcev1.GroupVersion.Group+"/revision"] = newObj.Status.Artifact.Digest
}

var oldChecksum string
Expand Down
Loading

0 comments on commit 85c5fc8

Please sign in to comment.