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

The following examples show how to use io.fabric8.kubernetes.api.model.batch.JobBuilder. 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: JobHandler.java    From module-ballerina-kubernetes with Apache License 2.0 6 votes vote down vote up
private Job getJob(JobModel jobModel) {
    JobBuilder jobBuilder = new JobBuilder()
            .withNewMetadata()
            .withName(jobModel.getName())
            .withNamespace(dataHolder.getNamespace())
            .endMetadata()
            .withNewSpec()
            .withNewTemplate()
            .withNewSpec()
            .withRestartPolicy(jobModel.getRestartPolicy())
            .withContainers(generateContainer(jobModel))
            .withImagePullSecrets(getImagePullSecrets(jobModel))
            .withNodeSelector(jobModel.getNodeSelector())
            .endSpec()
            .endTemplate()
            .endSpec();
    return jobBuilder.build();
}
 
Example #2
Source File: JobTest.java    From kubernetes-client with Apache License 2.0 6 votes vote down vote up
@Test
public void testBuilder() {
  final Job job = new JobBuilder()
    .withApiVersion("batch/v1")
    .withNewMetadata()
    .withName("pi")
    .withLabels(Collections.singletonMap("label1", "maximum-length-of-63-characters"))
    .withAnnotations(Collections.singletonMap("annotation1", "some-very-long-annotation"))
    .endMetadata()
    .withNewSpec()
    .withNewTemplate()
    .withNewSpec()
    .addNewContainer()
    .withName("pi")
    .withImage("perl")
    .withArgs("perl", "-Mbignum=bpi", "-wle", "print bpi(2000)")
    .endContainer()
    .withRestartPolicy("Never")
    .endSpec()
    .endTemplate()
    .endSpec()
    .build();

  assertEquals("pi", job.getMetadata().getName());
  assertEquals(1, job.getSpec().getTemplate().getSpec().getContainers().size());
}
 
Example #3
Source File: PropagationPolicyTest.java    From kubernetes-client with Apache License 2.0 6 votes vote down vote up
@Test
@DisplayName("Should delete a Job with PropagationPolicy=Background")
void testDeleteJob() throws InterruptedException {
  // Given
  server.expect().delete().withPath("/apis/batch/v1/namespaces/ns1/jobs/myjob")
    .andReturn(HttpURLConnection.HTTP_OK, new JobBuilder().build())
    .once();
  KubernetesClient client = server.getClient();

  // When
  Boolean isDeleted = client.batch().jobs().inNamespace("ns1").withName("myjob").delete();

  // Then
  assertTrue(isDeleted);
  assertDeleteOptionsInLastRecordedRequest(DeletionPropagation.BACKGROUND.toString(), server.getLastRequest());
}
 
Example #4
Source File: PropagationPolicyTest.java    From kubernetes-client with Apache License 2.0 6 votes vote down vote up
@Test
@DisplayName("Should delete a Job with explicitly set PropagationPolicy")
void testDeleteJobWithExplicitPropagationPolicy() throws InterruptedException {
  // Given
  server.expect().delete().withPath("/apis/batch/v1/namespaces/ns1/jobs/myjob")
    .andReturn(HttpURLConnection.HTTP_OK, new JobBuilder().build())
    .once();
  KubernetesClient client = server.getClient();

  // When
  Boolean isDeleted = client.batch().jobs().inNamespace("ns1").withName("myjob").withPropagationPolicy(DeletionPropagation.FOREGROUND).delete();

  // Then
  assertTrue(isDeleted);
  assertDeleteOptionsInLastRecordedRequest(DeletionPropagation.FOREGROUND.toString(), server.getLastRequest());
}
 
Example #5
Source File: JobTest.java    From kubernetes-client with Apache License 2.0 6 votes vote down vote up
@Test
public void testGet() {
 server.expect().withPath("/apis/batch/v1/namespaces/test/jobs/job1").andReturn(200, new JobBuilder().build()).once();
 server.expect().withPath("/apis/batch/v1/namespaces/ns1/jobs/job2").andReturn(200, new JobBuilder().build()).once();

  KubernetesClient client = server.getClient();

  Job job = client.batch().jobs().withName("job1").get();
  assertNotNull(job);

  job = client.batch().jobs().withName("job2").get();
  assertNull(job);

  job = client.batch().jobs().inNamespace("ns1").withName("job2").get();
  assertNotNull(job);
}
 
Example #6
Source File: JobTest.java    From kubernetes-client with Apache License 2.0 6 votes vote down vote up
private JobBuilder getJobBuilder() {
  return new JobBuilder()
    .withApiVersion("batch/v1")
    .withNewMetadata()
    .withName("job1")
    .withLabels(Collections.singletonMap("label1", "maximum-length-of-63-characters"))
    .withAnnotations(Collections.singletonMap("annotation1", "some-very-long-annotation"))
    .endMetadata()
    .withNewSpec()
    .withNewTemplate()
    .withNewSpec()
    .addNewContainer()
    .withName("pi")
    .withImage("perl")
    .withArgs("perl", "-Mbignum=bpi", "-wle", "print bpi(2000)")
    .endContainer()
    .withRestartPolicy("Never")
    .endSpec()
    .endTemplate()
    .endSpec();
}
 
Example #7
Source File: JobHandler.java    From jkube with Eclipse Public License 2.0 5 votes vote down vote up
public Job getJob(ResourceConfig config,
                  List<ImageConfiguration> images) {
    return new JobBuilder()
            .withMetadata(createJobSpecMetaData(config))
            .withSpec(createJobSpec(config, images))
            .build();
}
 
Example #8
Source File: TriggersAnnotationEnricherTest.java    From jkube with Eclipse Public License 2.0 5 votes vote down vote up
@Test
public void testNoEnrichment() {

    KubernetesListBuilder builder = new KubernetesListBuilder().addToItems(new JobBuilder()
        .withNewMetadata()
            .addToAnnotations("dummy", "annotation")
        .endMetadata()
        .withNewSpec()
            .withNewTemplate()
                .withNewSpec()
                    .withContainers(createContainers(
                        "c1", "is1:latest",
                        "c2", "is2:latest"
                    ))
                .endSpec()
            .endTemplate()
        .endSpec()
        .build());


    TriggersAnnotationEnricher enricher = new TriggersAnnotationEnricher(context);
    enricher.enrich(PlatformMode.kubernetes, builder);


    Job res = (Job) builder.build().getItems().get(0);
    String triggers = res.getMetadata().getAnnotations().get("image.openshift.io/triggers");
    assertNull(triggers);
}
 
Example #9
Source File: JobTest.java    From kubernetes-client with Apache License 2.0 5 votes vote down vote up
@Test
public void testDeleteWithNamespaceMismatch() {
  Assertions.assertThrows(KubernetesClientException.class, () -> {
    Job job1 = new JobBuilder().withNewMetadata().withName("job1").withNamespace("test").and().build();
    KubernetesClient client = server.getClient();

    Boolean deleted = client.batch().jobs().inNamespace("test1").delete(job1);
    assertTrue(deleted);
  });
}
 
Example #10
Source File: JobTest.java    From kubernetes-client with Apache License 2.0 5 votes vote down vote up
@Test
public void testCreateWithNameMismatch() {
  Assertions.assertThrows(KubernetesClientException.class, () -> {
    Job job1 = new JobBuilder().withNewMetadata().withName("job1").withNamespace("test").and().build();
    Job job2 = new JobBuilder().withNewMetadata().withName("job2").withNamespace("ns1").and().build();
    KubernetesClient client = server.getClient();

    client.batch().jobs().inNamespace("test1").withName("myjob1").create(job1);
  });
}
 
Example #11
Source File: MetadataVisitor.java    From jkube with Eclipse Public License 2.0 4 votes vote down vote up
@Override
protected ObjectMeta getOrCreateMetadata(JobBuilder item) {
    return addEmptyLabelsAndAnnotations(item::hasMetadata, item::withNewMetadata, item::editMetadata, item::buildMetadata)
            .endMetadata().buildMetadata();
}
 
Example #12
Source File: KafkaClientsResource.java    From strimzi-kafka-operator with Apache License 2.0 4 votes vote down vote up
public static DoneableJob producerStrimzi(String producerName, String bootstrapServer, String topicName, int messageCount, String additionalConfig) {
    Map<String, String> producerLabels = new HashMap<>();
    producerLabels.put("app", producerName);
    producerLabels.put(Constants.KAFKA_CLIENTS_LABEL_KEY, Constants.KAFKA_CLIENTS_LABEL_VALUE);

    return KubernetesResource.deployNewJob(new JobBuilder()
        .withNewMetadata()
            .withNamespace(ResourceManager.kubeClient().getNamespace())
            .withLabels(producerLabels)
            .withName(producerName)
        .endMetadata()
        .withNewSpec()
            .withNewTemplate()
                .withNewMetadata()
                    .withLabels(producerLabels)
                .endMetadata()
                .withNewSpec()
                    .withRestartPolicy("OnFailure")
                    .withContainers()
                        .addNewContainer()
                        .withName(producerName)
                            .withImage("strimzi/hello-world-producer:latest")
                                .addNewEnv()
                                    .withName("BOOTSTRAP_SERVERS")
                                    .withValue(bootstrapServer)
                                .endEnv()
                                .addNewEnv()
                                    .withName("TOPIC")
                                    .withValue(topicName)
                                .endEnv()
                                .addNewEnv()
                                    .withName("DELAY_MS")
                                    .withValue("1000")
                                .endEnv()
                                .addNewEnv()
                                    .withName("LOG_LEVEL")
                                    .withValue("DEBUG")
                                .endEnv()
                                .addNewEnv()
                                    .withName("MESSAGE_COUNT")
                                    .withValue(String.valueOf(messageCount))
                                .endEnv()
                                .addNewEnv()
                                    .withName("PRODUCER_ACKS")
                                    .withValue("all")
                                .endEnv()
                                .addNewEnv()
                                    .withName("ADDITIONAL_CONFIG")
                                    .withValue(additionalConfig)
                                .endEnv()
                        .endContainer()
                    .endSpec()
                .endTemplate()
            .endSpec()
            .build());
}
 
Example #13
Source File: KafkaClientsResource.java    From strimzi-kafka-operator with Apache License 2.0 4 votes vote down vote up
public static DoneableJob consumerStrimzi(String consumerName, String bootstrapServer, String topicName, int messageCount, String additionalConfig, String consumerGroup) {
    Map<String, String> consumerLabels = new HashMap<>();
    consumerLabels.put("app", consumerName);
    consumerLabels.put(Constants.KAFKA_CLIENTS_LABEL_KEY, Constants.KAFKA_CLIENTS_LABEL_VALUE);

    return KubernetesResource.deployNewJob(new JobBuilder()
        .withNewMetadata()
            .withNamespace(ResourceManager.kubeClient().getNamespace())
            .withLabels(consumerLabels)
            .withName(consumerName)
        .endMetadata()
        .withNewSpec()
            .withNewTemplate()
                .withNewMetadata()
                    .withLabels(consumerLabels)
                .endMetadata()
                .withNewSpec()
                    .withRestartPolicy("OnFailure")
                    .withContainers()
                        .addNewContainer()
                        .withName(consumerName)
                            .withImage("strimzi/hello-world-consumer:latest")
                                .addNewEnv()
                                    .withName("BOOTSTRAP_SERVERS")
                                    .withValue(bootstrapServer)
                                .endEnv()
                                .addNewEnv()
                                    .withName("TOPIC")
                                    .withValue(topicName)
                                .endEnv()
                                .addNewEnv()
                                    .withName("DELAY_MS")
                                    .withValue("1000")
                                .endEnv()
                                .addNewEnv()
                                    .withName("LOG_LEVEL")
                                    .withValue("DEBUG")
                                .endEnv()
                                .addNewEnv()
                                    .withName("MESSAGE_COUNT")
                                    .withValue(String.valueOf(messageCount))
                                .endEnv()
                               .addNewEnv()
                                    .withName("GROUP_ID")
                                    .withValue(consumerGroup)
                                .endEnv()
                                .addNewEnv()
                                    .withName("ADDITIONAL_CONFIG")
                                    .withValue(additionalConfig)
                                .endEnv()
                        .endContainer()
                    .endSpec()
                .endTemplate()
            .endSpec()
            .build());
}
 
Example #14
Source File: JobTest.java    From kubernetes-client with Apache License 2.0 4 votes vote down vote up
@Test
public void testDelete() {
 server.expect().withPath("/apis/batch/v1/namespaces/test/jobs/job1").andReturn(200, new JobBuilder().withNewMetadata()
    .withName("job1")
    .withResourceVersion("1")
    .endMetadata()
    .withNewSpec()
    .withParallelism(0)
    .endSpec()
    .withNewStatus()
    .withActive(1)
    .endStatus()
    .build()).once();
 server.expect().withPath("/apis/batch/v1/namespaces/test/jobs/job1").andReturn(200, new JobBuilder().withNewMetadata()
    .withName("job1")
    .withResourceVersion("1")
    .endMetadata()
    .withNewSpec()
    .withParallelism(0)
    .endSpec()
    .withNewStatus()
    .withActive(0)
    .endStatus()
    .build()).times(5);

 server.expect().withPath("/apis/batch/v1/namespaces/test/jobs/job2").andReturn(200, new JobBuilder().withNewMetadata()
    .withName("job2")
    .withResourceVersion("1")
    .endMetadata()
    .withNewSpec()
    .withParallelism(0)
    .endSpec()
    .withNewStatus()
    .withActive(1)
    .endStatus()
    .build()).once();
 server.expect().withPath("/apis/batch/v1/namespaces/test/jobs/job2").andReturn(200, new JobBuilder().withNewMetadata()
    .withName("job2")
    .withResourceVersion("1")
    .endMetadata()
    .withNewSpec()
    .withParallelism(0)
    .endSpec()
    .withNewStatus()
    .withActive(0)
    .endStatus()
    .build()).times(5);

  KubernetesClient client = server.getClient();

  Boolean deleted = client.batch().jobs().withName("job1").delete();
  assertTrue(deleted);

  deleted = client.batch().jobs().withName("job2").delete();
  assertTrue(deleted);
}
 
Example #15
Source File: JobTest.java    From kubernetes-client with Apache License 2.0 4 votes vote down vote up
@Test
public void testDeleteMulti() {
  Job job1 = new JobBuilder().withNewMetadata()
    .withNamespace("test")
    .withName("job1")
    .withResourceVersion("1")
    .endMetadata()
    .withNewSpec()
    .withParallelism(0)
    .endSpec()
    .withNewStatus()
    .withActive(1)
    .endStatus()
    .build();
  Job job2 = new JobBuilder().withNewMetadata()
    .withNamespace("ns1")
    .withName("job2")
    .withResourceVersion("1")
    .endMetadata()
    .withNewSpec()
    .withParallelism(0)
    .endSpec()
    .withNewStatus()
    .withActive(1)
    .endStatus()
    .build();
  Job job3 = new JobBuilder().withNewMetadata().withName("job3").withNamespace("any").and().build();

 server.expect().withPath("/apis/batch/v1/namespaces/test/jobs/job1").andReturn(200, job1).once();
 server.expect().withPath("/apis/batch/v1/namespaces/test/jobs/job1").andReturn(200, new JobBuilder(job1)
    .editStatus().withActive(0).endStatus().build()).times(5);
 server.expect().withPath("/apis/batch/v1/namespaces/ns1/jobs/job2").andReturn(200, job2).once();
 server.expect().withPath("/apis/batch/v1/namespaces/ns1/jobs/job2").andReturn(200, new JobBuilder(job2)
    .editStatus().withActive(0).endStatus().build()).times(5);

  KubernetesClient client = server.getClient();

  Boolean deleted = client.batch().jobs().inAnyNamespace().delete(job1, job2);
  assertTrue(deleted);

  deleted = client.batch().jobs().inAnyNamespace().delete(job3);
  assertFalse(deleted);
}
 
Example #16
Source File: JobExample.java    From kubernetes-client with Apache License 2.0 4 votes vote down vote up
public static void main(String[] args) {
    String master = "https://localhost:8443/";
    if (args.length == 1) {
        master = args[0];
    }

    final Config config = new ConfigBuilder().withMasterUrl(master).build();
    try (final KubernetesClient client = new DefaultKubernetesClient(config)) {
      final String namespace = "default";
      final Job job = new JobBuilder()
        .withApiVersion("batch/v1")
        .withNewMetadata()
        .withName("pi")
        .withLabels(Collections.singletonMap("label1", "maximum-length-of-63-characters"))
        .withAnnotations(Collections.singletonMap("annotation1", "some-very-long-annotation"))
        .endMetadata()
        .withNewSpec()
        .withNewTemplate()
        .withNewSpec()
        .addNewContainer()
        .withName("pi")
        .withImage("perl")
        .withArgs("perl", "-Mbignum=bpi", "-wle", "print bpi(2000)")
        .endContainer()
        .withRestartPolicy("Never")
        .endSpec()
        .endTemplate()
        .endSpec()
        .build();

      logger.info("Creating job pi.");
      client.batch().jobs().inNamespace(namespace).create(job);

      // Get All pods created by the job
      PodList podList = client.pods().inNamespace(namespace).withLabel("job-name", job.getMetadata().getName()).list();
      // Wait for pod to complete
      client.pods().inNamespace(namespace).withName(podList.getItems().get(0).getMetadata().getName())
        .waitUntilCondition(pod -> pod.getStatus().getPhase().equals("Succeeded"), 1, TimeUnit.MINUTES);

      // Print Job's log
      String joblog = client.batch().jobs().inNamespace(namespace).withName("pi").getLog();
      logger.info(joblog);

    } catch (final KubernetesClientException e) {
        logger.error("Unable to create job", e);
    } catch (InterruptedException interruptedException) {
      logger.warn("Thread interrupted!");
      Thread.currentThread().interrupt();
    }
}