use of io.fabric8.kubernetes.api.model.batch.v1.CronJob in project marduk by entur.
the class KubernetesJobRunner method runJob.
/**
* Run a Kubernetes job
*
* @param cronJobName name of the CronJob used as a template
* @param jobNamePrefix prefix for the Kubernetes job name
* @param envVars environment variables to be provided to the job
* @param timestamp timestamp used to create a unique name for the Kubernetes job.
*/
public void runJob(String cronJobName, String jobNamePrefix, List<EnvVar> envVars, String timestamp) {
try (final KubernetesClient kubernetesClient = new DefaultKubernetesClient()) {
String jobName = jobNamePrefix + '-' + timestamp;
final Job job = retrieveOrCreateJob(jobName, cronJobName, envVars, kubernetesClient);
final CountDownLatch watchLatch = new CountDownLatch(1);
MardukPodWatcher mardukPodWatcher = new MardukPodWatcher(job, watchLatch, jobName);
try (Watch watch = kubernetesClient.pods().inNamespace(kubernetesNamespace).withLabel("job-name", jobName).watch(mardukPodWatcher)) {
boolean jobCompletedBeforeTimeout = watchLatch.await(jobTimeoutSecond, TimeUnit.SECONDS);
if (!jobCompletedBeforeTimeout) {
throw new KubernetesJobRunnerException("Timeout while waiting for the Graph Builder job " + jobName + " to complete.");
}
JobStatus status = kubernetesClient.batch().v1().jobs().inNamespace(kubernetesNamespace).withName(jobName).get().getStatus();
LOGGER.debug("Kubernetes Job status on completion: {}", status);
// test the pod status rather than the job status since the job status may be out of sync with the pod status
if (mardukPodWatcher.isSucceeded()) {
LOGGER.info("The Graph Builder job {} completed successfully.", jobName);
} else if (mardukPodWatcher.isKubernetesClientError()) {
throw new KubernetesJobRunnerException("Kubernetes client error while watching the Graph Builder job " + jobName);
} else {
throw new KubernetesJobRunnerException("The Graph Builder job " + jobName + " failed.");
}
} catch (KubernetesClientException e) {
throw new KubernetesJobRunnerException("Could not watch pod", e);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
throw new KubernetesJobRunnerException("Interrupted while watching pod", e);
} finally {
// Delete job after completion unless there was a Kubernetes error that can be retried
if (!mardukPodWatcher.isKubernetesClientError() && deleteJobAfterCompletion) {
LOGGER.info("Deleting job {} after completion.", jobName);
deleteKubernetesJob(kubernetesClient, job);
LOGGER.info("Deleted job {} after completion.", jobName);
}
}
}
}
use of io.fabric8.kubernetes.api.model.batch.v1.CronJob in project module-ballerina-c2c by ballerina-platform.
the class JobTest method testKubernetesJobGeneration.
@Test
public void testKubernetesJobGeneration() throws IOException, InterruptedException {
Assert.assertEquals(KubernetesTestUtils.compileBallerinaProject(SOURCE_DIR_PATH), 0);
File dockerFile = DOCKER_TARGET_PATH.resolve("Dockerfile").toFile();
Assert.assertTrue(dockerFile.exists());
InspectImageResponse imageInspect = getDockerImage(DOCKER_IMAGE_JOB);
Assert.assertNotNull(imageInspect.getConfig());
File jobYAML = KUBERNETES_TARGET_PATH.resolve("hello.yaml").toFile();
CronJob job = KubernetesTestUtils.loadYaml(jobYAML);
Assert.assertEquals(job.getMetadata().getName(), "hello-hello-0-0-job");
Assert.assertEquals(job.getSpec().getJobTemplate().getSpec().getTemplate().getSpec().getContainers().size(), 1);
Container container = job.getSpec().getJobTemplate().getSpec().getTemplate().getSpec().getContainers().get(0);
Assert.assertEquals(container.getImage(), DOCKER_IMAGE_JOB);
Assert.assertEquals(job.getSpec().getJobTemplate().getSpec().getTemplate().getSpec().getRestartPolicy(), KubernetesConstants.RestartPolicy.OnFailure.name());
Assert.assertEquals(container.getEnv().size(), 1);
Assert.assertEquals(container.getEnv().get(0).getName(), "b7a_log_level");
}
use of io.fabric8.kubernetes.api.model.batch.v1.CronJob in project module-ballerina-c2c by ballerina-platform.
the class Sample4Test method validateJob.
@Test
public void validateJob() throws IOException {
File jobYAML = KUBERNETES_TARGET_PATH.resolve("hello_world_job.yaml").toFile();
CronJob job = KubernetesTestUtils.loadYaml(jobYAML);
Assert.assertEquals(job.getMetadata().getName(), "hello-world-job-job");
Assert.assertEquals(job.getSpec().getJobTemplate().getSpec().getTemplate().getSpec().getContainers().size(), 1);
Assert.assertEquals(job.getSpec().getSchedule(), "*/2 * * * *");
Container container = job.getSpec().getJobTemplate().getSpec().getTemplate().getSpec().getContainers().get(0);
Assert.assertEquals(container.getImage(), DOCKER_IMAGE);
Assert.assertEquals(job.getSpec().getJobTemplate().getSpec().getTemplate().getSpec().getRestartPolicy(), KubernetesConstants.RestartPolicy.OnFailure.name());
}
use of io.fabric8.kubernetes.api.model.batch.v1.CronJob in project kubernetes-client by fabric8io.
the class CronJobTest method testDeleteMulti.
@Test
void testDeleteMulti() {
CronJob cronjob1 = new CronJobBuilder().withNewMetadata().withNamespace("test").withName("cronjob1").withResourceVersion("1").endMetadata().withNewSpec().endSpec().withNewStatus().endStatus().build();
CronJob cronjob2 = new CronJobBuilder().withNewMetadata().withNamespace("ns1").withName("cronjob2").withResourceVersion("1").endMetadata().withNewSpec().endSpec().withNewStatus().endStatus().build();
CronJob cronjob3 = new CronJobBuilder().withNewMetadata().withName("cronjob3").withNamespace("any").and().build();
server.expect().withPath("/apis/batch/v1beta1/namespaces/test/cronjobs/cronjob1").andReturn(200, cronjob1).once();
server.expect().withPath("/apis/batch/v1beta1/namespaces/test/cronjobs/cronjob1").andReturn(200, new CronJobBuilder(cronjob1).editStatus().endStatus().build()).times(5);
server.expect().withPath("/apis/batch/v1beta1/namespaces/ns1/cronjobs/cronjob2").andReturn(200, cronjob2).once();
server.expect().withPath("/apis/batch/v1beta1/namespaces/ns1/cronjobs/cronjob2").andReturn(200, new CronJobBuilder(cronjob2).editStatus().endStatus().build()).times(5);
Boolean deleted = client.batch().cronjobs().inAnyNamespace().delete(cronjob1, cronjob2);
assertTrue(deleted);
deleted = client.batch().cronjobs().inAnyNamespace().delete(cronjob3);
assertFalse(deleted);
}
use of io.fabric8.kubernetes.api.model.batch.v1.CronJob in project kubernetes-client by fabric8io.
the class CronJobTest method testCreateWithNameMismatch.
@Test
void testCreateWithNameMismatch() {
Assertions.assertThrows(KubernetesClientException.class, () -> {
CronJob cronjob1 = new CronJobBuilder().withNewMetadata().withName("cronjob1").withNamespace("test").and().build();
client.batch().cronjobs().inNamespace("test1").withName("mycronjob1").create(cronjob1);
});
}
Aggregations