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