Search in sources :

Example 11 with CronJob

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);
            }
        }
    }
}
Also used : JobStatus(io.fabric8.kubernetes.api.model.batch.v1.JobStatus) KubernetesClient(io.fabric8.kubernetes.client.KubernetesClient) DefaultKubernetesClient(io.fabric8.kubernetes.client.DefaultKubernetesClient) Watch(io.fabric8.kubernetes.client.Watch) DefaultKubernetesClient(io.fabric8.kubernetes.client.DefaultKubernetesClient) CronJob(io.fabric8.kubernetes.api.model.batch.v1beta1.CronJob) Job(io.fabric8.kubernetes.api.model.batch.v1.Job) CountDownLatch(java.util.concurrent.CountDownLatch) KubernetesClientException(io.fabric8.kubernetes.client.KubernetesClientException)

Example 12 with CronJob

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");
}
Also used : Container(io.fabric8.kubernetes.api.model.Container) CronJob(io.fabric8.kubernetes.api.model.batch.v1.CronJob) InspectImageResponse(com.github.dockerjava.api.command.InspectImageResponse) File(java.io.File) Test(org.testng.annotations.Test)

Example 13 with CronJob

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());
}
Also used : Container(io.fabric8.kubernetes.api.model.Container) CronJob(io.fabric8.kubernetes.api.model.batch.v1.CronJob) File(java.io.File) Test(org.testng.annotations.Test)

Example 14 with CronJob

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);
}
Also used : CronJob(io.fabric8.kubernetes.api.model.batch.v1beta1.CronJob) CronJobBuilder(io.fabric8.kubernetes.api.model.batch.v1beta1.CronJobBuilder) Test(org.junit.jupiter.api.Test)

Example 15 with CronJob

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);
    });
}
Also used : CronJob(io.fabric8.kubernetes.api.model.batch.v1beta1.CronJob) CronJobBuilder(io.fabric8.kubernetes.api.model.batch.v1beta1.CronJobBuilder) Test(org.junit.jupiter.api.Test)

Aggregations

CronJob (io.fabric8.kubernetes.api.model.batch.v1beta1.CronJob)14 Test (org.junit.jupiter.api.Test)12 CronJob (io.fabric8.kubernetes.api.model.batch.v1.CronJob)9 CronJobBuilder (io.fabric8.kubernetes.api.model.batch.v1.CronJobBuilder)8 CronJobBuilder (io.fabric8.kubernetes.api.model.batch.v1beta1.CronJobBuilder)8 CronJob (com.marcnuri.yakc.model.io.k8s.api.batch.v1beta1.CronJob)5 Container (io.fabric8.kubernetes.api.model.Container)4 PodTemplateSpec (io.fabric8.kubernetes.api.model.PodTemplateSpec)4 JobTemplateSpec (io.fabric8.kubernetes.api.model.batch.v1beta1.JobTemplateSpec)4 ContainerBuilder (io.fabric8.kubernetes.api.model.ContainerBuilder)3 ObjectMeta (io.fabric8.kubernetes.api.model.ObjectMeta)3 Pod (io.fabric8.kubernetes.api.model.Pod)3 Job (io.fabric8.kubernetes.api.model.batch.v1.Job)3 Random (java.util.Random)3 JobTemplateSpec (com.marcnuri.yakc.model.io.k8s.api.batch.v1beta1.JobTemplateSpec)2 PodBuilder (io.fabric8.kubernetes.api.model.PodBuilder)2 PodTemplate (io.fabric8.kubernetes.api.model.PodTemplate)2 PodTemplateBuilder (io.fabric8.kubernetes.api.model.PodTemplateBuilder)2 ReplicationController (io.fabric8.kubernetes.api.model.ReplicationController)2 ReplicationControllerBuilder (io.fabric8.kubernetes.api.model.ReplicationControllerBuilder)2