Skip to content

Allow limiting the maximum release history on upgrades #14

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 22, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions pkg/client/actionclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,7 @@ func (c *actionClient) Upgrade(name, namespace string, chrt *chart.Chart, vals m
if rel != nil {
rollback := action.NewRollback(c.conf)
rollback.Force = true
rollback.MaxHistory = upgrade.MaxHistory

// As of Helm 2.13, if Upgrade returns a non-nil release, that
// means the release was also recorded in the release store.
Expand Down
25 changes: 25 additions & 0 deletions pkg/reconciler/reconciler.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ type Reconciler struct {
skipDependentWatches bool
maxConcurrentReconciles int
reconcilePeriod time.Duration
maxHistory int

annotSetupOnce sync.Once
annotations map[string]struct{}
Expand Down Expand Up @@ -291,6 +292,18 @@ func WithReconcilePeriod(rp time.Duration) Option {
}
}

// WithMaxReleaseHistory specifies the maximum size of the Helm release history maintained
// on upgrades/rollbacks. Zero (default) means unlimited.
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
return nil
}
}

// WithInstallAnnotations is an Option that configures Install annotations
// to enable custom action.Install fields to be set based on the value of
// annotations found in the custom resource watched by this reconciler.
Expand Down Expand Up @@ -666,6 +679,12 @@ func (r *Reconciler) getReleaseState(client helmclient.ActionInterface, obj meta
}

var opts []helmclient.UpgradeOption
if r.maxHistory > 0 {
opts = append(opts, func(u *action.Upgrade) error {
u.MaxHistory = r.maxHistory
return nil
})
}
for name, annot := range r.upgradeAnnotations {
if v, ok := obj.GetAnnotations()[name]; ok {
opts = append(opts, annot.UpgradeOption(v))
Expand Down Expand Up @@ -710,6 +729,12 @@ 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 {
opts = append(opts, func(u *action.Upgrade) error {
u.MaxHistory = r.maxHistory
return nil
})
}
for name, annot := range r.upgradeAnnotations {
if v, ok := obj.GetAnnotations()[name]; ok {
opts = append(opts, annot.UpgradeOption(v))
Expand Down
13 changes: 13 additions & 0 deletions pkg/reconciler/reconciler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,19 @@ var _ = Describe("Reconciler", func() {
Expect(WithReconcilePeriod(-time.Nanosecond)(r)).NotTo(Succeed())
})
})
var _ = Describe("WithMaxReleaseHistory", func() {
It("should set the max history size", func() {
Expect(WithMaxReleaseHistory(10)(r)).To(Succeed())
Expect(r.maxHistory).To(Equal(10))
})
It("should allow setting the history to unlimited", func() {
Expect(WithMaxReleaseHistory(0)(r)).To(Succeed())
Expect(r.maxHistory).To(Equal(0))
})
It("should fail if value is less than 0", func() {
Expect(WithMaxReleaseHistory(-1)(r)).NotTo(Succeed())
})
})
var _ = Describe("WithInstallAnnotations", func() {
It("should set multiple reconciler install annotations", func() {
a1 := annotation.InstallDisableHooks{CustomName: "my.domain/custom-name1"}
Expand Down