1
0
mirror of https://github.com/rancher/rke.git synced 2025-08-12 04:03:01 +00:00
rke/templates/templates.go

45 lines
1.3 KiB
Go
Raw Normal View History

package templates
import (
"bytes"
2019-03-12 20:40:19 +00:00
"encoding/json"
"text/template"
2019-02-26 23:14:01 +00:00
"github.com/rancher/norman/types/convert"
"github.com/rancher/rke/metadata"
2019-02-26 23:14:01 +00:00
"github.com/rancher/rke/util"
)
func CompileTemplateFromMap(tmplt string, configMap interface{}) (string, error) {
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))
if err := t.Execute(out, configMap); err != nil {
return "", err
}
return out.String(), nil
}
func GetVersionedTemplates(templateName string, data map[string]interface{}, k8sVersion string) string {
if template, ok := data[templateName]; ok {
return convert.ToString(template)
}
versionedTemplate := metadata.K8sVersionToTemplates[templateName]
2019-02-26 23:14:01 +00:00
if t, ok := versionedTemplate[util.GetTagMajorVersion(k8sVersion)]; ok {
return t
}
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)
}
func GetDefaultVersionedTemplate(templateName string, data map[string]interface{}) string {
if template, ok := data[templateName]; ok {
return convert.ToString(template)
}
return metadata.K8sVersionToTemplates[templateName]["default"]
}