Skip to content

Commit c8d813f

Browse files
committed
Add OCI revision to events
Signed-off-by: Matheus Pimenta <[email protected]>
1 parent 550576e commit c8d813f

File tree

3 files changed

+53
-18
lines changed

3 files changed

+53
-18
lines changed

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ require (
1919
github.com/fluxcd/cli-utils v0.36.0-flux.11
2020
github.com/fluxcd/kustomize-controller/api v1.4.0
2121
github.com/fluxcd/pkg/apis/acl v0.5.0
22-
github.com/fluxcd/pkg/apis/event v0.13.0
22+
github.com/fluxcd/pkg/apis/event v0.15.0
2323
github.com/fluxcd/pkg/apis/kustomize v1.8.0
2424
github.com/fluxcd/pkg/apis/meta v1.9.0
2525
github.com/fluxcd/pkg/http/fetch v0.14.0

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -181,8 +181,8 @@ github.com/fluxcd/cli-utils v0.36.0-flux.11 h1:W0y2uvCVkcE8bgV9jgoGSjzWbLFiNq1Aj
181181
github.com/fluxcd/cli-utils v0.36.0-flux.11/go.mod h1:WZ7xUpZbK+O6HBxA5UWqzWTLSSltdmj4wS1LstS5Dqs=
182182
github.com/fluxcd/pkg/apis/acl v0.5.0 h1:+ykKezgerKUlZwSYFUy03lPMOIAyWlqvMNNLIWWqOhk=
183183
github.com/fluxcd/pkg/apis/acl v0.5.0/go.mod h1:IVDZx3MAoDWjlLrJHMF9Z27huFuXAEQlnbWw0M6EcTs=
184-
github.com/fluxcd/pkg/apis/event v0.13.0 h1:m5qHAhYIC0+mRFy5OC8FZxBVBGJM3qxJ/sEg2Vgx4T8=
185-
github.com/fluxcd/pkg/apis/event v0.13.0/go.mod h1:aRK2AONnjjSNW61B6Iy3SW4YHozACntnJeGm3fFqDqA=
184+
github.com/fluxcd/pkg/apis/event v0.15.0 h1:k1suqIfVxnhEeKlGkvlHAbOYXjY8wRixT/OZcIuakqA=
185+
github.com/fluxcd/pkg/apis/event v0.15.0/go.mod h1:aRK2AONnjjSNW61B6Iy3SW4YHozACntnJeGm3fFqDqA=
186186
github.com/fluxcd/pkg/apis/kustomize v1.8.0 h1:HH6YRa3SMS72KK4cUyb9m5sK/dZH+Eti1qhjWDCgwKg=
187187
github.com/fluxcd/pkg/apis/kustomize v1.8.0/go.mod h1:QCKIFj1ocdndaWSkrLs5JKvdGNYyTzQX1ZB3lYTwma0=
188188
github.com/fluxcd/pkg/apis/meta v1.9.0 h1:wPgm7bWNJZ/ImS5GqikOxt362IgLPFBG73dZ27uWRiQ=

internal/controller/kustomization_controller.go

Lines changed: 50 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ func (r *KustomizationReconciler) Reconcile(ctx context.Context, req ctrl.Reques
193193
time.Since(reconcileStart).String(),
194194
obj.Spec.Interval.Duration.String())
195195
log.Info(msg, "revision", obj.Status.LastAttemptedRevision)
196-
r.event(obj, obj.Status.LastAppliedRevision, eventv1.EventSeverityInfo, msg,
196+
r.event(obj, obj.Status.LastAppliedRevision, "not-sure-what-to-put-here", eventv1.EventSeverityInfo, msg,
197197
map[string]string{
198198
kustomizev1.GroupVersion.Group + "/" + eventv1.MetaCommitStatusKey: eventv1.MetaCommitStatusUpdateValue,
199199
})
@@ -234,7 +234,7 @@ func (r *KustomizationReconciler) Reconcile(ctx context.Context, req ctrl.Reques
234234
if acl.IsAccessDenied(err) {
235235
conditions.MarkFalse(obj, meta.ReadyCondition, apiacl.AccessDeniedReason, "%s", err)
236236
log.Error(err, "Access denied to cross-namespace source")
237-
r.event(obj, "unknown", eventv1.EventSeverityError, err.Error(), nil)
237+
r.event(obj, "unknown", "unknown", eventv1.EventSeverityError, err.Error(), nil)
238238
return ctrl.Result{RequeueAfter: obj.GetRetryInterval()}, nil
239239
}
240240

@@ -249,14 +249,16 @@ func (r *KustomizationReconciler) Reconcile(ctx context.Context, req ctrl.Reques
249249
log.Info(msg)
250250
return ctrl.Result{RequeueAfter: r.requeueDependency}, nil
251251
}
252+
revision := artifactSource.GetArtifact().Revision
253+
originRevision := getOriginRevision(artifactSource)
252254

253255
// Check dependencies and requeue the reconciliation if the check fails.
254256
if len(obj.Spec.DependsOn) > 0 {
255257
if err := r.checkDependencies(ctx, obj, artifactSource); err != nil {
256258
conditions.MarkFalse(obj, meta.ReadyCondition, meta.DependencyNotReadyReason, "%s", err)
257259
msg := fmt.Sprintf("Dependencies do not meet ready condition, retrying in %s", r.requeueDependency.String())
258260
log.Info(msg)
259-
r.event(obj, artifactSource.GetArtifact().Revision, eventv1.EventSeverityInfo, msg, nil)
261+
r.event(obj, revision, originRevision, eventv1.EventSeverityInfo, msg, nil)
260262
return ctrl.Result{RequeueAfter: r.requeueDependency}, nil
261263
}
262264
log.Info("All dependencies are ready, proceeding with reconciliation")
@@ -279,8 +281,8 @@ func (r *KustomizationReconciler) Reconcile(ctx context.Context, req ctrl.Reques
279281
time.Since(reconcileStart).String(),
280282
obj.GetRetryInterval().String()),
281283
"revision",
282-
artifactSource.GetArtifact().Revision)
283-
r.event(obj, artifactSource.GetArtifact().Revision, eventv1.EventSeverityError,
284+
revision)
285+
r.event(obj, revision, originRevision, eventv1.EventSeverityError,
284286
reconcileErr.Error(), nil)
285287
return ctrl.Result{RequeueAfter: obj.GetRetryInterval()}, nil
286288
}
@@ -298,6 +300,7 @@ func (r *KustomizationReconciler) reconcile(
298300

299301
// Update status with the reconciliation progress.
300302
revision := src.GetArtifact().Revision
303+
originRevision := getOriginRevision(src)
301304
progressingMsg := fmt.Sprintf("Fetching manifests for revision %s with a timeout of %s", revision, obj.GetTimeout().String())
302305
conditions.MarkUnknown(obj, meta.ReadyCondition, meta.ProgressingReason, "%s", "Reconciliation in progress")
303306
conditions.MarkReconciling(obj, meta.ProgressingReason, "%s", progressingMsg)
@@ -419,7 +422,7 @@ func (r *KustomizationReconciler) reconcile(
419422
}
420423

421424
// Validate and apply resources in stages.
422-
drifted, changeSet, err := r.apply(ctx, resourceManager, obj, revision, objects)
425+
drifted, changeSet, err := r.apply(ctx, resourceManager, obj, revision, originRevision, objects)
423426
if err != nil {
424427
conditions.MarkFalse(obj, meta.ReadyCondition, meta.ReconciliationFailedReason, "%s", err)
425428
return err
@@ -444,7 +447,7 @@ func (r *KustomizationReconciler) reconcile(
444447
}
445448

446449
// Run garbage collection for stale resources that do not have pruning disabled.
447-
if _, err := r.prune(ctx, resourceManager, obj, revision, staleObjects); err != nil {
450+
if _, err := r.prune(ctx, resourceManager, obj, revision, originRevision, staleObjects); err != nil {
448451
conditions.MarkFalse(obj, meta.ReadyCondition, meta.PruneFailedReason, "%s", err)
449452
return err
450453
}
@@ -456,6 +459,7 @@ func (r *KustomizationReconciler) reconcile(
456459
patcher,
457460
obj,
458461
revision,
462+
originRevision,
459463
isNewRevision,
460464
drifted,
461465
changeSet.ToObjMetadataSet()); err != nil {
@@ -656,6 +660,7 @@ func (r *KustomizationReconciler) apply(ctx context.Context,
656660
manager *ssa.ResourceManager,
657661
obj *kustomizev1.Kustomization,
658662
revision string,
663+
originRevision string,
659664
objects []*unstructured.Unstructured) (bool, *ssa.ChangeSet, error) {
660665
log := ctrl.LoggerFrom(ctx)
661666

@@ -841,7 +846,7 @@ func (r *KustomizationReconciler) apply(ctx context.Context,
841846
// emit event only if the server-side apply resulted in changes
842847
applyLog := strings.TrimSuffix(changeSetLog.String(), "\n")
843848
if applyLog != "" {
844-
r.event(obj, revision, eventv1.EventSeverityInfo, applyLog, nil)
849+
r.event(obj, revision, originRevision, eventv1.EventSeverityInfo, applyLog, nil)
845850
}
846851

847852
return applyLog != "", resultSet, nil
@@ -852,6 +857,7 @@ func (r *KustomizationReconciler) checkHealth(ctx context.Context,
852857
patcher *patch.SerialPatcher,
853858
obj *kustomizev1.Kustomization,
854859
revision string,
860+
originRevision string,
855861
isNewRevision bool,
856862
drifted bool,
857863
objects object.ObjMetadataSet) error {
@@ -910,7 +916,7 @@ func (r *KustomizationReconciler) checkHealth(ctx context.Context,
910916
// Emit recovery event if the previous health check failed.
911917
msg := fmt.Sprintf("Health check passed in %s", time.Since(checkStart).String())
912918
if !wasHealthy || (isNewRevision && drifted) {
913-
r.event(obj, revision, eventv1.EventSeverityInfo, msg, nil)
919+
r.event(obj, revision, originRevision, eventv1.EventSeverityInfo, msg, nil)
914920
}
915921

916922
conditions.MarkTrue(obj, meta.HealthyCondition, meta.SucceededReason, "%s", msg)
@@ -925,6 +931,7 @@ func (r *KustomizationReconciler) prune(ctx context.Context,
925931
manager *ssa.ResourceManager,
926932
obj *kustomizev1.Kustomization,
927933
revision string,
934+
originRevision string,
928935
objects []*unstructured.Unstructured) (bool, error) {
929936
if !obj.Spec.Prune {
930937
return false, nil
@@ -949,7 +956,7 @@ func (r *KustomizationReconciler) prune(ctx context.Context,
949956
// emit event only if the prune operation resulted in changes
950957
if changeSet != nil && len(changeSet.Entries) > 0 {
951958
log.Info(fmt.Sprintf("garbage collection completed: %s", changeSet.String()))
952-
r.event(obj, revision, eventv1.EventSeverityInfo, changeSet.String(), nil)
959+
r.event(obj, revision, originRevision, eventv1.EventSeverityInfo, changeSet.String(), nil)
953960
return true, nil
954961
}
955962

@@ -965,6 +972,11 @@ func finalizerShouldDeleteResources(obj *kustomizev1.Kustomization) bool {
965972

966973
func (r *KustomizationReconciler) finalize(ctx context.Context,
967974
obj *kustomizev1.Kustomization) (ctrl.Result, error) {
975+
976+
// Here the origin revision is not known because the artifact was not fetched,
977+
// it's too soon in the Reconcile() method.
978+
const originRevision = ""
979+
968980
log := ctrl.LoggerFrom(ctx)
969981
if finalizerShouldDeleteResources(obj) &&
970982
!obj.Spec.Suspend &&
@@ -1004,19 +1016,19 @@ func (r *KustomizationReconciler) finalize(ctx context.Context,
10041016

10051017
changeSet, err := resourceManager.DeleteAll(ctx, objects, opts)
10061018
if err != nil {
1007-
r.event(obj, obj.Status.LastAppliedRevision, eventv1.EventSeverityError, "pruning for deleted resource failed", nil)
1019+
r.event(obj, obj.Status.LastAppliedRevision, originRevision, eventv1.EventSeverityError, "pruning for deleted resource failed", nil)
10081020
// Return the error so we retry the failed garbage collection
10091021
return ctrl.Result{}, err
10101022
}
10111023

10121024
if changeSet != nil && len(changeSet.Entries) > 0 {
1013-
r.event(obj, obj.Status.LastAppliedRevision, eventv1.EventSeverityInfo, changeSet.String(), nil)
1025+
r.event(obj, obj.Status.LastAppliedRevision, originRevision, eventv1.EventSeverityInfo, changeSet.String(), nil)
10141026
}
10151027
} else {
10161028
// when the account to impersonate is gone, log the stale objects and continue with the finalization
10171029
msg := fmt.Sprintf("unable to prune objects: \n%s", ssautil.FmtUnstructuredList(objects))
10181030
log.Error(fmt.Errorf("skiping pruning, failed to find account to impersonate"), msg)
1019-
r.event(obj, obj.Status.LastAppliedRevision, eventv1.EventSeverityError, msg, nil)
1031+
r.event(obj, obj.Status.LastAppliedRevision, originRevision, eventv1.EventSeverityError, msg, nil)
10201032
}
10211033
}
10221034

@@ -1027,13 +1039,16 @@ func (r *KustomizationReconciler) finalize(ctx context.Context,
10271039
}
10281040

10291041
func (r *KustomizationReconciler) event(obj *kustomizev1.Kustomization,
1030-
revision, severity, msg string,
1042+
revision, originRevision, severity, msg string,
10311043
metadata map[string]string) {
10321044
if metadata == nil {
10331045
metadata = map[string]string{}
10341046
}
10351047
if revision != "" {
1036-
metadata[kustomizev1.GroupVersion.Group+"/revision"] = revision
1048+
metadata[kustomizev1.GroupVersion.Group+"/"+eventv1.MetaRevisionKey] = revision
1049+
}
1050+
if originRevision != "" {
1051+
metadata[kustomizev1.GroupVersion.Group+"/"+eventv1.MetaOriginRevisionKey] = originRevision
10371052
}
10381053

10391054
reason := severity
@@ -1108,3 +1123,23 @@ func (r *KustomizationReconciler) patch(ctx context.Context,
11081123

11091124
return nil
11101125
}
1126+
1127+
// getOriginRevision returns the origin revision of the source artifact,
1128+
// or the empty string if it's not present, or if the artifact itself
1129+
// is not present.
1130+
func getOriginRevision(src sourcev1.Source) string {
1131+
a := src.GetArtifact()
1132+
if a == nil {
1133+
return ""
1134+
}
1135+
1136+
for _, key := range []string{
1137+
"org.opencontainers.image.revision",
1138+
} {
1139+
if r, ok := a.Metadata[key]; ok {
1140+
return r
1141+
}
1142+
}
1143+
1144+
return ""
1145+
}

0 commit comments

Comments
 (0)