Skip to content

Commit

Permalink
Add event metadata field to HelmRelease spec
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 7074d96 commit 9c9788f
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 2 deletions.
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
9 changes: 7 additions & 2 deletions internal/controllers/helmrelease_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -796,9 +796,14 @@ 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(ctx context.Context, hr v2.HelmRelease, revision, severity, msg string) {
meta := make(map[string]string)
addMetadata := func(key, value string) { meta[v2.GroupVersion.Group+"/"+key] = value }

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

if revision != "" {
meta[v2.GroupVersion.Group+"/revision"] = revision
addMetadata("revision", revision)
}

// values hash. here json.Marshal() gives stability to the hash
Expand All @@ -807,7 +812,7 @@ func (r *HelmReleaseReconciler) event(ctx context.Context, hr v2.HelmRelease, re
valuesBytes = nil
ctrl.LoggerFrom(ctx).Error(err, "unable to marshal helm values")
}
meta[v2.GroupVersion.Group+"/config_checksum"] = fmt.Sprintf("%x", sha256.Sum256(valuesBytes))
addMetadata("config_checksum", fmt.Sprintf("%x", sha256.Sum256(valuesBytes)))

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

0 comments on commit 9c9788f

Please sign in to comment.