feat(templater): Support single-template resource sets
Supports resource sets in which the `path` is pointed at a single template file. The example has been updated with ... an example of this. This closes #81.
This commit is contained in:
parent
77ca5b47cf
commit
c91cb21f70
3 changed files with 55 additions and 23 deletions
7
example/other-config.yaml
Normal file
7
example/other-config.yaml
Normal file
|
@ -0,0 +1,7 @@
|
||||||
|
---
|
||||||
|
apiVersion: extensions/v1beta1
|
||||||
|
kind: ConfigMap
|
||||||
|
metadata:
|
||||||
|
name: other-config
|
||||||
|
data:
|
||||||
|
globalData: {{ .globalVar }}
|
|
@ -3,8 +3,15 @@ context: k8s.prod.mydomain.com
|
||||||
global:
|
global:
|
||||||
globalVar: lizards
|
globalVar: lizards
|
||||||
include:
|
include:
|
||||||
|
# By default resource sets are included from a folder with the same
|
||||||
|
# name as the resource set's name
|
||||||
- name: some-api
|
- name: some-api
|
||||||
values:
|
values:
|
||||||
version: 1.0-0e6884d
|
version: 1.0-0e6884d
|
||||||
importantFeature: true
|
importantFeature: true
|
||||||
apiPort: 4567
|
apiPort: 4567
|
||||||
|
|
||||||
|
# Paths can also be specified manually (and point at single template
|
||||||
|
# files!)
|
||||||
|
- name: other-config
|
||||||
|
path: other-config.yaml
|
||||||
|
|
|
@ -58,43 +58,57 @@ func LoadAndApplyTemplates(include *[]string, exclude *[]string, c *context.Cont
|
||||||
return renderedResourceSets, nil
|
return renderedResourceSets, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func processResourceSet(c *context.Context, rs *context.ResourceSet) (*RenderedResourceSet, error) {
|
func processResourceSet(ctx *context.Context, rs *context.ResourceSet) (*RenderedResourceSet, error) {
|
||||||
fmt.Fprintf(os.Stderr, "Loading resources for %s\n", rs.Name)
|
fmt.Fprintf(os.Stderr, "Loading resources for %s\n", rs.Name)
|
||||||
|
|
||||||
rp := path.Join(c.BaseDir, rs.Path)
|
resourcePath := path.Join(ctx.BaseDir, rs.Path)
|
||||||
|
fileInfo, err := os.Stat(resourcePath)
|
||||||
// Explicitly discard this error, which will give us an empty list of files instead.
|
|
||||||
// This will end up printing a warning to the user, but it won't stop the rest of the process.
|
|
||||||
files, _ := ioutil.ReadDir(rp)
|
|
||||||
|
|
||||||
resources, err := processFiles(c, rs, rp, files)
|
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var files []os.FileInfo
|
||||||
|
var resources []RenderedResource
|
||||||
|
|
||||||
|
// Treat single-file resource paths separately from resource
|
||||||
|
// sets containing multiple templates
|
||||||
|
if fileInfo.IsDir() {
|
||||||
|
// Explicitly discard this error, which will give us an empty
|
||||||
|
// list of files instead.
|
||||||
|
// This will end up printing a warning to the user, but it
|
||||||
|
// won't stop the rest of the process.
|
||||||
|
files, _ = ioutil.ReadDir(resourcePath)
|
||||||
|
resources, err = processFiles(ctx, rs, files)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
resource, err := templateFile(ctx, rs, resourcePath)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
resources = []RenderedResource{resource}
|
||||||
|
}
|
||||||
|
|
||||||
return &RenderedResourceSet{
|
return &RenderedResourceSet{
|
||||||
Name: rs.Name,
|
Name: rs.Name,
|
||||||
Resources: resources,
|
Resources: resources,
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func processFiles(c *context.Context, rs *context.ResourceSet, rp string, files []os.FileInfo) ([]RenderedResource, error) {
|
func processFiles(ctx *context.Context, rs *context.ResourceSet, files []os.FileInfo) ([]RenderedResource, error) {
|
||||||
resources := make([]RenderedResource, 0)
|
resources := make([]RenderedResource, 0)
|
||||||
|
|
||||||
for _, file := range files {
|
for _, file := range files {
|
||||||
if !file.IsDir() && isResourceFile(file) {
|
if !file.IsDir() && isResourceFile(file) {
|
||||||
p := path.Join(rp, file.Name())
|
path := path.Join(ctx.BaseDir, rs.Path, file.Name())
|
||||||
o, err := templateFile(c, rs, p)
|
res, err := templateFile(ctx, rs, path)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return resources, err
|
return resources, err
|
||||||
}
|
}
|
||||||
|
|
||||||
res := RenderedResource{
|
|
||||||
Filename: file.Name(),
|
|
||||||
Rendered: o,
|
|
||||||
}
|
|
||||||
resources = append(resources, res)
|
resources = append(resources, res)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -102,22 +116,26 @@ func processFiles(c *context.Context, rs *context.ResourceSet, rp string, files
|
||||||
return resources, nil
|
return resources, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func templateFile(c *context.Context, rs *context.ResourceSet, filename string) (string, error) {
|
func templateFile(ctx *context.Context, rs *context.ResourceSet, filepath string) (RenderedResource, error) {
|
||||||
tpl, err := template.New(path.Base(filename)).Funcs(templateFuncs(c, rs)).Option(failOnMissingKeys).ParseFiles(filename)
|
var resource RenderedResource
|
||||||
|
|
||||||
|
tpl, err := template.New(path.Base(filepath)).Funcs(templateFuncs(ctx, rs)).Option(failOnMissingKeys).ParseFiles(filepath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", fmt.Errorf("Template %s not found: %v", filename, err)
|
return resource, fmt.Errorf("Could not load template %s: %v", filepath, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
var b bytes.Buffer
|
var b bytes.Buffer
|
||||||
|
|
||||||
err = tpl.Execute(&b, rs.Values)
|
err = tpl.Execute(&b, rs.Values)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", fmt.Errorf("Error while templating %s: %v", filename, err)
|
return resource, fmt.Errorf("Error while templating %s: %v", filepath, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
return b.String(), nil
|
resource = RenderedResource{
|
||||||
|
Filename: path.Base(filepath),
|
||||||
|
Rendered: b.String(),
|
||||||
|
}
|
||||||
|
|
||||||
|
return resource, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Applies the limits of explicitly included or excluded resources and returns the updated resource set.
|
// Applies the limits of explicitly included or excluded resources and returns the updated resource set.
|
||||||
|
|
Loading…
Add table
Reference in a new issue