kubernetes-cli 0.2.1 → 0.3.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +3 -0
- data/lib/kubernetes-cli.rb +126 -9
- data/lib/kubernetes-cli/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 44f3e261b827758d975c3e51c00f9319ac280c25592fd50d5980418de7fc4a46
|
4
|
+
data.tar.gz: ce69cc4e1c0a898e4baad326c14fd15baeebc18f39416157986a992a7e40596f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 73a7bff727ab4a7f5b42c3b1f1b4d6fd95d2176667978fe454b308c8fc78219eacfeb451ec2bffc50b42f6e8d7d895656ab282c888b48bb9aa0c3ded65fa945d
|
7
|
+
data.tar.gz: 931ebe19d76a237f4e357ec0cac817221a52ba96e53630bfd3f76c669096b50694a52dc27f70bcbc7404ece859249592b433169d795f21736970afa02c2dd3ea
|
data/CHANGELOG.md
CHANGED
data/lib/kubernetes-cli.rb
CHANGED
@@ -1,5 +1,6 @@
|
|
1
1
|
require 'kubectl-rb'
|
2
2
|
require 'open3'
|
3
|
+
require 'stringio'
|
3
4
|
|
4
5
|
class KubernetesCLI
|
5
6
|
class KubernetesError < StandardError; end
|
@@ -15,6 +16,8 @@ class KubernetesCLI
|
|
15
16
|
class GetResourceError < KubernetesError; end
|
16
17
|
|
17
18
|
STATUS_KEY = :kubernetes_cli_last_status
|
19
|
+
STDOUT_KEY = :kubernetes_cli_stdout
|
20
|
+
STDERR_KEY = :kubernetes_cli_stderr
|
18
21
|
|
19
22
|
attr_reader :kubeconfig_path, :executable
|
20
23
|
|
@@ -49,12 +52,19 @@ class KubernetesCLI
|
|
49
52
|
execc(cmd)
|
50
53
|
end
|
51
54
|
|
55
|
+
def system_cmd(container_cmd, namespace, pod, tty = true)
|
56
|
+
cmd = [executable, '--kubeconfig', kubeconfig_path, '-n', namespace, 'exec']
|
57
|
+
cmd += ['-it'] if tty
|
58
|
+
cmd += [pod, '--', *Array(container_cmd)]
|
59
|
+
systemm(cmd)
|
60
|
+
end
|
61
|
+
|
52
62
|
def apply(res, dry_run: false)
|
53
63
|
cmd = [executable, '--kubeconfig', kubeconfig_path, 'apply', '--validate']
|
54
64
|
cmd << '--dry-run=client' if dry_run
|
55
65
|
cmd += ['-f', '-']
|
56
66
|
|
57
|
-
open3_w(env, cmd) do |stdin
|
67
|
+
open3_w(env, cmd) do |stdin|
|
58
68
|
stdin.puts(res.to_resource.to_yaml)
|
59
69
|
end
|
60
70
|
|
@@ -160,6 +170,61 @@ class KubernetesCLI
|
|
160
170
|
backticks(cmd).strip
|
161
171
|
end
|
162
172
|
|
173
|
+
def api_resources
|
174
|
+
cmd = [executable, '--kubeconfig', kubeconfig_path, 'api-resources']
|
175
|
+
result = backticks(cmd)
|
176
|
+
|
177
|
+
unless last_status.success?
|
178
|
+
raise KubernetesError, 'could not fetch API resources: kubectl exited with '\
|
179
|
+
"status code #{last_status.exitstatus}. #{result}"
|
180
|
+
end
|
181
|
+
|
182
|
+
result
|
183
|
+
end
|
184
|
+
|
185
|
+
def restart_deployment(namespace, deployment)
|
186
|
+
cmd = [
|
187
|
+
executable,
|
188
|
+
'--kubeconfig', kubeconfig_path,
|
189
|
+
'-n', namespace,
|
190
|
+
'rollout', 'restart', 'deployment', deployment
|
191
|
+
]
|
192
|
+
|
193
|
+
systemm(cmd)
|
194
|
+
|
195
|
+
unless last_status.success?
|
196
|
+
raise KubernetesError, 'could not restart deployment: kubectl exited with '\
|
197
|
+
"status code #{last_status.exitstatus}"
|
198
|
+
end
|
199
|
+
end
|
200
|
+
|
201
|
+
def with_pipes(out = STDOUT, err = STDERR)
|
202
|
+
previous_stdout = self.stdout
|
203
|
+
previous_stderr = self.stderr
|
204
|
+
self.stdout = out
|
205
|
+
self.stderr = err
|
206
|
+
yield
|
207
|
+
ensure
|
208
|
+
self.stdout = previous_stdout
|
209
|
+
self.stderr = previous_stderr
|
210
|
+
end
|
211
|
+
|
212
|
+
def stdout
|
213
|
+
Thread.current[STDOUT_KEY] || STDOUT
|
214
|
+
end
|
215
|
+
|
216
|
+
def stdout=(new_stdout)
|
217
|
+
Thread.current[STDOUT_KEY] = new_stdout
|
218
|
+
end
|
219
|
+
|
220
|
+
def stderr
|
221
|
+
Thread.current[STDERR_KEY] || STDERR
|
222
|
+
end
|
223
|
+
|
224
|
+
def stderr=(new_stderr)
|
225
|
+
Thread.current[STDERR_KEY] = new_stderr
|
226
|
+
end
|
227
|
+
|
163
228
|
private
|
164
229
|
|
165
230
|
def env
|
@@ -179,30 +244,82 @@ class KubernetesCLI
|
|
179
244
|
def systemm(cmd)
|
180
245
|
run_before_callbacks(cmd)
|
181
246
|
cmd_s = cmd.join(' ')
|
182
|
-
|
183
|
-
|
247
|
+
|
248
|
+
Open3.popen3(cmd_s) do |p_stdin, p_stdout, p_stderr, wait_thread|
|
249
|
+
Thread.new(stdout) do |t_stdout|
|
250
|
+
begin
|
251
|
+
p_stdout.each { |line| t_stdout.puts(line) }
|
252
|
+
rescue IOError
|
253
|
+
end
|
254
|
+
end
|
255
|
+
|
256
|
+
Thread.new(stderr) do |t_stderr|
|
257
|
+
begin
|
258
|
+
p_stderr.each { |line| t_stderr.puts(line) }
|
259
|
+
rescue IOError
|
260
|
+
end
|
261
|
+
end
|
262
|
+
|
263
|
+
p_stdin.close
|
264
|
+
self.last_status = wait_thread.value
|
184
265
|
run_after_callbacks(cmd)
|
266
|
+
wait_thread.join
|
185
267
|
end
|
186
268
|
end
|
187
269
|
|
188
270
|
def backticks(cmd)
|
189
271
|
run_before_callbacks(cmd)
|
190
272
|
cmd_s = cmd.join(' ')
|
191
|
-
|
192
|
-
|
273
|
+
result = StringIO.new
|
274
|
+
|
275
|
+
Open3.popen3(cmd_s) do |p_stdin, p_stdout, p_stderr, wait_thread|
|
276
|
+
Thread.new do
|
277
|
+
begin
|
278
|
+
p_stdout.each { |line| result.puts(line) }
|
279
|
+
rescue IOError
|
280
|
+
end
|
281
|
+
end
|
282
|
+
|
283
|
+
Thread.new(stderr) do |t_stderr|
|
284
|
+
begin
|
285
|
+
p_stderr.each { |line| t_stderr.puts(line) }
|
286
|
+
rescue IOError
|
287
|
+
end
|
288
|
+
end
|
289
|
+
|
290
|
+
p_stdin.close
|
291
|
+
self.last_status = wait_thread.value
|
193
292
|
run_after_callbacks(cmd)
|
293
|
+
wait_thread.join
|
194
294
|
end
|
295
|
+
|
296
|
+
result.string
|
195
297
|
end
|
196
298
|
|
197
299
|
def open3_w(env, cmd, opts = {}, &block)
|
198
300
|
run_before_callbacks(cmd)
|
199
301
|
cmd_s = cmd.join(' ')
|
200
302
|
|
201
|
-
Open3.
|
202
|
-
|
203
|
-
|
204
|
-
|
303
|
+
Open3.popen3(env, cmd_s, opts) do |p_stdin, p_stdout, p_stderr, wait_thread|
|
304
|
+
Thread.new(stdout) do |t_stdout|
|
305
|
+
begin
|
306
|
+
p_stdout.each { |line| t_stdout.puts(line) }
|
307
|
+
rescue IOError
|
308
|
+
end
|
309
|
+
end
|
310
|
+
|
311
|
+
Thread.new(stderr) do |t_stderr|
|
312
|
+
begin
|
313
|
+
p_stderr.each { |line| t_stderr.puts(line) }
|
314
|
+
rescue IOError
|
315
|
+
end
|
316
|
+
end
|
317
|
+
|
318
|
+
yield(p_stdin).tap do
|
319
|
+
p_stdin.close
|
320
|
+
self.last_status = wait_thread.value
|
205
321
|
run_after_callbacks(cmd)
|
322
|
+
wait_thread.join
|
206
323
|
end
|
207
324
|
end
|
208
325
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: kubernetes-cli
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Cameron Dutro
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2020-
|
11
|
+
date: 2020-09-10 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: kubectl-rb
|