|  | 
|  | 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 | +    # Create and configurate a spec section | 
|  | 31 | +    template = client.V1PodTemplateSpec( | 
|  | 32 | +        metadata=client.V1ObjectMeta(labels={"app": "pi"}), | 
|  | 33 | +        spec=client.V1PodSpec(restart_policy="Never", containers=[container])) | 
|  | 34 | +    # Create the specification of deployment | 
|  | 35 | +    spec = client.V1JobSpec( | 
|  | 36 | +        template=template, | 
|  | 37 | +        backoff_limit=4) | 
|  | 38 | +    # Instantiate the job object | 
|  | 39 | +    job = client.V1Job( | 
|  | 40 | +        api_version="batch/v1", | 
|  | 41 | +        kind="Job", | 
|  | 42 | +        metadata=client.V1ObjectMeta(name=JOB_NAME), | 
|  | 43 | +        spec=spec) | 
|  | 44 | + | 
|  | 45 | +    return job | 
|  | 46 | + | 
|  | 47 | + | 
|  | 48 | +def create_job(api_instance, job): | 
|  | 49 | +    # Create job | 
|  | 50 | +    api_response = api_instance.create_namespaced_job( | 
|  | 51 | +        body=job, | 
|  | 52 | +        namespace="default") | 
|  | 53 | +    print("Job created. status='%s'" % str(api_response.status)) | 
|  | 54 | + | 
|  | 55 | + | 
|  | 56 | +def update_job(api_instance, job): | 
|  | 57 | +    # Update container image | 
|  | 58 | +    job.spec.template.spec.containers[0].image = "perl" | 
|  | 59 | +    # Update the job | 
|  | 60 | +    api_response = api_instance.patch_namespaced_job( | 
|  | 61 | +        name=JOB_NAME, | 
|  | 62 | +        namespace="default", | 
|  | 63 | +        body=job) | 
|  | 64 | +    print("Job updated. status='%s'" % str(api_response.status)) | 
|  | 65 | + | 
|  | 66 | + | 
|  | 67 | +def delete_job(api_instance): | 
|  | 68 | +    # Delete job | 
|  | 69 | +    api_response = api_instance.delete_namespaced_job( | 
|  | 70 | +        name=JOB_NAME, | 
|  | 71 | +        namespace="default", | 
|  | 72 | +        body=client.V1DeleteOptions( | 
|  | 73 | +            propagation_policy='Foreground', | 
|  | 74 | +            grace_period_seconds=5)) | 
|  | 75 | +    print("Job deleted. status='%s'" % str(api_response.status)) | 
|  | 76 | + | 
|  | 77 | + | 
|  | 78 | +def main(): | 
|  | 79 | +    # Configs can be set in Configuration class directly or using helper | 
|  | 80 | +    # utility. If no argument provided, the config will be loaded from | 
|  | 81 | +    # default location. | 
|  | 82 | +    config.load_kube_config() | 
|  | 83 | +    batch_v1 = client.BatchV1Api() | 
|  | 84 | +    # Create a job object with client-python API. The job we | 
|  | 85 | +    # created is same as the `pi-job.yaml` in the /examples folder. | 
|  | 86 | +    job = create_job_object() | 
|  | 87 | + | 
|  | 88 | +    create_job(batch_v1, job) | 
|  | 89 | + | 
|  | 90 | +    update_job(batch_v1, job) | 
|  | 91 | + | 
|  | 92 | +    delete_job(batch_v1) | 
|  | 93 | + | 
|  | 94 | + | 
|  | 95 | +if __name__ == '__main__': | 
|  | 96 | +    main() | 
0 commit comments