1
0
mirror of https://github.com/rancher/rke.git synced 2025-08-20 07:45:15 +00:00
rke/templates/templates.go
rajashree 9c1c0ea999 Accept extraEnv, volumes and volumeMounts for ingress addon
The fields for ExtraEnv, extraVolumes and extraVolumeMounts for ingress
addon refer the k8s native types EnvVar, Volume and VolumeMounts.
The k8s native types have json tags, so this commit adds a template func to
first marshal and get json encoding and then convert to yaml.
2019-11-14 10:54:00 -08:00

77 lines
2.3 KiB
Go

package templates
import (
"bytes"
"encoding/json"
"fmt"
"strings"
"text/template"
"github.com/Masterminds/sprig/v3"
"github.com/blang/semver"
"github.com/ghodss/yaml"
"github.com/rancher/kontainer-driver-metadata/rke/templates"
"github.com/rancher/norman/types/convert"
"github.com/rancher/rke/metadata"
"github.com/sirupsen/logrus"
)
func CompileTemplateFromMap(tmplt string, configMap interface{}) (string, error) {
out := new(bytes.Buffer)
templateFuncMap := sprig.TxtFuncMap()
templateFuncMap["GetKubednsStubDomains"] = GetKubednsStubDomains
templateFuncMap["toYaml"] = ToYAML
t := template.Must(template.New("compiled_template").Funcs(templateFuncMap).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, error) {
if template, ok := data[templateName]; ok {
return convert.ToString(template), nil
}
return getTemplate(templateName, k8sVersion)
}
func GetKubednsStubDomains(stubDomains map[string][]string) string {
json, _ := json.Marshal(stubDomains)
return string(json)
}
func ToYAML(v interface{}) string {
data, err := json.Marshal(v)
if err != nil {
// Swallow errors inside of a template so it doesn't affect remaining template lines
logrus.Errorf("[ToYAML] Error marshaling %v: %v", v, err)
return ""
}
yamlData, err := yaml.JSONToYAML(data)
if err != nil {
// Swallow errors inside of a template so it doesn't affect remaining template lines
logrus.Errorf("[ToYAML] Error converting json to yaml for %v: %v ", string(data), err)
return ""
}
return strings.TrimSuffix(string(yamlData), "\n")
}
func getTemplate(templateName, k8sVersion string) (string, error) {
versionData := metadata.K8sVersionToTemplates[templateName]
toMatch, err := semver.Make(k8sVersion[1:])
if err != nil {
return "", fmt.Errorf("k8sVersion not sem-ver %s %v", k8sVersion, err)
}
for k := range versionData {
testRange, err := semver.ParseRange(k)
if err != nil {
logrus.Errorf("range for %s not sem-ver %v %v", templateName, testRange, err)
continue
}
if testRange(toMatch) {
return metadata.K8sVersionToTemplates[templates.TemplateKeys][versionData[k]], nil
}
}
return "", fmt.Errorf("no %s template found for k8sVersion %s", templateName, k8sVersion)
}