When working with kubectl I can easily create a Job from existing Cronjob like this:
kubectl create job --from=cronjob/<existing-cronjob-name> <your-job-name>
How can I do the same using kubernetes-client?
/cc @fabianvf
The kubectl create job --from=cronjob/* command simply pulls down the cronjob, pulls the jobTemplate out of its spec, and then creates the Job object using that template. The below code should do the same thing:
from kubernetes import client, config
job_name = 'from-cron-job'
cron_job_name = 'example'
namespace = 'default'
config.load_kube_config()
batch_v1 = client.BatchV1Api()
batch_v1beta1 = client.BatchV1beta1Api()
cron_job = batch_v1beta1.read_namespaced_cron_job(cron_job_name, namespace)
job = client.V1Job(
api_version='batch/v1',
kind='Job',
metadata=client.models.V1ObjectMeta(
name=job_name,
# This annotation is added by kubectl, probably best to add it ourselves as well
annotations={"cronjob.kubernetes.io/instantiate": "manual"}
),
spec=cron_job.spec.job_template.spec
)
result = batch_v1.create_namespaced_job(namespace, job)
print(result)
@fabianvf great, thank you!
I've seen the source code for kubectl create job --from=cronjob/*. It doesn't translate directly to kubernetes API, just as you have shown here. But wouldn't it be nice to have a shortcut in kubernetes-client, an analogue for --from=cronjob/* in client.V1Job? It could be an optional argument.
I'll defer to @roycaihw , but my impression is that this sort of higher-level functionality isn't necessarily in scope, this client occupies a space closer to client-go than to kubectl. It would be nice to have a place for these sorts of helpful functions though, I'm not sure if there is one at the moment.
https://github.com/kubernetes-client/python/tree/master/kubernetes/utils is where we put some helpful high-level functions.
Issues go stale after 90d of inactivity.
Mark the issue as fresh with /remove-lifecycle stale.
Stale issues rot after an additional 30d of inactivity and eventually close.
If this issue is safe to close now please do so with /close.
Send feedback to sig-testing, kubernetes/test-infra and/or fejta.
/lifecycle stale
Stale issues rot after 30d of inactivity.
Mark the issue as fresh with /remove-lifecycle rotten.
Rotten issues close after an additional 30d of inactivity.
If this issue is safe to close now please do so with /close.
Send feedback to sig-testing, kubernetes/test-infra and/or fejta.
/lifecycle rotten
Rotten issues close after 30d of inactivity.
Reopen the issue with /reopen.
Mark the issue as fresh with /remove-lifecycle rotten.
Send feedback to sig-testing, kubernetes/test-infra and/or fejta.
/close
@fejta-bot: Closing this issue.
In response to this:
Rotten issues close after 30d of inactivity.
Reopen the issue with/reopen.
Mark the issue as fresh with/remove-lifecycle rotten.Send feedback to sig-testing, kubernetes/test-infra and/or fejta.
/close
Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository.
Most helpful comment
The
kubectl create job --from=cronjob/*command simply pulls down the cronjob, pulls thejobTemplateout of its spec, and then creates theJobobject using that template. The below code should do the same thing: