Skip to content
This repository has been archived by the owner on Dec 21, 2019. It is now read-only.

Commit

Permalink
feat(templater): Support single-template resource sets
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
tazjin authored and Vincent Ambo committed Jun 9, 2018
1 parent 77ca5b4 commit c91cb21
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 23 deletions.
7 changes: 7 additions & 0 deletions example/other-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
apiVersion: extensions/v1beta1
kind: ConfigMap
metadata:
name: other-config
data:
globalData: {{ .globalVar }}
7 changes: 7 additions & 0 deletions example/prod-cluster.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,15 @@ context: k8s.prod.mydomain.com
global:
globalVar: lizards
include:
# By default resource sets are included from a folder with the same
# name as the resource set's name
- name: some-api
values:
version: 1.0-0e6884d
importantFeature: true
apiPort: 4567

# Paths can also be specified manually (and point at single template
# files!)
- name: other-config
path: other-config.yaml
64 changes: 41 additions & 23 deletions templater/templater.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,66 +58,84 @@ func LoadAndApplyTemplates(include *[]string, exclude *[]string, c *context.Cont
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)

rp := path.Join(c.BaseDir, rs.Path)

// 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)

resourcePath := path.Join(ctx.BaseDir, rs.Path)
fileInfo, err := os.Stat(resourcePath)
if err != nil {
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{
Name: rs.Name,
Resources: resources,
}, 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)

for _, file := range files {
if !file.IsDir() && isResourceFile(file) {
p := path.Join(rp, file.Name())
o, err := templateFile(c, rs, p)
path := path.Join(ctx.BaseDir, rs.Path, file.Name())
res, err := templateFile(ctx, rs, path)

if err != nil {
return resources, err
}

res := RenderedResource{
Filename: file.Name(),
Rendered: o,
}
resources = append(resources, res)
}
}

return resources, nil
}

func templateFile(c *context.Context, rs *context.ResourceSet, filename string) (string, error) {
tpl, err := template.New(path.Base(filename)).Funcs(templateFuncs(c, rs)).Option(failOnMissingKeys).ParseFiles(filename)
func templateFile(ctx *context.Context, rs *context.ResourceSet, filepath string) (RenderedResource, error) {
var resource RenderedResource

tpl, err := template.New(path.Base(filepath)).Funcs(templateFuncs(ctx, rs)).Option(failOnMissingKeys).ParseFiles(filepath)
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

err = tpl.Execute(&b, rs.Values)

if err != nil {
return "", fmt.Errorf("Error while templating %s: %v", filename, err)
return resource, fmt.Errorf("Error while templating %s: %v", filepath, err)
}

resource = RenderedResource{
Filename: path.Base(filepath),
Rendered: b.String(),
}

return b.String(), nil
return resource, nil
}

// Applies the limits of explicitly included or excluded resources and returns the updated resource set.
Expand Down

0 comments on commit c91cb21

Please sign in to comment.