Skip to content

Commit

Permalink
Add the chart app version to events metadata
Browse files Browse the repository at this point in the history
Signed-off-by: Stefan Prodan <[email protected]>
  • Loading branch information
stefanprodan committed May 6, 2024
1 parent f8aa5b4 commit 7f78cdc
Show file tree
Hide file tree
Showing 21 changed files with 70 additions and 16 deletions.
3 changes: 3 additions & 0 deletions api/v2/snapshot_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,9 @@ type Snapshot struct {
// storage.
// +required
ChartVersion string `json:"chartVersion"`
// AppVersion is the chart app version of the release object in storage.
// +optional
AppVersion string `json:"appVersion,omitempty"`
// ConfigDigest is the checksum of the config (better known as
// "values") of the release object in storage.
// It has the format of `<algo>:<checksum>`.
Expand Down
4 changes: 4 additions & 0 deletions config/crd/bases/helm.toolkit.fluxcd.io_helmreleases.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1002,6 +1002,10 @@ spec:
Provisional: when the calculation method of the Digest field is changed,
this field will be used to distinguish between the old and new methods.
type: string
appVersion:
description: Version is the version of the release object in
storage.
type: string
chartName:
description: ChartName is the chart name of the release object
in storage.
Expand Down
12 changes: 12 additions & 0 deletions docs/api/v2/helm.md
Original file line number Diff line number Diff line change
Expand Up @@ -2243,6 +2243,18 @@ int
</tr>
<tr>
<td>
<code>appVersion</code><br>
<em>
string
</em>
</td>
<td>
<em>(Optional)</em>
<p>Version is the version of the release object in storage.</p>
</td>
</tr>
<tr>
<td>
<code>status</code><br>
<em>
string
Expand Down
6 changes: 4 additions & 2 deletions internal/reconcile/correct_cluster_drift.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,10 +104,12 @@ func (r *CorrectClusterDrift) report(obj *v2.HelmRelease, changeSet *ssa.ChangeS
sb.WriteString(changeSet.String())
}

r.eventRecorder.AnnotatedEventf(obj, eventMeta(cur.ChartVersion, cur.ConfigDigest, addOCIDigest(cur.OCIDigest)), corev1.EventTypeWarning,
r.eventRecorder.AnnotatedEventf(obj, eventMeta(cur.ChartVersion, cur.ConfigDigest,
addAppVersion(cur.AppVersion), addOCIDigest(cur.OCIDigest)), corev1.EventTypeWarning,
"DriftCorrectionFailed", sb.String())
case changeSet != nil && len(changeSet.Entries) > 0:
r.eventRecorder.AnnotatedEventf(obj, eventMeta(cur.ChartVersion, cur.ConfigDigest, addOCIDigest(cur.OCIDigest)), corev1.EventTypeNormal,
r.eventRecorder.AnnotatedEventf(obj, eventMeta(cur.ChartVersion, cur.ConfigDigest,
addAppVersion(cur.AppVersion), addOCIDigest(cur.OCIDigest)), corev1.EventTypeNormal,
"DriftCorrected", "Cluster state of release %s has been corrected:\n%s",
obj.Status.History.Latest().FullReleaseName(), changeSet.String())
}
Expand Down
4 changes: 2 additions & 2 deletions internal/reconcile/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ func (r *Install) failure(req *Request, buffer *action.LogBuffer, err error) {
r.eventRecorder.AnnotatedEventf(
req.Object,
eventMeta(req.Chart.Metadata.Version, chartutil.DigestValues(digest.Canonical, req.Values).String(),
addOCIDigest(req.Object.Status.LastAttemptedRevisionDigest)),
addAppVersion(req.Chart.AppVersion()), addOCIDigest(req.Object.Status.LastAttemptedRevisionDigest)),
corev1.EventTypeWarning,
v2.InstallFailedReason,
eventMessageWithLog(msg, buffer),
Expand All @@ -182,7 +182,7 @@ func (r *Install) success(req *Request) {
// Record event.
r.eventRecorder.AnnotatedEventf(
req.Object,
eventMeta(cur.ChartVersion, cur.ConfigDigest, addOCIDigest(cur.OCIDigest)),
eventMeta(cur.ChartVersion, cur.ConfigDigest, addAppVersion(cur.AppVersion), addOCIDigest(cur.OCIDigest)),
corev1.EventTypeNormal,
v2.InstallSucceededReason,
msg,
Expand Down
2 changes: 2 additions & 0 deletions internal/reconcile/install_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,7 @@ func TestInstall_failure(t *testing.T) {
Annotations: map[string]string{
eventMetaGroupKey(metaOCIDigestKey): obj.Status.LastAttemptedRevisionDigest,
eventMetaGroupKey(eventv1.MetaRevisionKey): chrt.Metadata.Version,
eventMetaGroupKey(metaAppVersionKey): chrt.Metadata.AppVersion,
eventMetaGroupKey(eventv1.MetaTokenKey): chartutil.DigestValues(digest.Canonical, req.Values).String(),
},
},
Expand Down Expand Up @@ -413,6 +414,7 @@ func TestInstall_success(t *testing.T) {
ObjectMeta: metav1.ObjectMeta{
Annotations: map[string]string{
eventMetaGroupKey(eventv1.MetaRevisionKey): obj.Status.History.Latest().ChartVersion,
eventMetaGroupKey(metaAppVersionKey): obj.Status.History.Latest().AppVersion,
eventMetaGroupKey(eventv1.MetaTokenKey): obj.Status.History.Latest().ConfigDigest,
},
},
Expand Down
14 changes: 14 additions & 0 deletions internal/reconcile/release.go
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,9 @@ type addMeta func(map[string]string)
// metaOCIDigestKey is the key for the OCI digest metadata.
const metaOCIDigestKey = "oci-digest"

// metaAppVersionKey is the key for the app version found in chart metadata.
const metaAppVersionKey = "app-version"

// eventMeta returns the event (annotation) metadata based on the given
// parameters.
func eventMeta(revision, token string, metas ...addMeta) map[string]string {
Expand Down Expand Up @@ -235,6 +238,17 @@ func addOCIDigest(digest string) addMeta {
}
}

func addAppVersion(appVersion string) addMeta {
return func(m map[string]string) {
if appVersion != "" {
if m == nil {
m = make(map[string]string)
}
m[eventMetaGroupKey(metaAppVersionKey)] = appVersion
}
}
}

// eventMetaGroupKey returns the event (annotation) metadata key prefixed with
// the group.
func eventMetaGroupKey(key string) string {
Expand Down
6 changes: 4 additions & 2 deletions internal/reconcile/rollback_remediation.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,8 @@ func (r *RollbackRemediation) failure(req *Request, prev *v2.Snapshot, buffer *a
// Condition summary.
r.eventRecorder.AnnotatedEventf(
req.Object,
eventMeta(prev.ChartVersion, chartutil.DigestValues(digest.Canonical, req.Values).String(), addOCIDigest(prev.OCIDigest)),
eventMeta(prev.ChartVersion, chartutil.DigestValues(digest.Canonical, req.Values).String(),
addAppVersion(prev.AppVersion), addOCIDigest(prev.OCIDigest)),
corev1.EventTypeWarning,
v2.RollbackFailedReason,
eventMessageWithLog(msg, buffer),
Expand All @@ -162,7 +163,8 @@ func (r *RollbackRemediation) success(req *Request, prev *v2.Snapshot) {
// Record event.
r.eventRecorder.AnnotatedEventf(
req.Object,
eventMeta(prev.ChartVersion, chartutil.DigestValues(digest.Canonical, req.Values).String(), addOCIDigest(prev.OCIDigest)),
eventMeta(prev.ChartVersion, chartutil.DigestValues(digest.Canonical, req.Values).String(),
addAppVersion(prev.AppVersion), addOCIDigest(prev.OCIDigest)),
corev1.EventTypeNormal,
v2.RollbackSucceededReason,
msg,
Expand Down
2 changes: 2 additions & 0 deletions internal/reconcile/rollback_remediation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -414,6 +414,7 @@ func TestRollbackRemediation_failure(t *testing.T) {
ObjectMeta: metav1.ObjectMeta{
Annotations: map[string]string{
eventMetaGroupKey(eventv1.MetaRevisionKey): prev.Chart.Metadata.Version,
eventMetaGroupKey(metaAppVersionKey): prev.Chart.Metadata.AppVersion,
eventMetaGroupKey(eventv1.MetaTokenKey): chartutil.DigestValues(digest.Canonical, req.Values).String(),
},
},
Expand Down Expand Up @@ -473,6 +474,7 @@ func TestRollbackRemediation_success(t *testing.T) {
ObjectMeta: metav1.ObjectMeta{
Annotations: map[string]string{
eventMetaGroupKey(eventv1.MetaRevisionKey): prev.Chart.Metadata.Version,
eventMetaGroupKey(metaAppVersionKey): prev.Chart.Metadata.AppVersion,
eventMetaGroupKey(eventv1.MetaTokenKey): chartutil.DigestValues(digest.Canonical, req.Values).String(),
},
},
Expand Down
4 changes: 2 additions & 2 deletions internal/reconcile/test.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ func (r *Test) failure(req *Request, err error) {
// Condition summary.
r.eventRecorder.AnnotatedEventf(
req.Object,
eventMeta(cur.ChartVersion, cur.ConfigDigest, addOCIDigest(cur.OCIDigest)),
eventMeta(cur.ChartVersion, cur.ConfigDigest, addAppVersion(cur.AppVersion), addOCIDigest(cur.OCIDigest)),
corev1.EventTypeWarning,
v2.TestFailedReason,
msg,
Expand Down Expand Up @@ -181,7 +181,7 @@ func (r *Test) success(req *Request) {
// Record event.
r.eventRecorder.AnnotatedEventf(
req.Object,
eventMeta(cur.ChartVersion, cur.ConfigDigest, addOCIDigest(cur.OCIDigest)),
eventMeta(cur.ChartVersion, cur.ConfigDigest, addAppVersion(cur.AppVersion), addOCIDigest(cur.OCIDigest)),
corev1.EventTypeNormal,
v2.TestSucceededReason,
msg,
Expand Down
2 changes: 2 additions & 0 deletions internal/reconcile/test_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -506,6 +506,7 @@ func TestTest_failure(t *testing.T) {
ObjectMeta: metav1.ObjectMeta{
Annotations: map[string]string{
eventMetaGroupKey(eventv1.MetaRevisionKey): cur.Chart.Metadata.Version,
eventMetaGroupKey(metaAppVersionKey): cur.Chart.Metadata.AppVersion,
eventMetaGroupKey(eventv1.MetaTokenKey): chartutil.DigestValues(digest.Canonical, cur.Config).String(),
},
},
Expand Down Expand Up @@ -602,6 +603,7 @@ func TestTest_success(t *testing.T) {
ObjectMeta: metav1.ObjectMeta{
Annotations: map[string]string{
eventMetaGroupKey(eventv1.MetaRevisionKey): cur.Chart.Metadata.Version,
eventMetaGroupKey(metaAppVersionKey): cur.Chart.Metadata.AppVersion,
eventMetaGroupKey(eventv1.MetaTokenKey): chartutil.DigestValues(digest.Canonical, cur.Config).String(),
},
},
Expand Down
4 changes: 2 additions & 2 deletions internal/reconcile/uninstall.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ func (r *Uninstall) failure(req *Request, buffer *action.LogBuffer, err error) {
// Condition summary.
r.eventRecorder.AnnotatedEventf(
req.Object,
eventMeta(cur.ChartVersion, cur.ConfigDigest, addOCIDigest(cur.OCIDigest)),
eventMeta(cur.ChartVersion, cur.ConfigDigest, addAppVersion(cur.AppVersion), addOCIDigest(cur.OCIDigest)),
corev1.EventTypeWarning, v2.UninstallFailedReason,
eventMessageWithLog(msg, buffer),
)
Expand All @@ -201,7 +201,7 @@ func (r *Uninstall) success(req *Request) {
// Condition summary.
r.eventRecorder.AnnotatedEventf(
req.Object,
eventMeta(cur.ChartVersion, cur.ConfigDigest, addOCIDigest(cur.OCIDigest)),
eventMeta(cur.ChartVersion, cur.ConfigDigest, addAppVersion(cur.AppVersion), addOCIDigest(cur.OCIDigest)),
corev1.EventTypeNormal,
v2.UninstallSucceededReason,
msg,
Expand Down
4 changes: 2 additions & 2 deletions internal/reconcile/uninstall_remediation.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ func (r *UninstallRemediation) failure(req *Request, buffer *action.LogBuffer, e
// Condition summary.
r.eventRecorder.AnnotatedEventf(
req.Object,
eventMeta(cur.ChartVersion, cur.ConfigDigest, addOCIDigest(cur.OCIDigest)),
eventMeta(cur.ChartVersion, cur.ConfigDigest, addAppVersion(cur.AppVersion), addOCIDigest(cur.OCIDigest)),
corev1.EventTypeWarning,
v2.UninstallFailedReason,
eventMessageWithLog(msg, buffer),
Expand All @@ -175,7 +175,7 @@ func (r *UninstallRemediation) success(req *Request) {
// Record event.
r.eventRecorder.AnnotatedEventf(
req.Object,
eventMeta(cur.ChartVersion, cur.ConfigDigest, addOCIDigest(cur.OCIDigest)),
eventMeta(cur.ChartVersion, cur.ConfigDigest, addAppVersion(cur.AppVersion), addOCIDigest(cur.OCIDigest)),
corev1.EventTypeNormal,
v2.UninstallSucceededReason,
msg,
Expand Down
2 changes: 2 additions & 0 deletions internal/reconcile/uninstall_remediation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -421,6 +421,7 @@ func TestUninstallRemediation_failure(t *testing.T) {
ObjectMeta: metav1.ObjectMeta{
Annotations: map[string]string{
eventMetaGroupKey(eventv1.MetaRevisionKey): cur.Chart.Metadata.Version,
eventMetaGroupKey(metaAppVersionKey): cur.Chart.Metadata.AppVersion,
eventMetaGroupKey(eventv1.MetaTokenKey): chartutil.DigestValues(digest.Canonical, cur.Config).String(),
},
},
Expand Down Expand Up @@ -490,6 +491,7 @@ func TestUninstallRemediation_success(t *testing.T) {
ObjectMeta: metav1.ObjectMeta{
Annotations: map[string]string{
eventMetaGroupKey(eventv1.MetaRevisionKey): cur.Chart.Metadata.Version,
eventMetaGroupKey(metaAppVersionKey): cur.Chart.Metadata.AppVersion,
eventMetaGroupKey(eventv1.MetaTokenKey): chartutil.DigestValues(digest.Canonical, cur.Config).String(),
},
},
Expand Down
2 changes: 2 additions & 0 deletions internal/reconcile/uninstall_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -549,6 +549,7 @@ func TestUninstall_failure(t *testing.T) {
ObjectMeta: metav1.ObjectMeta{
Annotations: map[string]string{
eventMetaGroupKey(eventv1.MetaRevisionKey): cur.Chart.Metadata.Version,
eventMetaGroupKey(metaAppVersionKey): cur.Chart.Metadata.AppVersion,
eventMetaGroupKey(eventv1.MetaTokenKey): chartutil.DigestValues(digest.Canonical, cur.Config).String(),
},
},
Expand Down Expand Up @@ -617,6 +618,7 @@ func TestUninstall_success(t *testing.T) {
ObjectMeta: metav1.ObjectMeta{
Annotations: map[string]string{
eventMetaGroupKey(eventv1.MetaRevisionKey): cur.Chart.Metadata.Version,
eventMetaGroupKey(metaAppVersionKey): cur.Chart.Metadata.AppVersion,
eventMetaGroupKey(eventv1.MetaTokenKey): chartutil.DigestValues(digest.Canonical, cur.Config).String(),
},
},
Expand Down
4 changes: 2 additions & 2 deletions internal/reconcile/unlock.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ func (r *Unlock) failure(req *Request, cur *v2.Snapshot, status helmrelease.Stat
// Record warning event.
r.eventRecorder.AnnotatedEventf(
req.Object,
eventMeta(cur.ChartVersion, cur.ConfigDigest, addOCIDigest(cur.OCIDigest)),
eventMeta(cur.ChartVersion, cur.ConfigDigest, addAppVersion(cur.AppVersion), addOCIDigest(cur.OCIDigest)),
corev1.EventTypeWarning,
"PendingRelease",
msg,
Expand All @@ -138,7 +138,7 @@ func (r *Unlock) success(req *Request, cur *v2.Snapshot, status helmrelease.Stat
// Record event.
r.eventRecorder.AnnotatedEventf(
req.Object,
eventMeta(cur.ChartVersion, cur.ConfigDigest, addOCIDigest(cur.OCIDigest)),
eventMeta(cur.ChartVersion, cur.ConfigDigest, addAppVersion(cur.AppVersion), addOCIDigest(cur.OCIDigest)),
corev1.EventTypeNormal,
"PendingRelease",
msg,
Expand Down
3 changes: 3 additions & 0 deletions internal/reconcile/unlock_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -404,6 +404,7 @@ func TestUnlock_failure(t *testing.T) {
ObjectMeta: metav1.ObjectMeta{
Annotations: map[string]string{
eventMetaGroupKey(eventv1.MetaRevisionKey): cur.Chart.Metadata.Version,
eventMetaGroupKey(metaAppVersionKey): cur.Chart.Metadata.AppVersion,
eventMetaGroupKey(eventv1.MetaTokenKey): chartutil.DigestValues(digest.Canonical, cur.Config).String(),
},
},
Expand Down Expand Up @@ -450,6 +451,7 @@ func TestUnlock_success(t *testing.T) {
ObjectMeta: metav1.ObjectMeta{
Annotations: map[string]string{
eventMetaGroupKey(eventv1.MetaRevisionKey): cur.Chart.Metadata.Version,
eventMetaGroupKey(metaAppVersionKey): cur.Chart.Metadata.AppVersion,
eventMetaGroupKey(eventv1.MetaTokenKey): chartutil.DigestValues(digest.Canonical, cur.Config).String(),
},
},
Expand Down Expand Up @@ -539,6 +541,7 @@ func TestUnlock_withOCIDigest(t *testing.T) {
Annotations: map[string]string{
eventMetaGroupKey(metaOCIDigestKey): expected.OCIDigest,
eventMetaGroupKey(eventv1.MetaRevisionKey): rls.Chart.Metadata.Version,
eventMetaGroupKey(metaAppVersionKey): rls.Chart.Metadata.AppVersion,
eventMetaGroupKey(eventv1.MetaTokenKey): chartutil.DigestValues(digest.Canonical, rls.Config).String(),
},
},
Expand Down
4 changes: 2 additions & 2 deletions internal/reconcile/upgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ func (r *Upgrade) failure(req *Request, buffer *action.LogBuffer, err error) {
r.eventRecorder.AnnotatedEventf(
req.Object,
eventMeta(req.Chart.Metadata.Version, chartutil.DigestValues(digest.Canonical, req.Values).String(),
addOCIDigest(req.Object.Status.LastAttemptedRevisionDigest)),
addAppVersion(req.Chart.AppVersion()), addOCIDigest(req.Object.Status.LastAttemptedRevisionDigest)),
corev1.EventTypeWarning,
v2.UpgradeFailedReason,
eventMessageWithLog(msg, buffer),
Expand All @@ -172,7 +172,7 @@ func (r *Upgrade) success(req *Request) {
// Record event.
r.eventRecorder.AnnotatedEventf(
req.Object,
eventMeta(cur.ChartVersion, cur.ConfigDigest, addOCIDigest(cur.OCIDigest)),
eventMeta(cur.ChartVersion, cur.ConfigDigest, addAppVersion(cur.AppVersion), addOCIDigest(cur.OCIDigest)),
corev1.EventTypeNormal,
v2.UpgradeSucceededReason,
msg,
Expand Down
2 changes: 2 additions & 0 deletions internal/reconcile/upgrade_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -473,6 +473,7 @@ func TestUpgrade_failure(t *testing.T) {
Annotations: map[string]string{
eventMetaGroupKey(metaOCIDigestKey): obj.Status.LastAttemptedRevisionDigest,
eventMetaGroupKey(eventv1.MetaRevisionKey): chrt.Metadata.Version,
eventMetaGroupKey(metaAppVersionKey): chrt.Metadata.AppVersion,
eventMetaGroupKey(eventv1.MetaTokenKey): chartutil.DigestValues(digest.Canonical, req.Values).String(),
},
},
Expand Down Expand Up @@ -544,6 +545,7 @@ func TestUpgrade_success(t *testing.T) {
ObjectMeta: metav1.ObjectMeta{
Annotations: map[string]string{
eventMetaGroupKey(eventv1.MetaRevisionKey): obj.Status.History.Latest().ChartVersion,
eventMetaGroupKey(metaAppVersionKey): obj.Status.History.Latest().AppVersion,
eventMetaGroupKey(eventv1.MetaTokenKey): obj.Status.History.Latest().ConfigDigest,
},
},
Expand Down
1 change: 1 addition & 0 deletions internal/release/observation.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@ func ObservedToSnapshot(rls Observation) *v2.Snapshot {
Name: rls.Name,
Namespace: rls.Namespace,
Version: rls.Version,
AppVersion: rls.ChartMetadata.AppVersion,
ChartName: rls.ChartMetadata.Name,
ChartVersion: rls.ChartMetadata.Version,
ConfigDigest: chartutil.DigestValues(digest.Canonical, rls.Config).String(),
Expand Down
1 change: 1 addition & 0 deletions internal/testutil/mock_chart.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ func BuildChart(opts ...ChartOption) *helmchart.Chart {
APIVersion: "v1",
Name: "hello",
Version: "0.1.0",
AppVersion: "1.2.3",
},
// This adds a basic template and hooks.
Templates: []*helmchart.File{
Expand Down

0 comments on commit 7f78cdc

Please sign in to comment.