1
0
mirror of https://github.com/rancher/rke.git synced 2025-07-09 13:23:09 +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"
) )
@ -46,14 +47,17 @@ const (
var DNSProviders = []string{KubeDNSProvider, CoreDNSProvider} var DNSProviders = []string{KubeDNSProvider, CoreDNSProvider}
type ingressOptions struct { type ingressOptions struct {
RBACConfig string RBACConfig string
Options map[string]string Options map[string]string
NodeSelector map[string]string NodeSelector map[string]string
ExtraArgs map[string]string ExtraArgs map[string]string
DNSPolicy string ExtraEnvs []v3.ExtraEnv
AlpineImage string ExtraVolumes []v3.ExtraVolume
IngressImage string ExtraVolumeMounts []v3.ExtraVolumeMount
IngressBackend string DNSPolicy string
AlpineImage string
IngressImage string
IngressBackend string
} }
type MetricsServerOptions struct { type MetricsServerOptions struct {
@ -484,13 +488,16 @@ func (c *Cluster) deployIngress(ctx context.Context, data map[string]interface{}
} }
log.Infof(ctx, "[ingress] Setting up %s ingress controller", c.Ingress.Provider) log.Infof(ctx, "[ingress] Setting up %s ingress controller", c.Ingress.Provider)
ingressConfig := ingressOptions{ ingressConfig := ingressOptions{
RBACConfig: c.Authorization.Mode, RBACConfig: c.Authorization.Mode,
Options: c.Ingress.Options, Options: c.Ingress.Options,
NodeSelector: c.Ingress.NodeSelector, NodeSelector: c.Ingress.NodeSelector,
ExtraArgs: c.Ingress.ExtraArgs, ExtraArgs: c.Ingress.ExtraArgs,
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:])