-
Notifications
You must be signed in to change notification settings - Fork 1
/
kubeclient-job.rb
executable file
·47 lines (38 loc) · 1.21 KB
/
kubeclient-job.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#!/usr/bin/env ruby
require 'kubeclient'
# Following the example from http://kubernetes.io/docs/user-guide/jobs/ with kubeclient
job_client = Kubeclient::Client.new('http://localhost:8080/apis/batch' , 'v1', ssl_options: { verify_ssl: 0 })
JOB = {
'apiVersion' => 'batch/v1',
'metadata' => {
'name' => 'pi',
'namespace' => 'default'
},
'spec' => {
'template' => {
'metadata' => {
'name' => 'pi'
},
'spec' => {
'containers' => [
{
'name' => 'pi',
'image' => 'perl',
'command' => ['perl', '-Mbignum=bpi', '-wle', 'print bpi(2000)']
}
],
'restartPolicy' => 'Never'
}
}
}
}
job = Kubeclient::Resource.new(JOB)
job_client.create_job(job)
client = Kubeclient::Client.new('http://localhost:8080/api' , 'v1', ssl_options: { verify_ssl: 0 })
# Wait for job completion
sleep 20
job_pod = client.get_pods(label_selector: 'job-name=pi').first
res = client.get_pod_log(job_pod.metadata.name, job_pod.metadata.namespace)
puts "result: #{res}"
job_client.delete_job(job.metadata.name, job.metadata.namespace)
client.get_pods.each {|x| client.delete_pod(x.metadata.name, x.metadata.namespace) }