1
0
mirror of https://github.com/rancher/rke.git synced 2025-07-10 13:54:04 +00:00

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.
This commit is contained in:
rajashree 2019-11-08 16:28:00 -08:00 committed by Alena Prokharchyk
parent 2006a61712
commit 9c1c0ea999
2 changed files with 48 additions and 19 deletions

View File

@ -20,6 +20,7 @@ import (
"github.com/rancher/rke/log" "github.com/rancher/rke/log"
"github.com/rancher/rke/services" "github.com/rancher/rke/services"
"github.com/rancher/rke/util" "github.com/rancher/rke/util"
"github.com/rancher/types/apis/management.cattle.io/v3"
"github.com/sirupsen/logrus" "github.com/sirupsen/logrus"
"gopkg.in/yaml.v2" "gopkg.in/yaml.v2"
) )
@ -50,6 +51,9 @@ type ingressOptions struct {
Options map[string]string Options map[string]string
NodeSelector map[string]string NodeSelector map[string]string
ExtraArgs map[string]string ExtraArgs map[string]string
ExtraEnvs []v3.ExtraEnv
ExtraVolumes []v3.ExtraVolume
ExtraVolumeMounts []v3.ExtraVolumeMount
DNSPolicy string DNSPolicy string
AlpineImage string AlpineImage string
IngressImage string IngressImage string
@ -491,6 +495,9 @@ func (c *Cluster) deployIngress(ctx context.Context, data map[string]interface{}
DNSPolicy: c.Ingress.DNSPolicy, DNSPolicy: c.Ingress.DNSPolicy,
IngressImage: c.SystemImages.Ingress, IngressImage: c.SystemImages.Ingress,
IngressBackend: c.SystemImages.IngressBackend, IngressBackend: c.SystemImages.IngressBackend,
ExtraEnvs: c.Ingress.ExtraEnvs,
ExtraVolumes: c.Ingress.ExtraVolumes,
ExtraVolumeMounts: c.Ingress.ExtraVolumeMounts,
} }
// since nginx ingress controller 0.16.0, it can be run as non-root and doesn't require privileged anymore. // since nginx ingress controller 0.16.0, it can be run as non-root and doesn't require privileged anymore.
// So we can use securityContext instead of setting privileges via initContainer. // So we can use securityContext instead of setting privileges via initContainer.

View File

@ -4,18 +4,24 @@ import (
"bytes" "bytes"
"encoding/json" "encoding/json"
"fmt" "fmt"
"github.com/blang/semver" "strings"
"github.com/rancher/kontainer-driver-metadata/rke/templates"
"github.com/sirupsen/logrus"
"text/template" "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/norman/types/convert"
"github.com/rancher/rke/metadata" "github.com/rancher/rke/metadata"
"github.com/sirupsen/logrus"
) )
func CompileTemplateFromMap(tmplt string, configMap interface{}) (string, error) { func CompileTemplateFromMap(tmplt string, configMap interface{}) (string, error) {
out := new(bytes.Buffer) out := new(bytes.Buffer)
t := template.Must(template.New("compiled_template").Funcs(template.FuncMap{"GetKubednsStubDomains": GetKubednsStubDomains}).Parse(tmplt)) 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 { if err := t.Execute(out, configMap); err != nil {
return "", err return "", err
} }
@ -34,6 +40,22 @@ func GetKubednsStubDomains(stubDomains map[string][]string) string {
return string(json) 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) { func getTemplate(templateName, k8sVersion string) (string, error) {
versionData := metadata.K8sVersionToTemplates[templateName] versionData := metadata.K8sVersionToTemplates[templateName]
toMatch, err := semver.Make(k8sVersion[1:]) toMatch, err := semver.Make(k8sVersion[1:])