fluxcd / helm-controller

The GitOps Toolkit Helm reconciler, for declarative Helming
https://fluxcd.io
Apache License 2.0
404 stars 161 forks source link

[proposal] add the app version to events #798

Open YohannHammad opened 10 months ago

YohannHammad commented 10 months ago

Hello everyone,

I made a small PoC to achieve this result: image

The goal would be to be able to indicate in a HelmRelease the key corresponding to the version of the application (usually the tag of an image). This way it would be possible to show the version of the application in alerts in addition to the version of the chart.

The code is obviously very dirty because I was just testing the feasibility:

// helmrelease_controller.go
func (r *HelmReleaseReconciler) event(_ context.Context, hr v2.HelmRelease, revision, severity, msg string) {
    var eventMeta map[string]string
    var appVersion string

    if revision != "" || hr.Status.LastAttemptedValuesChecksum != "" {
        eventMeta = make(map[string]string)
        appVersion = hr.GetAppVersion()
        if revision != "" {
            eventMeta[v2.GroupVersion.Group+"/"+eventv1.MetaRevisionKey] = revision
        }
        if hr.Status.LastAttemptedValuesChecksum != "" {
            eventMeta[v2.GroupVersion.Group+"/"+eventv1.MetaTokenKey] = hr.Status.LastAttemptedValuesChecksum
        }
        if (appVersion != "") {
            eventMeta[v2.GroupVersion.Group+"/appVersion"] = appVersion
        }
    }

    eventType := corev1.EventTypeNormal
    if severity == eventv1.EventSeverityError {
        eventType = corev1.EventTypeWarning
    }
    r.EventRecorder.AnnotatedEventf(&hr, eventMeta, eventType, severity, msg)
}
// helmrelease_types.go
func (in HelmRelease) GetAppVersion() string {
    var values map[string]map[string]map[string]string
    if in.Spec.Values != nil {
        _ = json.Unmarshal(in.Spec.Values.Raw, &values)
    }
    return values["app"]["image"]["tag"]
}

Do you think this is something that could be added?

leosunmo commented 6 months ago

It would be great to be able to add additional annotations to events from the controllers, especially the Helm Controller. We similarly struggle to get enough metadata about a release for developers to find it useful.

souleb commented 4 months ago

we are open to a PR for this. Some related discussions are happening in https://github.com/fluxcd/flux2/discussions/4718.