Skip to content

Commit 184db32

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 184db32

1 file changed

Lines changed: 25 additions & 4 deletions

File tree

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", eaRef.Namespace, eaRef.Name))
272+
}
273+
}
274+
275+
if len(eaChanged) > 0 {
276+
msg := fmt.Sprintf("external artifacts reconciled: %s", strings.Join(eaChanged, ", "))
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)