Skip to content

Commit

Permalink
Added directory rendering
Browse files Browse the repository at this point in the history
  • Loading branch information
JorgenEvens committed May 5, 2019
1 parent 4c55d1f commit 3ffb5ee
Show file tree
Hide file tree
Showing 5 changed files with 130 additions and 0 deletions.
2 changes: 2 additions & 0 deletions example/directory/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<!doctype html>
<title>*</title>
32 changes: 32 additions & 0 deletions example/directory/nginx.$.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# kubetpl:syntax:$

apiVersion: v1
kind: ConfigMap
metadata:
name: $NAME
data:
index.html: "$MESSAGE"
---
apiVersion: apps/v1beta1
kind: Deployment
metadata:
name: $NAME
spec:
replicas: 1
template:
metadata:
labels:
app: $NAME
spec:
containers:
- name: nginx
image: nginx:1.7.9
ports:
- containerPort: 80
volumeMounts:
- name: $NAME-volume
mountPath: /usr/share/nginx/html
volumes:
- name: $NAME-volume
configMap:
name: $NAME
32 changes: 32 additions & 0 deletions example/directory/nginx.go-template.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# kubetpl:syntax:go-template

apiVersion: v1
kind: ConfigMap
metadata:
name: {{ .NAME }}
data:
index.html: {{ .MESSAGE | quote }}
---
apiVersion: apps/v1beta1
kind: Deployment
metadata:
name: {{ .NAME }}
spec:
replicas: 1
template:
metadata:
labels:
app: {{ .NAME }}
spec:
containers:
- name: nginx
image: nginx:1.7.9
ports:
- containerPort: 80
volumeMounts:
- name: {{ .NAME }}-volume
mountPath: /usr/share/nginx/html
volumes:
- name: {{ .NAME }}-volume
configMap:
name: {{ .NAME }}
49 changes: 49 additions & 0 deletions kubetpl.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"net/http"
"os"
"path/filepath"
"sort"
"strings"
)

Expand Down Expand Up @@ -270,7 +271,55 @@ type renderOpts struct {
ignoreUnset bool
}

func expandDirectories(templateFiles []string) ([]string, error) {
var templates []string

extensions := sort.StringSlice{".yaml", ".yml", ".json"}
extensionCount := len(extensions)
sort.Strings(extensions)

for _, filename := range templateFiles {
fileInfo, err := os.Lstat(filename)
if err != nil {
return nil, err
}

if !fileInfo.Mode().IsDir() {
ext := filepath.Ext(filename)
idx := extensions.Search(ext)
if idx < extensionCount && extensions[idx] == ext {
templates = append(templates, filename)
}
continue
}

subfiles, err := ioutil.ReadDir(filename)
if err != nil {
return nil, err
}

var files []string
for _, file := range subfiles {
full := filepath.Join(filename, file.Name())
files = append(files, full)
}

files, err = expandDirectories(files)
if err != nil {
return nil, err
}

templates = append(templates, files...)
}

return templates, nil
}

func render(templateFiles []string, data map[string]interface{}, opts renderOpts) ([]byte, error) {
templateFiles, err := expandDirectories(templateFiles)
if err != nil {
return nil, err
}
objs, err := renderTemplates(templateFiles, data, opts)
if err != nil {
return nil, err
Expand Down
15 changes: 15 additions & 0 deletions kubetpl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,21 @@ func TestRender(t *testing.T) {
}
}

func TestRenderDirectory(t *testing.T) {
cfg := map[string]interface{}{
"NAME": "nm",
"MESSAGE": "msg",
}
opts := renderOpts{}
renderedSh, err := render([]string{"example/directory"}, cfg, opts)
if err != nil {
t.Fatal(err)
}
if len(renderedSh) == 0 {
t.Fatal("len(rendered) == 0")
}
}

func TestRenderWithDataFromFile(t *testing.T) {
// todo: test secret ("data" must be base64-encoded)
src := []string{"example/nginx-with-data-from-file.yml"}
Expand Down

0 comments on commit 3ffb5ee

Please sign in to comment.