about summary refs log tree commit diff
diff options
context:
space:
mode:
authorNiklas Wik <niklas.wik@nokia.com>2018-01-09T13·12+0200
committerVincent Ambo <github@tazj.in>2018-03-09T13·54+0100
commitbafb792339b5898a0e6b6219ad54b5f501d727c2 (patch)
tree1b364cfb260ddc24642c64ad0e17c1ac486dac58
parent850fdcf3e0270db885f661ae59225db92143c3f2 (diff)
feat(templater): Added support for file include
Adds a 'fileContent' template function to insert the literal contents
of a file specified in the template.

Signed-off-by: Niklas Wik <niklas.wik@nokia.com>
-rw-r--r--docs/templates.md5
-rw-r--r--example/some-api/some-api.yaml2
-rw-r--r--templater/fromfile.go24
-rw-r--r--templater/templater.go1
4 files changed, 30 insertions, 2 deletions
diff --git a/docs/templates.md b/docs/templates.md
index 8bcc2c7f5665..d9ff0fd126e3 100644
--- a/docs/templates.md
+++ b/docs/templates.md
@@ -72,10 +72,11 @@ right.
 
 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 two custom functions:
+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][]
+`passLookup`: Looks up the supplied key in [pass][].
+`fromFile`: Insert the contents of the given file from the resource set folder.
 
 ## Examples:
 
diff --git a/example/some-api/some-api.yaml b/example/some-api/some-api.yaml
index 57ab7c65275d..44b61541744c 100644
--- a/example/some-api/some-api.yaml
+++ b/example/some-api/some-api.yaml
@@ -25,6 +25,8 @@ spec:
               value: {{ .importantFeature }}
             - name: SOME_GLOBAL_VAR
               value: {{ .globalVar }}
+            - name: FILE_VAR
+              value: {{ fileContent "some-api/filevar.txt" }}
 ---
 apiVersion: v1
 kind: Service
diff --git a/templater/fromfile.go b/templater/fromfile.go
new file mode 100644
index 000000000000..f4f1e79cbfba
--- /dev/null
+++ b/templater/fromfile.go
@@ -0,0 +1,24 @@
+// 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
+}
diff --git a/templater/templater.go b/templater/templater.go
index 7fb85e8b235f..5bdb2f2e3bb1 100644
--- a/templater/templater.go
+++ b/templater/templater.go
@@ -193,6 +193,7 @@ func templateFuncs() template.FuncMap {
 	}
 	m["passLookup"] = GetFromPass
 	m["lookupIPAddr"] = GetIPsFromDNS
+	m["fileContent"] = GetFromFile
 
 	return m
 }