From 88fa1b88f88a97ccf461c013e9c55237f381c369 Mon Sep 17 00:00:00 2001 From: Matheus Pimenta Date: Mon, 1 May 2023 00:23:20 +0100 Subject: [PATCH] Add values hash to event metadata Signed-off-by: Matheus Pimenta --- internal/controllers/helmrelease_controller.go | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/internal/controllers/helmrelease_controller.go b/internal/controllers/helmrelease_controller.go index fb3dc3ada..1cc74884d 100644 --- a/internal/controllers/helmrelease_controller.go +++ b/internal/controllers/helmrelease_controller.go @@ -18,6 +18,8 @@ package controllers import ( "context" + "crypto/sha256" + "encoding/json" "errors" "fmt" "strings" @@ -792,11 +794,21 @@ func (r *HelmReleaseReconciler) requestsForHelmChartChange(o client.Object) []re } // event emits a Kubernetes event and forwards the event to notification controller if configured. -func (r *HelmReleaseReconciler) event(_ context.Context, hr v2.HelmRelease, revision, severity, msg string) { - var meta map[string]string +func (r *HelmReleaseReconciler) event(ctx context.Context, hr v2.HelmRelease, revision, severity, msg string) { + meta := make(map[string]string) + if revision != "" { - meta = map[string]string{v2.GroupVersion.Group + "/revision": revision} + meta[v2.GroupVersion.Group+"/revision"] = revision } + + // values hash. here json.Marshal() gives stability to the hash + valuesBytes, err := json.Marshal(hr.GetValues()) + if err != nil { + valuesBytes = nil + ctrl.LoggerFrom(ctx).Error(err, "unable to marshal helm values") + } + meta[v2.GroupVersion.Group+"/config_hash"] = fmt.Sprintf("%x", sha256.Sum256(valuesBytes)) + eventtype := "Normal" if severity == eventv1.EventSeverityError { eventtype = "Warning"