Skip to content

Commit 1780093

Browse files
Allow limiting the maximum release history on upgrades (#14) (#107)
Co-authored-by: Malte Isberner <[email protected]>
1 parent 3c0ba19 commit 1780093

File tree

3 files changed

+39
-0
lines changed

3 files changed

+39
-0
lines changed

pkg/client/actionclient.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,7 @@ func (c *actionClient) Upgrade(name, namespace string, chrt *chart.Chart, vals m
163163
if rel != nil {
164164
rollback := action.NewRollback(c.conf)
165165
rollback.Force = true
166+
rollback.MaxHistory = upgrade.MaxHistory
166167

167168
// As of Helm 2.13, if Upgrade returns a non-nil release, that
168169
// means the release was also recorded in the release store.

pkg/reconciler/reconciler.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ type Reconciler struct {
7373
skipDependentWatches bool
7474
maxConcurrentReconciles int
7575
reconcilePeriod time.Duration
76+
maxHistory int
7677

7778
annotSetupOnce sync.Once
7879
annotations map[string]struct{}
@@ -280,6 +281,18 @@ func WithReconcilePeriod(rp time.Duration) Option {
280281
}
281282
}
282283

284+
// WithMaxReleaseHistory specifies the maximum size of the Helm release history maintained
285+
// on upgrades/rollbacks. Zero (default) means unlimited.
286+
func WithMaxReleaseHistory(maxHistory int) Option {
287+
return func(r *Reconciler) error {
288+
if maxHistory < 0 {
289+
return errors.New("maximum Helm release history size must not be negative")
290+
}
291+
r.maxHistory = maxHistory
292+
return nil
293+
}
294+
}
295+
283296
// WithInstallAnnotations is an Option that configures Install annotations
284297
// to enable custom action.Install fields to be set based on the value of
285298
// annotations found in the custom resource watched by this reconciler.
@@ -592,6 +605,12 @@ func (r *Reconciler) getReleaseState(client helmclient.ActionInterface, obj meta
592605
}
593606

594607
var opts []helmclient.UpgradeOption
608+
if r.maxHistory > 0 {
609+
opts = append(opts, func(u *action.Upgrade) error {
610+
u.MaxHistory = r.maxHistory
611+
return nil
612+
})
613+
}
595614
for name, annot := range r.upgradeAnnotations {
596615
if v, ok := obj.GetAnnotations()[name]; ok {
597616
opts = append(opts, annot.UpgradeOption(v))
@@ -636,6 +655,12 @@ func (r *Reconciler) doInstall(actionClient helmclient.ActionInterface, u *updat
636655

637656
func (r *Reconciler) doUpgrade(actionClient helmclient.ActionInterface, u *updater.Updater, obj *unstructured.Unstructured, vals map[string]interface{}, log logr.Logger) (*release.Release, error) {
638657
var opts []helmclient.UpgradeOption
658+
if r.maxHistory > 0 {
659+
opts = append(opts, func(u *action.Upgrade) error {
660+
u.MaxHistory = r.maxHistory
661+
return nil
662+
})
663+
}
639664
for name, annot := range r.upgradeAnnotations {
640665
if v, ok := obj.GetAnnotations()[name]; ok {
641666
opts = append(opts, annot.UpgradeOption(v))

pkg/reconciler/reconciler_test.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,19 @@ var _ = Describe("Reconciler", func() {
173173
Expect(WithReconcilePeriod(-time.Nanosecond)(r)).NotTo(Succeed())
174174
})
175175
})
176+
var _ = Describe("WithMaxReleaseHistory", func() {
177+
It("should set the max history size", func() {
178+
Expect(WithMaxReleaseHistory(10)(r)).To(Succeed())
179+
Expect(r.maxHistory).To(Equal(10))
180+
})
181+
It("should allow setting the history to unlimited", func() {
182+
Expect(WithMaxReleaseHistory(0)(r)).To(Succeed())
183+
Expect(r.maxHistory).To(Equal(0))
184+
})
185+
It("should fail if value is less than 0", func() {
186+
Expect(WithMaxReleaseHistory(-1)(r)).NotTo(Succeed())
187+
})
188+
})
176189
var _ = Describe("WithInstallAnnotations", func() {
177190
It("should set multiple reconciler install annotations", func() {
178191
a1 := annotation.InstallDisableHooks{CustomName: "my.domain/custom-name1"}

0 commit comments

Comments
 (0)