Skip to content

Commit 9d12fe4

Browse files
committed
Add Job examples
1 parent 374233d commit 9d12fe4

File tree

2 files changed

+112
-0
lines changed

2 files changed

+112
-0
lines changed

examples/job_examples.py

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
# Copyright 2016 The Kubernetes Authors.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
from os import path
16+
17+
import yaml
18+
19+
from kubernetes import client, config
20+
21+
JOB_NAME = "pi"
22+
23+
24+
def create_job_object():
25+
# Configureate Pod template container
26+
container = client.V1Container(
27+
name="pi",
28+
image="perl",
29+
command=["perl", "-Mbignum=bpi", "-wle", "print bpi(2000)"],
30+
)
31+
# Create and configurate a spec section
32+
template = client.V1PodTemplateSpec(
33+
metadata=client.V1ObjectMeta(labels={"app": "pi"}),
34+
spec=client.V1PodSpec(restart_policy="Never", containers=[container]))
35+
# Create the specification of deployment
36+
spec = client.V1JobSpec(
37+
template=template,
38+
backoff_limit=4)
39+
# Instantiate the job object
40+
job = client.V1Job(
41+
api_version="batch/v1",
42+
kind="Job",
43+
metadata=client.V1ObjectMeta(name=JOB_NAME),
44+
spec=spec)
45+
46+
return job
47+
48+
49+
def create_job(api_instance, job):
50+
# Create job
51+
api_response = api_instance.create_namespaced_job(
52+
body=job,
53+
namespace="default")
54+
print("Job created. status='%s'" % str(api_response.status))
55+
56+
57+
def update_job(api_instance, job):
58+
# Update container image
59+
job.spec.template.spec.containers[0].image = "perl"
60+
# Update the job
61+
api_response = api_instance.patch_namespaced_job(
62+
name=JOB_NAME,
63+
namespace="default",
64+
body=job)
65+
print("Job updated. status='%s'" % str(api_response.status))
66+
67+
68+
def delete_job(api_instance):
69+
# Delete job
70+
api_response = api_instance.delete_namespaced_job(
71+
name=JOB_NAME,
72+
namespace="default",
73+
body=client.V1DeleteOptions(
74+
propagation_policy='Foreground',
75+
grace_period_seconds=5))
76+
print("Job deleted. status='%s'" % str(api_response.status))
77+
78+
79+
def main():
80+
# Configs can be set in Configuration class directly or using helper
81+
# utility. If no argument provided, the config will be loaded from
82+
# default location.
83+
config.load_kube_config()
84+
batch_v1 = client.BatchV1Api()
85+
# Create a job object with client-python API. The job we
86+
# created is same as the `pi-job.yaml` in the /examples folder.
87+
job = create_job_object()
88+
89+
create_job(batch_v1, job)
90+
91+
update_job(batch_v1, job)
92+
93+
delete_job(batch_v1)
94+
95+
96+
if __name__ == '__main__':
97+
main()

examples/pi-job.yaml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
apiVersion: batch/v1
2+
kind: Job
3+
metadata:
4+
name: pi
5+
spec:
6+
template:
7+
spec:
8+
containers:
9+
- name: pi
10+
image: perl
11+
command: ["perl", "-Mbignum=bpi", "-wle", "print bpi(2000)"]
12+
restartPolicy: Never
13+
backoffLimit: 4
14+
15+

0 commit comments

Comments
 (0)