Created
August 9, 2019 04:03
-
-
Save mshade/a7d7abc967ddf727adf9476cd3a32b16 to your computer and use it in GitHub Desktop.
manually trigger a kubernetes job from a cronjob with python from a pod
This file contains 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 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) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment