From f5ee757b86f8d04c364df0124af9e49915887a0e Mon Sep 17 00:00:00 2001 From: Hussein Galal Date: Mon, 22 Jun 2020 23:06:01 +0200 Subject: [PATCH] Add cluster dns configmap (#1785) --- pkg/server/server.go | 46 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) diff --git a/pkg/server/server.go b/pkg/server/server.go index 1da457ce2d..a8f067325d 100644 --- a/pkg/server/server.go +++ b/pkg/server/server.go @@ -13,6 +13,8 @@ import ( "strings" "time" + corev1 "k8s.io/api/core/v1" + "github.com/pkg/errors" "github.com/rancher/helm-controller/pkg/helm" "github.com/rancher/k3s/pkg/clientaccess" @@ -137,6 +139,9 @@ func runControllers(ctx context.Context, config *Config) error { if !config.DisableAgent { go setMasterRoleLabel(ctx, sc.Core.Core().V1().Node()) } + + go setClusterDNSConfig(ctx, config, sc.Core.Core().V1().ConfigMap()) + if controlConfig.NoLeaderElect { go func() { start(ctx) @@ -430,3 +435,44 @@ func setMasterRoleLabel(ctx context.Context, nodes v1.NodeClient) error { } return nil } + +func setClusterDNSConfig(ctx context.Context, controlConfig *Config, configMap v1.ConfigMapClient) error { + nodeName := os.Getenv("NODE_NAME") + // check if configmap already exists + _, err := configMap.Get("kube-system", "cluster-dns", metav1.GetOptions{}) + if err == nil { + logrus.Infof("cluster dns configmap already exists") + return nil + } + clusterDNS := controlConfig.ControlConfig.ClusterDNS + clusterDomain := controlConfig.ControlConfig.ClusterDomain + c := &corev1.ConfigMap{ + TypeMeta: metav1.TypeMeta{ + Kind: "ConfigMap", + APIVersion: "v1", + }, + ObjectMeta: metav1.ObjectMeta{ + Name: "cluster-dns", + Namespace: "kube-system", + }, + Data: map[string]string{ + "clusterDNS": clusterDNS.String(), + "clusterDomain": clusterDomain, + }, + } + for { + _, err = configMap.Create(c) + if err == nil { + logrus.Infof("cluster dns configmap has been set successfully") + break + } + logrus.Infof("Waiting for master node %s startup: %v", nodeName, err) + + select { + case <-ctx.Done(): + return ctx.Err() + case <-time.After(time.Second): + } + } + return nil +}