kubernetes-cli 0.1.0 → 0.3.2

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: bb7820fe8fd6c5426e502bfaeab2e5e7b3ae3350900ffdab42acaa3328bd0fd7
4
- data.tar.gz: ce6976bd46b6a7f98438738fc53910fef7ecce53082bb06062be49833892912c
3
+ metadata.gz: e2b3ae42d2a905be32a61c55740bff526f43874dfd9c194391814fea0bb4d8a5
4
+ data.tar.gz: 624b91b4024dbed3b29ccdd8b2537b4e5a2f65d4df72d6a05f6e5141f7bb3c1c
5
5
  SHA512:
6
- metadata.gz: 53d1088eb10bdcaa0373d595fcc8dede6495f16836a2a985957082f8f33a99f2e1ae8892001eb8c15d0327a02c75e75e6ca71b3cf9d57cd7d5cdd5b2e6396199
7
- data.tar.gz: 80b1971145ced2eb828df6cf61c63b909faa6a55650c745eaa1725b229e82f355ad1b7128834f38f1f3e6fe0a1205c917ab68ce9aa147f17674582aac209c724
6
+ metadata.gz: 892beee416b11f888cb91fa454bbfde2d4d9464cad77de079a9c1ccbe3c3c05ce285c24fb1e0036a00c999fb344101013cfb10e14a8aadaea95e4ca336bd0351
7
+ data.tar.gz: ffbf3c1358d19e589b5763c3dbe51de8e622d1dccf9dfda05727cefc0ef3fc8fed4a037ff664a3f7e42e9a6052559e2d9f6a4dd8ca5cba8c4bf74e6e465fbe77
data/CHANGELOG.md CHANGED
@@ -1,2 +1,19 @@
1
+ ## 0.3.2
2
+ * Add missing require statement.
3
+
4
+ ## 0.3.1
5
+ * Fix issue restricting Docker CLI output.
6
+
7
+ ## 0.3.0
8
+ * Add ability to redirect kubectl's stdout and stderr streams.
9
+
10
+ ## 0.2.1
11
+ * Add missing `require` for open3.
12
+
13
+ ## 0.2.0
14
+ * Remove doubly-defined method KubernetesCLI#backticks.
15
+ * Move `status_key` into a constant.
16
+ * Add error classes I missed during extraction from kuby-core.
17
+
1
18
  ## 0.1.0
2
19
  * Birthday!
@@ -1,6 +1,25 @@
1
1
  require 'kubectl-rb'
2
+ require 'open3'
3
+ require 'stringio'
4
+ require 'json'
2
5
 
3
6
  class KubernetesCLI
7
+ class KubernetesError < StandardError; end
8
+
9
+ class InvalidResourceError < KubernetesError
10
+ attr_accessor :resource
11
+ end
12
+
13
+ class InvalidResourceUriError < KubernetesError
14
+ attr_accessor :resource_uri
15
+ end
16
+
17
+ class GetResourceError < KubernetesError; end
18
+
19
+ STATUS_KEY = :kubernetes_cli_last_status
20
+ STDOUT_KEY = :kubernetes_cli_stdout
21
+ STDERR_KEY = :kubernetes_cli_stderr
22
+
4
23
  attr_reader :kubeconfig_path, :executable
5
24
 
6
25
  def initialize(kubeconfig_path, executable = KubectlRb.executable)
@@ -19,7 +38,7 @@ class KubernetesCLI
19
38
  end
20
39
 
21
40
  def last_status
22
- Thread.current[status_key]
41
+ Thread.current[STATUS_KEY]
23
42
  end
24
43
 
25
44
  def run_cmd(cmd)
@@ -34,12 +53,19 @@ class KubernetesCLI
34
53
  execc(cmd)
35
54
  end
36
55
 
56
+ def system_cmd(container_cmd, namespace, pod, tty = true)
57
+ cmd = [executable, '--kubeconfig', kubeconfig_path, '-n', namespace, 'exec']
58
+ cmd += ['-it'] if tty
59
+ cmd += [pod, '--', *Array(container_cmd)]
60
+ systemm(cmd)
61
+ end
62
+
37
63
  def apply(res, dry_run: false)
38
64
  cmd = [executable, '--kubeconfig', kubeconfig_path, 'apply', '--validate']
39
65
  cmd << '--dry-run=client' if dry_run
40
66
  cmd += ['-f', '-']
41
67
 
42
- open3_w(env, cmd) do |stdin, _wait_thread|
68
+ open3_w(env, cmd) do |stdin|
43
69
  stdin.puts(res.to_resource.to_yaml)
44
70
  end
45
71
 
@@ -127,7 +153,7 @@ class KubernetesCLI
127
153
  systemm(cmd)
128
154
 
129
155
  unless last_status.success?
130
- raise KubernetesCLIError, "could not annotate resource '#{name}': kubectl "\
156
+ raise KubernetesError, "could not annotate resource '#{name}': kubectl "\
131
157
  "exited with status code #{last_status.exitstatus}"
132
158
  end
133
159
  end
@@ -145,27 +171,71 @@ class KubernetesCLI
145
171
  backticks(cmd).strip
146
172
  end
147
173
 
174
+ def api_resources
175
+ cmd = [executable, '--kubeconfig', kubeconfig_path, 'api-resources']
176
+ result = backticks(cmd)
177
+
178
+ unless last_status.success?
179
+ raise KubernetesError, 'could not fetch API resources: kubectl exited with '\
180
+ "status code #{last_status.exitstatus}. #{result}"
181
+ end
182
+
183
+ result
184
+ end
185
+
186
+ def restart_deployment(namespace, deployment)
187
+ cmd = [
188
+ executable,
189
+ '--kubeconfig', kubeconfig_path,
190
+ '-n', namespace,
191
+ 'rollout', 'restart', 'deployment', deployment
192
+ ]
193
+
194
+ systemm(cmd)
195
+
196
+ unless last_status.success?
197
+ raise KubernetesError, 'could not restart deployment: kubectl exited with '\
198
+ "status code #{last_status.exitstatus}"
199
+ end
200
+ end
201
+
202
+ def with_pipes(out = STDOUT, err = STDERR)
203
+ previous_stdout = self.stdout
204
+ previous_stderr = self.stderr
205
+ self.stdout = out
206
+ self.stderr = err
207
+ yield
208
+ ensure
209
+ self.stdout = previous_stdout
210
+ self.stderr = previous_stderr
211
+ end
212
+
213
+ def stdout
214
+ Thread.current[STDOUT_KEY] || STDOUT
215
+ end
216
+
217
+ def stdout=(new_stdout)
218
+ Thread.current[STDOUT_KEY] = new_stdout
219
+ end
220
+
221
+ def stderr
222
+ Thread.current[STDERR_KEY] || STDERR
223
+ end
224
+
225
+ def stderr=(new_stderr)
226
+ Thread.current[STDERR_KEY] = new_stderr
227
+ end
228
+
148
229
  private
149
230
 
150
231
  def env
151
232
  @env ||= {}
152
233
  end
153
234
 
154
- def status_key
155
- :kubernetes_cli_last_status
156
- end
157
-
158
235
  def base_cmd
159
236
  [executable, '--kubeconfig', kubeconfig_path]
160
237
  end
161
238
 
162
- def backticks(cmd)
163
- cmd_s = cmd.join(' ')
164
- `#{cmd_s}`.tap do
165
- self.last_status = $?
166
- end
167
- end
168
-
169
239
  def execc(cmd)
170
240
  run_before_callbacks(cmd)
171
241
  cmd_s = cmd.join(' ')
@@ -173,6 +243,14 @@ class KubernetesCLI
173
243
  end
174
244
 
175
245
  def systemm(cmd)
246
+ if stdout == STDOUT && stderr == STDERR
247
+ systemm_default(cmd)
248
+ else
249
+ systemm_open3(cmd)
250
+ end
251
+ end
252
+
253
+ def systemm_default(cmd)
176
254
  run_before_callbacks(cmd)
177
255
  cmd_s = cmd.join(' ')
178
256
  system(cmd_s).tap do
@@ -181,7 +259,41 @@ class KubernetesCLI
181
259
  end
182
260
  end
183
261
 
262
+ def systemm_open3(cmd)
263
+ run_before_callbacks(cmd)
264
+ cmd_s = cmd.join(' ')
265
+
266
+ Open3.popen3(cmd_s) do |p_stdin, p_stdout, p_stderr, wait_thread|
267
+ Thread.new(stdout) do |t_stdout|
268
+ begin
269
+ p_stdout.each { |line| t_stdout.puts(line) }
270
+ rescue IOError
271
+ end
272
+ end
273
+
274
+ Thread.new(stderr) do |t_stderr|
275
+ begin
276
+ p_stderr.each { |line| t_stderr.puts(line) }
277
+ rescue IOError
278
+ end
279
+ end
280
+
281
+ p_stdin.close
282
+ self.last_status = wait_thread.value
283
+ run_after_callbacks(cmd)
284
+ wait_thread.join
285
+ end
286
+ end
287
+
184
288
  def backticks(cmd)
289
+ if stdout == STDOUT && stderr == STDERR
290
+ backticks_default(cmd)
291
+ else
292
+ backticks_open3(cmd)
293
+ end
294
+ end
295
+
296
+ def backticks_default(cmd)
185
297
  run_before_callbacks(cmd)
186
298
  cmd_s = cmd.join(' ')
187
299
  `#{cmd_s}`.tap do
@@ -190,15 +302,59 @@ class KubernetesCLI
190
302
  end
191
303
  end
192
304
 
305
+ def backticks_open3(cmd)
306
+ run_before_callbacks(cmd)
307
+ cmd_s = cmd.join(' ')
308
+ result = StringIO.new
309
+
310
+ Open3.popen3(cmd_s) do |p_stdin, p_stdout, p_stderr, wait_thread|
311
+ Thread.new do
312
+ begin
313
+ p_stdout.each { |line| result.puts(line) }
314
+ rescue IOError
315
+ end
316
+ end
317
+
318
+ Thread.new(stderr) do |t_stderr|
319
+ begin
320
+ p_stderr.each { |line| t_stderr.puts(line) }
321
+ rescue IOError
322
+ end
323
+ end
324
+
325
+ p_stdin.close
326
+ self.last_status = wait_thread.value
327
+ run_after_callbacks(cmd)
328
+ wait_thread.join
329
+ end
330
+
331
+ result.string
332
+ end
333
+
193
334
  def open3_w(env, cmd, opts = {}, &block)
194
335
  run_before_callbacks(cmd)
195
336
  cmd_s = cmd.join(' ')
196
337
 
197
- Open3.pipeline_w([env, cmd_s], opts) do |stdin, wait_threads|
198
- yield(stdin, wait_threads).tap do
199
- stdin.close
200
- self.last_status = wait_threads.last.value
338
+ Open3.popen3(env, cmd_s, opts) do |p_stdin, p_stdout, p_stderr, wait_thread|
339
+ Thread.new(stdout) do |t_stdout|
340
+ begin
341
+ p_stdout.each { |line| t_stdout.puts(line) }
342
+ rescue IOError
343
+ end
344
+ end
345
+
346
+ Thread.new(stderr) do |t_stderr|
347
+ begin
348
+ p_stderr.each { |line| t_stderr.puts(line) }
349
+ rescue IOError
350
+ end
351
+ end
352
+
353
+ yield(p_stdin).tap do
354
+ p_stdin.close
355
+ self.last_status = wait_thread.value
201
356
  run_after_callbacks(cmd)
357
+ wait_thread.join
202
358
  end
203
359
  end
204
360
  end
@@ -212,6 +368,6 @@ class KubernetesCLI
212
368
  end
213
369
 
214
370
  def last_status=(status)
215
- Thread.current[status_key] = status
371
+ Thread.current[STATUS_KEY] = status
216
372
  end
217
373
  end
@@ -1,3 +1,3 @@
1
1
  class KubernetesCLI
2
- VERSION = '0.1.0'
2
+ VERSION = '0.3.2'
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.1.0
4
+ version: 0.3.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Cameron Dutro
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-06-04 00:00:00.000000000 Z
11
+ date: 2021-06-06 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: kubectl-rb
@@ -42,7 +42,7 @@ homepage: http://github.com/getkuby/kubernetes-cli
42
42
  licenses:
43
43
  - MIT
44
44
  metadata: {}
45
- post_install_message:
45
+ post_install_message:
46
46
  rdoc_options: []
47
47
  require_paths:
48
48
  - lib
@@ -57,8 +57,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
57
57
  - !ruby/object:Gem::Version
58
58
  version: '0'
59
59
  requirements: []
60
- rubygems_version: 3.0.6
61
- signing_key:
60
+ rubygems_version: 3.1.4
61
+ signing_key:
62
62
  specification_version: 4
63
63
  summary: Ruby wrapper around the Kubernetes CLI.
64
64
  test_files: []