From 8e0eface2c9e8ab2ca99d7ffb047f470847b9640 Mon Sep 17 00:00:00 2001 From: Brad Beam Date: Wed, 20 Dec 2017 23:29:50 -0600 Subject: [PATCH] Making starter templates more versatile This adds support for changing '' occurances in starter chart to the destination chart name --- docs/charts.md | 2 ++ pkg/chartutil/create.go | 22 +++++++++++++------ pkg/chartutil/create_test.go | 6 ++++++ pkg/chartutil/testdata/mariner/values.yaml | 5 ++++- pkg/chartutil/transform.go | 25 ++++++++++++++++++++++ 5 files changed, 53 insertions(+), 7 deletions(-) create mode 100644 pkg/chartutil/transform.go diff --git a/docs/charts.md b/docs/charts.md index 1bcafb64b..7ddec0c65 100644 --- a/docs/charts.md +++ b/docs/charts.md @@ -849,6 +849,8 @@ considerations in mind: - The `Chart.yaml` will be overwritten by the generator. - Users will expect to modify such a chart's contents, so documentation should indicate how users can do so. +- All occurances of `` will be replaced with the specified chart + name so that starter charts can be used as templates. Currently the only way to add a chart to `$HELM_HOME/starters` is to manually copy it there. In your chart's documentation, you may want to explain that diff --git a/pkg/chartutil/create.go b/pkg/chartutil/create.go index 5cdf7ab4b..a848caac5 100644 --- a/pkg/chartutil/create.go +++ b/pkg/chartutil/create.go @@ -21,7 +21,6 @@ import ( "io/ioutil" "os" "path/filepath" - "strings" "k8s.io/helm/pkg/proto/hapi/chart" ) @@ -257,6 +256,17 @@ func CreateFrom(chartfile *chart.Metadata, dest string, src string) error { } schart.Metadata = chartfile + + var updatedTemplates []*chart.Template + + for _, template := range schart.Templates { + newData := Transform(string(template.Data), "", schart.Metadata.Name) + updatedTemplates = append(updatedTemplates, &chart.Template{Name: template.Name, Data: newData}) + } + + schart.Templates = updatedTemplates + schart.Values = &chart.Config{Raw: string(Transform(schart.Values.Raw, "", schart.Metadata.Name))} + return SaveDir(schart, dest) } @@ -324,27 +334,27 @@ func Create(chartfile *chart.Metadata, dir string) (string, error) { { // ingress.yaml path: filepath.Join(cdir, TemplatesDir, IngressFileName), - content: []byte(strings.Replace(defaultIngress, "", chartfile.Name, -1)), + content: Transform(defaultIngress, "", chartfile.Name), }, { // deployment.yaml path: filepath.Join(cdir, TemplatesDir, DeploymentName), - content: []byte(strings.Replace(defaultDeployment, "", chartfile.Name, -1)), + content: Transform(defaultDeployment, "", chartfile.Name), }, { // service.yaml path: filepath.Join(cdir, TemplatesDir, ServiceName), - content: []byte(strings.Replace(defaultService, "", chartfile.Name, -1)), + content: Transform(defaultService, "", chartfile.Name), }, { // NOTES.txt path: filepath.Join(cdir, TemplatesDir, NotesName), - content: []byte(strings.Replace(defaultNotes, "", chartfile.Name, -1)), + content: Transform(defaultNotes, "", chartfile.Name), }, { // _helpers.tpl path: filepath.Join(cdir, TemplatesDir, HelpersName), - content: []byte(strings.Replace(defaultHelpers, "", chartfile.Name, -1)), + content: Transform(defaultHelpers, "", chartfile.Name), }, } diff --git a/pkg/chartutil/create_test.go b/pkg/chartutil/create_test.go index ee88972f4..e9af83ad2 100644 --- a/pkg/chartutil/create_test.go +++ b/pkg/chartutil/create_test.go @@ -20,6 +20,7 @@ import ( "io/ioutil" "os" "path/filepath" + "strings" "testing" "k8s.io/helm/pkg/proto/hapi/chart" @@ -125,4 +126,9 @@ func TestCreateFrom(t *testing.T) { t.Errorf("Expected %s to be a file.", f) } } + + // Ensure we replace `` + if strings.Contains(mychart.Values.Raw, "") { + t.Errorf("Did not expect %s to be present in %s", "", mychart.Values.Raw) + } } diff --git a/pkg/chartutil/testdata/mariner/values.yaml b/pkg/chartutil/testdata/mariner/values.yaml index e1609e243..b0ccb0086 100644 --- a/pkg/chartutil/testdata/mariner/values.yaml +++ b/pkg/chartutil/testdata/mariner/values.yaml @@ -1,4 +1,7 @@ -# Default values for mariner. +# Default values for . # This is a YAML-formatted file. https://github.com/toml-lang/toml # Declare name/value pairs to be passed into your templates. # name: "value" + +: + test: true diff --git a/pkg/chartutil/transform.go b/pkg/chartutil/transform.go new file mode 100644 index 000000000..7cbb754fb --- /dev/null +++ b/pkg/chartutil/transform.go @@ -0,0 +1,25 @@ +/* +Copyright 2016 The Kubernetes Authors All rights reserved. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package chartutil + +import "strings" + +// Transform performs a string replacement of the specified source for +// a given key with the replacement string +func Transform(src string, key string, replacement string) []byte { + return []byte(strings.Replace(src, key, replacement, -1)) +}