Skip to content

Commit 20a5c31

Browse files
Add a default value for MaxReleaseHistory (#291)
* default MaxReleaseHistory to ten * make default history nullable * rename maxHistory * add gomega pointto in tests --------- Co-authored-by: Joe Lanford <[email protected]>
1 parent 994b1d5 commit 20a5c31

File tree

3 files changed

+19
-9
lines changed

3 files changed

+19
-9
lines changed

pkg/reconciler/internal/values/values.go

+2
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ import (
2828
"github.com/operator-framework/helm-operator-plugins/pkg/values"
2929
)
3030

31+
var DefaultMaxReleaseHistory = 10
32+
3133
var DefaultMapper = values.MapperFunc(func(v chartutil.Values) chartutil.Values { return v })
3234

3335
var DefaultTranslator = values.TranslatorFunc(func(ctx context.Context, u *unstructured.Unstructured) (chartutil.Values, error) {

pkg/reconciler/reconciler.go

+14-7
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ type Reconciler struct {
7878
skipDependentWatches bool
7979
maxConcurrentReconciles int
8080
reconcilePeriod time.Duration
81-
maxHistory int
81+
maxReleaseHistory *int
8282
skipPrimaryGVKSchemeRegistration bool
8383
controllerSetupFuncs []ControllerSetupFunc
8484

@@ -347,13 +347,15 @@ func WithReconcilePeriod(rp time.Duration) Option {
347347
}
348348

349349
// WithMaxReleaseHistory specifies the maximum size of the Helm release history maintained
350-
// on upgrades/rollbacks. Zero (default) means unlimited.
350+
// on upgrades/rollbacks. Zero means unlimited.
351+
//
352+
// Defaults is 10
351353
func WithMaxReleaseHistory(maxHistory int) Option {
352354
return func(r *Reconciler) error {
353355
if maxHistory < 0 {
354356
return errors.New("maximum Helm release history size must not be negative")
355357
}
356-
r.maxHistory = maxHistory
358+
r.maxReleaseHistory = &maxHistory
357359
return nil
358360
}
359361
}
@@ -745,9 +747,9 @@ func (r *Reconciler) getReleaseState(client helmclient.ActionInterface, obj meta
745747
}
746748

747749
var opts []helmclient.UpgradeOption
748-
if r.maxHistory > 0 {
750+
if *r.maxReleaseHistory > 0 {
749751
opts = append(opts, func(u *action.Upgrade) error {
750-
u.MaxHistory = r.maxHistory
752+
u.MaxHistory = *r.maxReleaseHistory
751753
return nil
752754
})
753755
}
@@ -802,9 +804,9 @@ func (r *Reconciler) doInstall(actionClient helmclient.ActionInterface, u *updat
802804

803805
func (r *Reconciler) doUpgrade(actionClient helmclient.ActionInterface, u *updater.Updater, obj *unstructured.Unstructured, vals map[string]interface{}, log logr.Logger) (*release.Release, error) {
804806
var opts []helmclient.UpgradeOption
805-
if r.maxHistory > 0 {
807+
if *r.maxReleaseHistory > 0 {
806808
opts = append(opts, func(u *action.Upgrade) error {
807-
u.MaxHistory = r.maxHistory
809+
u.MaxHistory = *r.maxReleaseHistory
808810
return nil
809811
})
810812
}
@@ -936,6 +938,11 @@ func (r *Reconciler) addDefaults(mgr ctrl.Manager, controllerName string) error
936938
if r.valueMapper == nil {
937939
r.valueMapper = internalvalues.DefaultMapper
938940
}
941+
942+
if r.maxReleaseHistory == nil {
943+
r.maxReleaseHistory = &internalvalues.DefaultMaxReleaseHistory
944+
}
945+
939946
return nil
940947
}
941948

pkg/reconciler/reconciler_test.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import (
2727
"github.com/go-logr/logr"
2828
. "github.com/onsi/ginkgo/v2"
2929
. "github.com/onsi/gomega"
30+
. "github.com/onsi/gomega/gstruct"
3031
sdkhandler "github.com/operator-framework/operator-lib/handler"
3132
"helm.sh/helm/v3/pkg/action"
3233
"helm.sh/helm/v3/pkg/chart"
@@ -216,11 +217,11 @@ var _ = Describe("Reconciler", func() {
216217
_ = Describe("WithMaxReleaseHistory", func() {
217218
It("should set the max history size", func() {
218219
Expect(WithMaxReleaseHistory(10)(r)).To(Succeed())
219-
Expect(r.maxHistory).To(Equal(10))
220+
Expect(r.maxReleaseHistory).To(PointTo(Equal(10)))
220221
})
221222
It("should allow setting the history to unlimited", func() {
222223
Expect(WithMaxReleaseHistory(0)(r)).To(Succeed())
223-
Expect(r.maxHistory).To(Equal(0))
224+
Expect(r.maxReleaseHistory).To(PointTo(Equal(0)))
224225
})
225226
It("should fail if value is less than 0", func() {
226227
Expect(WithMaxReleaseHistory(-1)(r)).NotTo(Succeed())

0 commit comments

Comments
 (0)