diff --git a/pkg/reconciler/internal/values/values.go b/pkg/reconciler/internal/values/values.go index 28d23438..cddcd0e6 100644 --- a/pkg/reconciler/internal/values/values.go +++ b/pkg/reconciler/internal/values/values.go @@ -28,6 +28,8 @@ import ( "github.com/operator-framework/helm-operator-plugins/pkg/values" ) +var DefaultMaxReleaseHistory = 10 + var DefaultMapper = values.MapperFunc(func(v chartutil.Values) chartutil.Values { return v }) var DefaultTranslator = values.TranslatorFunc(func(ctx context.Context, u *unstructured.Unstructured) (chartutil.Values, error) { diff --git a/pkg/reconciler/reconciler.go b/pkg/reconciler/reconciler.go index a72df23d..595d0583 100644 --- a/pkg/reconciler/reconciler.go +++ b/pkg/reconciler/reconciler.go @@ -78,7 +78,7 @@ type Reconciler struct { skipDependentWatches bool maxConcurrentReconciles int reconcilePeriod time.Duration - maxHistory int + maxReleaseHistory *int skipPrimaryGVKSchemeRegistration bool controllerSetupFuncs []ControllerSetupFunc @@ -347,13 +347,15 @@ func WithReconcilePeriod(rp time.Duration) Option { } // WithMaxReleaseHistory specifies the maximum size of the Helm release history maintained -// on upgrades/rollbacks. Zero (default) means unlimited. +// on upgrades/rollbacks. Zero means unlimited. +// +// Defaults is 10 func WithMaxReleaseHistory(maxHistory int) Option { return func(r *Reconciler) error { if maxHistory < 0 { return errors.New("maximum Helm release history size must not be negative") } - r.maxHistory = maxHistory + r.maxReleaseHistory = &maxHistory return nil } } @@ -745,9 +747,9 @@ func (r *Reconciler) getReleaseState(client helmclient.ActionInterface, obj meta } var opts []helmclient.UpgradeOption - if r.maxHistory > 0 { + if *r.maxReleaseHistory > 0 { opts = append(opts, func(u *action.Upgrade) error { - u.MaxHistory = r.maxHistory + u.MaxHistory = *r.maxReleaseHistory return nil }) } @@ -801,9 +803,9 @@ func (r *Reconciler) doInstall(actionClient helmclient.ActionInterface, u *updat func (r *Reconciler) doUpgrade(actionClient helmclient.ActionInterface, u *updater.Updater, obj *unstructured.Unstructured, vals map[string]interface{}, log logr.Logger) (*release.Release, error) { var opts []helmclient.UpgradeOption - if r.maxHistory > 0 { + if *r.maxReleaseHistory > 0 { opts = append(opts, func(u *action.Upgrade) error { - u.MaxHistory = r.maxHistory + u.MaxHistory = *r.maxReleaseHistory return nil }) } @@ -935,6 +937,11 @@ func (r *Reconciler) addDefaults(mgr ctrl.Manager, controllerName string) error if r.valueMapper == nil { r.valueMapper = internalvalues.DefaultMapper } + + if r.maxReleaseHistory == nil { + r.maxReleaseHistory = &internalvalues.DefaultMaxReleaseHistory + } + return nil } diff --git a/pkg/reconciler/reconciler_test.go b/pkg/reconciler/reconciler_test.go index b0143975..f2ff8508 100644 --- a/pkg/reconciler/reconciler_test.go +++ b/pkg/reconciler/reconciler_test.go @@ -27,6 +27,7 @@ import ( "github.com/go-logr/logr" . "github.com/onsi/ginkgo/v2" . "github.com/onsi/gomega" + . "github.com/onsi/gomega/gstruct" sdkhandler "github.com/operator-framework/operator-lib/handler" "helm.sh/helm/v3/pkg/action" "helm.sh/helm/v3/pkg/chart" @@ -216,11 +217,11 @@ var _ = Describe("Reconciler", func() { _ = Describe("WithMaxReleaseHistory", func() { It("should set the max history size", func() { Expect(WithMaxReleaseHistory(10)(r)).To(Succeed()) - Expect(r.maxHistory).To(Equal(10)) + Expect(r.maxReleaseHistory).To(PointTo(Equal(10))) }) It("should allow setting the history to unlimited", func() { Expect(WithMaxReleaseHistory(0)(r)).To(Succeed()) - Expect(r.maxHistory).To(Equal(0)) + Expect(r.maxReleaseHistory).To(PointTo(Equal(0))) }) It("should fail if value is less than 0", func() { Expect(WithMaxReleaseHistory(-1)(r)).NotTo(Succeed())