This repository has been archived by the owner on Mar 15, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
utils.rb
298 lines (249 loc) · 7.58 KB
/
utils.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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
require 'erb'
require 'json'
require 'tempfile'
CLUSTER_SIZE = ENV["CLUSTER_SIZE"] || 3
PACKAGES = %w(cri-tools kubeadm kubectl kubelet kubernetes-cni)
IMAGES = %w(kube-apiserver kube-controller-manager kube-proxy kube-scheduler)
MANIFESTS = %w(flannel)
CONTAINER_IMAGES = JSON.parse File.read(File.join(File.dirname(__FILE__), '..', 'base-box', 'configs', 'container_images.json')), symbolize_names: true
EXTRA_CONTAINER_IMAGES = JSON.parse(File.read(File.join(File.dirname(__FILE__), '..', 'base-box', 'configs', 'extra_container_images.json')), symbolize_names: true) rescue {}
KUBERNETES_PATH = "#{ENV["GOPATH"]}/src/k8s.io/kubernetes"
class KubernetesNetwork
attr_accessor :pod_subnet, :service_subnet
def initialize(pod_subnet:, service_subnet:)
@pod_subnet, @service_subnet = pod_subnet, service_subnet
end
end
class KubernetesCluster
attr_accessor :name, :token, :bootstrap, :machines, :network
def initialize(name:, token:, bootstrap:)
@name, @token, @bootstrap = name, token, bootstrap
end
def init_master
masters.first
end
def lb
@machines.find { |machine| machine.lb? }
end
def masters
@machines.select { |machine| machine.master? }
end
def cluster_machines
@machines.select { |machine| !machine.lb? }
end
def ha?
masters.length > 1
end
def instructions
puts
puts "Cluster information:"
puts " Name: #{name}"
puts " Automatic bootstrap: #{bootstrap ? "Yes" : "No"}"
puts " HA: #{ha? ? "Yes" : "No"}"
if ha?
puts " Load balancer: #{lb.ip}"
end
puts " SSH instructions for each machine:"
machines.each do |machine|
machine.ssh_instructions
end
if !bootstrap
puts
puts "Your configuration specifies that no automatic bootstrap should take"
puts "place, so you should SSH into all machines (except the load balancer)"
puts 'and bootstrap them. A file `bootstrap.sh` has been placed in all of them'
puts 'to make it easier for you to see what the bootstrap would have ran,'
puts 'and you can optionally run them too if you want.'
end
puts
end
end
class KubernetesMachine
attr_accessor :cluster, :name, :role, :ip
alias advertise_address ip
def initialize(cluster:, name:, role:, ip:)
@cluster, @name, @role, @ip = cluster, name, role, ip
end
def lb?
@role == "loadbalancer"
end
def master?
@role == "master"
end
def worker?
@role == "worker"
end
def init_master?
@cluster.init_master == self
end
def secondary_master?
master? && !init_master?
end
def full_name
"#{@cluster.name}_#{@name}"
end
def ssh_instructions
puts " - #{full_name} (#{role})"
puts " PROFILE=#{ENV["PROFILE"]} vagrant ssh #{full_name}"
end
end
def container_ref(name)
unless CONTAINER_IMAGES.include?(name) || EXTRA_CONTAINER_IMAGES.include?(name)
raise "Unknown container image: #{name}"
end
container_image = CONTAINER_IMAGES[name] || EXTRA_CONTAINER_IMAGES[name]
result = ""
if container_image[:repository] && !container_image[:repository].empty?
result = "#{container_image[:repository]}/"
end
result += container_image[:name]
if container_image[:tag] && !container_image[:tag].empty?
result += ":#{container_image[:tag]}"
end
result
end
def kubernetes_path(path)
File.join KUBERNETES_PATH, path
end
def package_path(package)
if ENV["PACKAGE_PATH"].nil?
kubernetes_path "bazel-bin/build/debs/#{package}.deb"
else
File.join ENV["PACKAGE_PATH"], "#{package}.deb"
end
end
def image_path(image)
if ENV["IMAGE_PATH"].nil?
kubernetes_path "bazel-bin/build/#{image}.tar"
else
File.join ENV["IMAGE_PATH"], "#{image}.tar"
end
end
def full_image_version(image)
File.read(kubernetes_path("bazel-genfiles/build/#{image}.docker_tag")).strip
end
def image_version(image)
full_image_version(image) =~ /^([^-]+)/
$1
end
def full_kubernetes_version
File.read(kubernetes_path("bazel-genfiles/build/version")).strip
end
def kubernetes_version
full_kubernetes_version =~ /^((?:\d+\.){2}\d+)/
"v#{$1}"
end
def kubernetes_target_path(path = nil)
if path.nil?
"/home/vagrant/kubernetes"
else
"/home/vagrant/kubernetes/#{path}"
end
end
def custom_container_image_target_path(image_path)
File.join "/home/vagrant/custom/images", File.basename(image_path)
end
def home_path(path = nil)
if path.nil?
"/home/vagrant"
else
"/home/vagrant/#{path}"
end
end
def template_file(config, b)
template = Tempfile.new
template.write template(config, b)
template.rewind
template
end
def template(config, b)
ERB.new(File.read(config)).result(b)
end
def kubeadm_config_target_path(path)
"/home/vagrant/kubeadm/#{path}"
end
def manifests_config_target_path(path)
"/home/vagrant/manifests/#{path}"
end
def packages
if ENV["PACKAGES"] == "all"
PACKAGES
else
(ENV["PACKAGES"] || "").split(",")
end
end
def images
if ENV["IMAGES"] == "all"
IMAGES
else
(ENV["IMAGES"] || "").split(",")
end
end
def custom_container_images
(ENV["CUSTOM_CONTAINER_IMAGES"] || "").split(",")
end
def default_manifests(cluster)
up? ? "flannel" : ""
end
def manifests(cluster)
if ENV["MANIFESTS"] == "all"
MANIFESTS
else
(ENV["MANIFESTS"] || default_manifests(cluster)).split(",")
end
end
def up?
ARGV.include?("up") || (ARGV.include?("reload") && ARGV.include?("--provision"))
end
def provisioning?
up? || ARGV.include?("provision")
end
def check_kubernetes
raise "Kubernetes not cloned under #{KUBERNETES_PATH}; please, run `git clone git@github.com:kubernetes/kubernetes.git #{KUBERNETES_PATH}`" unless Dir.exists?(KUBERNETES_PATH)
end
def check_packages(packages)
missing_packages = Array.new
packages.each do |package|
missing_packages << package unless File.exists?(package_path(package))
end
raise "Missing packages: #{missing_packages.join(", ")}; please, run `bazel build //build/debs` from #{KUBERNETES_PATH} or `make debs` from here" unless missing_packages.empty?
end
def check_images(images)
missing_images = Array.new
images.each do |image|
missing_images << image unless File.exists?(image_path(image))
end
raise "Missing images: #{missing_images.join(", ")}; please, run `bazel build //build:docker-artifacts` from #{KUBERNETES_PATH} or `make images` from here" unless missing_images.empty?
end
def check_profile
if ENV["PROFILE"].nil? || ENV["PROFILE"].empty?
raise "Please, set PROFILE envvar to point to a JSON profile (some examples can be found inside the profiles directory)"
end
if !File.exists?("profiles/#{ENV["PROFILE"]}.json") && !File.exists?(ENV["PROFILE"])
raise "Profile profiles/#{ENV["PROFILE"]}.json does not exist, and #{ENV["PROFILE"]} wasn't found either"
end
end
def profile
return $profile if $profile
check_profile
$profile = JSON.parse File.read(File.exists?(ENV["PROFILE"]) ? ENV["PROFILE"] : "profiles/#{ENV["PROFILE"]}.json"), symbolize_names: true
end
def cluster
return $cluster if $cluster
$cluster = KubernetesCluster.new name: profile[:cluster][:name], token: profile[:cluster][:token], bootstrap: profile[:cluster][:bootstrap]
$cluster.network = KubernetesNetwork.new pod_subnet: profile[:cluster][:network][:pod_subnet],
service_subnet: profile[:cluster][:network][:service_subnet]
$cluster.machines = profile[:machines].map do |machine|
KubernetesMachine.new cluster: $cluster, name: machine[:name], role: machine[:role], ip: machine[:ip]
end.sort_by.with_index do |machine, i|
case machine.role
when "loadbalancer"
[0, i]
when "master"
[1, i]
when "worker"
[2, i]
end
end
$cluster
end