Skip to content

Commit acc95d8

Browse files
committed
Add upstream source and revision to logs and events
Enrich the successful reconciliation event message with the upstream opencontainers annotations Signed-off-by: Stefan Prodan <[email protected]>
1 parent 799d7df commit acc95d8

File tree

4 files changed

+35
-16
lines changed

4 files changed

+35
-16
lines changed

api/v1beta2/ocirepository_types.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -141,9 +141,6 @@ type OCIRepositoryStatus struct {
141141
}
142142

143143
const (
144-
// OCIOperationSucceedReason signals that a Git operation (e.g. pull) succeeded.
145-
OCIOperationSucceedReason string = "OCIOperationSucceeded"
146-
147144
// OCIOperationFailedReason signals that an OCI operation (e.g. pull) failed.
148145
OCIOperationFailedReason string = "OCIOperationFailed"
149146
)

controllers/ocirepository_controller.go

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,11 @@ import (
6565
)
6666

6767
const (
68-
ClientCert = "certFile"
69-
ClientKey = "keyFile"
70-
CACert = "caFile"
68+
ClientCert = "certFile"
69+
ClientKey = "keyFile"
70+
CACert = "caFile"
71+
OCISourceKey = "org.opencontainers.image.source"
72+
OCIRevisionKey = "org.opencontainers.image.revision"
7173
)
7274

7375
// ociRepositoryReadyCondition contains the information required to summarize a
@@ -829,6 +831,20 @@ func (r *OCIRepositoryReconciler) notify(ctx context.Context,
829831

830832
message := fmt.Sprintf("stored artifact with digest '%s' from '%s'", newObj.Status.Artifact.Revision, newObj.Spec.URL)
831833

834+
// enrich message with upstream annotations if found
835+
if info := newObj.GetArtifact().Metadata; info != nil {
836+
var source, revision string
837+
if val, ok := info[OCISourceKey]; ok {
838+
source = val
839+
}
840+
if val, ok := info[OCIRevisionKey]; ok {
841+
revision = val
842+
}
843+
if source != "" && revision != "" {
844+
message = fmt.Sprintf("%s, origin source '%s', origin revision '%s'", message, source, revision)
845+
}
846+
}
847+
832848
// Notify on new artifact and failure recovery.
833849
if oldChecksum != newObj.GetArtifact().Checksum {
834850
r.AnnotatedEventf(newObj, annotations, corev1.EventTypeNormal,

controllers/ocirepository_controller_test.go

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -172,8 +172,8 @@ func TestOCIRepository_Reconcile(t *testing.T) {
172172
g.Expect(obj.Status.Artifact.Revision).To(Equal(tt.digest))
173173

174174
// Check if the metadata matches the expected annotations
175-
g.Expect(obj.Status.Artifact.Metadata["org.opencontainers.image.source"]).To(ContainSubstring("podinfo"))
176-
g.Expect(obj.Status.Artifact.Metadata["org.opencontainers.image.revision"]).To(ContainSubstring(tt.tag))
175+
g.Expect(obj.Status.Artifact.Metadata[OCISourceKey]).To(ContainSubstring("podinfo"))
176+
g.Expect(obj.Status.Artifact.Metadata[OCIRevisionKey]).To(ContainSubstring(tt.tag))
177177

178178
// Check if the artifact storage path matches the expected file path
179179
localPath := testStorage.LocalPath(*obj.Status.Artifact)
@@ -534,7 +534,6 @@ func TestOCIRepository_reconcileSource_authStrategy(t *testing.T) {
534534
}
535535
g.Expect(got).To(Equal(tt.want))
536536
g.Expect(obj.Status.Conditions).To(conditions.MatchConditions(tt.assertConditions))
537-
538537
})
539538
}
540539
}
@@ -1263,9 +1262,16 @@ func TestOCIRepositoryReconciler_notify(t *testing.T) {
12631262
resErr: nil,
12641263
newObjBeforeFunc: func(obj *sourcev1.OCIRepository) {
12651264
obj.Spec.URL = "oci://newurl.io"
1266-
obj.Status.Artifact = &sourcev1.Artifact{Revision: "xxx", Checksum: "yyy"}
1265+
obj.Status.Artifact = &sourcev1.Artifact{
1266+
Revision: "xxx",
1267+
Checksum: "yyy",
1268+
Metadata: map[string]string{
1269+
OCISourceKey: "https://github.com/stefanprodan/podinfo",
1270+
OCIRevisionKey: "6.1.8/b3b00fe35424a45d373bf4c7214178bc36fd7872",
1271+
},
1272+
}
12671273
},
1268-
wantEvent: "Normal NewArtifact stored artifact with digest 'xxx' from 'oci://newurl.io'",
1274+
wantEvent: "Normal NewArtifact stored artifact with digest 'xxx' from 'oci://newurl.io', origin source 'https://github.com/stefanprodan/podinfo', origin revision '6.1.8/b3b00fe35424a45d373bf4c7214178bc36fd7872'",
12691275
},
12701276
{
12711277
name: "recovery from failure",
@@ -1432,8 +1438,8 @@ func pushMultiplePodinfoImages(serverURL string, versions ...string) (map[string
14321438

14331439
func setPodinfoImageAnnotations(img gcrv1.Image, tag string) gcrv1.Image {
14341440
metadata := map[string]string{
1435-
"org.opencontainers.image.source": "https://github.com/stefanprodan/podinfo",
1436-
"org.opencontainers.image.revision": fmt.Sprintf("%s/SHA", tag),
1441+
OCISourceKey: "https://github.com/stefanprodan/podinfo",
1442+
OCIRevisionKey: fmt.Sprintf("%s/SHA", tag),
14371443
}
14381444
return mutate.Annotations(img, metadata).(gcrv1.Image)
14391445
}

docs/spec/v1beta2/ocirepositories.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ In the above example:
3131
by the `.spec.interval` field.
3232
- It pulls the `latest` tag of the `ghcr.io/stefanprodan/manifests/podinfo`
3333
repository, indicated by the `.spec.ref.tag` and `.spec.url` fields.
34-
- The specified tag and resolved digest are used as the Artifact
34+
- The resolved SHA256 digest is used as the Artifact
3535
revision, reported in-cluster in the `.status.artifact.revision` field.
3636
- When the current OCIRepository digest differs from the latest fetched
3737
digest, a new Artifact is archived.
@@ -49,7 +49,7 @@ You can run this example by saving the manifest into `ocirepository.yaml`.
4949

5050
```console
5151
NAME URL AGE READY STATUS
52-
podinfo oci://ghcr.io/stefanprodan/manifests/podinfo 5s True stored artifact for revision '3b6cdcc7adcc9a84d3214ee1c029543789d90b5ae69debe9efa3f66e982875de'
52+
podinfo oci://ghcr.io/stefanprodan/manifests/podinfo 5s True stored artifact with digest '3b6cdcc7adcc9a84d3214ee1c029543789d90b5ae69debe9efa3f66e982875de'
5353
```
5454

5555
3. Run `kubectl describe ocirepository podinfo` to see the [Artifact](#artifact)
@@ -63,7 +63,7 @@ You can run this example by saving the manifest into `ocirepository.yaml`.
6363
Last Update Time: 2022-06-14T11:23:36Z
6464
Path: ocirepository/default/podinfo/3b6cdcc7adcc9a84d3214ee1c029543789d90b5ae69debe9efa3f66e982875de.tar.gz
6565
Revision: 3b6cdcc7adcc9a84d3214ee1c029543789d90b5ae69debe9efa3f66e982875de
66-
URL: http://source-controller.flux-system.svc.cluster.local./ocirepository/oci/podinfo/3b6cdcc7adcc9a84d3214ee1c029543789d90b5ae69debe9efa3f66e982875de.tar.g
66+
URL: http://source-controller.flux-system.svc.cluster.local./ocirepository/oci/podinfo/3b6cdcc7adcc9a84d3214ee1c029543789d90b5ae69debe9efa3f66e982875de.tar.gz
6767
Conditions:
6868
Last Transition Time: 2022-06-14T11:23:36Z
6969
Message: stored artifact for digest '3b6cdcc7adcc9a84d3214ee1c029543789d90b5ae69debe9efa3f66e982875de'

0 commit comments

Comments
 (0)