Add the option to specify an alternative helm binary (#181)

`helm` may not be version compatible. Add an option to specify an alternative `helm` command on runtime, possibly according to the version of tiller running on your cluster.
This commit is contained in:
Hori Ryota 2018-07-03 22:19:02 +09:00 committed by KUOKA Yusuke
parent 88e84e5ae8
commit 9f57f8be7d
5 changed files with 57 additions and 2 deletions

View File

@ -11,6 +11,7 @@ const (
)
type execer struct {
helmBinary string
runner Runner
writer io.Writer
kubeContext string
@ -20,6 +21,7 @@ type execer struct {
// New for running helm commands
func New(writer io.Writer, kubeContext string) *execer {
return &execer{
helmBinary: command,
writer: writer,
kubeContext: kubeContext,
runner: &ShellRunner{},
@ -30,6 +32,10 @@ func (helm *execer) SetExtraArgs(args ...string) {
helm.extra = args
}
func (helm *execer) SetHelmBinary(bin string) {
helm.helmBinary = bin
}
func (helm *execer) AddRepo(name, repository, certfile, keyfile, username, password string) error {
var args []string
args = append(args, "repo", "add", name, repository)
@ -114,8 +120,8 @@ func (helm *execer) exec(args ...string) ([]byte, error) {
if helm.kubeContext != "" {
cmdargs = append(cmdargs, "--kube-context", helm.kubeContext)
}
helm.write([]byte(fmt.Sprintf("exec: helm %s\n", strings.Join(cmdargs, " "))))
return helm.runner.Execute(command, cmdargs)
helm.write([]byte(fmt.Sprintf("exec: %s %s\n", helm.helmBinary, strings.Join(cmdargs, " "))))
return helm.runner.Execute(helm.helmBinary, cmdargs)
}
func (helm *execer) write(out []byte) {

View File

@ -56,6 +56,17 @@ func Test_SetExtraArgs(t *testing.T) {
}
}
func Test_SetHelmBinary(t *testing.T) {
helm := New(new(bytes.Buffer), "dev")
if helm.helmBinary != "helm" {
t.Error("helmexec.command - default command is not helm")
}
helm.SetHelmBinary("foo")
if helm.helmBinary != "foo" {
t.Errorf("helmexec.SetHelmBinary() - actual = %s expect = foo", helm.helmBinary)
}
}
func Test_AddRepo(t *testing.T) {
var buffer bytes.Buffer
helm := MockExecer(&buffer, "dev")
@ -237,6 +248,15 @@ func Test_exec(t *testing.T) {
if buffer.String() != expected {
t.Errorf("helmexec.exec()\nactual = %v\nexpect = %v", buffer.String(), expected)
}
buffer.Reset()
helm = MockExecer(&buffer, "")
helm.SetHelmBinary("overwritten")
helm.exec("version")
expected = "exec: overwritten version\n"
if buffer.String() != expected {
t.Errorf("helmexec.exec()\nactual = %v\nexpect = %v", buffer.String(), expected)
}
}
func Test_Lint(t *testing.T) {

View File

@ -3,6 +3,7 @@ package helmexec
// Interface for executing helm commands
type Interface interface {
SetExtraArgs(args ...string)
SetHelmBinary(bin string)
AddRepo(name, repository, certfile, keyfile, username, password string) error
UpdateRepo() error

25
main.go
View File

@ -31,6 +31,10 @@ func main() {
app.Usage = ""
app.Version = Version
app.Flags = []cli.Flag{
cli.StringFlag{
Name: "helm-binary, b",
Usage: "path to helm binary",
},
cli.StringFlag{
Name: "file, f",
Value: DefaultHelmfile,
@ -74,6 +78,9 @@ func main() {
if len(args) > 0 {
helm.SetExtraArgs(args...)
}
if c.String("helm-binary") != "" {
helm.SetHelmBinary(c.String("helm-binary"))
}
return state.SyncRepos(helm)
})
@ -104,6 +111,9 @@ func main() {
if len(args) > 0 {
helm.SetExtraArgs(args...)
}
if c.String("helm-binary") != "" {
helm.SetHelmBinary(c.String("helm-binary"))
}
values := c.StringSlice("values")
workers := c.Int("concurrency")
@ -141,6 +151,9 @@ func main() {
if len(args) > 0 {
helm.SetExtraArgs(args...)
}
if c.String("helm-binary") != "" {
helm.SetHelmBinary(c.String("helm-binary"))
}
if c.Bool("sync-repos") {
if errs := state.SyncRepos(helm); errs != nil && len(errs) > 0 {
@ -180,6 +193,9 @@ func main() {
if len(args) > 0 {
helm.SetExtraArgs(strings.Split(args, " ")...)
}
if c.String("helm-binary") != "" {
helm.SetHelmBinary(c.String("helm-binary"))
}
values := c.StringSlice("values")
workers := c.Int("concurrency")
@ -221,6 +237,9 @@ func main() {
if len(args) > 0 {
helm.SetExtraArgs(args...)
}
if c.String("helm-binary") != "" {
helm.SetHelmBinary(c.String("helm-binary"))
}
values := c.StringSlice("values")
workers := c.Int("concurrency")
@ -252,6 +271,9 @@ func main() {
if len(args) > 0 {
helm.SetExtraArgs(args...)
}
if c.String("helm-binary") != "" {
helm.SetHelmBinary(c.String("helm-binary"))
}
return state.ReleaseStatuses(helm, workers)
})
@ -302,6 +324,9 @@ func main() {
if len(args) > 0 {
helm.SetExtraArgs(args...)
}
if c.String("helm-binary") != "" {
helm.SetHelmBinary(c.String("helm-binary"))
}
return state.TestReleases(helm, cleanup, timeout)
})

View File

@ -521,6 +521,9 @@ func (helm *mockHelmExec) UpdateDeps(chart string) error {
func (helm *mockHelmExec) SetExtraArgs(args ...string) {
return
}
func (helm *mockHelmExec) SetHelmBinary(bin string) {
return
}
func (helm *mockHelmExec) AddRepo(name, repository, certfile, keyfile, username, password string) error {
helm.repo = []string{name, repository, certfile, keyfile, username, password}
return nil