Skip to content

Commit

Permalink
reconcile: include "token" in event metadata
Browse files Browse the repository at this point in the history
This includes the "token" in the emitted events which is used to rate
limit events received by the notification-controller.

Either by using the already calculated config (values) digest, or by
calculating it for the current reconciliation request in scenarios
where it isn't available from made observations.

Signed-off-by: Hidde Beydals <hidde@hhh.computer>
  • Loading branch information
hiddeco committed Jun 14, 2023
1 parent 9de0d51 commit 2b2156b
Show file tree
Hide file tree
Showing 15 changed files with 177 additions and 40 deletions.
18 changes: 16 additions & 2 deletions internal/reconcile/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ import (

v2 "github.com/fluxcd/helm-controller/api/v2beta2"
"github.com/fluxcd/helm-controller/internal/action"
"github.com/fluxcd/helm-controller/internal/chartutil"
"github.com/fluxcd/helm-controller/internal/digest"
)

// Install is an ActionReconciler which attempts to install a Helm release
Expand Down Expand Up @@ -130,7 +132,13 @@ func (r *Install) failure(req *Request, buffer *action.LogBuffer, err error) {

// Record warning event, this message contains more data than the
// Condition summary.
r.eventRecorder.AnnotatedEventf(req.Object, eventMeta(req.Chart.Metadata.Version), corev1.EventTypeWarning, v2.InstallFailedReason, eventMessageWithLog(msg, buffer))
r.eventRecorder.AnnotatedEventf(
req.Object,
eventMeta(req.Chart.Metadata.Version, chartutil.DigestValues(digest.Canonical, req.Values).String()),
corev1.EventTypeWarning,
v2.InstallFailedReason,
eventMessageWithLog(msg, buffer),
)
}

// success records the success of a Helm installation action in the status of
Expand All @@ -150,5 +158,11 @@ func (r *Install) success(req *Request) {
}

// Record event.
r.eventRecorder.AnnotatedEventf(req.Object, eventMeta(cur.ChartVersion), corev1.EventTypeNormal, v2.InstallSucceededReason, msg)
r.eventRecorder.AnnotatedEventf(
req.Object,
eventMeta(cur.ChartVersion, cur.ConfigDigest),
corev1.EventTypeNormal,
v2.InstallSucceededReason,
msg,
)
}
15 changes: 10 additions & 5 deletions internal/reconcile/install_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import (
"github.com/go-logr/logr"
. "github.com/onsi/gomega"
"helm.sh/helm/v3/pkg/chart"
"helm.sh/helm/v3/pkg/chartutil"
helmchartutil "helm.sh/helm/v3/pkg/chartutil"
helmrelease "helm.sh/helm/v3/pkg/release"
"helm.sh/helm/v3/pkg/releaseutil"
helmstorage "helm.sh/helm/v3/pkg/storage"
Expand All @@ -35,11 +35,14 @@ import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/tools/record"

eventv1 "github.com/fluxcd/pkg/apis/event/v1beta1"
"github.com/fluxcd/pkg/apis/meta"
"github.com/fluxcd/pkg/runtime/conditions"

v2 "github.com/fluxcd/helm-controller/api/v2beta2"
"github.com/fluxcd/helm-controller/internal/action"
"github.com/fluxcd/helm-controller/internal/chartutil"
"github.com/fluxcd/helm-controller/internal/digest"
"github.com/fluxcd/helm-controller/internal/release"
"github.com/fluxcd/helm-controller/internal/storage"
"github.com/fluxcd/helm-controller/internal/testutil"
Expand All @@ -56,7 +59,7 @@ func TestInstall_Reconcile(t *testing.T) {
// chart to install.
chart *chart.Chart
// values to use during install.
values chartutil.Values
values helmchartutil.Values
// spec modifies the HelmRelease object spec before install.
spec func(spec *v2.HelmReleaseSpec)
// status to configure on the HelmRelease object before install.
Expand Down Expand Up @@ -296,7 +299,7 @@ func TestInstall_failure(t *testing.T) {
eventRecorder: recorder,
}

req := &Request{Object: obj.DeepCopy(), Chart: chrt}
req := &Request{Object: obj.DeepCopy(), Chart: chrt, Values: map[string]interface{}{"foo": "bar"}}
r.failure(req, nil, err)

expectMsg := fmt.Sprintf(fmtInstallFailure, mockReleaseNamespace, mockReleaseName, chrt.Name(),
Expand All @@ -313,7 +316,8 @@ func TestInstall_failure(t *testing.T) {
Message: expectMsg,
ObjectMeta: metav1.ObjectMeta{
Annotations: map[string]string{
"revision": chrt.Metadata.Version,
eventMetaGroupKey(eventv1.MetaRevisionKey): chrt.Metadata.Version,
eventMetaGroupKey(eventv1.MetaTokenKey): chartutil.DigestValues(digest.Canonical, req.Values).String(),
},
},
},
Expand Down Expand Up @@ -381,7 +385,8 @@ func TestInstall_success(t *testing.T) {
Message: expectMsg,
ObjectMeta: metav1.ObjectMeta{
Annotations: map[string]string{
"revision": obj.Status.Current.ChartVersion,
eventMetaGroupKey(eventv1.MetaRevisionKey): obj.Status.Current.ChartVersion,
eventMetaGroupKey(eventv1.MetaTokenKey): obj.Status.Current.ConfigDigest,
},
},
},
Expand Down
24 changes: 18 additions & 6 deletions internal/reconcile/release.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
helmrelease "helm.sh/helm/v3/pkg/release"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

eventv1 "github.com/fluxcd/pkg/apis/event/v1beta1"
"github.com/fluxcd/pkg/apis/meta"
"github.com/fluxcd/pkg/runtime/conditions"

Expand Down Expand Up @@ -198,11 +199,22 @@ func eventMessageWithLog(msg string, log *action.LogBuffer) string {

// eventMeta returns the event (annotation) metadata based on the given
// parameters.
func eventMeta(revision string) map[string]string {
if revision == "" {
return nil
}
return map[string]string{
"revision": revision,
func eventMeta(revision, token string) map[string]string {
var metadata map[string]string
if revision != "" || token != "" {
metadata = make(map[string]string)
if revision != "" {
metadata[eventMetaGroupKey(eventv1.MetaRevisionKey)] = revision
}
if token != "" {
metadata[eventMetaGroupKey(eventv1.MetaTokenKey)] = token
}
}
return metadata
}

// eventMetaGroupKey returns the event (annotation) metadata key prefixed with
// the group.
func eventMetaGroupKey(key string) string {
return v2.GroupVersion.Group + "/" + key
}
18 changes: 16 additions & 2 deletions internal/reconcile/rollback_remediation.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ import (

v2 "github.com/fluxcd/helm-controller/api/v2beta2"
"github.com/fluxcd/helm-controller/internal/action"
"github.com/fluxcd/helm-controller/internal/chartutil"
"github.com/fluxcd/helm-controller/internal/digest"
"github.com/fluxcd/helm-controller/internal/release"
"github.com/fluxcd/helm-controller/internal/storage"
)
Expand Down Expand Up @@ -142,7 +144,13 @@ func (r *RollbackRemediation) failure(req *Request, buffer *action.LogBuffer, er

// Record warning event, this message contains more data than the
// Condition summary.
r.eventRecorder.AnnotatedEventf(req.Object, eventMeta(prev.ChartVersion), corev1.EventTypeWarning, v2.RollbackFailedReason, eventMessageWithLog(msg, buffer))
r.eventRecorder.AnnotatedEventf(
req.Object,
eventMeta(prev.ChartVersion, chartutil.DigestValues(digest.Canonical, req.Values).String()),
corev1.EventTypeWarning,
v2.RollbackFailedReason,
eventMessageWithLog(msg, buffer),
)
}

// success records the success of a Helm rollback action in the status of the
Expand All @@ -156,7 +164,13 @@ func (r *RollbackRemediation) success(req *Request) {
conditions.MarkTrue(req.Object, v2.RemediatedCondition, v2.RollbackSucceededReason, msg)

// Record event.
r.eventRecorder.AnnotatedEventf(req.Object, eventMeta(prev.ChartVersion), corev1.EventTypeNormal, v2.RollbackSucceededReason, msg)
r.eventRecorder.AnnotatedEventf(
req.Object,
eventMeta(prev.ChartVersion, chartutil.DigestValues(digest.Canonical, req.Values).String()),
corev1.EventTypeNormal,
v2.RollbackSucceededReason,
msg,
)
}

// observeRollback returns a storage.ObserveFunc that can be used to observe
Expand Down
11 changes: 8 additions & 3 deletions internal/reconcile/rollback_remediation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"context"
"errors"
"fmt"
eventv1 "github.com/fluxcd/pkg/apis/event/v1beta1"
"testing"
"time"

Expand All @@ -38,6 +39,8 @@ import (

v2 "github.com/fluxcd/helm-controller/api/v2beta2"
"github.com/fluxcd/helm-controller/internal/action"
"github.com/fluxcd/helm-controller/internal/chartutil"
"github.com/fluxcd/helm-controller/internal/digest"
"github.com/fluxcd/helm-controller/internal/release"
"github.com/fluxcd/helm-controller/internal/storage"
"github.com/fluxcd/helm-controller/internal/testutil"
Expand Down Expand Up @@ -404,7 +407,8 @@ func TestRollbackRemediation_failure(t *testing.T) {
Message: expectMsg,
ObjectMeta: metav1.ObjectMeta{
Annotations: map[string]string{
"revision": prev.Chart.Metadata.Version,
eventMetaGroupKey(eventv1.MetaRevisionKey): prev.Chart.Metadata.Version,
eventMetaGroupKey(eventv1.MetaTokenKey): chartutil.DigestValues(digest.Canonical, req.Values).String(),
},
},
},
Expand Down Expand Up @@ -451,7 +455,7 @@ func TestRollbackRemediation_success(t *testing.T) {
},
}

req := &Request{Object: obj}
req := &Request{Object: obj, Values: map[string]interface{}{"foo": "bar"}}
r.success(req)

expectMsg := fmt.Sprintf(fmtRollbackRemediationSuccess,
Expand All @@ -469,7 +473,8 @@ func TestRollbackRemediation_success(t *testing.T) {
Message: expectMsg,
ObjectMeta: metav1.ObjectMeta{
Annotations: map[string]string{
"revision": prev.Chart.Metadata.Version,
eventMetaGroupKey(eventv1.MetaRevisionKey): prev.Chart.Metadata.Version,
eventMetaGroupKey(eventv1.MetaTokenKey): chartutil.DigestValues(digest.Canonical, req.Values).String(),
},
},
},
Expand Down
16 changes: 14 additions & 2 deletions internal/reconcile/test.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,13 @@ func (r *Test) failure(req *Request, buffer *action.LogBuffer, err error) {

// Record warning event, this message contains more data than the
// Condition summary.
r.eventRecorder.AnnotatedEventf(req.Object, eventMeta(cur.ChartVersion), corev1.EventTypeWarning, v2.TestFailedReason, eventMessageWithLog(msg, buffer))
r.eventRecorder.AnnotatedEventf(
req.Object,
eventMeta(cur.ChartVersion, cur.ConfigDigest),
corev1.EventTypeWarning,
v2.TestFailedReason,
eventMessageWithLog(msg, buffer),
)

if req.Object.GetCurrent().HasBeenTested() {
// Count the failure of the test for the active remediation strategy if enabled.
Expand Down Expand Up @@ -173,7 +179,13 @@ func (r *Test) success(req *Request) {
conditions.MarkTrue(req.Object, v2.TestSuccessCondition, v2.TestSucceededReason, msg)

// Record event.
r.eventRecorder.AnnotatedEventf(req.Object, eventMeta(cur.ChartVersion), corev1.EventTypeNormal, v2.TestSucceededReason, msg)
r.eventRecorder.AnnotatedEventf(
req.Object,
eventMeta(cur.ChartVersion, cur.ConfigDigest),
corev1.EventTypeNormal,
v2.TestSucceededReason,
msg,
)
}

// observeTest returns a storage.ObserveFunc that can be used to observe
Expand Down
9 changes: 7 additions & 2 deletions internal/reconcile/test_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,14 @@ import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/tools/record"

eventv1 "github.com/fluxcd/pkg/apis/event/v1beta1"
"github.com/fluxcd/pkg/apis/meta"
"github.com/fluxcd/pkg/runtime/conditions"

v2 "github.com/fluxcd/helm-controller/api/v2beta2"
"github.com/fluxcd/helm-controller/internal/action"
"github.com/fluxcd/helm-controller/internal/chartutil"
"github.com/fluxcd/helm-controller/internal/digest"
"github.com/fluxcd/helm-controller/internal/release"
"github.com/fluxcd/helm-controller/internal/testutil"
)
Expand Down Expand Up @@ -456,7 +459,8 @@ func TestTest_failure(t *testing.T) {
Message: expectMsg,
ObjectMeta: metav1.ObjectMeta{
Annotations: map[string]string{
"revision": cur.Chart.Metadata.Version,
eventMetaGroupKey(eventv1.MetaRevisionKey): cur.Chart.Metadata.Version,
eventMetaGroupKey(eventv1.MetaTokenKey): chartutil.DigestValues(digest.Canonical, cur.Config).String(),
},
},
},
Expand Down Expand Up @@ -567,7 +571,8 @@ func TestTest_success(t *testing.T) {
Message: expectMsg,
ObjectMeta: metav1.ObjectMeta{
Annotations: map[string]string{
"revision": cur.Chart.Metadata.Version,
eventMetaGroupKey(eventv1.MetaRevisionKey): cur.Chart.Metadata.Version,
eventMetaGroupKey(eventv1.MetaTokenKey): chartutil.DigestValues(digest.Canonical, cur.Config).String(),
},
},
},
Expand Down
15 changes: 13 additions & 2 deletions internal/reconcile/uninstall.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,12 @@ func (r *Uninstall) failure(req *Request, buffer *action.LogBuffer, err error) {

// Record warning event, this message contains more data than the
// Condition summary.
r.eventRecorder.AnnotatedEventf(req.Object, eventMeta(cur.ChartVersion), corev1.EventTypeWarning, v2.UninstallFailedReason, eventMessageWithLog(msg, buffer))
r.eventRecorder.AnnotatedEventf(
req.Object,
eventMeta(cur.ChartVersion, cur.ConfigDigest),
corev1.EventTypeWarning, v2.UninstallFailedReason,
eventMessageWithLog(msg, buffer),
)
}

// success records the success of a Helm uninstall action in the status of
Expand All @@ -170,7 +175,13 @@ func (r *Uninstall) success(req *Request) {

// Record warning event, this message contains more data than the
// Condition summary.
r.eventRecorder.AnnotatedEventf(req.Object, eventMeta(cur.ChartVersion), corev1.EventTypeNormal, v2.UninstallSucceededReason, msg)
r.eventRecorder.AnnotatedEventf(
req.Object,
eventMeta(cur.ChartVersion, cur.ConfigDigest),
corev1.EventTypeNormal,
v2.UninstallSucceededReason,
msg,
)
}

// observeUninstall returns a storage.ObserveFunc that can be used to observe
Expand Down
16 changes: 14 additions & 2 deletions internal/reconcile/uninstall_remediation.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,13 @@ func (r *UninstallRemediation) failure(req *Request, buffer *action.LogBuffer, e

// Record warning event, this message contains more data than the
// Condition summary.
r.eventRecorder.AnnotatedEventf(req.Object, eventMeta(cur.ChartVersion), corev1.EventTypeWarning, v2.UninstallFailedReason, eventMessageWithLog(msg, buffer))
r.eventRecorder.AnnotatedEventf(
req.Object,
eventMeta(cur.ChartVersion, cur.ConfigDigest),
corev1.EventTypeWarning,
v2.UninstallFailedReason,
eventMessageWithLog(msg, buffer),
)
}

// success records the success of a Helm uninstall remediation action in the
Expand All @@ -166,5 +172,11 @@ func (r *UninstallRemediation) success(req *Request) {
conditions.MarkTrue(req.Object, v2.RemediatedCondition, v2.UninstallSucceededReason, msg)

// Record event.
r.eventRecorder.AnnotatedEventf(req.Object, eventMeta(cur.ChartVersion), corev1.EventTypeNormal, v2.UninstallSucceededReason, msg)
r.eventRecorder.AnnotatedEventf(
req.Object,
eventMeta(cur.ChartVersion, cur.ConfigDigest),
corev1.EventTypeNormal,
v2.UninstallSucceededReason,
msg,
)
}
9 changes: 7 additions & 2 deletions internal/reconcile/uninstall_remediation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,13 @@ import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/tools/record"

eventv1 "github.com/fluxcd/pkg/apis/event/v1beta1"
"github.com/fluxcd/pkg/runtime/conditions"

v2 "github.com/fluxcd/helm-controller/api/v2beta2"
"github.com/fluxcd/helm-controller/internal/action"
"github.com/fluxcd/helm-controller/internal/chartutil"
"github.com/fluxcd/helm-controller/internal/digest"
"github.com/fluxcd/helm-controller/internal/release"
"github.com/fluxcd/helm-controller/internal/storage"
"github.com/fluxcd/helm-controller/internal/testutil"
Expand Down Expand Up @@ -406,7 +409,8 @@ func TestUninstallRemediation_failure(t *testing.T) {
Message: expectMsg,
ObjectMeta: metav1.ObjectMeta{
Annotations: map[string]string{
"revision": cur.Chart.Metadata.Version,
eventMetaGroupKey(eventv1.MetaRevisionKey): cur.Chart.Metadata.Version,
eventMetaGroupKey(eventv1.MetaTokenKey): chartutil.DigestValues(digest.Canonical, cur.Config).String(),
},
},
},
Expand Down Expand Up @@ -472,7 +476,8 @@ func TestUninstallRemediation_success(t *testing.T) {
Message: expectMsg,
ObjectMeta: metav1.ObjectMeta{
Annotations: map[string]string{
"revision": cur.Chart.Metadata.Version,
eventMetaGroupKey(eventv1.MetaRevisionKey): cur.Chart.Metadata.Version,
eventMetaGroupKey(eventv1.MetaTokenKey): chartutil.DigestValues(digest.Canonical, cur.Config).String(),
},
},
},
Expand Down
Loading

0 comments on commit 2b2156b

Please sign in to comment.