stax-helm 0.0.7 → 0.0.8
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/stax/helm.rb +1 -0
- data/lib/stax/helm/runcmd.rb +101 -0
- data/lib/stax/helm/version.rb +1 -1
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 90efb4bc5fd77b16200e4693747b303df7644ebfe21149d715327a56397f1d25
|
4
|
+
data.tar.gz: 19d2b39ef1cbd165dbd32091f317f8c11b12624d381920bdffd5295b8b8c5b68
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 5566799f8341ace21b46a2ec7c3d90a3c5dc11d26f54c7efa1dc593df439e6dd59732286153823995573a53337252f8249000d54f26dd4813e72ebad23e943ff
|
7
|
+
data.tar.gz: 8d1b6ca7b1e7465cac0f77754438ccff9ad880e12bf7d1eb9b2c58a6f48921bbad3471696ab4cd2e609aeee1cd749c0ce6304c23ad248ece555a2291140c5785
|
data/lib/stax/helm.rb
CHANGED
@@ -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
|
data/lib/stax/helm/version.rb
CHANGED
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.
|
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-
|
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
|