diff --git a/docs/charts.md b/docs/charts.md index 3d4ae3a22131cac2d0130546bca26abdb07abe6c..5627c8600bdf0413fff112af32475caad5c5d3ec 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 `<CHARTNAME>` 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 7a44aaed6fc3aa17d9923ad215301b677d862470..319a75e2f50e1b6777a259e0dba8b9e69ca10bd8 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" ) @@ -297,6 +296,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), "<CHARTNAME>", 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, "<CHARTNAME>", schart.Metadata.Name))} + return SaveDir(schart, dest) } @@ -364,27 +374,27 @@ func Create(chartfile *chart.Metadata, dir string) (string, error) { { // ingress.yaml path: filepath.Join(cdir, TemplatesDir, IngressFileName), - content: []byte(strings.Replace(defaultIngress, "<CHARTNAME>", chartfile.Name, -1)), + content: Transform(defaultIngress, "<CHARTNAME>", chartfile.Name), }, { // deployment.yaml path: filepath.Join(cdir, TemplatesDir, DeploymentName), - content: []byte(strings.Replace(defaultDeployment, "<CHARTNAME>", chartfile.Name, -1)), + content: Transform(defaultDeployment, "<CHARTNAME>", chartfile.Name), }, { // service.yaml path: filepath.Join(cdir, TemplatesDir, ServiceName), - content: []byte(strings.Replace(defaultService, "<CHARTNAME>", chartfile.Name, -1)), + content: Transform(defaultService, "<CHARTNAME>", chartfile.Name), }, { // NOTES.txt path: filepath.Join(cdir, TemplatesDir, NotesName), - content: []byte(strings.Replace(defaultNotes, "<CHARTNAME>", chartfile.Name, -1)), + content: Transform(defaultNotes, "<CHARTNAME>", chartfile.Name), }, { // _helpers.tpl path: filepath.Join(cdir, TemplatesDir, HelpersName), - content: []byte(strings.Replace(defaultHelpers, "<CHARTNAME>", chartfile.Name, -1)), + content: Transform(defaultHelpers, "<CHARTNAME>", chartfile.Name), }, } diff --git a/pkg/chartutil/create_test.go b/pkg/chartutil/create_test.go index ee88972f43a82c3304d23ddc0400b17ceac7d0dc..e9af83ad2841a673ef0640c0f4cf693a81253da8 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 `<CHARTNAME>` + if strings.Contains(mychart.Values.Raw, "<CHARTNAME>") { + t.Errorf("Did not expect %s to be present in %s", "<CHARTNAME>", mychart.Values.Raw) + } } diff --git a/pkg/chartutil/testdata/mariner/values.yaml b/pkg/chartutil/testdata/mariner/values.yaml index e1609e243b76dbaff2316d372533111a0201dcea..b0ccb008629ddbadb061c018701af3ebf15fa388 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 <CHARTNAME>. # This is a YAML-formatted file. https://github.com/toml-lang/toml # Declare name/value pairs to be passed into your templates. # name: "value" + +<CHARTNAME>: + test: true diff --git a/pkg/chartutil/transform.go b/pkg/chartutil/transform.go new file mode 100644 index 0000000000000000000000000000000000000000..7cbb754fb325136adf3e999fd9b10ce607194ba4 --- /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)) +}