Created
May 24, 2017 20:31
-
-
Save nomastmas/6f8c9290b38a7042eba6be6491246b28 to your computer and use it in GitHub Desktop.
script to reproduce pods not being deleted on job delete through python api client
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from __future__ import unicode_literals | |
from time import sleep | |
import yaml | |
from kubernetes import client, config | |
from kubernetes.client.rest import ApiException | |
job_template = """ | |
apiVersion: batch/v1 | |
kind: Job | |
metadata: | |
name: pi | |
spec: | |
template: | |
metadata: | |
name: pi | |
spec: | |
containers: | |
- name: pi | |
image: perl | |
command: ["perl", "-Mbignum=bpi", "-wle", "print bpi(2000)"] | |
restartPolicy: Never | |
""" | |
propagation_policies = ['Ophaned', 'Background', 'Foreground'] | |
# load config for ssl stuff | |
config.load_kube_config() | |
job_name = 'pi' | |
namespace = 'sharedservices' | |
job_api = client.BatchV1Api() | |
pod_api = client.CoreV1Api() | |
# load job template | |
job_yaml = yaml.load(job_template) | |
for propagation_policy in propagation_policies: | |
# create job | |
try: | |
job_api.create_namespaced_job(namespace, job_yaml) | |
print("Started job '{job}' in '{namespace}'".format(job=job_name, namespace=namespace)) | |
except ApiException as e: | |
print("Exception when calling BatchV1Api->create_namespaced_job: %s" % e) | |
sleep(1) | |
# check pods | |
api_response = pod_api.list_pod_for_all_namespaces( | |
label_selector="job-name={}".format(job_name), pretty=True) | |
pod_name = api_response.items[0].metadata.name | |
# sleeping because cbb to check container status | |
sleep(10) | |
# clean up | |
delete_options = client.V1DeleteOptions() | |
print("Deleting job '{}' with '{}' propagation policy".format(job_name, propagation_policy)) | |
job_api.delete_namespaced_job(job_name, namespace, delete_options, | |
propagation_policy="Ophaned") | |
api_response = pod_api.list_pod_for_all_namespaces( | |
label_selector="job-name={}".format(job_name), pretty=True) | |
# sleeping a bit just in case delete cascades are slow | |
sleep(10) | |
for item in api_response.items: | |
pod_name = item.metadata.name | |
print("Found '{}', deleting...".format(pod_name)) | |
api_response = pod_api.delete_namespaced_pod(pod_name, namespace, delete_options) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
sample out: