io.fabric8.kubernetes.api.model.batch.CronJobBuilder Java Examples

The following examples show how to use io.fabric8.kubernetes.api.model.batch.CronJobBuilder. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: KubernetesScheduler.java    From spring-cloud-deployer-kubernetes with Apache License 2.0 6 votes vote down vote up
protected CronJob createCronJob(ScheduleRequest scheduleRequest) {
	Map<String, String> labels = Collections.singletonMap(SPRING_CRONJOB_ID_KEY,
			scheduleRequest.getDefinition().getName());

	Map<String, String> schedulerProperties = scheduleRequest.getSchedulerProperties();
	String schedule = schedulerProperties.get(SchedulerPropertyKeys.CRON_EXPRESSION);
	Assert.hasText(schedule, "The property: " + SchedulerPropertyKeys.CRON_EXPRESSION + " must be defined");

	PodSpec podSpec = createPodSpec(scheduleRequest);
	String taskServiceAccountName = this.deploymentPropertiesResolver.getTaskServiceAccountName(scheduleRequest.getSchedulerProperties());
	if (StringUtils.hasText(taskServiceAccountName)) {
		podSpec.setServiceAccountName(taskServiceAccountName);
	}

	CronJob cronJob = new CronJobBuilder().withNewMetadata().withName(scheduleRequest.getScheduleName())
			.withLabels(labels).endMetadata().withNewSpec().withSchedule(schedule).withNewJobTemplate()
			.withNewSpec().withNewTemplate().withSpec(podSpec).endTemplate().endSpec()
			.endJobTemplate().endSpec().build();

	setImagePullSecret(scheduleRequest, cronJob);

	return this.client.batch().cronjobs().create(cronJob);
}
 
Example #2
Source File: JobHandler.java    From module-ballerina-kubernetes with Apache License 2.0 5 votes vote down vote up
private CronJob getCronJob(JobModel jobModel) {
    return new CronJobBuilder()
            .withNewMetadata()
            .withName(jobModel.getName())
            .endMetadata()
            .withNewSpec()
            .withNewJobTemplate()
            .withNewSpec()
            .withActiveDeadlineSeconds((long) jobModel.getActiveDeadlineSeconds())
            .endSpec()
            .endJobTemplate()
            .withSchedule(jobModel.getSchedule())
            .endSpec()
            .build();
}
 
Example #3
Source File: CronJobCrudTest.java    From kubernetes-client with Apache License 2.0 4 votes vote down vote up
@Test
public void testCrud() {
  KubernetesClient client = server.getClient();

  CronJob cronJob1 = new CronJobBuilder().withNewMetadata()
    .withName("cronJob1")
    .withResourceVersion("1")
    .addToLabels("foo", "bar")
    .endMetadata()
    .withNewSpec()
    .withSchedule("1 2-14 * * 0-1,5-6")
    .withNewJobTemplate()
    .withNewSpec()
    .withNewTemplate()
    .withNewSpec()
    .addNewImagePullSecret()
    .withName("gcr-secret")
    .endImagePullSecret()
    .addNewContainer()
    .withName("devopsish-netlify-cronjob")
    .withImage("gcr.io/chrisshort-net/devopsish-netlify-cron:latest")
    .addNewEnv()
    .withName("URL")
    .withNewValueFrom()
    .withNewSecretKeyRef()
    .withName("devops-build-hook")
    .withKey("url")
    .endSecretKeyRef()
    .endValueFrom()
    .endEnv()
    .endContainer()
    .endSpec()
    .endTemplate()
    .endSpec()
    .endJobTemplate()
    .endSpec()
    .build();

  CronJob cronJob2 = new CronJobBuilder().withNewMetadata()
    .withName("cronJob2")
    .withResourceVersion("1")
    .endMetadata()
    .withNewSpec()
    .withSchedule("*/1 * * * *")
    .withNewJobTemplate()
    .withNewSpec()
    .withNewTemplate()
    .withNewSpec()
    .addNewContainer()
    .withName("app")
    .withImage("bitnami/kubecfg:0.5.0")
    .addNewEnv()
    .withName("TOKEN")
    .withNewValueFrom()
    .withNewSecretKeyRef()
    .withName("default-token-rtw2m")
    .withKey("token")
    .endSecretKeyRef()
    .endValueFrom()
    .endEnv()
    .endContainer()
    .endSpec()
    .endTemplate()
    .endSpec()
    .endJobTemplate()
    .endSpec()
    .build();

  client.batch().cronjobs().inNamespace("ns1").create(cronJob1);
  client.batch().cronjobs().inNamespace("ns2").create(cronJob2);

  CronJobList cronJobList = client.batch().cronjobs().list();
  assertNotNull(cronJobList);
  assertEquals(0, cronJobList.getItems().size());

  cronJobList = client.batch().cronjobs().inAnyNamespace().list();
  assertNotNull(cronJobList);
  assertEquals(2, cronJobList.getItems().size());

  cronJobList = client.batch().cronjobs().inNamespace("ns1").list();
  assertNotNull(cronJobList);
  assertEquals(1, cronJobList.getItems().size());

  cronJobList = client.batch().cronjobs().inNamespace("ns2").list();
  assertNotNull(cronJobList);
  assertEquals(1, cronJobList.getItems().size());

  cronJobList = client.batch().cronjobs().inNamespace("ns1").withLabels(Collections.singletonMap("foo", "bar")).list();
  assertNotNull(cronJobList);
  assertEquals(1, cronJobList.getItems().size());

  boolean bDeleted = client.batch().cronjobs().inNamespace("ns1").withName("cronJob1").delete();
  cronJobList = client.batch().cronjobs().inNamespace("ns1").list();
  assertTrue(bDeleted);
  assertEquals(0, cronJobList.getItems().size());

  cronJob2 = client.batch().cronjobs().inNamespace("ns2").withName("cronJob2").edit().editSpec().withSchedule("*/1 * * * *").and().done();
  assertNotNull(cronJob2);
  assertEquals("*/1 * * * *", cronJob2.getSpec().getSchedule());
}
 
Example #4
Source File: CronJobIT.java    From kubernetes-client with Apache License 2.0 4 votes vote down vote up
@Before
public void init() {
  currentNamespace = session.getNamespace();
  cronJob1 = new CronJobBuilder().withApiVersion("batch/v1beta1").withNewMetadata()
    .withName("cronjob1")
    .endMetadata()
    .withNewSpec()
    .withSchedule("*/1 * * * *")
    .withNewJobTemplate()
    .withNewSpec()
    .withNewTemplate()
    .withNewSpec()
    .addNewContainer()
    .withName("hello")
    .withImage("busybox")
    .withArgs("/bin/sh", "-c", "date; echo Hello from Kubernetes")
    .endContainer()
    .withRestartPolicy("OnFailure")
    .endSpec()
    .endTemplate()
    .endSpec()
    .endJobTemplate()
    .endSpec()
    .build();
  cronJob2 = new CronJobBuilder().withApiVersion("batch/v1beta1").withNewMetadata()
    .withName("cronjob2")
    .endMetadata()
    .withNewSpec()
    .withSchedule("*/1 * * * *")
    .withNewJobTemplate()
    .withNewSpec()
    .withNewTemplate()
    .withNewSpec()
    .addNewContainer()
    .withName("pi")
    .withImage("perl")
    .withCommand("perl")
    .withArgs(Arrays.asList("-Mbignum=bpi", "-wle", "print bpi(2000)"))
    .endContainer()
    .withRestartPolicy("OnFailure")
    .endSpec()
    .endTemplate()
    .endSpec()
    .endJobTemplate()
    .endSpec()
    .build();

  client.batch().cronjobs().inNamespace(currentNamespace).create(cronJob1);
  client.batch().cronjobs().inNamespace(currentNamespace).create(cronJob2);
}