Skip to content

Commit

Permalink
Add the revision to FluxInstance events metadata
Browse files Browse the repository at this point in the history
Signed-off-by: Stefan Prodan <[email protected]>
  • Loading branch information
stefanprodan committed Dec 23, 2024
1 parent b0cee38 commit 9e3e6da
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ func TestFluxInstanceArtifactReconciler(t *testing.T) {
Namespace: ns.Name,
Annotations: tt.annotations,
},
Spec: getDefaultFluxSpec(),
Spec: getDefaultFluxSpec(t),
}
obj.Spec.Distribution.Artifact = tt.manifestsURL

Expand Down
15 changes: 11 additions & 4 deletions internal/controller/fluxinstance_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ func (r *FluxInstanceReconciler) reconcile(ctx context.Context,
meta.ReadyCondition,
meta.ArtifactFailedReason,
"%s", msg)
r.EventRecorder.Event(obj, corev1.EventTypeWarning, meta.ArtifactFailedReason, msg)
r.notify(ctx, obj, meta.ArtifactFailedReason, corev1.EventTypeWarning, msg)
return ctrl.Result{}, err
}

Expand Down Expand Up @@ -528,7 +528,9 @@ func (r *FluxInstanceReconciler) apply(ctx context.Context,
action = "installed"
}

msg := fmt.Sprintf("Flux %s revision %s\n%s", action, buildResult.Revision, applyLog)
ver := strings.Split(buildResult.Revision, "@")[0]

msg := fmt.Sprintf("Flux %s %s\n%s", ver, action, applyLog)
r.notify(ctx, obj, meta.ReconciliationSucceededReason, corev1.EventTypeNormal, msg)
}

Expand Down Expand Up @@ -641,7 +643,7 @@ func (r *FluxInstanceReconciler) recordMetrics(obj *fluxcdv1.FluxInstance) error

func (r *FluxInstanceReconciler) notify(ctx context.Context, obj *fluxcdv1.FluxInstance, reason, eventType, msg string) {
notificationAddress := ""
if builder.ContainElementString(obj.GetComponents(), builder.MakeDefaultOptions().NotificationController) {
if os.Getenv("NOTIFICATIONS_DISABLED") == "" && builder.ContainElementString(obj.GetComponents(), builder.MakeDefaultOptions().NotificationController) {
notificationAddress = fmt.Sprintf("http://%s.%s.svc.%s/",
builder.MakeDefaultOptions().NotificationController,
obj.GetNamespace(),
Expand All @@ -662,5 +664,10 @@ func (r *FluxInstanceReconciler) notify(ctx context.Context, obj *fluxcdv1.FluxI
return
}

go eventRecorder.Eventf(obj, eventType, reason, "%s", msg)
annotations := map[string]string{}
if obj.Status.LastAttemptedRevision != "" {
annotations[fluxcdv1.RevisionAnnotation] = obj.Status.LastAttemptedRevision
}

eventRecorder.AnnotatedEventf(obj, annotations, eventType, reason, "%s", msg)
}
29 changes: 17 additions & 12 deletions internal/controller/fluxinstance_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func TestFluxInstanceReconciler_LifeCycle(t *testing.T) {
Name: ns.Name,
Namespace: ns.Name,
},
Spec: getDefaultFluxSpec(),
Spec: getDefaultFluxSpec(t),
}
obj.Spec.Distribution.Artifact = manifestsURL

Expand Down Expand Up @@ -196,16 +196,19 @@ func TestFluxInstanceReconciler_LifeCycle(t *testing.T) {
}
messages := []string{
"is outdated",
"Installing revision",
"Flux installed revision",
"Upgrading to revision",
"Flux updated revision",
"Installing",
"installed",
"Upgrading",
"updated",
"Reconciliation finished",
}
for _, message := range messages {
g.Expect(events).Should(ContainElement(WithTransform(func(e corev1.Event) string { return e.Message }, ContainSubstring(message))))
}

// Check if events contain the revision metadata.
g.Expect(events[len(events)-1].Annotations).To(HaveKeyWithValue(fluxcdv1.RevisionAnnotation, resultFinal.Status.LastAppliedRevision))

// Uninstall the instance.
err = testClient.Delete(ctx, obj)
g.Expect(err).ToNot(HaveOccurred())
Expand Down Expand Up @@ -238,7 +241,7 @@ func TestFluxInstanceReconciler_FetchFail(t *testing.T) {
Name: ns.Name,
Namespace: ns.Name,
},
Spec: getDefaultFluxSpec(),
Spec: getDefaultFluxSpec(t),
}
obj.Spec.Distribution.Artifact = manifestsURL

Expand Down Expand Up @@ -300,7 +303,7 @@ func TestFluxInstanceReconciler_BuildFail(t *testing.T) {
Name: ns.Name,
Namespace: ns.Name,
},
Spec: getDefaultFluxSpec(),
Spec: getDefaultFluxSpec(t),
}

err = testClient.Create(ctx, obj)
Expand Down Expand Up @@ -355,7 +358,7 @@ func TestFluxInstanceReconciler_BuildFail(t *testing.T) {
func TestFluxInstanceReconciler_Downgrade(t *testing.T) {
g := NewWithT(t)
reconciler := getFluxInstanceReconciler()
spec := getDefaultFluxSpec()
spec := getDefaultFluxSpec(t)
ctx, cancel := context.WithTimeout(context.Background(), timeout)
defer cancel()

Expand Down Expand Up @@ -433,7 +436,7 @@ func TestFluxInstanceReconciler_Downgrade(t *testing.T) {
func TestFluxInstanceReconciler_Disabled(t *testing.T) {
g := NewWithT(t)
reconciler := getFluxInstanceReconciler()
spec := getDefaultFluxSpec()
spec := getDefaultFluxSpec(t)
ctx, cancel := context.WithTimeout(context.Background(), timeout)
defer cancel()

Expand Down Expand Up @@ -530,7 +533,7 @@ func TestFluxInstanceReconciler_Disabled(t *testing.T) {
func TestFluxInstanceReconciler_Profiles(t *testing.T) {
g := NewWithT(t)
reconciler := getFluxInstanceReconciler()
spec := getDefaultFluxSpec()
spec := getDefaultFluxSpec(t)
spec.Distribution.Version = "v2.4.x"
spec.Cluster = &fluxcdv1.Cluster{
Type: "openshift",
Expand Down Expand Up @@ -640,7 +643,7 @@ func TestFluxInstanceReconciler_Profiles(t *testing.T) {
func TestFluxInstanceReconciler_NewVersion(t *testing.T) {
g := NewWithT(t)
reconciler := getFluxInstanceReconciler()
spec := getDefaultFluxSpec()
spec := getDefaultFluxSpec(t)
spec.Distribution.Version = "v2.2.x"

ctx, cancel := context.WithTimeout(context.Background(), timeout)
Expand Down Expand Up @@ -689,7 +692,9 @@ func TestFluxInstanceReconciler_NewVersion(t *testing.T) {

}

func getDefaultFluxSpec() fluxcdv1.FluxInstanceSpec {
func getDefaultFluxSpec(t *testing.T) fluxcdv1.FluxInstanceSpec {
t.Setenv("NOTIFICATIONS_DISABLED", "yes")

return fluxcdv1.FluxInstanceSpec{
Wait: ptr.To(false),
MigrateResources: ptr.To(true),
Expand Down
4 changes: 2 additions & 2 deletions internal/controller/fluxreport_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func TestFluxReportReconciler_Reconcile(t *testing.T) {
Name: ns.Name,
Namespace: ns.Name,
},
Spec: getDefaultFluxSpec(),
Spec: getDefaultFluxSpec(t),
}
err = testEnv.Create(ctx, instance)
g.Expect(err).ToNot(HaveOccurred())
Expand Down Expand Up @@ -161,7 +161,7 @@ func TestFluxReportReconciler_CustomSyncName(t *testing.T) {
Name: ns.Name,
Namespace: ns.Name,
},
Spec: getDefaultFluxSpec(),
Spec: getDefaultFluxSpec(t),
}

// Set custom sync name.
Expand Down

0 comments on commit 9e3e6da

Please sign in to comment.