Skip to content

Commit

Permalink
Keep only 1 historical helm release per app on cluster (#211)
Browse files Browse the repository at this point in the history
   Currently, we don't specify MaxHistory and a number of helm releases
keeps growing meaning ketch creates lots of secrets named
"sh.helm.release.v1.<app-name>.v<version>".
  • Loading branch information
aleksej-paschenko authored Dec 21, 2021
1 parent 4b2850a commit 009a4d6
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 5 deletions.
2 changes: 1 addition & 1 deletion ci/limits.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"github.com/theketchio/ketch/internal/api/v1beta1": 33.2,
"github.com/theketchio/ketch/internal/api/v1beta1/mocks": 0,
"github.com/theketchio/ketch/internal/build": 92.3,
"github.com/theketchio/ketch/internal/chart": 67.8,
"github.com/theketchio/ketch/internal/chart": 67.7,
"github.com/theketchio/ketch/internal/controllers": 55.7,
"github.com/theketchio/ketch/internal/deploy": 26.5,
"github.com/theketchio/ketch/internal/errors": 50,
Expand Down
16 changes: 12 additions & 4 deletions internal/chart/helm_client.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
package chart

import (
"errors"
"log"
"os"

"helm.sh/helm/v3/pkg/action"
"helm.sh/helm/v3/pkg/chart/loader"
"helm.sh/helm/v3/pkg/release"
"helm.sh/helm/v3/pkg/storage/driver"
"k8s.io/cli-runtime/pkg/genericclioptions"
"log"
"os"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
)
Expand Down Expand Up @@ -73,7 +76,7 @@ func (c HelmClient) UpdateChart(tv TemplateValuer, config ChartConfig, opts ...I
getValuesClient := action.NewGetValues(c.cfg)
getValuesClient.AllValues = true
_, err = getValuesClient.Run(appName)
if err != nil && err.Error() == "release: not found" {
if err != nil && errors.Is(err, driver.ErrReleaseNotFound) {
clientInstall := action.NewInstall(c.cfg)
clientInstall.ReleaseName = appName
clientInstall.Namespace = c.namespace
Expand All @@ -91,6 +94,11 @@ func (c HelmClient) UpdateChart(tv TemplateValuer, config ChartConfig, opts ...I
}
updateClient := action.NewUpgrade(c.cfg)
updateClient.Namespace = c.namespace

// MaxHistory specifies the maximum number of historical releases that will be retained, including the most recent release.
// Values of 0 or less are ignored (meaning no limits are imposed).
// Let's set it to minimal to disable "helm rollback".
updateClient.MaxHistory = 1
updateClient.PostRenderer = &postRender{
namespace: c.namespace,
cli: c.c,
Expand All @@ -102,7 +110,7 @@ func (c HelmClient) UpdateChart(tv TemplateValuer, config ChartConfig, opts ...I
func (c HelmClient) DeleteChart(appName string) error {
uninstall := action.NewUninstall(c.cfg)
_, err := uninstall.Run(appName)
if err != nil && err.Error() == "release: not found" {
if err != nil && errors.Is(err, driver.ErrReleaseNotFound) {
return nil
}
return err
Expand Down

0 comments on commit 009a4d6

Please sign in to comment.