ref(init): refactor init command to use kube client

pull/616/head
Adam Reese 8 years ago
parent 65a7be5618
commit 03fe44f3da

@ -7,7 +7,6 @@ import (
"os"
"github.com/kubernetes/helm/pkg/client"
"github.com/kubernetes/helm/pkg/kubectl"
"github.com/spf13/cobra"
)
@ -52,28 +51,18 @@ func runInit(cmd *cobra.Command, args []string) error {
}
func installTiller() error {
// TODO: take value of global flag kubectl and pass that in
runner := buildKubectlRunner("")
i := client.NewInstaller()
i.Tiller["Image"] = tillerImg
out, err := i.Install(runner)
err := i.Install()
if err != nil {
return fmt.Errorf("error installing %s %s", string(out), err)
return fmt.Errorf("error installing: %s", err)
}
fmt.Println("\nTiller (the helm server side component) has been installed into your Kubernetes Cluster.")
return nil
}
func buildKubectlRunner(kubectlPath string) kubectl.Runner {
if kubectlPath != "" {
kubectl.Path = kubectlPath
}
return &kubectl.RealRunner{}
}
// ensureHome checks to see if $HELM_HOME exists
//
// If $HELM_HOME does not exist, this function will create it.

@ -5,7 +5,7 @@ import (
"text/template"
"github.com/Masterminds/sprig"
"github.com/kubernetes/helm/pkg/kubectl"
"github.com/kubernetes/helm/pkg/kube"
)
// Installer installs tiller into Kubernetes
@ -28,25 +28,22 @@ func NewInstaller() *Installer {
}
}
// Install uses kubectl to install tiller
// Install uses kubernetes client to install tiller
//
// Returns the string output received from the operation, and an error if the
// command failed.
func (i *Installer) Install(runner kubectl.Runner) (string, error) {
b, err := i.expand()
func (i *Installer) Install() error {
var b bytes.Buffer
err := template.Must(template.New("manifest").Funcs(sprig.TxtFuncMap()).
Parse(InstallYAML)).
Execute(&b, i)
if err != nil {
return "", err
return err
}
o, err := runner.Create(b)
return string(o), err
}
func (i *Installer) expand() ([]byte, error) {
var b bytes.Buffer
t := template.Must(template.New("manifest").Funcs(sprig.TxtFuncMap()).Parse(InstallYAML))
err := t.Execute(&b, i)
return b.Bytes(), err
return kube.Create("helm", &b, nil)
}
// InstallYAML is the installation YAML for DM.

@ -1,32 +0,0 @@
package kubectl
import (
"bytes"
"fmt"
"io/ioutil"
"os/exec"
"strings"
)
type cmd struct {
*exec.Cmd
}
func command(args ...string) *cmd {
return &cmd{exec.Command(Path, args...)}
}
func assignStdin(cmd *cmd, in []byte) {
cmd.Stdin = bytes.NewBuffer(in)
}
func (c *cmd) String() string {
var stdin string
if c.Stdin != nil {
b, _ := ioutil.ReadAll(c.Stdin)
stdin = fmt.Sprintf("< %s", string(b))
}
return fmt.Sprintf("[CMD] %s %s", strings.Join(c.Args, " "), stdin)
}

@ -1,21 +0,0 @@
package kubectl
// Create uploads a chart to Kubernetes
func (r RealRunner) Create(stdin []byte) ([]byte, error) {
args := []string{"create", "-f", "-"}
cmd := command(args...)
assignStdin(cmd, stdin)
return cmd.CombinedOutput()
}
// Create returns the commands to kubectl
func (r PrintRunner) Create(stdin []byte) ([]byte, error) {
args := []string{"create", "-f", "-"}
cmd := command(args...)
assignStdin(cmd, stdin)
return []byte(cmd.String()), nil
}

@ -1,22 +0,0 @@
package kubectl
import (
"testing"
)
func TestPrintCreate(t *testing.T) {
var client Runner = PrintRunner{}
expected := `[CMD] kubectl create -f - < some stdin data`
out, err := client.Create([]byte("some stdin data"))
if err != nil {
t.Error(err)
}
actual := string(out)
if expected != actual {
t.Fatalf("actual %s != expected %s", actual, expected)
}
}

@ -1,19 +0,0 @@
package kubectl
// Path is the path of the kubectl binary
var Path = "kubectl"
// Runner is an interface to wrap kubectl convenience methods
type Runner interface {
// Create uploads a chart to Kubernetes
Create(stdin []byte) ([]byte, error)
}
// RealRunner implements Runner to execute kubectl commands
type RealRunner struct{}
// PrintRunner implements Runner to return a []byte of the command to be executed
type PrintRunner struct{}
// Client stores the instance of Runner
var Client Runner = RealRunner{}

@ -1,12 +0,0 @@
package kubectl
type TestRunner struct {
Runner
out []byte
err error
}
func (r TestRunner) Create(stdin []byte, ns string) ([]byte, error) {
return r.out, r.err
}
Loading…
Cancel
Save