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

Commit

Permalink
Add readTemplate function for descriptor templates (#36, #37)
Browse files Browse the repository at this point in the history
  • Loading branch information
wtschreiter authored Jun 9, 2019
1 parent fc41756 commit 64158fa
Show file tree
Hide file tree
Showing 12 changed files with 121 additions and 12 deletions.
3 changes: 2 additions & 1 deletion internal/pkg/rancher/client/app_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package client
import (
"fmt"
"reflect"
"strings"

projectModel "github.com/bitgrip/cattlectl/internal/pkg/rancher/project/model"
"github.com/rancher/norman/types"
Expand Down Expand Up @@ -148,7 +149,7 @@ func (client *appClient) Upgrade() error {
ExternalID: externalID,
}
if client.app.ValuesYaml != "" {
if installedApp.ValuesYaml == client.app.ValuesYaml {
if strings.TrimSpace(installedApp.ValuesYaml) == strings.TrimSpace(client.app.ValuesYaml) {
client.logger.Debug("Skip upgrade app - no changes")
return nil
}
Expand Down
53 changes: 50 additions & 3 deletions internal/pkg/template/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,11 @@ func BuildTemplate(templateData []byte, values map[string]interface{}, baseDir s
}
descriptorTemplate := template.New("data-template")
descriptorTemplate.Funcs(template.FuncMap{
"read": readFunc(absBaseDir),
"indent": indent,
"toYaml": toYaml,
"read": readFunc(absBaseDir),
"readWithTemplate": readTemplateFunc(baseDir, values),
"indent": indent,
"trim": trim,
"toYaml": toYaml,
})
if truncated {
descriptorTemplate.Funcs(template.FuncMap{
Expand Down Expand Up @@ -84,6 +86,41 @@ func readFunc(baseDir string) func(fileName string) []byte {
}
}

func readTemplateFunc(baseDir string, values map[string]interface{}) func(fileName string) string {
return func(fileName string) string {

var absFileName string
if filepath.IsAbs(fileName) {
absFileName = fileName
} else {
absFileName = filepath.Clean(fmt.Sprintf("%s/%s", baseDir, fileName))
}

fileContent, err := ioutil.ReadFile(absFileName)
if err != nil {
log.Fatal(err)
}

descriptorTemplate := template.New(absFileName)
descriptorTemplate.Funcs(template.FuncMap{
"read": readFunc(filepath.Dir(absFileName)),
"indent": indent,
"toYaml": toYaml,
"base64": toBase64,
})
descriptorTemplate, err = descriptorTemplate.Parse(string(fileContent))
if err != nil {
log.Fatal(err)
}
descriptorTemplate = descriptorTemplate.Option("missingkey=error")
var templateBuffer bytes.Buffer
if err := descriptorTemplate.Execute(&templateBuffer, values); err != nil {
log.Fatal(err)
}
return string(templateBuffer.Bytes())
}
}

func toBase64(data interface{}) string {
var encoded string
if bytes, isBytes := data.([]byte); isBytes {
Expand Down Expand Up @@ -113,6 +150,16 @@ func indent(indents int, data interface{}) string {
return prefix + result
}

func trim(data interface{}) string {
var toTrim string
if bytes, isBytes := data.([]byte); isBytes {
toTrim = string(bytes)
} else {
toTrim = fmt.Sprint(data)
}
return strings.TrimSpace(toTrim)
}

func toYaml(data interface{}) (string, error) {
marshalledYaml, err := yaml.Marshal(data)
if err != nil {
Expand Down
1 change: 1 addition & 0 deletions internal/pkg/template/testdata/read-template/input.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{{readWithTemplate "template.txt" | trim }}
3 changes: 3 additions & 0 deletions internal/pkg/template/testdata/read-template/read-file1.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
line1
line2
line3
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
line1
line2
line3

line1
line2
line3
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
line1
line2
line3

line1
line2
line3
3 changes: 3 additions & 0 deletions internal/pkg/template/testdata/read-template/template.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{{read .file_name1 | indent 4 }}

{{read .file_name1 | indent 5 }}
15 changes: 15 additions & 0 deletions sample/project-with-app-with-values-yaml/app-values.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
---
wordpressUsername: "{{ .wordpress.username }}"
wordpressPassword: "{{ .wordpress.password }}"
wordpressEmail: "{{ .wordpress.email }}"
mariadb:
master:
persistence:
enabled: false
persistence:
enabled: false
ingress:
enabled: true
hosts:
- name: xip.io
serviceType: ClusterIP
15 changes: 15 additions & 0 deletions sample/project-with-app-with-values-yaml/project.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
---
api_version: v1.0
kind: Project
metadata:
name: "{{ .project_name }}"
namespaces:
- name: "{{ .project_name }}-web"
apps:
- name: blog-wordpress-from-values-yaml
catalog: library
chart: wordpress
version: "2.1.10"
namespace: "{{ .project_name }}-web"
values_yaml: |
{{readWithTemplate "app-values.yaml" | indent 2 | trim}}
5 changes: 5 additions & 0 deletions sample/project-with-app-with-values-yaml/values1.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
project_name: test-project
wordpress:
username: 'my-wordpress-user'
password: 'a-simple-password-to-be-changed'
email: 'info@my-wordpress-blog'
5 changes: 5 additions & 0 deletions sample/project-with-app-with-values-yaml/values2.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
project_name: test-project
wordpress:
username: 'my-wordpress-user'
password: 'a-simple-password-to-be-changed-changed'
email: 'info@my-wordpress-blog'
16 changes: 8 additions & 8 deletions sample/project-with-app/project.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,21 @@
api_version: v1.0
kind: Project
metadata:
name: {{ .project_name }}
name: "{{ .project_name }}"
namespaces:
- name: {{ .project_name }}-web
- name: "{{ .project_name }}-web"
apps:
- name: blog-wordpress
catalog: library
chart: wordpress
version: "2.1.10"
namespace: {{ .project_name }}-web
namespace: "{{ .project_name }}-web"
answers:
wordpressUsername: {{ .wordpress.username }}
wordpressPassword: {{ .wordpress.password }}
wordpressEmail: {{ .wordpress.email }}
mariadb.master.persistence.enabled: 'false'
persistence.enabled: 'false'
wordpressUsername: "{{ .wordpress.username }}"
wordpressPassword: "{{ .wordpress.password }}"
wordpressEmail: "{{ .wordpress.email }}"
mariadb.master.persistence.enabled: "false"
persistence.enabled: "false"
ingress.enabled: true
'ingress.hosts[0].name': xip.io
serviceType: ClusterIP

0 comments on commit 64158fa

Please sign in to comment.