Skip to content

Commit

Permalink
add interface functions to rollback releases (#28)
Browse files Browse the repository at this point in the history
* add interface functions to rollback releases

* bump ci tools

* re-generate

* fix comment
  • Loading branch information
elenz97 authored Jun 22, 2021
1 parent d504b9f commit 98c47de
Show file tree
Hide file tree
Showing 7 changed files with 297 additions and 132 deletions.
10 changes: 5 additions & 5 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ jobs:
runs-on: ubuntu-latest
steps:

- name: Set up Go 1.15
- name: Set up Go 1.16
uses: actions/setup-go@v2
with:
go-version: 1.15
go-version: 1.16

- uses: actions/checkout@v2

Expand All @@ -19,7 +19,7 @@ jobs:
set -x
apt-get -y update && apt-get -y install git
cd /
go get -u github.com/golang/mock/mockgen@v1.5.0
go get -u github.com/golang/mock/mockgen@v1.6.0
cd -
go env
go generate ./...
Expand All @@ -34,13 +34,13 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
go: [ '1.15', '1.14' ]
go: [ '1.16', '1.15' ]

steps:
- uses: actions/checkout@v2

- name: Set up Go
uses: actions/setup-go@v1
uses: actions/setup-go@v2
with:
go-version: ${{ matrix.go }}

Expand Down
28 changes: 28 additions & 0 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,12 @@ func (c *HelmClient) GetRelease(name string) (*release.Release, error) {
return c.getRelease(name)
}

// RollbackRelease rollbacks a release to a specific version.
// Specifying '0' as the value for 'version' will result in a rollback to the previous release version.
func (c *HelmClient) RollbackRelease(spec *ChartSpec, version int) error {
return c.rollbackRelease(spec, version)
}

// DeleteChartFromCache deletes the provided chart from the client's cache
func (c *HelmClient) DeleteChartFromCache(spec *ChartSpec) error {
return c.deleteChartFromCache(spec)
Expand Down Expand Up @@ -616,6 +622,28 @@ func (c *HelmClient) getRelease(name string) (*release.Release, error) {
return getReleaseClient.Run(name)
}

func (c *HelmClient) rollbackRelease(spec *ChartSpec, version int) error {
client := action.NewRollback(c.ActionConfig)

mergeRollbackOptions(spec, client)

client.Version = version

return client.Run(spec.ReleaseName)
}

// mergeRollbackOptions merges values of the provided chart to helm rollback options used by the client
func mergeRollbackOptions(chartSpec *ChartSpec, rollbackOptions *action.Rollback) {
rollbackOptions.DisableHooks = chartSpec.DisableHooks
rollbackOptions.DryRun = chartSpec.DryRun
rollbackOptions.Timeout = chartSpec.Timeout
rollbackOptions.CleanupOnFail = chartSpec.CleanupOnFail
rollbackOptions.Force = chartSpec.Force
rollbackOptions.MaxHistory = chartSpec.MaxHistory
rollbackOptions.Recreate = chartSpec.Recreate
rollbackOptions.Wait = chartSpec.Wait
}

// mergeInstallOptions merges values of the provided chart to helm install options used by the client
func mergeInstallOptions(chartSpec *ChartSpec, installOptions *action.Install) {
installOptions.DisableHooks = chartSpec.DisableHooks
Expand Down
16 changes: 16 additions & 0 deletions client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,3 +155,19 @@ func ExampleHelmClient_GetRelease() {
panic(err)
}
}

func ExampleHelmClient_RollbackRelease() {
// Define the released chart to be installed
chartSpec := ChartSpec{
ReleaseName: "etcd-operator",
ChartName: "stable/etcd-operator",
Namespace: "default",
UpgradeCRDs: true,
Wait: true,
}

// Rollback to the previous version of the release by setting the release version to '0'.
if err := helmClient.RollbackRelease(&chartSpec, 0); err != nil {
return
}
}
14 changes: 7 additions & 7 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
module github.com/mittwald/go-helm-client

go 1.15
go 1.16

require (
github.com/golang/mock v1.4.4
github.com/golang/mock v1.6.0
github.com/spf13/pflag v1.0.5
helm.sh/helm/v3 v3.5.1
k8s.io/apiextensions-apiserver v0.20.1
k8s.io/apimachinery v0.20.1
k8s.io/cli-runtime v0.20.1
k8s.io/client-go v0.20.1
helm.sh/helm/v3 v3.6.1
k8s.io/apiextensions-apiserver v0.21.0
k8s.io/apimachinery v0.21.0
k8s.io/cli-runtime v0.21.0
k8s.io/client-go v0.21.0
rsc.io/letsencrypt v0.0.3 // indirect
sigs.k8s.io/yaml v1.2.0
)
Loading

0 comments on commit 98c47de

Please sign in to comment.