Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from kubernetes import client, config
- import datetime
- with open('/run/secrets/kubernetes.io/serviceaccount/namespace', 'r') as ns:
- namespace = ns.read().strip()
- # Reads config from in-container token / cert
- config.load_incluster_config()
- # API client for cronjobs
- batch = client.BatchV1beta1Api()
- # API client for jobs
- jobber = client.BatchV1Api()
- cronjobs = batch.list_namespaced_cron_job(namespace)
- # Just grab the first cronjob as a demo. Pull the job_template to feed to jobs API
- manual_job = cronjobs.items[0].spec.job_template
- # Job names must be unique, so let's tag this as manually triggered and add date
- date_str = datetime.datetime.now().strftime("%Y%m%d%H%M%S.%f")
- manual_job.metadata.name = str(manual_job.metadata.name + '-manual-' + date_str)[:50]
- # Spawn it.
- create_job = jobber.create_namespaced_job(body=manual_job, namespace=namespace)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement