Skip to content

Commit

Permalink
Merge pull request #6 from MChorfa/develop
Browse files Browse the repository at this point in the history
Fix issues with clientVersion
  • Loading branch information
MChorfa authored May 13, 2020
2 parents 8e58a93 + 46f5564 commit a7781be
Show file tree
Hide file tree
Showing 12 changed files with 156 additions and 79 deletions.
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ build-client: generate
$(GO) build -ldflags '$(LDFLAGS)' -o $(BINDIR)/$(MIXIN)$(FILE_EXT) ./cmd/$(MIXIN)

generate: packr2
$(GO) mod tidy
$(GO) generate ./...

HAS_PACKR2 := $(shell command -v packr2)
Expand Down
140 changes: 70 additions & 70 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ Helm client

```yaml
- helm3:
clientVersion: v3.1.2
clientVersion: v3.2.1
```
Repositories
Expand All @@ -41,48 +41,48 @@ Install
```yaml
install:
- helm3:
description: "Description of the command"
name: RELEASE_NAME
chart: STABLE_CHART_NAME
version: CHART_VERSION
namespace: NAMESPACE
replace: BOOL
devel: BOOL
wait: BOOL # default true
set:
VAR1: VALUE1
VAR2: VALUE2
- helm3:
description: "Description of the command"
name: RELEASE_NAME
chart: STABLE_CHART_NAME
version: CHART_VERSION
namespace: NAMESPACE
replace: BOOL
devel: BOOL
wait: BOOL # default true
set:
VAR1: VALUE1
VAR2: VALUE2
```
Upgrade
```yaml
install:
- helm3:
description: "Description of the command"
name: RELEASE_NAME
chart: STABLE_CHART_NAME
version: CHART_VERSION
namespace: NAMESPACE
resetValues: BOOL
reuseValues: BOOL
wait: BOOL # default true
set:
VAR1: VALUE1
VAR2: VALUE2
- helm3:
description: "Description of the command"
name: RELEASE_NAME
chart: STABLE_CHART_NAME
version: CHART_VERSION
namespace: NAMESPACE
resetValues: BOOL
reuseValues: BOOL
wait: BOOL # default true
set:
VAR1: VALUE1
VAR2: VALUE2
```
Uninstall
```yaml
uninstall:
- helm3:
description: "Description of command"
namespace: NAMESPACE
releases:
- RELEASE_NAME1
- RELASE_NAME2
- helm3:
description: "Description of command"
namespace: NAMESPACE
releases:
- RELEASE_NAME1
- RELASE_NAME2
```
#### Outputs
Expand All @@ -91,9 +91,9 @@ The mixin supports saving secrets from Kuberentes as outputs.
```yaml
outputs:
- name: NAME
secret: SECRET_NAME
key: SECRET_KEY
- name: NAME
secret: SECRET_NAME
key: SECRET_KEY
```
### Examples
Expand All @@ -102,51 +102,51 @@ Install
```yaml
install:
- helm3:
description: "Install MySQL"
name: mydb
chart: stable/mysql
version: 0.10.2
namespace: mydb
replace: true
set:
mysqlDatabase: wordpress
mysqlUser: wordpress
outputs:
- name: mysql-root-password
secret: mydb-mysql
key: mysql-root-password
- name: mysql-password
secret: mydb-mysql
key: mysql-password
- helm3:
description: "Install MySQL"
name: mydb
chart: stable/mysql
version: 0.10.2
namespace: mydb
replace: true
set:
mysqlDatabase: wordpress
mysqlUser: wordpress
outputs:
- name: mysql-root-password
secret: mydb-mysql
key: mysql-root-password
- name: mysql-password
secret: mydb-mysql
key: mysql-password
```
Upgrade
```yaml
upgrade:
- helm3:
description: "Upgrade MySQL"
name: porter-ci-mysql
chart: stable/mysql
version: 0.10.2
wait: true
resetValues: true
reuseValues: false
set:
mysqlDatabase: mydb
mysqlUser: myuser
livenessProbe.initialDelaySeconds: 30
persistence.enabled: true
- helm3:
description: "Upgrade MySQL"
name: porter-ci-mysql
chart: stable/mysql
version: 0.10.2
wait: true
resetValues: true
reuseValues: false
set:
mysqlDatabase: mydb
mysqlUser: myuser
livenessProbe.initialDelaySeconds: 30
persistence.enabled: true
```
Uninstall
```yaml
uninstall:
- helm3:
description: "Uninstall MySQL"
namespace: mydb
releases:
- mydb
```
- helm3:
description: "Uninstall MySQL"
namespace: mydb
releases:
- mydb
```
4 changes: 2 additions & 2 deletions example/porter.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
mixins:
- helm3:
clientVersion: v3.1.2
clientVersion: v3.2.1
repositories:
stable:
url: "https://kubernetes-charts.storage.googleapis.com"
Expand Down Expand Up @@ -79,7 +79,7 @@ upgrade:
uninstall:
- helm3:
description: "Uninstall MySQL"
purge: true
namespace: "{{ bundle.parameters.namespace }}"
releases:
- "{{ bundle.parameters.mysql-name }}"

Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ require (
cloud.google.com/go v0.55.0 // indirect
get.porter.sh/porter v0.23.0-beta.1
github.com/Azure/go-autorest/autorest v0.10.0 // indirect
github.com/Masterminds/semver v1.5.0
github.com/PaesslerAG/gval v1.0.1 // indirect
github.com/PuerkitoBio/goquery v1.5.1 // indirect
github.com/ghodss/yaml v1.0.0
Expand Down
35 changes: 31 additions & 4 deletions pkg/helm3/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,14 @@ import (
"strings"

"get.porter.sh/porter/pkg/exec/builder"
"github.com/Masterminds/semver"
"github.com/pkg/errors"
yaml "gopkg.in/yaml.v2"
)

// These values may be referenced elsewhere (init.go), hence consts
var helmClientVersion string
// clientVersionConstraint represents the semver constraint for the Helm client version
// Currently, this mixin only supports Helm clients versioned v2.x.x
const clientVersionConstraint string = "^v3.x"

// BuildInput represents stdin passed to the mixin for the build command.
type BuildInput struct {
Expand Down Expand Up @@ -55,8 +58,17 @@ func (m *Mixin) Build() error {
return err
}

if input.Config.ClientVersion != "" {
m.HelmClientVersion = input.Config.ClientVersion
suppliedClientVersion := input.Config.ClientVersion
if suppliedClientVersion != "" {
ok, err := validate(suppliedClientVersion, clientVersionConstraint)
if err != nil {
return err
}
if !ok {
return errors.Errorf("supplied clientVersion %q does not meet semver constraint %q",
suppliedClientVersion, clientVersionConstraint)
}
m.HelmClientVersion = suppliedClientVersion
}

// Install helm3
Expand Down Expand Up @@ -100,3 +112,18 @@ func GetAddRepositoryCommand(name, url, cafile, certfile, keyfile, username, pas

return commandBuilder, nil
}

// validate validates that the supplied clientVersion meets the supplied semver constraint
func validate(clientVersion, constraint string) (bool, error) {
c, err := semver.NewConstraint(constraint)
if err != nil {
return false, errors.Wrapf(err, "unable to parse version constraint %q", constraint)
}

v, err := semver.NewVersion(clientVersion)
if err != nil {
return false, errors.Wrapf(err, "supplied client version %q cannot be parsed as semver", clientVersion)
}

return c.Check(v), nil
}
24 changes: 24 additions & 0 deletions pkg/helm3/build_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,4 +67,28 @@ RUN mv linux-amd64/helm /usr/local/bin/helm3`
gotOutput := m.TestContext.GetOutput()
assert.Equal(t, wantOutput, gotOutput)
})

t.Run("build with a defined helm client version that does not meet the semver constraint", func(t *testing.T) {

b, err := ioutil.ReadFile("testdata/build-input-with-unsupported-client-version.yaml")
require.NoError(t, err)

m := NewTestMixin(t)
m.Debug = false
m.In = bytes.NewReader(b)
err = m.Build()
require.EqualError(t, err, `supplied clientVersion "v2.16.1" does not meet semver constraint "^v3.x"`)
})

t.Run("build with a defined helm client version that does not parse as valid semver", func(t *testing.T) {

b, err := ioutil.ReadFile("testdata/build-input-with-invalid-client-version.yaml")
require.NoError(t, err)

m := NewTestMixin(t)
m.Debug = false
m.In = bytes.NewReader(b)
err = m.Build()
require.EqualError(t, err, `supplied client version "v3.2.1.0" cannot be parsed as semver: Invalid Semantic Version`)
})
}
2 changes: 1 addition & 1 deletion pkg/helm3/helm3.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import (
k8s "k8s.io/client-go/kubernetes"
)

const defaultHelmClientVersion string = "v3.1.2"
const defaultHelmClientVersion string = "v3.2.1"

// Helm is the logic behind the helm mixin
type Mixin struct {
Expand Down
2 changes: 1 addition & 1 deletion pkg/helm3/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
testclient "k8s.io/client-go/kubernetes/fake"
)

const MockHelmClientVersion string = "v3.1.2"
const MockHelmClientVersion string = "v3.2.1"

type TestMixin struct {
*Mixin
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
config:
clientVersion: v3.2.1.0
install:
- helm3:
description: "Install MySQL"
name: porter-ci-mysql
chart: stable/mysql
version: 0.10.2
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
config:
clientVersion: v3.2.1
install:
- helm3:
description: "Install MySQL"
name: porter-ci-mysql
chart: stable/mysql
version: 0.10.2
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
config:
clientVersion: v2.16.1
install:
- helm3:
description: "Install MySQL"
name: porter-ci-mysql
chart: stable/mysql
version: 0.10.2
2 changes: 1 addition & 1 deletion pkg/helm3/testdata/build-input-with-version.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
config:
version: v3.1.2
version: v3.2.1
install:
- helm3:
description: "Install MySQL"
Expand Down

0 comments on commit a7781be

Please sign in to comment.