Upload job_crud.py
Browse files- job_crud.py +112 -0
job_crud.py
ADDED
|
@@ -0,0 +1,112 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
+
"""
|
| 16 |
+
Creates, updates, and deletes a job object.
|
| 17 |
+
"""
|
| 18 |
+
|
| 19 |
+
from os import path
|
| 20 |
+
from time import sleep
|
| 21 |
+
|
| 22 |
+
import yaml
|
| 23 |
+
|
| 24 |
+
from kubernetes import client, config
|
| 25 |
+
|
| 26 |
+
JOB_NAME = "pi"
|
| 27 |
+
|
| 28 |
+
|
| 29 |
+
def create_job_object():
|
| 30 |
+
# Configure Pod template container
|
| 31 |
+
container = client.V1Container(
|
| 32 |
+
name="pi",
|
| 33 |
+
image="perl",
|
| 34 |
+
command=["perl", "-Mbignum=bpi", "-wle", "print bpi(2000)"])
|
| 35 |
+
# Create and configure a spec section
|
| 36 |
+
template = client.V1PodTemplateSpec(
|
| 37 |
+
metadata=client.V1ObjectMeta(labels={"app": "pi"}),
|
| 38 |
+
spec=client.V1PodSpec(restart_policy="Never", containers=[container]))
|
| 39 |
+
# Create the specification of deployment
|
| 40 |
+
spec = client.V1JobSpec(
|
| 41 |
+
template=template,
|
| 42 |
+
backoff_limit=4)
|
| 43 |
+
# Instantiate the job object
|
| 44 |
+
job = client.V1Job(
|
| 45 |
+
api_version="batch/v1",
|
| 46 |
+
kind="Job",
|
| 47 |
+
metadata=client.V1ObjectMeta(name=JOB_NAME),
|
| 48 |
+
spec=spec)
|
| 49 |
+
|
| 50 |
+
return job
|
| 51 |
+
|
| 52 |
+
|
| 53 |
+
def create_job(api_instance, job):
|
| 54 |
+
api_response = api_instance.create_namespaced_job(
|
| 55 |
+
body=job,
|
| 56 |
+
namespace="default")
|
| 57 |
+
print(f"Job created. status='{str(api_response.status)}'")
|
| 58 |
+
get_job_status(api_instance)
|
| 59 |
+
|
| 60 |
+
|
| 61 |
+
def get_job_status(api_instance):
|
| 62 |
+
job_completed = False
|
| 63 |
+
while not job_completed:
|
| 64 |
+
api_response = api_instance.read_namespaced_job_status(
|
| 65 |
+
name=JOB_NAME,
|
| 66 |
+
namespace="default")
|
| 67 |
+
if api_response.status.succeeded is not None or \
|
| 68 |
+
api_response.status.failed is not None:
|
| 69 |
+
job_completed = True
|
| 70 |
+
sleep(1)
|
| 71 |
+
print(f"Job status='{str(api_response.status)}'")
|
| 72 |
+
|
| 73 |
+
|
| 74 |
+
def update_job(api_instance, job):
|
| 75 |
+
# Update container image
|
| 76 |
+
job.spec.template.spec.containers[0].image = "perl"
|
| 77 |
+
api_response = api_instance.patch_namespaced_job(
|
| 78 |
+
name=JOB_NAME,
|
| 79 |
+
namespace="default",
|
| 80 |
+
body=job)
|
| 81 |
+
print(f"Job updated. status='{str(api_response.status)}'")
|
| 82 |
+
|
| 83 |
+
|
| 84 |
+
def delete_job(api_instance):
|
| 85 |
+
api_response = api_instance.delete_namespaced_job(
|
| 86 |
+
name=JOB_NAME,
|
| 87 |
+
namespace="default",
|
| 88 |
+
body=client.V1DeleteOptions(
|
| 89 |
+
propagation_policy='Foreground',
|
| 90 |
+
grace_period_seconds=5))
|
| 91 |
+
print(f"Job deleted. status='{str(api_response.status)}'")
|
| 92 |
+
|
| 93 |
+
|
| 94 |
+
def main():
|
| 95 |
+
# Configs can be set in Configuration class directly or using helper
|
| 96 |
+
# utility. If no argument provided, the config will be loaded from
|
| 97 |
+
# default location.
|
| 98 |
+
config.load_kube_config()
|
| 99 |
+
batch_v1 = client.BatchV1Api()
|
| 100 |
+
# Create a job object with client-python API. The job we
|
| 101 |
+
# created is same as the `pi-job.yaml` in the /examples folder.
|
| 102 |
+
job = create_job_object()
|
| 103 |
+
|
| 104 |
+
create_job(batch_v1, job)
|
| 105 |
+
|
| 106 |
+
update_job(batch_v1, job)
|
| 107 |
+
|
| 108 |
+
delete_job(batch_v1)
|
| 109 |
+
|
| 110 |
+
|
| 111 |
+
if __name__ == '__main__':
|
| 112 |
+
main()
|