1
0
mirror of https://github.com/rancher/rke.git synced 2025-04-28 03:31:24 +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/services"
"github.com/rancher/rke/util"
"github.com/rancher/types/apis/management.cattle.io/v3"
"github.com/sirupsen/logrus"
"gopkg.in/yaml.v2"
)
@ -46,14 +47,17 @@ const (
var DNSProviders = []string{KubeDNSProvider, CoreDNSProvider}
type ingressOptions struct {
RBACConfig string
Options map[string]string
NodeSelector map[string]string
ExtraArgs map[string]string
DNSPolicy string
AlpineImage string
IngressImage string
IngressBackend string
RBACConfig string
Options map[string]string
NodeSelector map[string]string
ExtraArgs map[string]string
ExtraEnvs []v3.ExtraEnv
ExtraVolumes []v3.ExtraVolume
ExtraVolumeMounts []v3.ExtraVolumeMount
DNSPolicy string
AlpineImage string
IngressImage string
IngressBackend string
}
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)
ingressConfig := ingressOptions{
RBACConfig: c.Authorization.Mode,
Options: c.Ingress.Options,
NodeSelector: c.Ingress.NodeSelector,
ExtraArgs: c.Ingress.ExtraArgs,
DNSPolicy: c.Ingress.DNSPolicy,
IngressImage: c.SystemImages.Ingress,
IngressBackend: c.SystemImages.IngressBackend,
RBACConfig: c.Authorization.Mode,
Options: c.Ingress.Options,
NodeSelector: c.Ingress.NodeSelector,
ExtraArgs: c.Ingress.ExtraArgs,
DNSPolicy: c.Ingress.DNSPolicy,
IngressImage: c.SystemImages.Ingress,
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.
// So we can use securityContext instead of setting privileges via initContainer.

View File

@ -4,18 +4,24 @@ import (
"bytes"
"encoding/json"
"fmt"
"github.com/blang/semver"
"github.com/rancher/kontainer-driver-metadata/rke/templates"
"github.com/sirupsen/logrus"
"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)
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 {
return "", err
}
@ -34,6 +40,22 @@ func GetKubednsStubDomains(stubDomains map[string][]string) string {
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:])