From d72d5622b49e8f728f9598b61f655be1f59f1427 Mon Sep 17 00:00:00 2001
From: Matheus Pimenta
Date: Mon, 1 May 2023 01:15:51 +0100
Subject: [PATCH] Add event metadata field to HelmRelease spec
Signed-off-by: Matheus Pimenta
---
api/v2beta1/helmrelease_types.go | 6 ++++
api/v2beta1/zz_generated.deepcopy.go | 7 +++++
.../helm.toolkit.fluxcd.io_helmreleases.yaml | 7 +++++
docs/api/helmrelease.md | 28 +++++++++++++++++++
docs/spec/v2beta1/helmreleases.md | 6 ++++
.../controllers/helmrelease_controller.go | 10 +++++++
6 files changed, 64 insertions(+)
diff --git a/api/v2beta1/helmrelease_types.go b/api/v2beta1/helmrelease_types.go
index 24d0e1d0e..8dbc52958 100644
--- a/api/v2beta1/helmrelease_types.go
+++ b/api/v2beta1/helmrelease_types.go
@@ -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
diff --git a/api/v2beta1/zz_generated.deepcopy.go b/api/v2beta1/zz_generated.deepcopy.go
index 5fb7ee4db..5608f2543 100644
--- a/api/v2beta1/zz_generated.deepcopy.go
+++ b/api/v2beta1/zz_generated.deepcopy.go
@@ -276,6 +276,13 @@ func (in *HelmReleaseSpec) DeepCopyInto(out *HelmReleaseSpec) {
(*in)[i].DeepCopyInto(&(*out)[i])
}
}
+ if in.EventMetadata != nil {
+ in, out := &in.EventMetadata, &out.EventMetadata
+ *out = make(map[string]string, len(*in))
+ for key, val := range *in {
+ (*out)[key] = val
+ }
+ }
}
// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new HelmReleaseSpec.
diff --git a/config/crd/bases/helm.toolkit.fluxcd.io_helmreleases.yaml b/config/crd/bases/helm.toolkit.fluxcd.io_helmreleases.yaml
index 80dbdb60d..7b6ee77b1 100644
--- a/config/crd/bases/helm.toolkit.fluxcd.io_helmreleases.yaml
+++ b/config/crd/bases/helm.toolkit.fluxcd.io_helmreleases.yaml
@@ -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.
diff --git a/docs/api/helmrelease.md b/docs/api/helmrelease.md
index c1f5bcdc7..e912296ce 100644
--- a/docs/api/helmrelease.md
+++ b/docs/api/helmrelease.md
@@ -357,6 +357,20 @@ Kubernetes pkg/apis/apiextensions/v1.JSON
of their definition.
+
+
+eventMetadata
+
+map[string]string
+
+ |
+
+(Optional)
+ 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.
+ |
+
@@ -1166,6 +1180,20 @@ Kubernetes pkg/apis/apiextensions/v1.JSON
of their definition.
+
+
+eventMetadata
+
+map[string]string
+
+ |
+
+(Optional)
+ 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.
+ |
+
diff --git a/docs/spec/v2beta1/helmreleases.md b/docs/spec/v2beta1/helmreleases.md
index c396146e5..982d68c39 100644
--- a/docs/spec/v2beta1/helmreleases.md
+++ b/docs/spec/v2beta1/helmreleases.md
@@ -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.
diff --git a/internal/controllers/helmrelease_controller.go b/internal/controllers/helmrelease_controller.go
index fb3dc3ada..79e1612fa 100644
--- a/internal/controllers/helmrelease_controller.go
+++ b/internal/controllers/helmrelease_controller.go
@@ -794,8 +794,18 @@ 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
+ 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)
}
eventtype := "Normal"
if severity == eventv1.EventSeverityError {