From 9d12fe40f221cf89878201b8e6493415258e8a90 Mon Sep 17 00:00:00 2001 From: haiker2011 Date: Tue, 19 Mar 2019 15:44:35 +0800 Subject: [PATCH 1/2] Add Job examples --- examples/job_examples.py | 97 ++++++++++++++++++++++++++++++++++++++++ examples/pi-job.yaml | 15 +++++++ 2 files changed, 112 insertions(+) create mode 100644 examples/job_examples.py create mode 100644 examples/pi-job.yaml diff --git a/examples/job_examples.py b/examples/job_examples.py new file mode 100644 index 0000000000..441f0bb0eb --- /dev/null +++ b/examples/job_examples.py @@ -0,0 +1,97 @@ +# Copyright 2016 The Kubernetes Authors. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +from os import path + +import yaml + +from kubernetes import client, config + +JOB_NAME = "pi" + + +def create_job_object(): + # Configureate Pod template container + container = client.V1Container( + name="pi", + image="perl", + command=["perl", "-Mbignum=bpi", "-wle", "print bpi(2000)"], + ) + # Create and configurate a spec section + template = client.V1PodTemplateSpec( + metadata=client.V1ObjectMeta(labels={"app": "pi"}), + spec=client.V1PodSpec(restart_policy="Never", containers=[container])) + # Create the specification of deployment + spec = client.V1JobSpec( + template=template, + backoff_limit=4) + # Instantiate the job object + job = client.V1Job( + api_version="batch/v1", + kind="Job", + metadata=client.V1ObjectMeta(name=JOB_NAME), + spec=spec) + + return job + + +def create_job(api_instance, job): + # Create job + api_response = api_instance.create_namespaced_job( + body=job, + namespace="default") + print("Job created. status='%s'" % str(api_response.status)) + + +def update_job(api_instance, job): + # Update container image + job.spec.template.spec.containers[0].image = "perl" + # Update the job + api_response = api_instance.patch_namespaced_job( + name=JOB_NAME, + namespace="default", + body=job) + print("Job updated. status='%s'" % str(api_response.status)) + + +def delete_job(api_instance): + # Delete job + api_response = api_instance.delete_namespaced_job( + name=JOB_NAME, + namespace="default", + body=client.V1DeleteOptions( + propagation_policy='Foreground', + grace_period_seconds=5)) + print("Job deleted. status='%s'" % str(api_response.status)) + + +def main(): + # Configs can be set in Configuration class directly or using helper + # utility. If no argument provided, the config will be loaded from + # default location. + config.load_kube_config() + batch_v1 = client.BatchV1Api() + # Create a job object with client-python API. The job we + # created is same as the `pi-job.yaml` in the /examples folder. + job = create_job_object() + + create_job(batch_v1, job) + + update_job(batch_v1, job) + + delete_job(batch_v1) + + +if __name__ == '__main__': + main() diff --git a/examples/pi-job.yaml b/examples/pi-job.yaml new file mode 100644 index 0000000000..dc736f2da9 --- /dev/null +++ b/examples/pi-job.yaml @@ -0,0 +1,15 @@ +apiVersion: batch/v1 +kind: Job +metadata: + name: pi +spec: + template: + spec: + containers: + - name: pi + image: perl + command: ["perl", "-Mbignum=bpi", "-wle", "print bpi(2000)"] + restartPolicy: Never + backoffLimit: 4 + + From 630b0ac15d46c781e4b0c30c7c5f0cf39bd99733 Mon Sep 17 00:00:00 2001 From: haiker2011 Date: Tue, 19 Mar 2019 16:03:37 +0800 Subject: [PATCH 2/2] fix code style --- examples/job_examples.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/examples/job_examples.py b/examples/job_examples.py index 441f0bb0eb..39a71b8921 100644 --- a/examples/job_examples.py +++ b/examples/job_examples.py @@ -26,8 +26,7 @@ def create_job_object(): container = client.V1Container( name="pi", image="perl", - command=["perl", "-Mbignum=bpi", "-wle", "print bpi(2000)"], - ) + command=["perl", "-Mbignum=bpi", "-wle", "print bpi(2000)"]) # Create and configurate a spec section template = client.V1PodTemplateSpec( metadata=client.V1ObjectMeta(labels={"app": "pi"}),