-
Notifications
You must be signed in to change notification settings - Fork 0
/
prepare.py
214 lines (158 loc) · 7 KB
/
prepare.py
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
#!/usr/bin/env python
import argparse
import ruamel.yaml
import os
try:
import configparser
except ImportError:
import ConfigParser as configparser
from ruamel.yaml.util import load_yaml_guess_indent
# Define path
BASEPATH = os.path.abspath(os.path.dirname(__file__))
K8S_CLUSTER_CONFIG_PATH = os.path.abspath(
BASEPATH + '/../inventory/local/group_vars/k8s-cluster.yml')
INVENTORY_CONFIG_PATH = os.path.abspath(
BASEPATH + '/../inventory/inventory.cfg')
POSTINSTALL_CONFIG_PATH = os.path.abspath(
BASEPATH + '/roles/postinstall/defaults/main.yml')
# Define ansible host string format
SINGLE_HOST_TPL = 'master ansible_ssh_host={0} ansible_python_interpreter={1} # ip=10.3.0.1'
MULTI_HOST_TPL = '{0} ansible_ssh_host={1} ansible_python_interpreter={2} # ip=10.3.0.{3}'
# Define args
config_parser = argparse.ArgumentParser(add_help=False)
# Kubernetes cluster options
config_parser.add_argument(
'--network', help='Cluster network plugin. Default: flannel', default='flannel',
choices=["calico", "contiv", "weave", "flannel", "cloud"])
config_parser.add_argument(
'--enable-helm', help='Enable helm. Default: False', action="store_true")
config_parser.add_argument(
'--helm-deployment-type', help='Helm deployment type. Default: docker', choices=["host", "docker"], default='docker')
config_parser.add_argument(
'--enable-local-volumes', help='Enable local volumes. Default: False', action="store_true")
config_parser.add_argument(
'--disable-swap-fail', help='Disable swap fail. Default: False', action="store_false")
# Inventory options
config_parser.add_argument(
'--master-ips', help='Master IP list: 1.2.3.4 1.2.3.5 ... 1.2.3.n', nargs='+', required=True)
config_parser.add_argument(
'--python-interpreter', help='Ansible remote python interpreter. Default: /usr/bin/python3', default='/usr/bin/python3')
# Postinstall options
config_parser.add_argument(
'--enable-kube-shell', help='Install kube-shell. Default: False', action="store_true")
config_parser.add_argument(
'--enable-monitoring', help='Install Elasticsearch/Fluentd/Kibana monitoring apps. Default: False', action="store_true")
config_parser.add_argument(
'--enable-ingress', help='Install simple nginx ingress. Default: False', action="store_true")
parser = argparse.ArgumentParser(add_help=True)
subparsers = parser.add_subparsers(help='Available Types', dest='type')
singlenode_parser = subparsers.add_parser(
'singlenode', help='Single node cluster', parents=[config_parser])
multinode_parser = subparsers.add_parser(
'multinode', help='Multi node cluster', parents=[config_parser])
multinode_parser.add_argument(
'--node-ips', help='Nodes IP list: 1.2.3.4 1.2.3.5 ... 1.2.3.n', nargs='+', required=True)
multinode_parser.add_argument(
'--etcd-ips', help='Etcd IP list: 1.2.3.4 1.2.3.5 ... 1.2.3.n', nargs='+')
multinode_parser.add_argument(
'--master-as-node', help='Use master as node. Default: False', action="store_true")
multinode_parser.add_argument(
'--loadbalancer', help='External loadbalancer. Default: None')
# Configure yaml files
def set_yaml_config(config_path, options):
config, ind, bsi = load_yaml_guess_indent(
open(config_path), preserve_quotes=True)
for key in options:
config[key] = options[key]
ruamel.yaml.round_trip_dump(config, open(config_path, 'w'),
width=8000, indent=2, block_seq_indent=bsi)
# Configure inventory/local/group_vars/k8s-cluster.yml
def prepare_k8s_cluster_config(args):
config = {}
config["kube_network_plugin"] = args.network
config["helm_enabled"] = args.enable_helm
config["helm_deployment_type"] = args.helm_deployment_type
config["kubelet_fail_swap_on"] = args.disable_swap_fail
config["local_volumes_enabled"] = args.enable_local_volumes
config["ingress_nginx_enabled"] = args.enable_ingress
config["efk_enabled"] = args.enable_monitoring
set_yaml_config(K8S_CLUSTER_CONFIG_PATH, config)
# Init config for inventory
def inventory_config_init():
config = configparser.ConfigParser(allow_no_value=True)
config.add_section('all')
config.add_section('kube-master')
config.add_section('etcd')
config.add_section('kube-node')
config.add_section('k8s-cluster:children')
config.add_section('k8s-cluster:vars')
return config
# Write inventory config file
def inventory_config_write(config):
cfgfile = open(INVENTORY_CONFIG_PATH, 'w')
config.write(cfgfile)
cfgfile.close()
# Set singlenode inventory
def inventory_config_single(args, config):
ansible_ssh_host = args.master_ips[0]
config.set('all', SINGLE_HOST_TPL.format(
ansible_ssh_host, args.python_interpreter))
config.set('kube-master', 'master')
config.set('etcd', 'master')
config.set('kube-node', 'master')
return config
# Set nodes (workers or etcd if defined)
def set_nodes(node_type, args, config, config_section):
if node_type == 'node':
ips = args.node_ips
if node_type == 'etcd':
ips = args.etcd_ips
for n in range(len(ips)):
ansible_ssh_host = ips[n]
node_name = node_type + '-' + str(n + 1)
config.set('all', MULTI_HOST_TPL.format(
node_name, ansible_ssh_host, args.python_interpreter, str(n + 10)))
config.set(config_section, node_name)
return config
# Set multinode inventory
def inventory_config_multi(args, config):
etcd_on_masters = True
if args.etcd_ips is not None:
config = set_nodes('etcd', args, config, 'etcd')
etcd_on_masters = False
for m in range(len(args.master_ips)):
ansible_ssh_host = args.master_ips[m]
master_name = 'master-' + str(m + 1)
config.set('all', MULTI_HOST_TPL.format(
master_name, ansible_ssh_host, args.python_interpreter, str(m + 1)))
config.set('kube-master', master_name)
if etcd_on_masters:
config.set('etcd', master_name)
if args.master_as_node:
config.set('kube-node', master_name)
config = set_nodes('node', args, config, 'kube-node')
return config
# Configure inventory/inventory.cfg
def prepare_inventory(args):
config = inventory_config_init()
if args.type == 'singlenode':
config = inventory_config_single(args, config)
if args.type == 'multinode':
config = inventory_config_multi(args, config)
config.set('k8s-cluster:children', 'kube-node')
config.set('k8s-cluster:children', 'kube-master')
if args.loadbalancer is not None:
config.set('k8s-cluster:vars',
'apiserver_loadbalancer_domain_name', '"' + args.loadbalancer + '"')
inventory_config_write(config)
# Configure roles/postinstall/defaults/main.yml
def prepare_postinstall_config(args):
config = {}
config['kube_shell'] = args.enable_kube_shell
config['ingress'] = args.enable_ingress
set_yaml_config(POSTINSTALL_CONFIG_PATH, config)
if __name__ == "__main__":
args = parser.parse_args()
prepare_k8s_cluster_config(args)
prepare_inventory(args)
prepare_postinstall_config(args)