Commit 8e0eface authored by Brad Beam's avatar Brad Beam Committed by Brad Beam

Making starter templates more versatile

This adds support for changing '<CHARTNAME>'
occurances in starter chart to the destination
chart name
parent 1e373043
...@@ -849,6 +849,8 @@ considerations in mind: ...@@ -849,6 +849,8 @@ considerations in mind:
- The `Chart.yaml` will be overwritten by the generator. - The `Chart.yaml` will be overwritten by the generator.
- Users will expect to modify such a chart's contents, so documentation - Users will expect to modify such a chart's contents, so documentation
should indicate how users can do so. 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 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 copy it there. In your chart's documentation, you may want to explain that
......
...@@ -21,7 +21,6 @@ import ( ...@@ -21,7 +21,6 @@ import (
"io/ioutil" "io/ioutil"
"os" "os"
"path/filepath" "path/filepath"
"strings"
"k8s.io/helm/pkg/proto/hapi/chart" "k8s.io/helm/pkg/proto/hapi/chart"
) )
...@@ -257,6 +256,17 @@ func CreateFrom(chartfile *chart.Metadata, dest string, src string) error { ...@@ -257,6 +256,17 @@ func CreateFrom(chartfile *chart.Metadata, dest string, src string) error {
} }
schart.Metadata = chartfile 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) return SaveDir(schart, dest)
} }
...@@ -324,27 +334,27 @@ func Create(chartfile *chart.Metadata, dir string) (string, error) { ...@@ -324,27 +334,27 @@ func Create(chartfile *chart.Metadata, dir string) (string, error) {
{ {
// ingress.yaml // ingress.yaml
path: filepath.Join(cdir, TemplatesDir, IngressFileName), path: filepath.Join(cdir, TemplatesDir, IngressFileName),
content: []byte(strings.Replace(defaultIngress, "<CHARTNAME>", chartfile.Name, -1)), content: Transform(defaultIngress, "<CHARTNAME>", chartfile.Name),
}, },
{ {
// deployment.yaml // deployment.yaml
path: filepath.Join(cdir, TemplatesDir, DeploymentName), path: filepath.Join(cdir, TemplatesDir, DeploymentName),
content: []byte(strings.Replace(defaultDeployment, "<CHARTNAME>", chartfile.Name, -1)), content: Transform(defaultDeployment, "<CHARTNAME>", chartfile.Name),
}, },
{ {
// service.yaml // service.yaml
path: filepath.Join(cdir, TemplatesDir, ServiceName), path: filepath.Join(cdir, TemplatesDir, ServiceName),
content: []byte(strings.Replace(defaultService, "<CHARTNAME>", chartfile.Name, -1)), content: Transform(defaultService, "<CHARTNAME>", chartfile.Name),
}, },
{ {
// NOTES.txt // NOTES.txt
path: filepath.Join(cdir, TemplatesDir, NotesName), path: filepath.Join(cdir, TemplatesDir, NotesName),
content: []byte(strings.Replace(defaultNotes, "<CHARTNAME>", chartfile.Name, -1)), content: Transform(defaultNotes, "<CHARTNAME>", chartfile.Name),
}, },
{ {
// _helpers.tpl // _helpers.tpl
path: filepath.Join(cdir, TemplatesDir, HelpersName), path: filepath.Join(cdir, TemplatesDir, HelpersName),
content: []byte(strings.Replace(defaultHelpers, "<CHARTNAME>", chartfile.Name, -1)), content: Transform(defaultHelpers, "<CHARTNAME>", chartfile.Name),
}, },
} }
......
...@@ -20,6 +20,7 @@ import ( ...@@ -20,6 +20,7 @@ import (
"io/ioutil" "io/ioutil"
"os" "os"
"path/filepath" "path/filepath"
"strings"
"testing" "testing"
"k8s.io/helm/pkg/proto/hapi/chart" "k8s.io/helm/pkg/proto/hapi/chart"
...@@ -125,4 +126,9 @@ func TestCreateFrom(t *testing.T) { ...@@ -125,4 +126,9 @@ func TestCreateFrom(t *testing.T) {
t.Errorf("Expected %s to be a file.", f) 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)
}
} }
# Default values for mariner. # Default values for <CHARTNAME>.
# This is a YAML-formatted file. https://github.com/toml-lang/toml # This is a YAML-formatted file. https://github.com/toml-lang/toml
# Declare name/value pairs to be passed into your templates. # Declare name/value pairs to be passed into your templates.
# name: "value" # name: "value"
<CHARTNAME>:
test: true
/*
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))
}
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