Skip to content

Commit

Permalink
Add values hash to event metadata
Browse files Browse the repository at this point in the history
Signed-off-by: Matheus Pimenta <[email protected]>
  • Loading branch information
matheuscscp committed May 1, 2023
1 parent fd78f93 commit 0a05c42
Showing 1 changed file with 15 additions and 1 deletion.
16 changes: 15 additions & 1 deletion internal/controllers/helmrelease_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ package controllers

import (
"context"
"crypto/sha256"
"encoding/json"
"errors"
"fmt"
"strings"
Expand Down Expand Up @@ -792,20 +794,32 @@ 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) {
func (r *HelmReleaseReconciler) event(ctx context.Context, hr v2.HelmRelease, revision, severity, msg string) {
var meta map[string]string
addMetadata := func(key, value string) {
if meta == nil {
meta = make(map[string]string)
}
meta[v2.GroupVersion.Group+"/"+key] = value
}

for key, value := range hr.Spec.EventMetadata {
addMetadata(key, value)
}

if revision != "" {
addMetadata("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")
}
valuesHash := fmt.Sprintf("%x", sha256.Sum256(valuesBytes))
addMetadata("config_checksum", valuesHash)

eventtype := "Normal"
if severity == eventv1.EventSeverityError {
eventtype = "Warning"
Expand Down

0 comments on commit 0a05c42

Please sign in to comment.