Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create a Job from existing Cronjob #991

Closed
pilosus opened this issue Oct 21, 2019 · 11 comments
Closed

Create a Job from existing Cronjob #991

pilosus opened this issue Oct 21, 2019 · 11 comments
Labels
lifecycle/rotten Denotes an issue or PR that has aged beyond stale and will be auto-closed.

Comments

@pilosus
Copy link

pilosus commented Oct 21, 2019

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?

@roycaihw
Copy link
Member

/cc @fabianvf

@fabianvf
Copy link
Contributor

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)

@pilosus
Copy link
Author

pilosus commented Oct 23, 2019

@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.

@fabianvf
Copy link
Contributor

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.

@roycaihw
Copy link
Member

roycaihw commented Nov 1, 2019

https://github.com/kubernetes-client/python/tree/master/kubernetes/utils is where we put some helpful high-level functions.

@fejta-bot
Copy link

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

@k8s-ci-robot k8s-ci-robot added the lifecycle/stale Denotes an issue or PR has remained open with no activity and has become stale. label Jan 30, 2020
@fejta-bot
Copy link

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

@k8s-ci-robot k8s-ci-robot added lifecycle/rotten Denotes an issue or PR that has aged beyond stale and will be auto-closed. and removed lifecycle/stale Denotes an issue or PR has remained open with no activity and has become stale. labels Feb 29, 2020
@fejta-bot
Copy link

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

@k8s-ci-robot
Copy link
Contributor

@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.

@yamikarajput546
Copy link

For creating a fresh new cronjob on GKE cluster using bearer token for authentication you can refer to this: https://techhub.go1percent.com/dashboard/projects/devops/62a1e1d8f20000af4db0c0cd

@Razihmad
Copy link

how can i add th

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)

how can i add time to live for this job created. after this job is finished. it should not be listed in job let say after 1 min

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
lifecycle/rotten Denotes an issue or PR that has aged beyond stale and will be auto-closed.
Projects
None yet
Development

No branches or pull requests

7 participants