Skip to content

Instantly share code, notes, and snippets.

@StevenACoffman
Forked from nomastmas/cascade_delete.py
Created July 25, 2018 01:21
Show Gist options
  • Save StevenACoffman/f3403a78e39fdfc94377cff7348c7ea5 to your computer and use it in GitHub Desktop.
Save StevenACoffman/f3403a78e39fdfc94377cff7348c7ea5 to your computer and use it in GitHub Desktop.
script to reproduce pods not being deleted on job delete through python api client
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)
@StevenACoffman
Copy link
Author

    def kubernetes_job(self, containers_info,  job_name='default_job', shutdown_on_finish=True):

        # Scale the Kubernetes to 3 nodes
        self.setClusterSize(3)
        timestampped_job_name = "{}-{:%Y-%m-%d-%H-%M-%S}".format(job_name, datetime.datetime.now())
        # Adding the container to a pod definition
        pod = kubernetes.client.V1PodSpec()
        pod.containers = self.create_containers(containers_info)
        pod.name = "p-{}".format(timestampped_job_name)
        pod.restart_policy = 'OnFailure'
        # Adding the pod to a Job template
        template = kubernetes.client.V1PodTemplateSpec()
        template_metadata = kubernetes.client.V1ObjectMeta()
        template_metadata.name = "tpl-{}".format(timestampped_job_name)
        template.metadata = template_metadata
        template.spec = pod
        # Adding the Job Template to the Job spec
        spec = kubernetes.client.V1JobSpec()
        spec.template = template
        # Adding the final job spec to the top level Job object
        body = kubernetes.client.V1Job()
        body.api_version = "batch/v1"
        body.kind = "Job"
        metadata = kubernetes.client.V1ObjectMeta()
        metadata.name = timestampped_job_name
        body.metadata = metadata
        body.spec = spec
        try:
            # Creating the job
            api_response = self.api_instance.create_namespaced_job(self.namespace, body)
            logging.info('job creations result'.format(api_response))
        except ApiException as e:
            print("Exception when calling BatchV1Api->create_namespaced_job: %s\n" % e)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment