Commit 5c942226 authored by Matt Butcher's avatar Matt Butcher

feat(helm): allow overriding values

This supports the `-f` flag for overriding values with a specified
TOML file.
parent 0cbb6c6e
...@@ -23,4 +23,9 @@ message Chart { ...@@ -23,4 +23,9 @@ message Chart {
// Default config for this template. // Default config for this template.
hapi.chart.Config values = 4; hapi.chart.Config values = 4;
// License is the text of the LICENSE file, if included.
string license = 5;
// Readme is the text of the README.md file, if included.
string readme = 6;
} }
...@@ -2,6 +2,7 @@ package main ...@@ -2,6 +2,7 @@ package main
import ( import (
"fmt" "fmt"
"io/ioutil"
"github.com/spf13/cobra" "github.com/spf13/cobra"
...@@ -24,6 +25,8 @@ var ( ...@@ -24,6 +25,8 @@ var (
installArg string installArg string
// installDryRun performs a dry-run install // installDryRun performs a dry-run install
installDryRun bool installDryRun bool
// installValues is the filename of supplied values.
installValues string
) )
var installCmd = &cobra.Command{ var installCmd = &cobra.Command{
...@@ -33,13 +36,27 @@ var installCmd = &cobra.Command{ ...@@ -33,13 +36,27 @@ var installCmd = &cobra.Command{
RunE: runInstall, RunE: runInstall,
} }
func init() {
f := installCmd.Flags()
f.StringVar(&tillerHost, "host", "", "address of tiller server (default \":44134\")")
f.StringVarP(&installValues, "values", "f", "", "path to a values TOML file")
f.BoolVar(&installDryRun, "dry-run", false, "simulate an install")
RootCommand.AddCommand(installCmd)
}
func runInstall(cmd *cobra.Command, args []string) error { func runInstall(cmd *cobra.Command, args []string) error {
if err := checkArgsLength(1, len(args), "chart name"); err != nil { if err := checkArgsLength(1, len(args), "chart name"); err != nil {
return err return err
} }
installArg = args[0] installArg = args[0]
res, err := helm.InstallRelease(installArg, installDryRun) rawVals, err := vals()
if err != nil {
return err
}
res, err := helm.InstallRelease(rawVals, installArg, installDryRun)
if err != nil { if err != nil {
return prettyError(err) return prettyError(err)
} }
...@@ -49,6 +66,13 @@ func runInstall(cmd *cobra.Command, args []string) error { ...@@ -49,6 +66,13 @@ func runInstall(cmd *cobra.Command, args []string) error {
return nil return nil
} }
func vals() ([]byte, error) {
if installValues == "" {
return []byte{}, nil
}
return ioutil.ReadFile(installValues)
}
func printRelease(rel *release.Release) { func printRelease(rel *release.Release) {
if rel == nil { if rel == nil {
return return
...@@ -62,9 +86,3 @@ func printRelease(rel *release.Release) { ...@@ -62,9 +86,3 @@ func printRelease(rel *release.Release) {
fmt.Println(rel.Name) fmt.Println(rel.Name)
} }
} }
func init() {
installCmd.Flags().BoolVar(&installDryRun, "dry-run", false, "simulate an install")
RootCommand.AddCommand(installCmd)
}
...@@ -101,6 +101,13 @@ func TemplatesToProto(ch *chartutil.Chart) (tpls []*chartpbs.Template, err error ...@@ -101,6 +101,13 @@ func TemplatesToProto(ch *chartutil.Chart) (tpls []*chartpbs.Template, err error
return return
} }
// OverridesToProto converts arbitary TOML override data to Config data.
func OverridesToProto(values []byte) *chartpbs.Config {
return &chartpbs.Config{
Raw: string(values),
}
}
// ValuesToProto converts a chart's values.toml data to protobuf. // ValuesToProto converts a chart's values.toml data to protobuf.
func ValuesToProto(ch *chartutil.Chart) (*chartpbs.Config, error) { func ValuesToProto(ch *chartutil.Chart) (*chartpbs.Config, error) {
if ch == nil { if ch == nil {
......
package helm
import (
"testing"
chartutil "github.com/kubernetes/helm/pkg/chart"
"gopkg.in/yaml.v2"
)
func TestInstallReleaseOverrides(t *testing.T) {
vals := `name = "mariner"`
ch := "./testdata/albatross"
ir, err := InstallRelease([]byte(vals), ch, true)
if err != nil {
t.Fatalf("Failed to release: %s", err)
}
if len(ir.Release.Manifest) == 0 {
t.Fatalf("Expected a manifest.")
}
// Parse the result and see if the override worked
d := map[string]interface{}{}
if err := yaml.Unmarshal([]byte(ir.Release.Manifest), d); err != nil {
t.Fatalf("Failed to unmarshal manifest: %s", err)
}
if d["name"] != "mariner" {
t.Errorf("Unexpected name %q", d["name"])
}
if d["home"] != "nest" {
t.Errorf("Unexpected home %q", d["home"])
}
}
func TestOverridesToProto(t *testing.T) {
override := []byte(`test = "foo"`)
c := OverridesToProto(override)
if c.Raw != string(override) {
t.Errorf("Expected %q to match %q", c.Raw, override)
}
}
func TestChartToProto(t *testing.T) {
c, err := chartutil.LoadDir("./testdata/albatross")
if err != nil {
t.Fatalf("failed to load testdata chart: %s", err)
}
p, err := ChartToProto(c)
if err != nil {
t.Fatalf("failed to conver chart to proto: %s", err)
}
if p.Metadata.Name != c.Chartfile().Name {
t.Errorf("Expected names to match.")
}
}
...@@ -74,7 +74,7 @@ func UninstallRelease(name string) (*services.UninstallReleaseResponse, error) { ...@@ -74,7 +74,7 @@ func UninstallRelease(name string) (*services.UninstallReleaseResponse, error) {
} }
// InstallRelease installs a new chart and returns the release response. // InstallRelease installs a new chart and returns the release response.
func InstallRelease(chStr string, dryRun bool) (*services.InstallReleaseResponse, error) { func InstallRelease(rawVals []byte, chStr string, dryRun bool) (*services.InstallReleaseResponse, error) {
chfi, err := chartutil.LoadChart(chStr) chfi, err := chartutil.LoadChart(chStr)
if err != nil { if err != nil {
return nil, err return nil, err
...@@ -85,10 +85,7 @@ func InstallRelease(chStr string, dryRun bool) (*services.InstallReleaseResponse ...@@ -85,10 +85,7 @@ func InstallRelease(chStr string, dryRun bool) (*services.InstallReleaseResponse
return nil, err return nil, err
} }
vals, err := ValuesToProto(chfi) vals := OverridesToProto(rawVals)
if err != nil {
return nil, err
}
return Config.client().install(&services.InstallReleaseRequest{ return Config.client().install(&services.InstallReleaseRequest{
Chart: chpb, Chart: chpb,
......
name: albatross
description: testing chart
version: 0.1.0
home: "https://github.com/kubernetes/helm"
# Test data. Not a valid Kubernetes manifest
name: {{.name}}
home: {{.home}}
name = "albatross"
home = "nest"
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment