Skip to content
This repository has been archived by the owner on Mar 9, 2022. It is now read-only.

Support version issue #50 #64

Merged
merged 7 commits into from
Apr 3, 2020
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
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
/bin
/helm
*-packr.go

.vscode/launch.json
11 changes: 10 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,16 @@ appropriate helm command based on which action it is included within: `install`,
porter mixin install helm
```

### Configure mixin repositories
### Mixin Configuration

Helm client

```yaml
- helm:
clientVersion: v2.15.2
```

Repositories

```yaml
- helm:
Expand Down
7 changes: 5 additions & 2 deletions examples/mysql/porter.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
mixins:
- helm
- helm:
clientVersion: v2.15.2
repositories:
stable:
url: "https://kubernetes-charts.storage.googleapis.com"

name: helm-mysql
version: 0.1.0
Expand Down Expand Up @@ -87,4 +91,3 @@ outputs:
- install
- upgrade
sensitive: true

13 changes: 8 additions & 5 deletions pkg/helm/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
)

// These values may be referenced elsewhere (init.go), hence consts
const helmClientVersion string = "v2.15.2"
const helmArchiveTmpl string = "helm-%s-linux-amd64.tar.gz"
const helmDownloadURLTmpl string = "https://get.helm.sh/%s"

Expand All @@ -22,9 +21,6 @@ const getHelm string = `RUN apt-get update && \
RUN helm init --client-only
`

var helmArchiveVersion = fmt.Sprintf(helmArchiveTmpl, helmClientVersion)
var helmDownloadURL = fmt.Sprintf(helmDownloadURLTmpl, helmArchiveVersion)

// kubectl may be necessary; for example, to set up RBAC for Helm's Tiller component if needed
const kubeVersion string = "v1.15.3"
const getKubectl string = `RUN apt-get update && \
Expand All @@ -50,7 +46,8 @@ type BuildInput struct {
// username: "username"
// password: "password"
type MixinConfig struct {
Repositories map[string]Repository
ClientVersion string `yaml:"clientVersion,omitempty"`
Repositories map[string]Repository
}

type Repository struct {
Expand All @@ -73,6 +70,12 @@ func (m *Mixin) Build() error {
if err != nil {
return err
}
if input.Config.ClientVersion != "" {
m.HelmClientVersion = input.Config.ClientVersion
}

var helmArchiveVersion = fmt.Sprintf(helmArchiveTmpl, m.HelmClientVersion)
var helmDownloadURL = fmt.Sprintf(helmDownloadURLTmpl, helmArchiveVersion)

// Define helm
fmt.Fprintf(m.Out, getHelm, helmDownloadURL)
Expand Down
25 changes: 20 additions & 5 deletions pkg/helm/build_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package helm

import (
"bytes"
"fmt"
"io/ioutil"
"testing"

Expand All @@ -17,7 +18,7 @@ func TestMixin_Build(t *testing.T) {

buildOutput := `RUN apt-get update && \
apt-get install -y curl && \
curl -o helm.tgz https://get.helm.sh/helm-v2.15.2-linux-amd64.tar.gz && \
curl -o helm.tgz https://get.helm.sh/helm-%s-linux-amd64.tar.gz && \
tar -xzf helm.tgz && \
mv linux-amd64/helm /usr/local/bin && \
rm helm.tgz
Expand All @@ -38,9 +39,7 @@ RUN apt-get update && \

err = m.Build()
require.NoError(t, err, "build failed")

wantOutput := buildOutput + "\nRUN helm repo add stable kubernetes-charts --username username --password password"

wantOutput := fmt.Sprintf(buildOutput, m.HelmClientVersion) + "\nRUN helm repo add stable kubernetes-charts --username username --password password"
gotOutput := m.TestContext.GetOutput()
assert.Equal(t, wantOutput, gotOutput)
})
Expand All @@ -55,7 +54,23 @@ RUN apt-get update && \

err = m.Build()
require.NoError(t, err, "build failed")
wantOutput := fmt.Sprintf(buildOutput, m.HelmClientVersion)
gotOutput := m.TestContext.GetOutput()
assert.Equal(t, buildOutput, gotOutput)
assert.Equal(t, wantOutput, gotOutput)
})

t.Run("build with a defined helm client version", func(t *testing.T) {

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

m := NewTestMixin(t)
m.Debug = false
m.In = bytes.NewReader(b)
err = m.Build()
require.NoError(t, err, "build failed")
wantOutput := fmt.Sprintf(buildOutput, m.HelmClientVersion)
gotOutput := m.TestContext.GetOutput()
assert.Equal(t, wantOutput, gotOutput)
})
}
12 changes: 8 additions & 4 deletions pkg/helm/helm.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,25 @@ import (
k8s "k8s.io/client-go/kubernetes"
)

const defaultHelmClientVersion string = "v2.15.2"

// Helm is the logic behind the helm mixin
type Mixin struct {
*context.Context
schema *packr.Box
ClientFactory kubernetes.ClientFactory
TillerIniter
HelmClientVersion string
}

// New helm mixin client, initialized with useful defaults.
func New() *Mixin {
return &Mixin{
schema: packr.New("schema", "./schema"),
Context: context.New(),
ClientFactory: kubernetes.New(),
TillerIniter: RealTillerIniter{},
schema: packr.New("schema", "./schema"),
Context: context.New(),
ClientFactory: kubernetes.New(),
TillerIniter: RealTillerIniter{},
HelmClientVersion: defaultHelmClientVersion,
}
}

Expand Down
5 changes: 4 additions & 1 deletion pkg/helm/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import (
testclient "k8s.io/client-go/kubernetes/fake"
)

const MockHelmClientVersion string = "v2.15.2"

type TestMixin struct {
*Mixin
TestContext *context.TestContext
Expand Down Expand Up @@ -47,7 +49,7 @@ func (t MockTillerIniter) installHelmClient(m *Mixin, version string) error {
func NewMockTillerIniter() MockTillerIniter {
return MockTillerIniter{
GetTillerVersion: func(m *Mixin) (string, error) {
return helmClientVersion, nil
return MockHelmClientVersion, nil
},
SetupTillerRBAC: func(m *Mixin) error {
return nil
Expand All @@ -68,6 +70,7 @@ func NewTestMixin(t *testing.T) *TestMixin {
m.Context = c.Context
m.ClientFactory = &testKubernetesFactory{}
m.TillerIniter = NewMockTillerIniter()
m.HelmClientVersion = MockHelmClientVersion
return &TestMixin{
Mixin: m,
TestContext: c,
Expand Down
4 changes: 2 additions & 2 deletions pkg/helm/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,9 @@ func (m *Mixin) Init() error {
return errors.Wrap(err, "unable to communicate with Tiller")
}
} else {
if helmClientVersion != tillerVersion {
if m.HelmClientVersion != tillerVersion {
fmt.Fprintf(m.Out, "Tiller version (%s) does not match client version (%s); downloading a compatible client.\n",
tillerVersion, helmClientVersion)
tillerVersion, m.HelmClientVersion)

err := ti.installHelmClient(m, tillerVersion)
if err != nil {
Expand Down
5 changes: 3 additions & 2 deletions pkg/helm/init_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package helm

import (
"fmt"
"os"
"testing"

Expand Down Expand Up @@ -62,7 +63,7 @@ func TestMixin_Init_MismatchedVersion(t *testing.T) {
require.NoError(t, err)

gotOutput := h.TestContext.GetOutput()
wantOutput := "Tiller version (mismatchedVersion) does not match client version (v2.15.2); downloading a compatible client.\n"
wantOutput := fmt.Sprintf("Tiller version (mismatchedVersion) does not match client version (%s); downloading a compatible client.\n", h.HelmClientVersion)
require.Equal(t, wantOutput, gotOutput)
}

Expand All @@ -82,6 +83,6 @@ func TestMixin_Init_FailedClientInstall(t *testing.T) {
require.EqualError(t, err, "unable to install a compatible helm client: failed to install helm client")

gotOutput := h.TestContext.GetOutput()
wantOutput := "Tiller version (mismatchedVersion) does not match client version (v2.15.2); downloading a compatible client.\n"
wantOutput := fmt.Sprintf("Tiller version (mismatchedVersion) does not match client version (%s); downloading a compatible client.\n", h.HelmClientVersion)
require.Equal(t, wantOutput, gotOutput)
}
8 changes: 8 additions & 0 deletions pkg/helm/testdata/build-input-with-version.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
config:
version: v2.16.1
install:
- helm:
description: "Install MySQL"
name: porter-ci-mysql
chart: stable/mysql
version: 0.10.2