From e3f440f40c64265fd3cb2e0a3c8e7d44b1022550 Mon Sep 17 00:00:00 2001
From: zhangxiaoyu <zhangxiaoyu58@huawei.com>
Date: Wed, 8 Sep 2021 17:11:43 +0800
Subject: [PATCH 2/4] add coredns checker

Signed-off-by: zhangxiaoyu <zhangxiaoyu58@huawei.com>
---
 cmd/api.go                                    |  1 +
 cmd/configs.go                                | 20 +++++++++++--------
 cmd/join.go                                   |  9 +++++++--
 config/centos.config                          |  7 ++++---
 config/openEuler.config                       |  5 +++--
 docs/manual.md                                |  7 ++++---
 .../binary/coredns/coredns.go                 |  8 ++++++++
 pkg/utils/infra/infra.go                      |  6 ++++++
 8 files changed, 45 insertions(+), 18 deletions(-)

diff --git a/cmd/api.go b/cmd/api.go
index fdc8949..538c2e0 100644
--- a/cmd/api.go
+++ b/cmd/api.go
@@ -28,6 +28,7 @@ type InstallConfig struct {
 	LoadBalance      []*PackageConfig            `yaml:"loadbalance"`
 	Container        []*PackageConfig            `yaml:"container"`
 	Image            []*PackageConfig            `yaml:"image"`
+	Dns              []*PackageConfig            `yaml:"dns"`
 	Addition         map[string][]*PackageConfig `yaml:"addition"` // key: master, worker, etcd, loadbalance
 }
 
diff --git a/cmd/configs.go b/cmd/configs.go
index 7555a92..dfc4c45 100644
--- a/cmd/configs.go
+++ b/cmd/configs.go
@@ -28,6 +28,7 @@ import (
 
 	"github.com/sirupsen/logrus"
 	"isula.org/eggo/pkg/api"
+	"isula.org/eggo/pkg/clusterdeployment/binary/coredns"
 	"isula.org/eggo/pkg/constants"
 	"isula.org/eggo/pkg/utils"
 	"isula.org/eggo/pkg/utils/infra"
@@ -279,6 +280,10 @@ func fillPackageConfig(ccfg *api.ClusterConfig, icfg *InstallConfig) {
 		ccfg.RoleInfra[s.role].Softwares = appendSoftware(ccfg.RoleInfra[s.role].Softwares, s.pc, s.dpc)
 	}
 
+	if coredns.IsTypeBinary(ccfg.ServiceCluster.DNS.CorednsType) {
+		ccfg.RoleInfra[api.Master].Softwares = appendSoftware(ccfg.RoleInfra[api.Master].Softwares, ToEggoPackageConfig(icfg.Dns), infra.DnsPackages)
+	}
+
 	if len(icfg.Addition) == 0 {
 		return
 	}
@@ -306,12 +311,9 @@ func fillOpenPort(ccfg *api.ClusterConfig, openports map[string][]*OpenPorts, dn
 		ccfg.RoleInfra[role].OpenPorts = append(ccfg.RoleInfra[role].OpenPorts, ToEggoOpenPort(p)...)
 	}
 
-	if dnsType == "binary" || dnsType == "" {
+	if coredns.IsTypeBinary(dnsType) {
 		ccfg.RoleInfra[api.Master].OpenPorts =
 			append(ccfg.RoleInfra[api.Master].OpenPorts, infra.CorednsPorts...)
-	} else if dnsType == "pod" {
-		ccfg.RoleInfra[api.Worker].OpenPorts =
-			append(ccfg.RoleInfra[api.Worker].OpenPorts, infra.CorednsPorts...)
 	}
 }
 
@@ -766,6 +768,12 @@ func createDeployConfigTemplate(file string) error {
 					Type: "image",
 				},
 			},
+			Dns: []*PackageConfig{
+				{
+					Name: "coredns",
+					Type: "pkg",
+				},
+			},
 			Addition: map[string][]*PackageConfig{
 				"master": {
 					{
@@ -778,10 +786,6 @@ func createDeployConfigTemplate(file string) error {
 						Name: "calico.yaml",
 						Type: "yaml",
 					},
-					{
-						Name: "coredns",
-						Type: "pkg",
-					},
 				},
 				"worker": {
 					{
diff --git a/cmd/join.go b/cmd/join.go
index 7d0b4a1..79d68fc 100644
--- a/cmd/join.go
+++ b/cmd/join.go
@@ -117,8 +117,13 @@ func getMergedAndDiffConfigs(conf *DeployConfig, joinConf *DeployConfig) (*Deplo
 		if getHostConfigByIp(mergedConfig.Workers, host.Ip) != nil {
 			continue
 		}
-		h := createHostConfig(getHostConfigByIp(allHostConfigs, host.Ip), host,
-			defaultHostName(conf.ClusterID, "worker", len(conf.Workers)+i))
+
+		h := getHostConfigByIp(diffConfig.Masters, host.Ip)
+		if h == nil {
+			h = createHostConfig(getHostConfigByIp(allHostConfigs, host.Ip), host,
+				defaultHostName(conf.ClusterID, "worker", len(conf.Workers)+i))
+		}
+
 		mergedConfig.Workers = append(mergedConfig.Workers, h)
 		diffConfig.Workers = append(diffConfig.Workers, h)
 	}
diff --git a/config/centos.config b/config/centos.config
index c9b0d0e..a5a2924 100755
--- a/config/centos.config
+++ b/config/centos.config
@@ -54,13 +54,14 @@ install:
   image:
   - name: images.tar
     type: image
+  dns:
+  - name: coredns
+    type: bin
+    dst: /usr/bin
   addition:
     master:
     - name: calico.yaml
       type: yaml
-    - name: coredns
-      type: bin
-      dst: /usr/bin
     worker:
     - name: conntrack,socat
       type: bin
diff --git a/config/openEuler.config b/config/openEuler.config
index 90e5892..07acf9b 100755
--- a/config/openEuler.config
+++ b/config/openEuler.config
@@ -36,9 +36,10 @@ install:
   image:
   - name: images.tar
     type: image
+  dns:
+  - name: coredns
+    type: pkg
   addition:
     master:
     - name: calico.yaml
       type: yaml
-    - name: coredns
-      type: pkg
diff --git a/docs/manual.md b/docs/manual.md
index 0957069..a4913b1 100644
--- a/docs/manual.md
+++ b/docs/manual.md
@@ -300,6 +300,10 @@ install:                                    // 配置各种类型节点上需要
   - name: pause.tar
     type: image
     dst: ""
+  dns:                                      // k8s coredns安装包。如果corednstype配置为pod，此处无需配置
+  - name: coredns
+    type: pkg
+    dst: ""
   addition:                                 // 额外的安装包或二进制文件列表
     master:
     - name: prejoin.sh
@@ -309,9 +313,6 @@ install:                                    // 配置各种类型节点上需要
     - name: calico.yaml
       type: yaml
       dst: ""
-    - name: coredns
-      type: pkg
-      dst: ""
     worker:
     - name: docker.service
       type: file
diff --git a/pkg/clusterdeployment/binary/coredns/coredns.go b/pkg/clusterdeployment/binary/coredns/coredns.go
index 35edb08..a33c71f 100644
--- a/pkg/clusterdeployment/binary/coredns/coredns.go
+++ b/pkg/clusterdeployment/binary/coredns/coredns.go
@@ -33,6 +33,14 @@ func init() {
 	cbs[CorednsTypeOfBinary] = &BinaryCoredns{}
 }
 
+func IsTypeBinary(configType string) bool {
+	return configType == CorednsTypeOfBinary || configType == ""
+}
+
+func IsTypePod(configType string) bool {
+	return configType == CorednsTypeOfPod
+}
+
 func getTypeOfCoredns(configType string) string {
 	if configType != "" {
 		return configType
diff --git a/pkg/utils/infra/infra.go b/pkg/utils/infra/infra.go
index b0c8ecd..2b36e2a 100644
--- a/pkg/utils/infra/infra.go
+++ b/pkg/utils/infra/infra.go
@@ -110,6 +110,12 @@ var (
 	}
 
 	// coredns
+	DnsPackages = []*api.PackageConfig{
+		{
+			Name: "coredns",
+			Type: "repo",
+		},
+	}
 	CorednsPorts = []*api.OpenPorts{
 		{
 			Port:     53,
-- 
2.25.1

