mirror of https://github.com/helm/helm.git
feat(helm): allow overriding values
This supports the `-f` flag for overriding values with a specified TOML file.
This commit is contained in:
parent
0cbb6c6e86
commit
5c942226a3
|
@ -23,4 +23,9 @@ message Chart {
|
|||
// Default config for this template.
|
||||
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
|
|||
|
||||
import (
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
|
||||
|
@ -24,6 +25,8 @@ var (
|
|||
installArg string
|
||||
// installDryRun performs a dry-run install
|
||||
installDryRun bool
|
||||
// installValues is the filename of supplied values.
|
||||
installValues string
|
||||
)
|
||||
|
||||
var installCmd = &cobra.Command{
|
||||
|
@ -33,13 +36,27 @@ var installCmd = &cobra.Command{
|
|||
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 {
|
||||
if err := checkArgsLength(1, len(args), "chart name"); err != nil {
|
||||
return err
|
||||
}
|
||||
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 {
|
||||
return prettyError(err)
|
||||
}
|
||||
|
@ -49,6 +66,13 @@ func runInstall(cmd *cobra.Command, args []string) error {
|
|||
return nil
|
||||
}
|
||||
|
||||
func vals() ([]byte, error) {
|
||||
if installValues == "" {
|
||||
return []byte{}, nil
|
||||
}
|
||||
return ioutil.ReadFile(installValues)
|
||||
}
|
||||
|
||||
func printRelease(rel *release.Release) {
|
||||
if rel == nil {
|
||||
return
|
||||
|
@ -62,9 +86,3 @@ func printRelease(rel *release.Release) {
|
|||
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
|
|||
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.
|
||||
func ValuesToProto(ch *chartutil.Chart) (*chartpbs.Config, error) {
|
||||
if ch == nil {
|
||||
|
|
|
@ -0,0 +1,59 @@
|
|||
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) {
|
|||
}
|
||||
|
||||
// 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)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
|
@ -85,10 +85,7 @@ func InstallRelease(chStr string, dryRun bool) (*services.InstallReleaseResponse
|
|||
return nil, err
|
||||
}
|
||||
|
||||
vals, err := ValuesToProto(chfi)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
vals := OverridesToProto(rawVals)
|
||||
|
||||
return Config.client().install(&services.InstallReleaseRequest{
|
||||
Chart: chpb,
|
||||
|
|
|
@ -0,0 +1 @@
|
|||
name = "foo"
|
|
@ -0,0 +1,4 @@
|
|||
name: albatross
|
||||
description: testing chart
|
||||
version: 0.1.0
|
||||
home: "https://github.com/kubernetes/helm"
|
|
@ -0,0 +1,3 @@
|
|||
# Test data. Not a valid Kubernetes manifest
|
||||
name: {{.name}}
|
||||
home: {{.home}}
|
|
@ -0,0 +1,2 @@
|
|||
name = "albatross"
|
||||
home = "nest"
|
Loading…
Reference in New Issue