stax-helm 0.0.7 → 0.0.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: ce6add2c9a7276df7ed801b49c7f3a7151ba4d98c34b3690a4fdaca6678fdeb5
4
- data.tar.gz: c04af79c0c1fe2a14d7493e175980aeef5864f074f2f405bc67bd7fef35696c8
3
+ metadata.gz: 90efb4bc5fd77b16200e4693747b303df7644ebfe21149d715327a56397f1d25
4
+ data.tar.gz: 19d2b39ef1cbd165dbd32091f317f8c11b12624d381920bdffd5295b8b8c5b68
5
5
  SHA512:
6
- metadata.gz: fb825149e84265f3f50ea1d17554f4514d2db401d771251063f9f1ce81fb90cfe221053b4a7f9c168797777493679a11f56d850af34fbb3c548cff32237174c3
7
- data.tar.gz: 29ff4a39a0ac91b12ea218cff57fd6afa81658ff9fc4ec26d8a90019bab133e8ef3fe5cf7adf3ae895a89a0c3b79f946a8b819e801093ffdf13e8d89418f668f
6
+ metadata.gz: 5566799f8341ace21b46a2ec7c3d90a3c5dc11d26f54c7efa1dc593df439e6dd59732286153823995573a53337252f8249000d54f26dd4813e72ebad23e943ff
7
+ data.tar.gz: 8d1b6ca7b1e7465cac0f77754438ccff9ad880e12bf7d1eb9b2c58a6f48921bbad3471696ab4cd2e609aeee1cd749c0ce6304c23ad248ece555a2291140c5785
@@ -5,4 +5,5 @@ require 'stax/helm/ingress'
5
5
  require 'stax/helm/pod'
6
6
  require 'stax/helm/deployment'
7
7
  require 'stax/helm/stern'
8
+ require 'stax/helm/runcmd'
8
9
  Stax.add_command(:helm, Stax::Helm::Cmd)
@@ -0,0 +1,101 @@
1
+ require 'securerandom'
2
+
3
+ module Stax
4
+ module Helm
5
+ class Cmd
6
+
7
+ no_commands do
8
+ ## construct a Job template from passed container spec
9
+ def helm_run_template(name, container_spec)
10
+ {
11
+ apiVersion: 'batch/v1',
12
+ kind: :Job,
13
+ metadata: {
14
+ name: name,
15
+ labels: {
16
+ 'app.kubernetes.io/managed-by' => :stax
17
+ }
18
+ },
19
+ spec: {
20
+ template: {
21
+ spec: {
22
+ restartPolicy: :Never,
23
+ containers: [ container_spec ],
24
+ }
25
+ }
26
+ }
27
+ }.to_json
28
+ end
29
+
30
+ ## Deployment to clone for container
31
+ def helm_run_deployment
32
+ "#{helm_release_name}-web"
33
+ end
34
+
35
+ ## name of container to clone from Deployment
36
+ def helm_run_container
37
+ 'web'
38
+ end
39
+
40
+ ## name for Job to create based on container
41
+ def helm_run_job
42
+ "#{helm_release_name}-run-#{SecureRandom.hex(4)}"
43
+ end
44
+
45
+ ## default command to run
46
+ def helm_run_cmd
47
+ 'bash'
48
+ end
49
+ end
50
+
51
+ ## task creates a dedicated unique kubernetes Job, with
52
+ ## container spec based on the helm release deployment
53
+ ## requested, then does an interactive exec to the pod and
54
+ ## container created, and deletes the Job on exit
55
+ desc 'runcmd [CMD]', 'run dedicated interactive container'
56
+ method_option :sleep, type: :string, default: '1h', description: 'kill container after time'
57
+ method_option :keep, type: :boolean, default: false, description: 'do not delete job'
58
+ def runcmd(*cmd)
59
+ ## use default if not set
60
+ cmd = Array(helm_run_cmd) if cmd.empty?
61
+
62
+ ## name of k8s Job to create
63
+ job = helm_run_job
64
+
65
+ ## get deployment and extract container spec
66
+ deployment = kubectl_json(:get, :deployment, helm_run_deployment)
67
+ spec = deployment['spec']['template']['spec']['containers'].find do |c|
68
+ c['name'] == helm_run_container
69
+ end
70
+
71
+ ## cleanup the container spec so we can use it in a Job
72
+ spec.delete('livenessProbe')
73
+ spec.delete('readinessProbe')
74
+ spec.delete('volumeMounts')
75
+ spec['name'] = 'run'
76
+ spec['args'] = ['sleep', options[:sleep]]
77
+
78
+ ## create new unique Job based on the container spec
79
+ debug("Creating job #{job}")
80
+ Open3.popen2('kubectl create -f -') { |stdin, stdout, _|
81
+ stdin.print(helm_run_template(job, spec))
82
+ stdin.close
83
+ puts stdout.gets
84
+ }
85
+
86
+ ## get name of the Pod created by the Job
87
+ pod = kubectl_json(:get, :pod, '-l', "job-name=#{job}")['items'].first['metadata']['name']
88
+
89
+ ## exec into the pod and run interactive command
90
+ debug("Connecting to pod #{pod}")
91
+ kubectl_run(:wait, '--for=condition=Ready', '--timeout=5m', :pod, pod)
92
+ kubectl_run(:exec, '-it', pod, '--', *cmd)
93
+ rescue JSON::ParserError
94
+ fail_task('cannot get kubernetes resource')
95
+ ensure
96
+ ## delete Job
97
+ kubectl_run(:delete, :job, job) unless options[:keep]
98
+ end
99
+ end
100
+ end
101
+ end
@@ -1,5 +1,5 @@
1
1
  module Stax
2
2
  module Helm
3
- VERSION = '0.0.7'
3
+ VERSION = '0.0.8'
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: stax-helm
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.7
4
+ version: 0.0.8
5
5
  platform: ruby
6
6
  authors:
7
7
  - Richard Lister
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-09-26 00:00:00.000000000 Z
11
+ date: 2020-12-03 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -73,6 +73,7 @@ files:
73
73
  - lib/stax/helm/ingress.rb
74
74
  - lib/stax/helm/kubectl.rb
75
75
  - lib/stax/helm/pod.rb
76
+ - lib/stax/helm/runcmd.rb
76
77
  - lib/stax/helm/stern.rb
77
78
  - lib/stax/helm/version.rb
78
79
  - stax-helm.gemspec