// Install uses kubernetes client to install tiller // // Returns the string output received from the operation, and an error if the // command failed. // // If verbose is true, this will print the manifest to stdout. func Install(namespace, image string, verbose bool) error { kc := kube.New(nil) if namespace == "" { ns, _, err := kc.DefaultNamespace() if err != nil { return err } namespace = ns } var b bytes.Buffer // Add main install YAML istpl := template.New("install").Funcs(sprig.TxtFuncMap()) cfg := struct { Namespace, Image string }{namespace, image} if err := template.Must(istpl.Parse(InstallYAML)).Execute(&b, cfg); err != nil { return err } if verbose { fmt.Println(b.String()) } return kc.Create(namespace, &b) }
// New creates a new Go template Engine instance. // // The FuncMap is initialized here. You may modify the FuncMap _prior to_ the // first invocation of Render. // // The FuncMap sets all of the Sprig functions except for those that provide // access to the underlying OS (env, expandenv). func New() *Engine { f := sprig.TxtFuncMap() delete(f, "env") delete(f, "expandenv") return &Engine{ FuncMap: f, } }
func tmpl(in io.Reader, out io.Writer, ctx interface{}) error { i, err := ioutil.ReadAll(in) if err != nil { return err } tmpl, err := template.New("format string").Funcs(sprig.TxtFuncMap()).Parse(string(i)) if err != nil { return err } return tmpl.Execute(out, ctx) }
func generateName(nameTemplate string) (string, error) { t, err := template.New("name-template").Funcs(sprig.TxtFuncMap()).Parse(nameTemplate) if err != nil { return "", err } var b bytes.Buffer err = t.Execute(&b, nil) if err != nil { return "", err } return b.String(), nil }
// WriteConfig dynamically produces valid nginx configuration by combining a Router configuration // object with a data-driven template. func WriteConfig(routerConfig *model.RouterConfig, filePath string) error { tmpl, err := template.New("nginx").Funcs(sprig.TxtFuncMap()).Parse(confTemplate) if err != nil { return err } file, err := os.Create(filePath) if err != nil { return err } err = tmpl.Execute(file, routerConfig) return err }
// renderTemplate renders a template and values into an output stream. // // tpl should be a string template. func renderTemplate(out io.Writer, tpl string, vals interface{}) error { t, err := template.New("helmTpl").Funcs(sprig.TxtFuncMap()).Parse(tpl) if err != nil { return err } log.Debug("Vals: %#v", vals) if err = t.ExecuteTemplate(out, "helmTpl", vals); err != nil { return err } return nil }
// validateNonMissingValues checks that all the {{}} functions returns a non empty value (<no value> or "") // and return an error otherwise. func validateNonMissingValues(fileName string, templatesPath string, chartValues chartutil.Values, templateContent []byte) (lintError support.LintError) { // 1 - Load Main and associated templates // Main template that we will parse dynamically tmpl := template.New("tpl").Funcs(sprig.TxtFuncMap()) // If the templatesPath includes any *.tpl files we will parse and import them as associated templates associatedTemplates, err := filepath.Glob(filepath.Join(templatesPath, "*.tpl")) if len(associatedTemplates) > 0 { tmpl, err = tmpl.ParseFiles(associatedTemplates...) if err != nil { return err } } var buf bytes.Buffer var emptyValues []string // 2 - Extract every function and execute them agains the loaded values // Supported {{ .Chart.Name }}, {{ .Chart.Name | quote }} r, _ := regexp.Compile(`{{[\w|\.|\s|\|\"|\']+}}`) functions := r.FindAllString(string(templateContent), -1) // Iterate over the {{ FOO }} templates, executing them against the chartValues // We do individual templates parsing so we keep the reference for the key (str) that we want it to be interpolated. for _, str := range functions { newtmpl, err := tmpl.Parse(str) if err != nil { lintError = fmt.Errorf("templates: %s", err.Error()) return } err = newtmpl.ExecuteTemplate(&buf, "tpl", chartValues) if err != nil { return err } renderedValue := buf.String() if renderedValue == "<no value>" || renderedValue == "" { emptyValues = append(emptyValues, str) } buf.Reset() } if len(emptyValues) > 0 { lintError = fmt.Errorf("templates: %s: The following functions are not returning any value %v", fileName, emptyValues) } return }
// FuncMap returns a mapping of all of the functions that Engine has. // // Because some functions are late-bound (e.g. contain context-sensitive // data), the functions may not all perform identically outside of an // Engine as they will inside of an Engine. // // Known late-bound functions: // // - "include": This is late-bound in Engine.Render(). The version // included in the FuncMap is a placeholder. func FuncMap() template.FuncMap { f := sprig.TxtFuncMap() delete(f, "env") delete(f, "expandenv") // Add a function to convert to YAML: f["toYaml"] = toYaml // This is a placeholder for the "include" function, which is // late-bound to a template. By declaring it here, we preserve the // integrity of the linter. f["include"] = func(string, interface{}) string { return "not implemented" } return f }
// FuncMap returns a mapping of all of the functions that Engine has. // // Because some functions are late-bound (e.g. contain context-sensitive // data), the functions may not all perform identically outside of an // Engine as they will inside of an Engine. // // Known late-bound functions: // // - "include": This is late-bound in Engine.Render(). The version // included in the FuncMap is a placeholder. func FuncMap() template.FuncMap { f := sprig.TxtFuncMap() delete(f, "env") delete(f, "expandenv") // Add some extra functionality extra := template.FuncMap{ "toYaml": chartutil.ToYaml, "fromYaml": chartutil.FromYaml, // This is a placeholder for the "include" function, which is // late-bound to a template. By declaring it here, we preserve the // integrity of the linter. "include": func(string, interface{}) string { return "not implemented" }, } for k, v := range extra { f[k] = v } return f }
func createTpl(tplName, fileName string) (*template.Template, error) { return template.New(tplName).Funcs(sprig.TxtFuncMap()).ParseFiles(fileName) }
func (i *Installer) expand() ([]byte, error) { var b bytes.Buffer t := template.Must(template.New("manifest").Funcs(sprig.TxtFuncMap()).Parse(InstallYAML)) err := t.Execute(&b, i) return b.Bytes(), err }
func createTemplate() *template.Template { fmap := sprig.TxtFuncMap() return template.New("base").Funcs(fmap) }