Skip to content

Commit 4a8d2ff

Browse files
committed
action: provide reason for failures count reset
Signed-off-by: Hidde Beydals <hidde@hhh.computer>
1 parent 3ce6e8d commit 4a8d2ff

File tree

3 files changed

+41
-24
lines changed

3 files changed

+41
-24
lines changed

internal/action/reset.go

+19-11
Original file line numberDiff line numberDiff line change
@@ -25,25 +25,33 @@ import (
2525
intchartutil "github.com/fluxcd/helm-controller/internal/chartutil"
2626
)
2727

28-
// MustResetFailures returns true if the HelmRelease's status indicates that
29-
// the HelmRelease failure counters must be reset. This is the case if the
30-
// data used to make the last (failed) attempt has changed in a way that
31-
// indicates that a new attempt should be made. For example, a change in
32-
// generation, chart version, or values.
33-
func MustResetFailures(obj *v2.HelmRelease, chart *chart.Metadata, values chartutil.Values) bool {
28+
const (
29+
differentGenerationReason = "generation differs from last attempt"
30+
differentRevisionReason = "chart version differs from last attempt"
31+
differentValuesReason = "values differ from last attempt"
32+
)
33+
34+
// MustResetFailures returns a reason and true if the HelmRelease's status
35+
// indicates that the HelmRelease failure counters must be reset.
36+
// This is the case if the data used to make the last (failed) attempt has
37+
// changed in a way that indicates that a new attempt should be made.
38+
// For example, a change in generation, chart version, or values.
39+
// If no change is detected, an empty string is returned along with false.
40+
func MustResetFailures(obj *v2.HelmRelease, chart *chart.Metadata, values chartutil.Values) (string, bool) {
3441
switch {
3542
case obj.Status.LastAttemptedGeneration != obj.Generation:
36-
return true
43+
return differentGenerationReason, true
3744
case obj.Status.LastAttemptedRevision != chart.Version:
38-
return true
45+
return differentRevisionReason, true
3946
case obj.Status.LastAttemptedConfigDigest != "" || obj.Status.LastAttemptedValuesChecksum != "":
4047
d := obj.Status.LastAttemptedConfigDigest
4148
if d == "" {
4249
// TODO: remove this when the deprecated field is removed.
4350
d = "sha1:" + obj.Status.LastAttemptedValuesChecksum
4451
}
45-
return !intchartutil.VerifyValues(digest.Digest(d), values)
46-
default:
47-
return false
52+
if ok := intchartutil.VerifyValues(digest.Digest(d), values); !ok {
53+
return differentValuesReason, true
54+
}
4855
}
56+
return "", false
4957
}

internal/action/reset_test.go

+20-12
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ package action
1919
import (
2020
"testing"
2121

22+
. "github.com/onsi/gomega"
2223
"helm.sh/helm/v3/pkg/chart"
2324
"helm.sh/helm/v3/pkg/chartutil"
2425
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
@@ -28,11 +29,12 @@ import (
2829

2930
func TestMustResetFailures(t *testing.T) {
3031
tests := []struct {
31-
name string
32-
obj *v2.HelmRelease
33-
chart *chart.Metadata
34-
values chartutil.Values
35-
want bool
32+
name string
33+
obj *v2.HelmRelease
34+
chart *chart.Metadata
35+
values chartutil.Values
36+
want bool
37+
wantReason string
3638
}{
3739
{
3840
name: "on generation change",
@@ -44,7 +46,8 @@ func TestMustResetFailures(t *testing.T) {
4446
LastAttemptedGeneration: 2,
4547
},
4648
},
47-
want: true,
49+
want: true,
50+
wantReason: differentGenerationReason,
4851
},
4952
{
5053
name: "on revision change",
@@ -60,7 +63,8 @@ func TestMustResetFailures(t *testing.T) {
6063
chart: &chart.Metadata{
6164
Version: "1.1.0",
6265
},
63-
want: true,
66+
want: true,
67+
wantReason: differentRevisionReason,
6468
},
6569
{
6670
name: "on config digest change",
@@ -80,7 +84,8 @@ func TestMustResetFailures(t *testing.T) {
8084
values: chartutil.Values{
8185
"foo": "bar",
8286
},
83-
want: true,
87+
want: true,
88+
wantReason: differentValuesReason,
8489
},
8590
{
8691
name: "on (deprecated) values checksum change",
@@ -100,7 +105,8 @@ func TestMustResetFailures(t *testing.T) {
100105
values: chartutil.Values{
101106
"foo": "bar",
102107
},
103-
want: true,
108+
want: true,
109+
wantReason: differentValuesReason,
104110
},
105111
{
106112
name: "without change no reset",
@@ -125,9 +131,11 @@ func TestMustResetFailures(t *testing.T) {
125131
}
126132
for _, tt := range tests {
127133
t.Run(tt.name, func(t *testing.T) {
128-
if got := MustResetFailures(tt.obj, tt.chart, tt.values); got != tt.want {
129-
t.Errorf("MustResetFailures() = %v, want %v", got, tt.want)
130-
}
134+
g := NewWithT(t)
135+
136+
reason, got := MustResetFailures(tt.obj, tt.chart, tt.values)
137+
g.Expect(got).To(Equal(tt.want))
138+
g.Expect(reason).To(Equal(tt.wantReason))
131139
})
132140
}
133141
}

internal/controller/helmrelease_controller.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -321,7 +321,8 @@ func (r *HelmReleaseReconciler) reconcileRelease(ctx context.Context, patchHelpe
321321
obj.Status.StorageNamespace = obj.GetStorageNamespace()
322322

323323
// Reset the failure count if the chart or values have changed.
324-
if action.MustResetFailures(obj, loadedChart.Metadata, values) {
324+
if reason, ok := action.MustResetFailures(obj, loadedChart.Metadata, values); ok {
325+
log.V(logger.DebugLevel).Info(fmt.Sprintf("resetting failure count (%s)", reason))
325326
obj.Status.ClearFailures()
326327
}
327328

0 commit comments

Comments
 (0)