Merge pull request #616 from adamreese/ref/remove-kubectl

ref(init): refactor init command to use kube client
pull/618/head
Adam Reese 9 years ago
commit f3b225fe3a

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

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