-
Notifications
You must be signed in to change notification settings - Fork 14
/
ManualCronJobTrigger.java
64 lines (58 loc) · 2.58 KB
/
ManualCronJobTrigger.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package io.fabric8;
import io.fabric8.kubernetes.api.model.batch.v1.CronJob;
import io.fabric8.kubernetes.api.model.batch.v1.CronJobBuilder;
import io.fabric8.kubernetes.api.model.batch.v1.Job;
import io.fabric8.kubernetes.api.model.batch.v1.JobBuilder;
import io.fabric8.kubernetes.client.KubernetesClientBuilder;
import io.fabric8.kubernetes.client.KubernetesClient;
public class ManualCronJobTrigger {
public static void main(String[] args) {
try (KubernetesClient kubernetesClient = new KubernetesClientBuilder().build()) {
createNewCronJob(kubernetesClient);
createManualJob(kubernetesClient, "default", "hello", "hello-rokumar");
}
}
private static Job createManualJob(KubernetesClient kubernetesClient, String namespace, String cronJobName, String jobName) {
CronJob cronJob = kubernetesClient.batch().v1().cronjobs().inNamespace(namespace).withName(cronJobName).get();
Job newJobToCreate = new JobBuilder()
.withNewMetadata()
.withName(jobName)
.addNewOwnerReference()
.withApiVersion("batch/v1beta1")
.withKind("CronJob")
.withName(cronJob.getMetadata().getName())
.withUid(cronJob.getMetadata().getUid())
.endOwnerReference()
.addToAnnotations("cronjob.kubernetes.io/instantiate", "manual")
.endMetadata()
.withSpec(cronJob.getSpec().getJobTemplate().getSpec())
.build();
return kubernetesClient.batch().v1().jobs().inNamespace(namespace).resource(newJobToCreate).create();
}
private static CronJob createNewCronJob(KubernetesClient kubernetesClient) {
CronJob cronJob = new CronJobBuilder()
.withNewMetadata()
.withName("hello")
.endMetadata()
.withNewSpec()
.withSchedule("* * * * *")
.withNewJobTemplate()
.withNewSpec()
.withNewTemplate()
.withNewSpec()
.addNewContainer()
.withName("hello")
.withImage("busybox:1.28")
.withImagePullPolicy("IfNotPresent")
.withCommand("/bin/sh", "-c", "date; echo Hello from the Kubernetes cluster")
.endContainer()
.withRestartPolicy("OnFailure")
.endSpec()
.endTemplate()
.endSpec()
.endJobTemplate()
.endSpec()
.build();
return kubernetesClient.batch().v1().cronjobs().inNamespace("default").resource(cronJob).create();
}
}