-
Notifications
You must be signed in to change notification settings - Fork 116
/
configs.go
80 lines (70 loc) · 1.99 KB
/
configs.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
package clustermanager
import (
"fmt"
"strings"
)
//GenerateMasterConfiguration generate the kubernetes config for master
func GenerateMasterConfiguration(masterNode Node, masterNodes, etcdNodes []Node) string {
masterConfigTpl := `apiVersion: kubeadm.k8s.io/v1alpha1
kind: MasterConfiguration
api:
advertiseAddress: %s
networking:
podSubnet: 10.244.0.0/16
apiServerCertSANs:
- %s
- 127.0.0.1
`
etcdConfig := `etcd:
endpoints:`
masterConfig := fmt.Sprintf(masterConfigTpl, masterNode.PrivateIPAddress, masterNode.IPAddress)
for _, node := range masterNodes {
masterConfig = fmt.Sprintf("%s%s\n", masterConfig, " - "+node.PrivateIPAddress)
}
if len(etcdNodes) > 0 {
masterConfig = masterConfig + etcdConfig + "\n"
for _, node := range etcdNodes {
masterConfig = fmt.Sprintf("%s%s\n", masterConfig, " - http://"+node.PrivateIPAddress+":2379")
}
}
return masterConfig
}
//GenerateEtcdSystemdService generate configuration file used to manage etcd service on systemd
func GenerateEtcdSystemdService(node Node, etcdNodes []Node) string {
serviceTpls := `# /etc/systemd/system/etcd.service
[Unit]
Description=etcd
After=network.target [email protected]
[Service]
ExecStart=/opt/etcd/etcd --name %s \
--data-dir /var/lib/etcd \
--listen-client-urls "http://%s:2379,http://localhost:2379" \
--advertise-client-urls "http://%s:2379" \
--listen-peer-urls "http://%s:2380" \
--initial-cluster "%s" \
--initial-advertise-peer-urls "http://%s:2380" \
--heartbeat-interval 200 \
--election-timeout 5000
Restart=always
RestartSec=5
TimeoutStartSec=0
StartLimitInterval=0
[Install]
WantedBy=multi-user.target
`
var ips []string
for _, node := range etcdNodes {
ips = append(ips, fmt.Sprintf("%s=http://%s:2380", node.Name, node.PrivateIPAddress))
}
initialCluster := strings.Join(ips, ",")
service := fmt.Sprintf(
serviceTpls,
node.Name,
node.PrivateIPAddress,
node.PrivateIPAddress,
node.PrivateIPAddress,
initialCluster,
node.PrivateIPAddress,
)
return service
}