Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add values hash to event metadata #681

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions api/v2beta1/helmrelease_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,12 @@ type HelmReleaseSpec struct {
// of their definition.
// +optional
PostRenderers []PostRenderer `json:"postRenderers,omitempty"`

// EventMetadata is an optional field for adding metadata to events emitted by the
// controller. Metadata fields added by the controller have priority over the fields
// added here.
// +optional
EventMetadata map[string]string `json:"eventMetadata,omitempty"`
}

// GetInstall returns the configuration for Helm install actions for the
Expand Down
7 changes: 7 additions & 0 deletions api/v2beta1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions config/crd/bases/helm.toolkit.fluxcd.io_helmreleases.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,13 @@ spec:
- name
type: object
type: array
eventMetadata:
additionalProperties:
type: string
description: EventMetadata is an optional field for adding metadata
to events emitted by the controller. Metadata fields added by the
controller have priority over the fields added here.
type: object
install:
description: Install holds the configuration for Helm install actions
for this HelmRelease.
Expand Down
28 changes: 28 additions & 0 deletions docs/api/helmrelease.md
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,20 @@ Kubernetes pkg/apis/apiextensions/v1.JSON
of their definition.</p>
</td>
</tr>
<tr>
<td>
<code>eventMetadata</code><br>
<em>
map[string]string
</em>
</td>
<td>
<em>(Optional)</em>
<p>EventMetadata is an optional field for adding metadata to events emitted by the
controller. Metadata fields added by the controller have priority over the fields
added here.</p>
</td>
</tr>
</table>
</td>
</tr>
Expand Down Expand Up @@ -1166,6 +1180,20 @@ Kubernetes pkg/apis/apiextensions/v1.JSON
of their definition.</p>
</td>
</tr>
<tr>
<td>
<code>eventMetadata</code><br>
<em>
map[string]string
</em>
</td>
<td>
<em>(Optional)</em>
<p>EventMetadata is an optional field for adding metadata to events emitted by the
controller. Metadata fields added by the controller have priority over the fields
added here.</p>
</td>
</tr>
</tbody>
</table>
</div>
Expand Down
6 changes: 6 additions & 0 deletions docs/spec/v2beta1/helmreleases.md
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,12 @@ type HelmReleaseSpec struct {
// of their definition.
// +optional
PostRenderers []PostRenderer `json:"postRenderers,omitempty"`

// EventMetadata is an optional field for adding metadata to events emitted by the
// controller. Metadata fields added by the controller have priority over the fields
// added here.
// +optional
EventMetadata map[string]string `json:"eventMetadata,omitempty"`
}

// KubeConfig references a Kubernetes secret that contains a kubeconfig file.
Expand Down
27 changes: 25 additions & 2 deletions 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,11 +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 != "" {
meta = map[string]string{v2.GroupVersion.Group + "/revision": 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