-
Notifications
You must be signed in to change notification settings - Fork 5
/
settings.rb
259 lines (233 loc) · 10.9 KB
/
settings.rb
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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
require 'fileutils'
require 'teracy-dev'
require 'teracy-dev/processors/processor'
require 'teracy-dev/util'
require 'teracy-dev/location/manager'
module TeracyDevK8s
module Processors
class Settings < TeracyDev::Processors::Processor
def process(settings)
k8s_config = settings['teracy-dev-k8s']
@logger.debug("k8s_config: #{k8s_config}")
# guest mode currently supports only 1 node
if k8s_config['ansible']['mode'] == 'guest' and k8s_config['num_instances'] > 1
@logger.error("ansible guest mode supports only 1 num_instances, you need to use 'host' mode instead")
@logger.error("and follow: https://github.com/kubernetes-incubator/kubespray#vagrant")
abort
end
setup(k8s_config)
nodes = generate_nodes(k8s_config)
@logger.debug("nodes: #{nodes}")
#abort
# should override
TeracyDev::Util.override(settings, {"nodes" => nodes})
end
private
def setup(k8s_config)
updated = sync_kubespray(k8s_config['kubespray'])
setup_inventory(k8s_config, updated)
end
def sync_kubespray(kubespray)
lookup_path = File.join(TeracyDev::BASE_DIR, kubespray['lookup_path'] ||= TeracyDev::DEFAULT_EXTENSION_LOOKUP_PATH)
path = File.join(lookup_path, 'kubespray')
kubespray['location'].merge!({
"lookup_path" => lookup_path,
"path" => path
})
sync_existing = kubespray['lookup_path'] == TeracyDev::DEFAULT_EXTENSION_LOOKUP_PATH
return TeracyDev::Location::Manager.sync(kubespray['location'], sync_existing)
end
def setup_inventory(k8s_config, clean_inventory)
@logger.debug("clean_inventory: #{clean_inventory}")
kubespray = k8s_config['kubespray']
# copy the sample inventory to `workspace/inventory` if not exists yet and we can configure anything there
src_inventory = File.join(TeracyDev::BASE_DIR, kubespray['lookup_path'], "kubespray", "inventory", "sample", ".")
dest_inventory = File.join(TeracyDev::BASE_DIR, 'workspace', 'inventory')
# delete the dest_inventory if exist and require cleaning
FileUtils.remove_dir dest_inventory if File.directory? dest_inventory and clean_inventory == true
if !File.exist? File.join(dest_inventory)
@logger.info("mkdir -p #{dest_inventory} && cp -r #{src_inventory} #{dest_inventory}")
FileUtils.mkdir_p dest_inventory
FileUtils.cp_r src_inventory, dest_inventory
end
if k8s_config['ansible']['mode'] == "host"
vagrant_ansible = File.join(TeracyDev::BASE_DIR, ".vagrant", "provisioners", "ansible")
FileUtils.mkdir_p(vagrant_ansible) if !File.exist? vagrant_ansible
if !File.exist? File.join(vagrant_ansible, "inventory")
FileUtils.ln_s(dest_inventory, File.join(vagrant_ansible, "inventory"))
end
# delelete #{dest_inventory}/vagrant_ansible_local_inventory if generated on "guest" mode
guest_generated_file_path = File.join("#{dest_inventory}", "vagrant_ansible_local_inventory")
FileUtils.remove_file(guest_generated_file_path) if File.file? guest_generated_file_path
elsif k8s_config['ansible']['mode'] == "guest"
# delelete #{dest_inventory}/vagrant_ansible_inventory if generated on "host" mode
host_generated_file_path = File.join("#{dest_inventory}", "vagrant_ansible_inventory")
FileUtils.remove_file(host_generated_file_path) if File.file? host_generated_file_path
end
end
def generate_nodes(k8s_config)
num_instances = k8s_config['num_instances']
instance_name_prefix = k8s_config['instance_name_prefix']
vm_gui = k8s_config['vm_gui']
vm_memory = k8s_config['vm_memory']
vm_cpus = k8s_config['vm_cpus']
network_type = k8s_config['network']['type']
subnet = k8s_config['network']['subnet']
os = k8s_config['os']
supported_os = k8s_config['supported_oses'].find { |item| item['_id'] == os }
if supported_os.nil?
@logger.error("#{os} is not supported")
abort
end
network_plugin = k8s_config['network_plugin']
etcd_instances = num_instances
# The first two nodes are kube masters
kube_master_instances = num_instances == 1 ? num_instances : (num_instances - 1)
# All nodes are kube nodes
kube_node_instances = num_instances
ansible_host_vars = k8s_config['ansible']['host_vars']
ansible_extra_vars = k8s_config['ansible']['extra_vars']
local_release_dir = k8s_config['local_release_dir']
host_vars = {}
box = supported_os['box']
if supported_os.has_key?('box_url')
box_url = supported_os['box_url']
end
kubespray_lookup_path = k8s_config['kubespray']['lookup_path']
host_inventory = File.join(TeracyDev::BASE_DIR, 'workspace', 'inventory')
nodes = []
playbook_path = k8s_config['ansible']['playbook_path']
if playbook_path.start_with? "/"
playbook_path = File.join(TeracyDev::BASE_DIR, playbook_path)
else
playbook_path = File.join(kubespray_lookup_path, 'kubespray', playbook_path)
end
@logger.debug("playbook_path: #{playbook_path}")
(1..num_instances).each do |i|
vm_name = "%s-%02d" % [instance_name_prefix, i]
ip = "#{subnet}.#{i+100}"
host_vars[vm_name] = {
"ip": ip
}
host_vars[vm_name].merge!(ansible_host_vars)
@logger.debug("host_vars[#{vm_name}]: #{host_vars[vm_name]}")
node = {
"_id" => "#{i-1}",
"name" => vm_name,
"vm" => {
"box" => box,
"box_url" => box_url,
"hostname" => "#{vm_name}",
"networks" => [{
"_id" => "0",
"type" => network_type,
"ip" => ip
}]
},
"ssh" => {
"username" => supported_os['user']
},
"providers" => [{
"_id" => "0",
"gui" => vm_gui,
"memory" => vm_memory,
"cpus" => vm_cpus
}]
}
# Only execute once the Ansible provisioner,
# when all the machines are up and ready.
if i == num_instances
if k8s_config['ansible']['mode'] == 'guest'
k8s_requirement_extra_vars = {
"kubespray_lookup_path" => kubespray_lookup_path
}
provisioner_k8s_requirement = {
"_id" => "k8s-pip-requirements",
"type" => "ansible_local",
"name" => "set-up-teracy-dev-k8s-requirements",
"enabled" => true,
"playbook" => "%{teracy-dev-k8s-path}/provisioners/ansible/k8s-requirements.yml",
"become" => true,
"limit" => "all",
"raw_arguments" => ["--forks=#{num_instances}", "--flush-cache"],
"verbose" => k8s_config['ansible']['verbose'],
"install_mode" => "pip",
"extra_vars" => k8s_requirement_extra_vars
}
provisioner = {
"_id" => "k8s-1",
"type" => "ansible_local",
"name" => "teracy-dev-k8s",
"enabled" => true,
"playbook" => "#{playbook_path}",
"config_file" => "#{kubespray_lookup_path}/kubespray/ansible.cfg",
"become" => true,
"limit" => "all",
"raw_arguments" => ["--forks=#{num_instances}", "--flush-cache"],
"extra_vars" => ansible_extra_vars,
"host_vars" => host_vars,
"verbose" => k8s_config['ansible']['verbose'],
"groups" => {
"localhost" => ["#{instance_name_prefix}-0[1:#{num_instances}]"],
"etcd" => ["#{instance_name_prefix}-0[1:#{etcd_instances}]"],
"kube-master" => ["#{instance_name_prefix}-0[1:#{kube_master_instances}]"],
"kube-node" => ["#{instance_name_prefix}-0[1:#{kube_node_instances}]"],
"k8s-cluster:children" => ["kube-master", "kube-node"],
}
}
if TeracyDev::Util.exist?(k8s_config['ansible']['version'])
provisioner['version'] = k8s_config['ansible']['version']
provisioner['install_mode'] = 'pip'
end
node["provisioners"] = [provisioner_k8s_requirement, provisioner]
# map example inventory to /tmp/vagrant-ansible/inventory with the guest
node['vm']['synced_folders'] = [{
"_id" => "k8s-0",
"type" => "virtualbox",
"host" => "#{host_inventory}",
"guest" => "/tmp/vagrant-ansible/inventory/"
}]
# bug on vagrant 2.1.5
# see: https://github.com/teracyhq-incubator/teracy-dev-k8s/issues/26#issuecomment-426205878
if Gem::Version.new(Vagrant::VERSION) == Gem::Version.new('2.1.5')
node['vm']['synced_folders'][0]['guest'] = "/tmp/vagrant-ansible/inventory/vagrant_ansible_local_inventory/"
end
elsif k8s_config['ansible']['mode'] == 'host'
provisioner = {
"_id" => "k8s-1",
"type" => "ansible",
"name" => "teracy-dev-k8s",
"enabled" => true,
"playbook" => "#{playbook_path}",
"config_file" => "#{kubespray_lookup_path}/kubespray/ansible.cfg",
"become" => true,
"limit" => "all",
"host_key_checking" => false,
"raw_arguments" => ["--forks=#{num_instances}", "--flush-cache"],
"extra_vars" => ansible_extra_vars,
"host_vars" => host_vars,
"verbose" => k8s_config['ansible']['verbose'],
"groups" => {
"localhost" => ["#{instance_name_prefix}-0[1:#{num_instances}]"],
"etcd" => ["#{instance_name_prefix}-0[1:#{etcd_instances}]"],
"kube-master" => ["#{instance_name_prefix}-0[1:#{kube_master_instances}]"],
"kube-node" => ["#{instance_name_prefix}-0[1:#{kube_node_instances}]"],
"k8s-cluster:children" => ["kube-master", "kube-node"],
}
}
if File.exist?(File.join(host_inventory, "hosts"))
provisioner["inventory_path"] = host_inventory
end
node["provisioners"] = [provisioner]
end
end
node_template = k8s_config['node_template']
# @logger.debug("node_template: #{node_template}")
@logger.debug("generate_nodes: node: #{node}")
nodes << TeracyDev::Util.override(node_template, node)
end
nodes
end
end
end
end