feat(dm install): use template to generate manifest

Closes #306.
pull/318/head
Matt Butcher 9 years ago
parent b4e58ac7a3
commit 73c469eb6a

@ -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

@ -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

Loading…
Cancel
Save