Skip to content

Commit d540df7

Browse files
Expose OCI origin revision in events
OCI artifacts can record their source revision in the org.opencontainers.image.revision annotation. Include that value in successful events so notification consumers can correlate an artifact with its source commit. Keep the existing artifact revision unchanged and omit the extra event metadata when the annotation is empty. Signed-off-by: Ruslan Shaydullin <shaydullin.r.d@outlook.com>
1 parent 55ef6eb commit d540df7

2 files changed

Lines changed: 29 additions & 10 deletions

File tree

internal/controller/ocirepository_controller.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1336,6 +1336,9 @@ func (r *OCIRepositoryReconciler) notify(ctx context.Context, oldObj, newObj *so
13361336
if val, ok := info[oci.RevisionAnnotation]; ok {
13371337
revision = val
13381338
}
1339+
if revision != "" {
1340+
annotations[fmt.Sprintf("%s/%s", sourcev1.GroupVersion.Group, eventv1.MetaOriginRevisionKey)] = revision
1341+
}
13391342
if source != "" && revision != "" {
13401343
message = fmt.Sprintf("%s, origin source '%s', origin revision '%s'", message, source, revision)
13411344
}

internal/controller/ocirepository_controller_test.go

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ import (
6060
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
6161

6262
kstatus "github.com/fluxcd/cli-utils/pkg/kstatus/status"
63+
eventv1 "github.com/fluxcd/pkg/apis/event/v1beta1"
6364
"github.com/fluxcd/pkg/apis/meta"
6465
intdigest "github.com/fluxcd/pkg/artifact/digest"
6566
"github.com/fluxcd/pkg/artifact/storage"
@@ -3413,13 +3414,14 @@ func TestOCIRepositoryReconciler_notify(t *testing.T) {
34133414
noopErr.Ignore = true
34143415

34153416
tests := []struct {
3416-
name string
3417-
res sreconcile.Result
3418-
resErr error
3419-
oldObjBeforeFunc func(obj *sourcev1.OCIRepository)
3420-
newObjBeforeFunc func(obj *sourcev1.OCIRepository)
3421-
commit git.Commit
3422-
wantEvent string
3417+
name string
3418+
res sreconcile.Result
3419+
resErr error
3420+
oldObjBeforeFunc func(obj *sourcev1.OCIRepository)
3421+
newObjBeforeFunc func(obj *sourcev1.OCIRepository)
3422+
commit git.Commit
3423+
wantEvent string
3424+
wantOriginRevision string
34233425
}{
34243426
{
34253427
name: "error - no event",
@@ -3441,7 +3443,8 @@ func TestOCIRepositoryReconciler_notify(t *testing.T) {
34413443
},
34423444
}
34433445
},
3444-
wantEvent: "Normal NewArtifact stored artifact with revision 'xxx' from 'oci://newurl.io', origin source 'https://github.com/stefanprodan/podinfo', origin revision '6.1.8/b3b00fe35424a45d373bf4c7214178bc36fd7872'",
3446+
wantEvent: "Normal NewArtifact stored artifact with revision 'xxx' from 'oci://newurl.io', origin source 'https://github.com/stefanprodan/podinfo', origin revision '6.1.8/b3b00fe35424a45d373bf4c7214178bc36fd7872'",
3447+
wantOriginRevision: "6.1.8/b3b00fe35424a45d373bf4c7214178bc36fd7872",
34453448
},
34463449
{
34473450
name: "recovery from failure",
@@ -3454,10 +3457,17 @@ func TestOCIRepositoryReconciler_notify(t *testing.T) {
34543457
},
34553458
newObjBeforeFunc: func(obj *sourcev1.OCIRepository) {
34563459
obj.Spec.URL = "oci://newurl.io"
3457-
obj.Status.Artifact = &meta.Artifact{Revision: "xxx", Digest: "yyy"}
3460+
obj.Status.Artifact = &meta.Artifact{
3461+
Revision: "xxx",
3462+
Digest: "yyy",
3463+
Metadata: map[string]string{
3464+
oci.RevisionAnnotation: "6.1.8/b3b00fe35424a45d373bf4c7214178bc36fd7872",
3465+
},
3466+
}
34583467
conditions.MarkTrue(obj, meta.ReadyCondition, meta.SucceededReason, "ready")
34593468
},
3460-
wantEvent: "Normal Succeeded stored artifact with revision 'xxx' from 'oci://newurl.io'",
3469+
wantEvent: "Normal Succeeded stored artifact with revision 'xxx' from 'oci://newurl.io'",
3470+
wantOriginRevision: "6.1.8/b3b00fe35424a45d373bf4c7214178bc36fd7872",
34613471
},
34623472
{
34633473
name: "recovery and new artifact",
@@ -3525,6 +3535,12 @@ func TestOCIRepositoryReconciler_notify(t *testing.T) {
35253535
g.Expect(ok).To(Equal(tt.wantEvent != ""), "unexpected event received")
35263536
if tt.wantEvent != "" {
35273537
g.Expect(x).To(ContainSubstring(tt.wantEvent))
3538+
originRevisionKey := fmt.Sprintf("%s/%s", sourcev1.GroupVersion.Group, eventv1.MetaOriginRevisionKey)
3539+
if tt.wantOriginRevision != "" {
3540+
g.Expect(x).To(ContainSubstring(fmt.Sprintf("%s:%s", originRevisionKey, tt.wantOriginRevision)))
3541+
} else {
3542+
g.Expect(x).NotTo(ContainSubstring(originRevisionKey))
3543+
}
35283544
}
35293545
default:
35303546
if tt.wantEvent != "" {

0 commit comments

Comments
 (0)