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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 2c8ff82b1830c530f1f26441a1ec99b26a8ae8c1fe0616aff6496e363305ac0b
4
- data.tar.gz: 28340df51d8024ac817b65307bcec08ebaa4a839aac55e3e43f50fd9ba7eba8a
3
+ metadata.gz: 44f3e261b827758d975c3e51c00f9319ac280c25592fd50d5980418de7fc4a46
4
+ data.tar.gz: ce69cc4e1c0a898e4baad326c14fd15baeebc18f39416157986a992a7e40596f
5
5
  SHA512:
6
- metadata.gz: c4b46653caac5ff7620678ac1d17dc2fa6c16f7adf68e7c412aee48eb579a072d394fbf1c2d79de6ee56df7c21f6cc9c63eeee49df900c0366928d29c2e91ff8
7
- data.tar.gz: '0946d1421aa008222e8c7dc154182188525d36e4745bd591ae7edf64a5e9e8c7e54cdc23f60410c2f03fee693d6f905ef072c0b014e490b9cb35ceee2e6465e8'
6
+ metadata.gz: 73a7bff727ab4a7f5b42c3b1f1b4d6fd95d2176667978fe454b308c8fc78219eacfeb451ec2bffc50b42f6e8d7d895656ab282c888b48bb9aa0c3ded65fa945d
7
+ data.tar.gz: 931ebe19d76a237f4e357ec0cac817221a52ba96e53630bfd3f76c669096b50694a52dc27f70bcbc7404ece859249592b433169d795f21736970afa02c2dd3ea
@@ -1,3 +1,6 @@
1
+ ## 0.3.0
2
+ * Add ability to redirect kubectl's stdout and stderr streams.
3
+
1
4
  ## 0.2.1
2
5
  * Add missing `require` for open3.
3
6
 
@@ -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, _wait_thread|
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
- system(cmd_s).tap do
183
- self.last_status = $?
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
- `#{cmd_s}`.tap do
192
- self.last_status = $?
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.pipeline_w([env, cmd_s], opts) do |stdin, wait_threads|
202
- yield(stdin, wait_threads).tap do
203
- stdin.close
204
- self.last_status = wait_threads.last.value
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
@@ -1,3 +1,3 @@
1
1
  class KubernetesCLI
2
- VERSION = '0.2.1'
2
+ VERSION = '0.3.0'
3
3
  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.2.1
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-08-02 00:00:00.000000000 Z
11
+ date: 2020-09-10 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: kubectl-rb