Advertisement
Guest User

Untitled

a guest
Aug 21st, 2019
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.84 KB | None | 0 0
  1. from kubernetes import client, config
  2. import datetime
  3.  
  4.  
  5. with open('/run/secrets/kubernetes.io/serviceaccount/namespace', 'r') as ns:
  6. namespace = ns.read().strip()
  7.  
  8. # Reads config from in-container token / cert
  9. config.load_incluster_config()
  10.  
  11. # API client for cronjobs
  12. batch = client.BatchV1beta1Api()
  13. # API client for jobs
  14. jobber = client.BatchV1Api()
  15.  
  16. cronjobs = batch.list_namespaced_cron_job(namespace)
  17.  
  18. # Just grab the first cronjob as a demo. Pull the job_template to feed to jobs API
  19. manual_job = cronjobs.items[0].spec.job_template
  20.  
  21. # Job names must be unique, so let's tag this as manually triggered and add date
  22. date_str = datetime.datetime.now().strftime("%Y%m%d%H%M%S.%f")
  23. manual_job.metadata.name = str(manual_job.metadata.name + '-manual-' + date_str)[:50]
  24.  
  25. # Spawn it.
  26. create_job = jobber.create_namespaced_job(body=manual_job, namespace=namespace)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement