2017-12-16 05:37:45 +02:00
|
|
|
package templates
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2019-03-12 21:40:19 +01:00
|
|
|
"encoding/json"
|
2017-12-16 05:37:45 +02:00
|
|
|
"text/template"
|
2019-02-27 01:14:01 +02:00
|
|
|
|
2019-06-17 13:52:15 -07:00
|
|
|
"github.com/rancher/norman/types/convert"
|
2019-05-28 11:51:53 -07:00
|
|
|
"github.com/rancher/rke/metadata"
|
|
|
|
|
2019-02-27 01:14:01 +02:00
|
|
|
"github.com/rancher/rke/util"
|
2017-12-16 05:37:45 +02:00
|
|
|
)
|
|
|
|
|
2018-02-01 23:28:31 +02:00
|
|
|
func CompileTemplateFromMap(tmplt string, configMap interface{}) (string, error) {
|
2017-12-16 05:37:45 +02:00
|
|
|
out := new(bytes.Buffer)
|
2019-03-12 21:40:19 +01:00
|
|
|
t := template.Must(template.New("compiled_template").Funcs(template.FuncMap{"GetKubednsStubDomains": GetKubednsStubDomains}).Parse(tmplt))
|
2017-12-16 05:37:45 +02:00
|
|
|
if err := t.Execute(out, configMap); err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return out.String(), nil
|
|
|
|
}
|
2019-01-08 22:04:42 +02:00
|
|
|
|
2019-06-17 13:52:15 -07:00
|
|
|
func GetVersionedTemplates(templateName string, data map[string]interface{}, k8sVersion string) string {
|
|
|
|
if template, ok := data[templateName]; ok {
|
|
|
|
return convert.ToString(template)
|
|
|
|
}
|
2019-05-28 11:51:53 -07:00
|
|
|
versionedTemplate := metadata.K8sVersionToTemplates[templateName]
|
2019-02-27 01:14:01 +02:00
|
|
|
if t, ok := versionedTemplate[util.GetTagMajorVersion(k8sVersion)]; ok {
|
|
|
|
return t
|
2019-01-08 22:04:42 +02:00
|
|
|
}
|
|
|
|
return versionedTemplate["default"]
|
|
|
|
}
|
2019-03-12 21:40:19 +01:00
|
|
|
|
|
|
|
func GetKubednsStubDomains(stubDomains map[string][]string) string {
|
|
|
|
json, _ := json.Marshal(stubDomains)
|
|
|
|
return string(json)
|
|
|
|
}
|
2019-05-28 11:51:53 -07:00
|
|
|
|
|
|
|
func GetDefaultVersionedTemplate(templateName string, data map[string]interface{}) string {
|
|
|
|
if template, ok := data[templateName]; ok {
|
|
|
|
return convert.ToString(template)
|
|
|
|
}
|
|
|
|
return metadata.K8sVersionToTemplates[templateName]["default"]
|
|
|
|
}
|