kubernetes-deploy 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +9 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +21 -0
- data/README.md +41 -0
- data/Rakefile +10 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/exe/kubernetes-deploy +662 -0
- data/kubernetes-deploy.gemspec +28 -0
- data/lib/kubernetes-deploy/version.rb +3 -0
- metadata +113 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 46dca891703362d1e3ca9e3b2288b19e80754666
|
4
|
+
data.tar.gz: 774825787ad0cf61ad6a9e08c21d792fe4a11051
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 47fffc399b1d15427022b0eb8dd69cb6574e7fa3e5c7d8e873b5d92734a080648a0fc5aed2943f72464fda038a7df26ae580666e43e45905c1c6e8c7744e463c
|
7
|
+
data.tar.gz: 4a99fdb8a396f6508260cf30d9fda9d704d9dc84b80b7a5e92557bec2bbf75b5c1a98c1bdd038903d61555424a56e5575d370fccc7b0a7f6511aeb64a4659102
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2017 Kir Shatrov
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,41 @@
|
|
1
|
+
# Kubernetes::Deploy
|
2
|
+
|
3
|
+
Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/kubernetes/deploy`. To experiment with that code, run `bin/console` for an interactive prompt.
|
4
|
+
|
5
|
+
TODO: Delete this and the text above, and describe your gem
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Add this line to your application's Gemfile:
|
10
|
+
|
11
|
+
```ruby
|
12
|
+
gem 'kubernetes-deploy'
|
13
|
+
```
|
14
|
+
|
15
|
+
And then execute:
|
16
|
+
|
17
|
+
$ bundle
|
18
|
+
|
19
|
+
Or install it yourself as:
|
20
|
+
|
21
|
+
$ gem install kubernetes-deploy
|
22
|
+
|
23
|
+
## Usage
|
24
|
+
|
25
|
+
TODO: Write usage instructions here
|
26
|
+
|
27
|
+
## Development
|
28
|
+
|
29
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
30
|
+
|
31
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
32
|
+
|
33
|
+
## Contributing
|
34
|
+
|
35
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/kubernetes-deploy.
|
36
|
+
|
37
|
+
|
38
|
+
## License
|
39
|
+
|
40
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
41
|
+
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "kubernetes/deploy"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start
|
data/bin/setup
ADDED
@@ -0,0 +1,662 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
# Usage: kubernetes-deploy <app's namespace> <kube context>
|
4
|
+
|
5
|
+
# Prerequisites:
|
6
|
+
# - kubectl 1.5.1+ binary must be available in the shipit machine's path
|
7
|
+
# - ENV['KUBECONFIG'] must point to a valid kubeconfig file that includes all the contexts you want to deploy to
|
8
|
+
# - ENV['GOOGLE_APPLICATION_CREDENTIALS'] must point to the credentials for an authenticated service account if your user's auth provider is gcp
|
9
|
+
|
10
|
+
# Optionally, the following variables can be used to override script defaults:
|
11
|
+
# - ENV['K8S_TEMPLATE_FOLDER']: Location of Kubernetes files to deploy. Default is config/deploy/#{environment}.
|
12
|
+
|
13
|
+
require 'open3'
|
14
|
+
require 'securerandom'
|
15
|
+
require 'erb'
|
16
|
+
require 'json'
|
17
|
+
require 'yaml'
|
18
|
+
require 'shellwords'
|
19
|
+
require 'tempfile'
|
20
|
+
require 'logger'
|
21
|
+
require 'active_support/core_ext/object/blank'
|
22
|
+
require 'active_support/descendants_tracker'
|
23
|
+
require 'active_support/core_ext/hash/slice'
|
24
|
+
require 'active_support/core_ext/numeric/time'
|
25
|
+
|
26
|
+
class KubernetesDeploy
|
27
|
+
class FatalDeploymentError < StandardError; end
|
28
|
+
|
29
|
+
# Things removed from default prune whitelist:
|
30
|
+
# core/v1/Namespace -- not namespaced
|
31
|
+
# core/v1/PersistentVolume -- not namespaced
|
32
|
+
# core/v1/Endpoints -- managed by services
|
33
|
+
# core/v1/PersistentVolumeClaim -- would delete data
|
34
|
+
# core/v1/ReplicationController -- superseded by deployments/replicasets
|
35
|
+
# extensions/v1beta1/ReplicaSet -- managed by deployments
|
36
|
+
# core/v1/Secret -- should not committed / managed by shipit
|
37
|
+
PRUNE_WHITELIST = %w(
|
38
|
+
core/v1/ConfigMap
|
39
|
+
core/v1/Pod
|
40
|
+
core/v1/Service
|
41
|
+
batch/v1/Job
|
42
|
+
extensions/v1beta1/DaemonSet
|
43
|
+
extensions/v1beta1/Deployment
|
44
|
+
extensions/v1beta1/HorizontalPodAutoscaler
|
45
|
+
extensions/v1beta1/Ingress
|
46
|
+
apps/v1beta1/StatefulSet
|
47
|
+
).freeze
|
48
|
+
|
49
|
+
PREDEPLOY_SEQUENCE = %w(
|
50
|
+
ConfigMap
|
51
|
+
PersistentVolumeClaim
|
52
|
+
Pod
|
53
|
+
)
|
54
|
+
|
55
|
+
def initialize(namespace:, environment:, current_sha:, template_folder: nil, context:)
|
56
|
+
@namespace = namespace
|
57
|
+
@context = context
|
58
|
+
@current_sha = current_sha
|
59
|
+
@template_path = File.expand_path('./' + (template_folder || "config/deploy/#{environment}"))
|
60
|
+
# Max length of podname is only 63chars so try to save some room by truncating sha to 8 chars
|
61
|
+
@id = current_sha[0...8] + "-#{SecureRandom.hex(4)}" if current_sha
|
62
|
+
end
|
63
|
+
|
64
|
+
def run
|
65
|
+
@current_phase = 0
|
66
|
+
phase_heading("Validating configuration")
|
67
|
+
validate_configuration
|
68
|
+
|
69
|
+
phase_heading("Configuring kubectl")
|
70
|
+
set_kubectl_context
|
71
|
+
validate_namespace
|
72
|
+
|
73
|
+
phase_heading("Parsing deploy content")
|
74
|
+
resources = discover_resources
|
75
|
+
|
76
|
+
phase_heading("Checking initial resource statuses")
|
77
|
+
resources.each(&:sync)
|
78
|
+
|
79
|
+
phase_heading("Predeploying priority resources")
|
80
|
+
predeploy_priority_resources(resources)
|
81
|
+
|
82
|
+
phase_heading("Deploying all resources")
|
83
|
+
deploy_resources(resources, prune: true)
|
84
|
+
wait_for_completion(resources)
|
85
|
+
|
86
|
+
report_final_status(resources)
|
87
|
+
rescue FatalDeploymentError => error
|
88
|
+
KubernetesDeploy.logger.fatal(error.message)
|
89
|
+
exit 1
|
90
|
+
end
|
91
|
+
|
92
|
+
def template_variables
|
93
|
+
{
|
94
|
+
'current_sha' => @current_sha,
|
95
|
+
'deployment_id' => @id,
|
96
|
+
}
|
97
|
+
end
|
98
|
+
|
99
|
+
private
|
100
|
+
|
101
|
+
def predeploy_priority_resources(resource_list)
|
102
|
+
PREDEPLOY_SEQUENCE.each do |resource_type|
|
103
|
+
matching_resources = resource_list.select { |r| r.type == resource_type }
|
104
|
+
next if matching_resources.empty?
|
105
|
+
deploy_resources(matching_resources)
|
106
|
+
wait_for_completion(matching_resources)
|
107
|
+
fail_count = matching_resources.count { |r| r.deploy_failed? || r.deploy_timed_out? }
|
108
|
+
if fail_count > 0
|
109
|
+
raise FatalDeploymentError, "#{fail_count} priority resources failed to deploy"
|
110
|
+
end
|
111
|
+
end
|
112
|
+
end
|
113
|
+
|
114
|
+
def discover_resources
|
115
|
+
resources = []
|
116
|
+
Dir.foreach(@template_path) do |filename|
|
117
|
+
next unless filename.end_with?(".yml.erb", ".yml")
|
118
|
+
|
119
|
+
split_templates(filename) do |tempfile|
|
120
|
+
resource_id = discover_resource_via_dry_run(tempfile)
|
121
|
+
type, name = resource_id.split("/", 2) # e.g. "pod/web-198612918-dzvfb"
|
122
|
+
resources << KubernetesResource.for_type(type, name, @namespace, tempfile)
|
123
|
+
KubernetesDeploy.logger.info "Discovered template for #{resource_id}"
|
124
|
+
end
|
125
|
+
end
|
126
|
+
resources
|
127
|
+
end
|
128
|
+
|
129
|
+
def discover_resource_via_dry_run(tempfile)
|
130
|
+
resource_id, err, st = run_kubectl("apply", "-f", tempfile.path, "--dry-run", "--output=name")
|
131
|
+
raise FatalDeploymentError, "Dry run failed for template #{File.basename(tempfile.path)}." unless st.success?
|
132
|
+
resource_id
|
133
|
+
end
|
134
|
+
|
135
|
+
def split_templates(filename)
|
136
|
+
file_content = File.read(File.join(@template_path, filename))
|
137
|
+
rendered_content = render_template(filename, file_content)
|
138
|
+
YAML.load_stream(rendered_content) do |doc|
|
139
|
+
f = Tempfile.new(filename)
|
140
|
+
f.write(YAML.dump(doc))
|
141
|
+
f.close
|
142
|
+
yield f
|
143
|
+
end
|
144
|
+
rescue Psych::SyntaxError => e
|
145
|
+
KubernetesDeploy.logger.error(rendered_content)
|
146
|
+
raise FatalDeploymentError, "Template #{filename} cannot be parsed: #{e.message}"
|
147
|
+
end
|
148
|
+
|
149
|
+
def report_final_status(resources)
|
150
|
+
if resources.all?(&:deploy_succeeded?)
|
151
|
+
log_green("Deploy succeeded!")
|
152
|
+
else
|
153
|
+
fail_list = resources.select { |r| r.deploy_failed? || r.deploy_timed_out? }.map(&:id)
|
154
|
+
KubernetesDeploy.logger.error("The following resources failed to deploy: #{fail_list.join(", ")}")
|
155
|
+
raise FatalDeploymentError, "#{fail_list.length} resources failed to deploy"
|
156
|
+
end
|
157
|
+
end
|
158
|
+
|
159
|
+
def wait_for_completion(watched_resources)
|
160
|
+
delay_sync_until = Time.now.utc
|
161
|
+
while watched_resources.present?
|
162
|
+
if Time.now.utc < delay_sync_until
|
163
|
+
sleep (delay_sync_until - Time.now.utc)
|
164
|
+
end
|
165
|
+
delay_sync_until = Time.now.utc + 3 # don't pummel the API if the sync is fast
|
166
|
+
watched_resources.each(&:sync)
|
167
|
+
newly_finished_resources, watched_resources = watched_resources.partition(&:deploy_finished?)
|
168
|
+
newly_finished_resources.each do |resource|
|
169
|
+
next unless resource.deploy_failed? || resource.deploy_timed_out?
|
170
|
+
KubernetesDeploy.logger.error("#{resource.id} failed to deploy with status '#{resource.status}'.")
|
171
|
+
KubernetesDeploy.logger.error("This script will continue to poll until the status of all resources deployed in this phase is resolved, but the deploy is now doomed and you may wish abort it.")
|
172
|
+
KubernetesDeploy.logger.error(resource.status_data)
|
173
|
+
end
|
174
|
+
end
|
175
|
+
end
|
176
|
+
|
177
|
+
def render_template(filename, raw_template)
|
178
|
+
return raw_template unless File.extname(filename) == ".erb"
|
179
|
+
|
180
|
+
erb_template = ERB.new(raw_template)
|
181
|
+
erb_binding = binding
|
182
|
+
template_variables.each do |var_name, value|
|
183
|
+
erb_binding.local_variable_set(var_name, value)
|
184
|
+
end
|
185
|
+
erb_template.result(erb_binding)
|
186
|
+
end
|
187
|
+
|
188
|
+
def validate_configuration
|
189
|
+
errors = []
|
190
|
+
if ENV["KUBECONFIG"].blank? || !File.file?(ENV["KUBECONFIG"])
|
191
|
+
errors << "Kube config not found at #{ENV["KUBECONFIG"]}"
|
192
|
+
end
|
193
|
+
|
194
|
+
if @current_sha.blank?
|
195
|
+
errors << "Current SHA must be specified"
|
196
|
+
end
|
197
|
+
|
198
|
+
if !File.directory?(@template_path)
|
199
|
+
errors << "Template path #{@template_path} doesn't exist"
|
200
|
+
elsif Dir.entries(@template_path).none? { |file| file =~ /\.yml(\.erb)?$/ }
|
201
|
+
errors << "#{@template_path} doesn't contain valid templates (postfix .yml or .yml.erb)"
|
202
|
+
end
|
203
|
+
|
204
|
+
if @namespace.blank?
|
205
|
+
errors << "Namespace must be specified"
|
206
|
+
end
|
207
|
+
|
208
|
+
if @context.blank?
|
209
|
+
errors << "Context must be specified"
|
210
|
+
end
|
211
|
+
|
212
|
+
raise FatalDeploymentError, "Configuration invalid: #{errors.join(", ")}" unless errors.empty?
|
213
|
+
KubernetesDeploy.logger.info("All required parameters and files are present")
|
214
|
+
end
|
215
|
+
|
216
|
+
def deploy_resources(resources, prune: false)
|
217
|
+
command = ["apply", "--namespace=#{@namespace}"]
|
218
|
+
KubernetesDeploy.logger.info("Deploying resources:")
|
219
|
+
|
220
|
+
resources.each do |r|
|
221
|
+
KubernetesDeploy.logger.info("- #{r.id}")
|
222
|
+
command.push("-f", r.file.path)
|
223
|
+
r.deploy_started = Time.now.utc
|
224
|
+
end
|
225
|
+
|
226
|
+
if prune
|
227
|
+
command.push("--prune", "--all")
|
228
|
+
PRUNE_WHITELIST.each { |type| command.push("--prune-whitelist=#{type}") }
|
229
|
+
end
|
230
|
+
|
231
|
+
run_kubectl(*command)
|
232
|
+
end
|
233
|
+
|
234
|
+
def set_kubectl_context
|
235
|
+
out, err, st = run_kubectl("config", "get-contexts", "-o", "name", namespaced: false)
|
236
|
+
available_contexts = out.split("\n")
|
237
|
+
if !st.success?
|
238
|
+
raise FatalDeploymentError, err
|
239
|
+
elsif !available_contexts.include?(@context)
|
240
|
+
raise FatalDeploymentError, "Context #{@context} is not available. Valid contexts: #{available_contexts}"
|
241
|
+
end
|
242
|
+
|
243
|
+
_, err, st = run_kubectl("config", "use-context", @context, namespaced: false)
|
244
|
+
raise FatalDeploymentError, "Kubectl config is not valid: #{err}" unless st.success?
|
245
|
+
KubernetesDeploy.logger.info("Kubectl configured to use context #{@context}")
|
246
|
+
end
|
247
|
+
|
248
|
+
def validate_namespace
|
249
|
+
_, _, st = run_kubectl("get", "namespace", @namespace, namespaced: false)
|
250
|
+
raise FatalDeploymentError, "Failed to validate namespace #{@namespace}" unless st.success?
|
251
|
+
KubernetesDeploy.logger.info("Namespace #{@namespace} validated")
|
252
|
+
end
|
253
|
+
|
254
|
+
def run_kubectl(*args, namespaced: true)
|
255
|
+
args = args.unshift("kubectl")
|
256
|
+
if namespaced
|
257
|
+
raise FatalDeploymentError, "Namespace missing for namespaced command" unless @namespace
|
258
|
+
args.push("--namespace=#{@namespace}")
|
259
|
+
end
|
260
|
+
KubernetesDeploy.logger.debug Shellwords.join(args)
|
261
|
+
out, err, st = Open3.capture3(*args)
|
262
|
+
KubernetesDeploy.logger.debug(out.shellescape)
|
263
|
+
KubernetesDeploy.logger.warn(err) unless st.success?
|
264
|
+
[out.chomp, err.chomp, st]
|
265
|
+
end
|
266
|
+
|
267
|
+
def phase_heading(phase_name)
|
268
|
+
@current_phase += 1
|
269
|
+
heading = "Phase #{@current_phase}: #{phase_name}"
|
270
|
+
padding = (100.0 - heading.length)/2
|
271
|
+
KubernetesDeploy.logger.info("")
|
272
|
+
KubernetesDeploy.logger.info("#{'-' * padding.floor}#{heading}#{'-' * padding.ceil}")
|
273
|
+
end
|
274
|
+
|
275
|
+
def log_green(msg)
|
276
|
+
STDOUT.puts "\033[0;32m#{msg}\x1b[0m\n" # green
|
277
|
+
end
|
278
|
+
|
279
|
+
def self.logger
|
280
|
+
@logger ||= begin
|
281
|
+
l = Logger.new(STDOUT)
|
282
|
+
l.level = ENV["DEBUG"] ? Logger::DEBUG : Logger::INFO
|
283
|
+
l.formatter = proc do |severity, _datetime, _progname, msg|
|
284
|
+
case severity
|
285
|
+
when "FATAL", "ERROR" then "\033[0;31m[#{severity}]\t#{msg}\x1b[0m\n" # red
|
286
|
+
when "WARN" then "\033[0;33m[#{severity}]\t#{msg}\x1b[0m\n" # yellow
|
287
|
+
when "INFO" then "\033[0;36m#{msg}\x1b[0m\n" # blue
|
288
|
+
else "[#{severity}]\t#{msg}\n"
|
289
|
+
end
|
290
|
+
end
|
291
|
+
l
|
292
|
+
end
|
293
|
+
end
|
294
|
+
|
295
|
+
class KubernetesResource
|
296
|
+
extend ActiveSupport::DescendantsTracker
|
297
|
+
|
298
|
+
attr_reader :name, :namespace, :file
|
299
|
+
attr_writer :type, :deploy_started
|
300
|
+
|
301
|
+
TIMEOUT = 5.minutes
|
302
|
+
|
303
|
+
def self.handled_type
|
304
|
+
name.split('::').last
|
305
|
+
end
|
306
|
+
|
307
|
+
def self.for_type(type, name, namespace, file)
|
308
|
+
if subclass = descendants.find { |subclass| subclass.handled_type.downcase == type }
|
309
|
+
subclass.new(name, namespace, file)
|
310
|
+
else
|
311
|
+
self.new(name, namespace, file).tap { |r| r.type = type }
|
312
|
+
end
|
313
|
+
end
|
314
|
+
|
315
|
+
def initialize(name, namespace, file)
|
316
|
+
# subclasses must also set these
|
317
|
+
@name, @namespace, @file = name, namespace, file
|
318
|
+
end
|
319
|
+
|
320
|
+
def id
|
321
|
+
"#{type}/#{name}"
|
322
|
+
end
|
323
|
+
|
324
|
+
def sync
|
325
|
+
log_status
|
326
|
+
end
|
327
|
+
|
328
|
+
def deploy_failed?
|
329
|
+
false
|
330
|
+
end
|
331
|
+
|
332
|
+
def deploy_succeeded?
|
333
|
+
if @deploy_started && !@success_assumption_warning_shown
|
334
|
+
KubernetesDeploy.logger.warn("Don't know how to monitor resources of type #{type}. Assuming #{id} deployed successfully.")
|
335
|
+
@success_assumption_warning_shown = true
|
336
|
+
end
|
337
|
+
true
|
338
|
+
end
|
339
|
+
|
340
|
+
def exists?
|
341
|
+
nil
|
342
|
+
end
|
343
|
+
|
344
|
+
def status
|
345
|
+
@status ||= "Unknown"
|
346
|
+
deploy_timed_out? ? "Timed out with status #{@status}" : @status
|
347
|
+
end
|
348
|
+
|
349
|
+
def type
|
350
|
+
@type || self.class.handled_type
|
351
|
+
end
|
352
|
+
|
353
|
+
def deploy_finished?
|
354
|
+
deploy_failed? || deploy_succeeded? || deploy_timed_out?
|
355
|
+
end
|
356
|
+
|
357
|
+
def deploy_timed_out?
|
358
|
+
return false unless @deploy_started
|
359
|
+
!deploy_succeeded? && !deploy_failed? && (Time.now.utc - @deploy_started > self.class::TIMEOUT)
|
360
|
+
end
|
361
|
+
|
362
|
+
def status_data
|
363
|
+
{
|
364
|
+
group: group_name,
|
365
|
+
name: name,
|
366
|
+
status_string: status,
|
367
|
+
exists: exists?,
|
368
|
+
succeeded: deploy_succeeded?,
|
369
|
+
failed: deploy_failed?,
|
370
|
+
timed_out: deploy_timed_out?
|
371
|
+
}
|
372
|
+
end
|
373
|
+
|
374
|
+
def group_name
|
375
|
+
type + "s"
|
376
|
+
end
|
377
|
+
|
378
|
+
def run_kubectl(*args)
|
379
|
+
raise FatalDeploymentError, "Namespace missing for namespaced command" if namespace.blank?
|
380
|
+
args = args.unshift("kubectl").push("--namespace=#{namespace}")
|
381
|
+
KubernetesDeploy.logger.debug Shellwords.join(args)
|
382
|
+
out, err, st = Open3.capture3(*args)
|
383
|
+
KubernetesDeploy.logger.debug(out.shellescape)
|
384
|
+
KubernetesDeploy.logger.debug("[ERROR] #{err.shellescape}") unless st.success?
|
385
|
+
[out.chomp, st]
|
386
|
+
end
|
387
|
+
|
388
|
+
def log_status
|
389
|
+
STDOUT.puts "[KUBESTATUS] #{JSON.dump(status_data)}"
|
390
|
+
end
|
391
|
+
end
|
392
|
+
|
393
|
+
class ConfigMap < KubernetesResource
|
394
|
+
TIMEOUT = 30.seconds
|
395
|
+
|
396
|
+
def initialize(name, namespace, file)
|
397
|
+
@name, @namespace, @file = name, namespace, file
|
398
|
+
end
|
399
|
+
|
400
|
+
def sync
|
401
|
+
_, st = run_kubectl("get", type, @name)
|
402
|
+
@status = st.success? ? "Available" : "Unknown"
|
403
|
+
@found = st.success?
|
404
|
+
log_status
|
405
|
+
end
|
406
|
+
|
407
|
+
def deploy_succeeded?
|
408
|
+
exists?
|
409
|
+
end
|
410
|
+
|
411
|
+
def deploy_failed?
|
412
|
+
false
|
413
|
+
end
|
414
|
+
|
415
|
+
def exists?
|
416
|
+
@found
|
417
|
+
end
|
418
|
+
end
|
419
|
+
|
420
|
+
class PersistentVolumeClaim < KubernetesResource
|
421
|
+
TIMEOUT = 5.minutes
|
422
|
+
|
423
|
+
def initialize(name, namespace, file)
|
424
|
+
@name, @namespace, @file = name, namespace, file
|
425
|
+
end
|
426
|
+
|
427
|
+
def sync
|
428
|
+
@status, st = run_kubectl("get", type, @name, "--output=jsonpath={.status.phase}")
|
429
|
+
@found = st.success?
|
430
|
+
log_status
|
431
|
+
end
|
432
|
+
|
433
|
+
def deploy_succeeded?
|
434
|
+
@status == "Bound"
|
435
|
+
end
|
436
|
+
|
437
|
+
def deploy_failed?
|
438
|
+
@status == "Lost"
|
439
|
+
end
|
440
|
+
|
441
|
+
def exists?
|
442
|
+
@found
|
443
|
+
end
|
444
|
+
end
|
445
|
+
|
446
|
+
class Ingress < KubernetesResource
|
447
|
+
TIMEOUT = 30.seconds
|
448
|
+
|
449
|
+
def initialize(name, namespace, file)
|
450
|
+
@name, @namespace, @file = name, namespace, file
|
451
|
+
end
|
452
|
+
|
453
|
+
def sync
|
454
|
+
_, st = run_kubectl("get", type, @name)
|
455
|
+
@status = st.success? ? "Created" : "Unknown"
|
456
|
+
@found = st.success?
|
457
|
+
log_status
|
458
|
+
end
|
459
|
+
|
460
|
+
def deploy_succeeded?
|
461
|
+
exists?
|
462
|
+
end
|
463
|
+
|
464
|
+
def deploy_failed?
|
465
|
+
false
|
466
|
+
end
|
467
|
+
|
468
|
+
def exists?
|
469
|
+
@found
|
470
|
+
end
|
471
|
+
|
472
|
+
def group_name
|
473
|
+
"Ingresses"
|
474
|
+
end
|
475
|
+
end
|
476
|
+
|
477
|
+
class Service < KubernetesResource
|
478
|
+
TIMEOUT = 15.minutes
|
479
|
+
|
480
|
+
def initialize(name, namespace, file)
|
481
|
+
@name, @namespace, @file = name, namespace, file
|
482
|
+
end
|
483
|
+
|
484
|
+
def sync
|
485
|
+
_, st = run_kubectl("get", type, @name)
|
486
|
+
@found = st.success?
|
487
|
+
if @found
|
488
|
+
endpoints, st = run_kubectl("get", "endpoints", @name, "--output=jsonpath={.subsets[*].addresses[*].ip}")
|
489
|
+
@num_endpoints = (st.success? ? endpoints.split.length : 0)
|
490
|
+
else
|
491
|
+
@num_endpoints = 0
|
492
|
+
end
|
493
|
+
@status = "#{@num_endpoints} endpoints"
|
494
|
+
log_status
|
495
|
+
end
|
496
|
+
|
497
|
+
def deploy_succeeded?
|
498
|
+
@num_endpoints > 0
|
499
|
+
end
|
500
|
+
|
501
|
+
def deploy_failed?
|
502
|
+
false
|
503
|
+
end
|
504
|
+
|
505
|
+
def exists?
|
506
|
+
@found
|
507
|
+
end
|
508
|
+
end
|
509
|
+
|
510
|
+
class Pod < KubernetesResource
|
511
|
+
TIMEOUT = 15.minutes
|
512
|
+
SUSPICIOUS_CONTAINER_STATES = %w(ImagePullBackOff RunContainerError).freeze
|
513
|
+
|
514
|
+
def initialize(name, namespace, file, parent: nil)
|
515
|
+
@name, @namespace, @file, @parent = name, namespace, file, parent
|
516
|
+
@bare = !@parent
|
517
|
+
end
|
518
|
+
|
519
|
+
def sync
|
520
|
+
out, st = run_kubectl("get", type, @name, "-a", "--output=json")
|
521
|
+
if @found = st.success?
|
522
|
+
pod_data = JSON.parse(out)
|
523
|
+
interpret_json_data(pod_data)
|
524
|
+
else # reset
|
525
|
+
@status = @phase = nil
|
526
|
+
@ready = false
|
527
|
+
@containers = []
|
528
|
+
end
|
529
|
+
display_logs if @bare && deploy_finished?
|
530
|
+
log_status
|
531
|
+
end
|
532
|
+
|
533
|
+
def interpret_json_data(pod_data)
|
534
|
+
@phase = (pod_data["metadata"]["deletionTimestamp"] ? "Terminating" : pod_data["status"]["phase"])
|
535
|
+
@containers = pod_data["spec"]["containers"].map { |c| c["name"] }
|
536
|
+
|
537
|
+
if @deploy_started && pod_data["status"]["containerStatuses"]
|
538
|
+
pod_data["status"]["containerStatuses"].each do |status|
|
539
|
+
waiting_state = status["state"]["waiting"] if status["state"]
|
540
|
+
reason = waiting_state["reason"] if waiting_state
|
541
|
+
next unless SUSPICIOUS_CONTAINER_STATES.include?(reason)
|
542
|
+
KubernetesDeploy.logger.warn("#{id} has container in state #{reason} (#{waiting_state["message"]})")
|
543
|
+
end
|
544
|
+
end
|
545
|
+
|
546
|
+
if @phase == "Failed"
|
547
|
+
@status = "#{@phase} (Reason: #{pod_data["status"]["reason"]})"
|
548
|
+
elsif @phase == "Terminating"
|
549
|
+
@status = @phase
|
550
|
+
else
|
551
|
+
ready_condition = pod_data["status"]["conditions"].find { |condition| condition["type"] == "Ready" }
|
552
|
+
@ready = ready_condition.present? && (ready_condition["status"] == "True")
|
553
|
+
@status = "#{@phase} (Ready: #{@ready})"
|
554
|
+
end
|
555
|
+
end
|
556
|
+
|
557
|
+
def deploy_succeeded?
|
558
|
+
if @bare
|
559
|
+
@phase == "Succeeded"
|
560
|
+
else
|
561
|
+
@phase == "Running" && @ready
|
562
|
+
end
|
563
|
+
end
|
564
|
+
|
565
|
+
def deploy_failed?
|
566
|
+
@phase == "Failed"
|
567
|
+
end
|
568
|
+
|
569
|
+
def exists?
|
570
|
+
@bare ? @found : true
|
571
|
+
end
|
572
|
+
|
573
|
+
def group_name
|
574
|
+
@bare ? "Bare pods" : @parent
|
575
|
+
end
|
576
|
+
|
577
|
+
private
|
578
|
+
|
579
|
+
def display_logs
|
580
|
+
return {} unless exists? && @containers.present? && !@already_displayed
|
581
|
+
|
582
|
+
@containers.each do |container_name|
|
583
|
+
out, st = run_kubectl("logs", @name, "--timestamps=true", "--since-time=#{@deploy_started.to_datetime.rfc3339}")
|
584
|
+
next unless st.success? && out.present?
|
585
|
+
|
586
|
+
KubernetesDeploy.logger.info "Logs from #{id} container #{container_name}:"
|
587
|
+
STDOUT.puts "#{out}"
|
588
|
+
@already_displayed = true
|
589
|
+
end
|
590
|
+
end
|
591
|
+
end
|
592
|
+
|
593
|
+
class Deployment < KubernetesResource
|
594
|
+
TIMEOUT = 15.minutes
|
595
|
+
|
596
|
+
def initialize(name, namespace, file)
|
597
|
+
@name, @namespace, @file = name, namespace, file
|
598
|
+
end
|
599
|
+
|
600
|
+
def sync
|
601
|
+
json_data, st = run_kubectl("get", type, @name, "--output=json")
|
602
|
+
@found = st.success?
|
603
|
+
@rollout_data = {}
|
604
|
+
@status = nil
|
605
|
+
@pods = []
|
606
|
+
|
607
|
+
if @found
|
608
|
+
@rollout_data = JSON.parse(json_data)["status"].slice("updatedReplicas", "replicas", "availableReplicas", "unavailableReplicas")
|
609
|
+
@status, _ = run_kubectl("rollout", "status", type, @name, "--watch=false") if @deploy_started
|
610
|
+
|
611
|
+
pod_list, st = run_kubectl("get", "pods", "-a", "-l", "name=#{name}", "--output=json")
|
612
|
+
if st.success?
|
613
|
+
pods_json = JSON.parse(pod_list)["items"]
|
614
|
+
pods_json.each do |pod_json|
|
615
|
+
pod_name = pod_json["metadata"]["name"]
|
616
|
+
pod = Pod.new(pod_name, namespace, nil, parent: "#{@name.capitalize} deployment")
|
617
|
+
pod.deploy_started = @deploy_started
|
618
|
+
pod.interpret_json_data(pod_json)
|
619
|
+
pod.log_status
|
620
|
+
@pods << pod
|
621
|
+
end
|
622
|
+
end
|
623
|
+
end
|
624
|
+
|
625
|
+
log_status
|
626
|
+
end
|
627
|
+
|
628
|
+
def deploy_succeeded?
|
629
|
+
return false unless @rollout_data.key?("availableReplicas")
|
630
|
+
# TODO: this should look at the current replica set's pods too
|
631
|
+
@rollout_data["availableReplicas"].to_i == @pods.length &&
|
632
|
+
@rollout_data.values.uniq.length == 1 # num desired, current, up-to-date and available are equal
|
633
|
+
end
|
634
|
+
|
635
|
+
def deploy_failed?
|
636
|
+
# TODO: this should look at the current replica set's pods only or it'll never be true for rolling updates
|
637
|
+
@pods.present? && @pods.all?(&:deploy_failed?)
|
638
|
+
end
|
639
|
+
|
640
|
+
def deploy_timed_out?
|
641
|
+
# TODO: this should look at the current replica set's pods only or it'll never be true for rolling updates
|
642
|
+
super || @pods.present? && @pods.all?(&:deploy_timed_out?)
|
643
|
+
end
|
644
|
+
|
645
|
+
def exists?
|
646
|
+
@found
|
647
|
+
end
|
648
|
+
|
649
|
+
def status_data
|
650
|
+
super.merge(replicas: @rollout_data, num_pods: @pods.length)
|
651
|
+
end
|
652
|
+
end
|
653
|
+
end
|
654
|
+
|
655
|
+
deployment = KubernetesDeploy.new(
|
656
|
+
namespace: ARGV[0],
|
657
|
+
context: ARGV[1],
|
658
|
+
environment: ENV['ENVIRONMENT'],
|
659
|
+
current_sha: ENV['REVISION'],
|
660
|
+
template_folder: ENV['K8S_TEMPLATE_FOLDER']
|
661
|
+
)
|
662
|
+
deployment.run
|
@@ -0,0 +1,28 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'kubernetes-deploy/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "kubernetes-deploy"
|
8
|
+
spec.version = KubernetesDeploy::VERSION
|
9
|
+
spec.authors = ["Kir Shatrov", "Jean Boussier", "Katrina Verey"]
|
10
|
+
spec.email = ["ops-accounts+shipit@shopify.com"]
|
11
|
+
|
12
|
+
spec.summary = %q{Kubernetes deploy scripts}
|
13
|
+
spec.description = spec.summary
|
14
|
+
spec.homepage = "https://github.com/Shopify/kubernetes-deploy"
|
15
|
+
spec.license = "MIT"
|
16
|
+
|
17
|
+
spec.files = `git ls-files -z`.split("\x0").reject do |f|
|
18
|
+
f.match(%r{^(test|spec|features)/})
|
19
|
+
end
|
20
|
+
spec.bindir = "exe"
|
21
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
22
|
+
spec.require_paths = ["lib"]
|
23
|
+
spec.add_dependency "activesupport", "~> 4.2"
|
24
|
+
|
25
|
+
spec.add_development_dependency "bundler", "~> 1.13"
|
26
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
27
|
+
spec.add_development_dependency "minitest", "~> 5.0"
|
28
|
+
end
|
metadata
ADDED
@@ -0,0 +1,113 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: kubernetes-deploy
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Kir Shatrov
|
8
|
+
- Jean Boussier
|
9
|
+
- Katrina Verey
|
10
|
+
autorequire:
|
11
|
+
bindir: exe
|
12
|
+
cert_chain: []
|
13
|
+
date: 2017-01-18 00:00:00.000000000 Z
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: activesupport
|
17
|
+
requirement: !ruby/object:Gem::Requirement
|
18
|
+
requirements:
|
19
|
+
- - "~>"
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '4.2'
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
requirements:
|
26
|
+
- - "~>"
|
27
|
+
- !ruby/object:Gem::Version
|
28
|
+
version: '4.2'
|
29
|
+
- !ruby/object:Gem::Dependency
|
30
|
+
name: bundler
|
31
|
+
requirement: !ruby/object:Gem::Requirement
|
32
|
+
requirements:
|
33
|
+
- - "~>"
|
34
|
+
- !ruby/object:Gem::Version
|
35
|
+
version: '1.13'
|
36
|
+
type: :development
|
37
|
+
prerelease: false
|
38
|
+
version_requirements: !ruby/object:Gem::Requirement
|
39
|
+
requirements:
|
40
|
+
- - "~>"
|
41
|
+
- !ruby/object:Gem::Version
|
42
|
+
version: '1.13'
|
43
|
+
- !ruby/object:Gem::Dependency
|
44
|
+
name: rake
|
45
|
+
requirement: !ruby/object:Gem::Requirement
|
46
|
+
requirements:
|
47
|
+
- - "~>"
|
48
|
+
- !ruby/object:Gem::Version
|
49
|
+
version: '10.0'
|
50
|
+
type: :development
|
51
|
+
prerelease: false
|
52
|
+
version_requirements: !ruby/object:Gem::Requirement
|
53
|
+
requirements:
|
54
|
+
- - "~>"
|
55
|
+
- !ruby/object:Gem::Version
|
56
|
+
version: '10.0'
|
57
|
+
- !ruby/object:Gem::Dependency
|
58
|
+
name: minitest
|
59
|
+
requirement: !ruby/object:Gem::Requirement
|
60
|
+
requirements:
|
61
|
+
- - "~>"
|
62
|
+
- !ruby/object:Gem::Version
|
63
|
+
version: '5.0'
|
64
|
+
type: :development
|
65
|
+
prerelease: false
|
66
|
+
version_requirements: !ruby/object:Gem::Requirement
|
67
|
+
requirements:
|
68
|
+
- - "~>"
|
69
|
+
- !ruby/object:Gem::Version
|
70
|
+
version: '5.0'
|
71
|
+
description: Kubernetes deploy scripts
|
72
|
+
email:
|
73
|
+
- ops-accounts+shipit@shopify.com
|
74
|
+
executables:
|
75
|
+
- kubernetes-deploy
|
76
|
+
extensions: []
|
77
|
+
extra_rdoc_files: []
|
78
|
+
files:
|
79
|
+
- ".gitignore"
|
80
|
+
- Gemfile
|
81
|
+
- LICENSE.txt
|
82
|
+
- README.md
|
83
|
+
- Rakefile
|
84
|
+
- bin/console
|
85
|
+
- bin/setup
|
86
|
+
- exe/kubernetes-deploy
|
87
|
+
- kubernetes-deploy.gemspec
|
88
|
+
- lib/kubernetes-deploy/version.rb
|
89
|
+
homepage: https://github.com/Shopify/kubernetes-deploy
|
90
|
+
licenses:
|
91
|
+
- MIT
|
92
|
+
metadata: {}
|
93
|
+
post_install_message:
|
94
|
+
rdoc_options: []
|
95
|
+
require_paths:
|
96
|
+
- lib
|
97
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
98
|
+
requirements:
|
99
|
+
- - ">="
|
100
|
+
- !ruby/object:Gem::Version
|
101
|
+
version: '0'
|
102
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
103
|
+
requirements:
|
104
|
+
- - ">="
|
105
|
+
- !ruby/object:Gem::Version
|
106
|
+
version: '0'
|
107
|
+
requirements: []
|
108
|
+
rubyforge_project:
|
109
|
+
rubygems_version: 2.5.2
|
110
|
+
signing_key:
|
111
|
+
specification_version: 4
|
112
|
+
summary: Kubernetes deploy scripts
|
113
|
+
test_files: []
|