refactor(templater): Pass resource set path to insertFile function

This is actually several refactors in one:

* rename "fileContent" function to "insertFile"
* pass the resource set path to the "insetFile" function
* update docs and example with a pipeline including indentation
  adjustments for the inserted file
This commit is contained in:
Vincent Ambo 2018-03-09 14:49:33 +01:00 committed by Vincent Ambo
parent bafb792339
commit b8722ce83b
4 changed files with 26 additions and 33 deletions

View file

@ -74,9 +74,10 @@ Some template functions come from Go's standard library and are listed in the
[Go documentation][]. In addition the functions declared by [sprig][] are
available in kontemplate, as well as three custom functions:
`json`: Encodes any supplied data structure as JSON.
`passLookup`: Looks up the supplied key in [pass][].
`fromFile`: Insert the contents of the given file from the resource set folder.
* `json`: Encodes any supplied data structure as JSON.
* `passLookup`: Looks up the supplied key in [pass][].
* `insertFile`: Insert the contents of the given file in the resource
set folder as a string.
## Examples:

View file

@ -4,7 +4,18 @@ kind: Secret
metadata:
name: secret-certificate
data:
cert.pem: {{ passLookup "my/secret/certificate" | b64enc }}
cert.pem: { passLookup "my/secret/certificate" | b64enc }}
---
apiVersion: extensions/v1beta1
kind: ConfigMap
metadata:
name: some-config
data:
# The content of the example configuration file is templated in here
# by the 'insertFile' function and indented for YAML-compatibility
# with the 'indent' function:
some.cfg: |
{{ insertFile "some.cfg" | indent 4 }}
---
apiVersion: extensions/v1beta1
kind: Deployment
@ -25,8 +36,6 @@ spec:
value: {{ .importantFeature }}
- name: SOME_GLOBAL_VAR
value: {{ .globalVar }}
- name: FILE_VAR
value: {{ fileContent "some-api/filevar.txt" }}
---
apiVersion: v1
kind: Service

View file

@ -1,24 +0,0 @@
// Copyright (C) 2017 Niklas Wik <niklas.wik@nokia.com>
//
// This file is part of Kontemplate.
//
// Kontemplate is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
package templater
import (
"io/ioutil"
)
//GetFromFile returns file content as string
func GetFromFile(file string) (string, error) {
data, err := ioutil.ReadFile(file)
if err != nil {
return "", err
}
return string(data), nil
}

View file

@ -119,7 +119,7 @@ func processFiles(c *context.Context, rs *context.ResourceSet, rp string, files
}
func templateFile(c *context.Context, rs *context.ResourceSet, filename string) (string, error) {
tpl, err := template.New(path.Base(filename)).Funcs(templateFuncs()).Option(failOnMissingKeys).ParseFiles(filename)
tpl, err := template.New(path.Base(filename)).Funcs(templateFuncs(rs)).Option(failOnMissingKeys).ParseFiles(filename)
if err != nil {
return "", meep.New(
@ -185,7 +185,7 @@ func matchesResourceSet(s *[]string, rs *context.ResourceSet) bool {
return false
}
func templateFuncs() template.FuncMap {
func templateFuncs(rs *context.ResourceSet) template.FuncMap {
m := sprig.TxtFuncMap()
m["json"] = func(data interface{}) string {
b, _ := json.Marshal(data)
@ -193,7 +193,14 @@ func templateFuncs() template.FuncMap {
}
m["passLookup"] = GetFromPass
m["lookupIPAddr"] = GetIPsFromDNS
m["fileContent"] = GetFromFile
m["insertFile"] = func(file string) (string, error) {
data, err := ioutil.ReadFile(path.Join(rs.Path, file))
if err != nil {
return "", err
}
return string(data), nil
}
return m
}