From 03fe44f3daa76c174d0c3258aa57e59af7eaca73 Mon Sep 17 00:00:00 2001 From: Adam Reese Date: Thu, 28 Apr 2016 13:41:36 -0700 Subject: [PATCH] ref(init): refactor init command to use kube client --- cmd/helm/init.go | 15 ++------------- pkg/client/install.go | 25 +++++++++++-------------- pkg/kubectl/command.go | 32 -------------------------------- pkg/kubectl/create.go | 21 --------------------- pkg/kubectl/create_test.go | 22 ---------------------- pkg/kubectl/kubectl.go | 19 ------------------- pkg/kubectl/kubectl_test.go | 12 ------------ 7 files changed, 13 insertions(+), 133 deletions(-) delete mode 100644 pkg/kubectl/command.go delete mode 100644 pkg/kubectl/create.go delete mode 100644 pkg/kubectl/create_test.go delete mode 100644 pkg/kubectl/kubectl.go delete mode 100644 pkg/kubectl/kubectl_test.go diff --git a/cmd/helm/init.go b/cmd/helm/init.go index 3fc777c10..27ff0b3d1 100644 --- a/cmd/helm/init.go +++ b/cmd/helm/init.go @@ -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. diff --git a/pkg/client/install.go b/pkg/client/install.go index 5fb15b20a..26aafe2ed 100644 --- a/pkg/client/install.go +++ b/pkg/client/install.go @@ -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. diff --git a/pkg/kubectl/command.go b/pkg/kubectl/command.go deleted file mode 100644 index b36e0ad33..000000000 --- a/pkg/kubectl/command.go +++ /dev/null @@ -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) -} diff --git a/pkg/kubectl/create.go b/pkg/kubectl/create.go deleted file mode 100644 index af9297aa9..000000000 --- a/pkg/kubectl/create.go +++ /dev/null @@ -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 -} diff --git a/pkg/kubectl/create_test.go b/pkg/kubectl/create_test.go deleted file mode 100644 index bca94f6e5..000000000 --- a/pkg/kubectl/create_test.go +++ /dev/null @@ -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) - } -} diff --git a/pkg/kubectl/kubectl.go b/pkg/kubectl/kubectl.go deleted file mode 100644 index e527b71fe..000000000 --- a/pkg/kubectl/kubectl.go +++ /dev/null @@ -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{} diff --git a/pkg/kubectl/kubectl_test.go b/pkg/kubectl/kubectl_test.go deleted file mode 100644 index c3348bba5..000000000 --- a/pkg/kubectl/kubectl_test.go +++ /dev/null @@ -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 -}