From 73c469eb6ab3da9ae8716d10b5a60280f25f4ffb Mon Sep 17 00:00:00 2001 From: Matt Butcher Date: Thu, 3 Mar 2016 13:19:55 -0700 Subject: [PATCH] feat(dm install): use template to generate manifest Closes #306. --- cmd/helm/dm.go | 32 +++++++++++++++++++++++++++---- pkg/dm/install.go | 49 ++++++++++++++++++++++++++++++++++++++++++----- 2 files changed, 72 insertions(+), 9 deletions(-) diff --git a/cmd/helm/dm.go b/cmd/helm/dm.go index 75630253c..9f04cb1a5 100644 --- a/cmd/helm/dm.go +++ b/cmd/helm/dm.go @@ -48,9 +48,28 @@ func dmCmd() cli.Command { Name: "dry-run", Usage: "Show what would be installed, but don't install anything.", }, + cli.StringFlag{ + Name: "resourcifier-image", + Usage: "The full image name of the Docker image for resourcifier.", + EnvVar: "HELM_RESOURCIFIER_IMAGE", + }, + cli.StringFlag{ + Name: "expandybird-image", + Usage: "The full image name of the Docker image for expandybird.", + EnvVar: "HELM_EXPANDYBIRD_IMAGE", + }, + cli.StringFlag{ + Name: "manager-image", + Usage: "The full image name of the Docker image for manager.", + EnvVar: "HELM_MANAGER_IMAGE", + }, }, Action: func(c *cli.Context) { - if err := install(c.Bool("dry-run")); err != nil { + dry := c.Bool("dry-run") + ri := c.String("resourcifier-image") + ei := c.String("expandybird-image") + mi := c.String("manager-image") + if err := install(dry, ei, mi, ri); err != nil { format.Err("%s (Run 'helm doctor' for more information)", err) os.Exit(1) } @@ -104,12 +123,17 @@ func dmCmd() cli.Command { } } -func install(dryRun bool) error { +func install(dryRun bool, ebImg, manImg, resImg string) error { runner := getKubectlRunner(dryRun) - out, err := dm.Install(runner) + i := dm.NewInstaller() + i.Manager["Image"] = manImg + i.Resourcifier["Image"] = resImg + i.Expandybird["Image"] = ebImg + + out, err := i.Install(runner) if err != nil { - format.Err("Error installing: %s %s", out, err) + return err } format.Msg(out) return nil diff --git a/pkg/dm/install.go b/pkg/dm/install.go index 3edb95b4f..2c7042a92 100644 --- a/pkg/dm/install.go +++ b/pkg/dm/install.go @@ -1,19 +1,58 @@ package dm import ( + "bytes" + "text/template" + + "github.com/Masterminds/sprig" "github.com/kubernetes/deployment-manager/pkg/format" "github.com/kubernetes/deployment-manager/pkg/kubectl" ) +// Installer is capable of installing DM into Kubernetes. +// +// See InstallYAML. +type Installer struct { + // TODO: At some point we could transform these from maps to structs. + + // Expandybird params are used to render the expandybird manifest. + Expandybird map[string]interface{} + // Resourcifier params are used to render the resourcifier manifest. + Resourcifier map[string]interface{} + // Manager params are used to render the manager manifest. + Manager map[string]interface{} +} + +// NewInstaller creates a new Installer. +func NewInstaller() *Installer { + return &Installer{ + Expandybird: map[string]interface{}{}, + Resourcifier: map[string]interface{}{}, + Manager: map[string]interface{}{}, + } +} + // Install uses kubectl to install the base DM. // // Returns the string output received from the operation, and an error if the // command failed. -func Install(runner kubectl.Runner) (string, error) { - o, err := runner.Create([]byte(InstallYAML)) +func (i *Installer) Install(runner kubectl.Runner) (string, error) { + b, err := i.expand() + if err != nil { + 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 +} + // IsInstalled checks whether DM has been installed. func IsInstalled(runner kubectl.Runner) bool { // Basically, we test "all-or-nothing" here: if this returns without error @@ -88,7 +127,7 @@ spec: spec: containers: - env: [] - image: gcr.io/dm-k8s-testing/expandybird:latest + image: {{default "gcr.io/dm-k8s-testing/expandybird:latest" .Expandybird.Image}} name: expandybird ports: - containerPort: 8080 @@ -132,7 +171,7 @@ spec: spec: containers: - env: [] - image: gcr.io/dm-k8s-testing/resourcifier:latest + image: {{ default "gcr.io/dm-k8s-testing/resourcifier:latest" .Resourcifier.Image }} name: resourcifier ports: - containerPort: 8080 @@ -176,7 +215,7 @@ spec: spec: containers: - env: [] - image: gcr.io/dm-k8s-testing/manager:latest + image: {{ default "gcr.io/dm-k8s-testing/manager:latest" .Manager.Image }} name: manager ports: - containerPort: 8080