Skip to content

Commit 81fa84f

Browse files
Creates notify function to only emit events if artifacts changed
Signed-off-by: Renato Vassão <renato.vassao@mindbodyonline.com>
1 parent ea23398 commit 81fa84f

2 files changed

Lines changed: 26 additions & 5 deletions

File tree

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ replace github.com/opencontainers/go-digest => github.com/opencontainers/go-dige
1010

1111
require (
1212
github.com/bmatcuk/doublestar/v4 v4.9.2
13+
github.com/fluxcd/pkg/apis/event v0.22.0
1314
github.com/fluxcd/pkg/apis/meta v1.25.0
1415
github.com/fluxcd/pkg/artifact v0.7.0
1516
github.com/fluxcd/pkg/http/fetch v0.22.0
@@ -49,7 +50,6 @@ require (
4950
github.com/exponent-io/jsonpath v0.0.0-20210407135951-1de76d718b3f // indirect
5051
github.com/fluxcd/cli-utils v0.37.1-flux.1 // indirect
5152
github.com/fluxcd/pkg/apis/acl v0.9.0 // indirect
52-
github.com/fluxcd/pkg/apis/event v0.22.0 // indirect
5353
github.com/fluxcd/pkg/lockedfile v0.7.0 // indirect
5454
github.com/fluxcd/pkg/oci v0.59.0 // indirect
5555
github.com/fluxcd/pkg/sourceignore v0.16.0 // indirect

internal/controller/artifactgenerator_controller.go

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ import (
3535
"sigs.k8s.io/controller-runtime/pkg/client"
3636
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
3737

38+
eventv1 "github.com/fluxcd/pkg/apis/event/v1beta1"
3839
gotkmeta "github.com/fluxcd/pkg/apis/meta"
3940
gotkstroage "github.com/fluxcd/pkg/artifact/storage"
4041
gotkfetch "github.com/fluxcd/pkg/http/fetch"
@@ -102,7 +103,7 @@ func (r *ArtifactGeneratorReconciler) Reconcile(ctx context.Context, req ctrl.Re
102103
// Pause reconciliation if the object has the reconcile annotation set to 'disabled'.
103104
if obj.IsDisabled() {
104105
log.Error(errors.New("can't reconcile"), msgReconciliationDisabled)
105-
r.Event(obj, corev1.EventTypeWarning, swapi.ReconciliationDisabledReason, msgReconciliationDisabled)
106+
r.Event(obj, eventv1.EventTypeTrace, swapi.ReconciliationDisabledReason, msgReconciliationDisabled)
106107
return ctrl.Result{}, nil
107108
}
108109

@@ -120,6 +121,7 @@ func (r *ArtifactGeneratorReconciler) reconcile(ctx context.Context,
120121
obj *swapi.ArtifactGenerator,
121122
patcher *gotkpatch.SerialPatcher) (ctrl.Result, error) {
122123
log := ctrl.LoggerFrom(ctx)
124+
oldObj := obj.DeepCopy()
123125

124126
// Create a temporary directory to fetch sources and build artifacts.
125127
tmpDir, err := builder.MkdirTempAbs("", "ag-")
@@ -157,7 +159,7 @@ func (r *ArtifactGeneratorReconciler) reconcile(ctx context.Context,
157159
if !hasDrifted {
158160
msg := fmt.Sprintf("No drift detected, %d artifact(s) up to date", len(obj.Status.Inventory))
159161
log.Info(msg)
160-
r.Event(obj, corev1.EventTypeNormal, gotkmeta.ReadyCondition, msg)
162+
r.Event(obj, eventv1.EventTypeTrace, gotkmeta.ReadyCondition, msg)
161163
return ctrl.Result{RequeueAfter: obj.GetRequeueAfter()}, nil
162164
}
163165

@@ -252,11 +254,30 @@ func (r *ArtifactGeneratorReconciler) reconcile(ctx context.Context,
252254
gotkmeta.ReadyCondition,
253255
gotkmeta.SucceededReason,
254256
"%s", msg)
255-
r.Event(obj, corev1.EventTypeNormal, gotkmeta.ReadyCondition, msg)
257+
r.Event(obj, eventv1.EventTypeTrace, gotkmeta.ReadyCondition, msg)
258+
259+
r.notify(oldObj, obj, eaRefs)
256260

257261
return ctrl.Result{RequeueAfter: gotkjitter.JitteredIntervalDuration(obj.GetRequeueAfter())}, nil
258262
}
259263

264+
// notify emits notification related to the result of reconciliation. It will only send events if
265+
// there is a least one external artifact update
266+
func (r *ArtifactGeneratorReconciler) notify(oldObj, newObj *swapi.ArtifactGenerator, eaRefs []swapi.ExternalArtifactReference) {
267+
eaChanged := make([]string, 0)
268+
269+
for _, eaRef := range eaRefs {
270+
if !oldObj.HasArtifactInInventory(eaRef.Name, eaRef.Namespace, eaRef.Digest) {
271+
eaChanged = append(eaChanged, fmt.Sprintf("%s/%s (%s)", eaRef.Namespace, eaRef.Name, eaRef.Digest))
272+
}
273+
}
274+
275+
if len(eaChanged) > 0 {
276+
msg := fmt.Sprintf("external artifacts reconciled: %s", strings.Join(eaChanged, "\n"))
277+
r.Event(newObj, corev1.EventTypeNormal, gotkmeta.ReadyCondition, msg)
278+
}
279+
}
280+
260281
// observeSources retrieves the current state of sources,
261282
// including their artifact URLs, digests, and revisions.
262283
// It returns a map of source alias to observed state.
@@ -464,7 +485,7 @@ func (r *ArtifactGeneratorReconciler) reconcileExternalArtifact(ctx context.Cont
464485
msg := fmt.Sprintf("%s/%s/%s reconciled with revision %s",
465486
ea.Kind, ea.Namespace, ea.Name, artifact.Revision)
466487
log.Info(msg)
467-
r.Event(obj, corev1.EventTypeNormal, gotkmeta.ReadyCondition, msg)
488+
r.Event(obj, eventv1.EventTypeTrace, gotkmeta.ReadyCondition, msg)
468489
}
469490

470491
return &swapi.ExternalArtifactReference{

0 commit comments

Comments
 (0)