io.fabric8.kubernetes.api.model.PodSpecBuilder Java Examples

The following examples show how to use io.fabric8.kubernetes.api.model.PodSpecBuilder. 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: PodMergerTest.java    From che with Eclipse Public License 2.0 6 votes vote down vote up
@Test(expectedExceptions = ValidationException.class)
public void shouldFailServiceAccountDiffersInPods() throws Exception {
  // given
  PodSpec podSpec1 = new PodSpecBuilder().withServiceAccount("sa").build();
  podSpec1.setAdditionalProperty("add1", 1L);
  PodData podData1 = new PodData(podSpec1, new ObjectMetaBuilder().build());

  PodSpec podSpec2 = new PodSpecBuilder().withServiceAccount("sb").build();
  podSpec2.setAdditionalProperty("add2", 2L);
  PodData podData2 = new PodData(podSpec2, new ObjectMetaBuilder().build());

  // when
  Deployment merged = podMerger.merge(Arrays.asList(podData1, podData2));

  // then
  // exception is thrown
}
 
Example #2
Source File: PodMergerTest.java    From che with Eclipse Public License 2.0 6 votes vote down vote up
@Test(expectedExceptions = ValidationException.class)
public void shouldFailIfSecurityContextDiffersInPods() throws Exception {
  // given
  PodSpec podSpec1 =
      new PodSpecBuilder()
          .withSecurityContext(new PodSecurityContextBuilder().withRunAsUser(42L).build())
          .build();
  podSpec1.setAdditionalProperty("add1", 1L);
  PodData podData1 = new PodData(podSpec1, new ObjectMetaBuilder().build());

  PodSpec podSpec2 =
      new PodSpecBuilder()
          .withSecurityContext(new PodSecurityContextBuilder().withRunAsUser(43L).build())
          .build();
  podSpec2.setAdditionalProperty("add2", 2L);
  PodData podData2 = new PodData(podSpec2, new ObjectMetaBuilder().build());

  // when
  Deployment merged = podMerger.merge(Arrays.asList(podData1, podData2));

  // then
  // exception is thrown
}
 
Example #3
Source File: PodMergerTest.java    From che with Eclipse Public License 2.0 6 votes vote down vote up
@Test
public void shouldAssignSecurityContextSharedByPods() throws Exception {
  // given
  PodSpec podSpec1 =
      new PodSpecBuilder()
          .withSecurityContext(new PodSecurityContextBuilder().withRunAsUser(42L).build())
          .build();
  podSpec1.setAdditionalProperty("add1", 1L);
  PodData podData1 = new PodData(podSpec1, new ObjectMetaBuilder().build());

  PodSpec podSpec2 =
      new PodSpecBuilder()
          .withSecurityContext(new PodSecurityContextBuilder().withRunAsUser(42L).build())
          .build();
  podSpec2.setAdditionalProperty("add2", 2L);
  PodData podData2 = new PodData(podSpec2, new ObjectMetaBuilder().build());

  // when
  Deployment merged = podMerger.merge(Arrays.asList(podData1, podData2));

  // then
  PodTemplateSpec podTemplate = merged.getSpec().getTemplate();
  PodSecurityContext sc = podTemplate.getSpec().getSecurityContext();
  assertEquals(sc.getRunAsUser(), (Long) 42L);
}
 
Example #4
Source File: LanderPodFactory.java    From data-highway with Apache License 2.0 6 votes vote down vote up
public Pod newInstance(LanderConfiguration config, List<String> args) {
  String truckParkName = podNameFactory.newName(config);
  log.info("Creating pod named: {}", truckParkName);
  Map<String, String> configMap = configMapSupplier.get().getData();
  return new PodBuilder()
      .withMetadata(new ObjectMetaBuilder()
          .withName(truckParkName)
          .withLabels(labels(config.getRoadName(), configMap))
          .withAnnotations(annotations(configMap))
          .build())
      .withSpec(new PodSpecBuilder()
          .withRestartPolicy(RESTART_POLICY_NEVER)
          .withContainers(container(config.getRoadName(), args, configMap, truckParkName))
          .build())
      .build();
}
 
Example #5
Source File: PodMergerTest.java    From che with Eclipse Public License 2.0 6 votes vote down vote up
@Test(dataProvider = "terminationGracePeriodProvider")
public void shouldBeAbleToMergeTerminationGracePeriodS(
    List<Long> terminationGracePeriods, Long expectedResultLong) throws ValidationException {
  List<PodData> podData =
      terminationGracePeriods
          .stream()
          .map(
              p ->
                  new PodData(
                      new PodSpecBuilder().withTerminationGracePeriodSeconds(p).build(),
                      new ObjectMetaBuilder().build()))
          .collect(Collectors.toList());

  // when
  Deployment merged = podMerger.merge(podData);
  // then
  PodTemplateSpec podTemplate = merged.getSpec().getTemplate();
  assertEquals(podTemplate.getSpec().getTerminationGracePeriodSeconds(), expectedResultLong);
}
 
Example #6
Source File: PodMergerTest.java    From che with Eclipse Public License 2.0 6 votes vote down vote up
@Test
public void shouldNotAddImagePullPolicyTwice() throws Exception {
  // given
  PodSpec podSpec1 =
      new PodSpecBuilder()
          .withImagePullSecrets(new LocalObjectReferenceBuilder().withName("secret").build())
          .build();
  podSpec1.setAdditionalProperty("add1", 1L);
  PodData podData1 = new PodData(podSpec1, new ObjectMetaBuilder().build());

  PodSpec podSpec2 =
      new PodSpecBuilder()
          .withImagePullSecrets(new LocalObjectReferenceBuilder().withName("secret").build())
          .build();
  podSpec2.setAdditionalProperty("add2", 2L);
  PodData podData2 = new PodData(podSpec2, new ObjectMetaBuilder().build());

  // when
  Deployment merged = podMerger.merge(Arrays.asList(podData1, podData2));

  // then
  PodTemplateSpec podTemplate = merged.getSpec().getTemplate();
  List<LocalObjectReference> imagePullSecrets = podTemplate.getSpec().getImagePullSecrets();
  assertEquals(imagePullSecrets.size(), 1);
  assertEquals(imagePullSecrets.get(0).getName(), "secret");
}
 
Example #7
Source File: PodMergerTest.java    From che with Eclipse Public License 2.0 6 votes vote down vote up
@Test(
    expectedExceptions = ValidationException.class,
    expectedExceptionsMessageRegExp =
        "Pods have to have volumes with unique names but there are multiple `volume` volumes")
public void shouldThrownAnExceptionIfVolumeNameCollisionHappened() throws Exception {
  // given
  PodSpec podSpec1 =
      new PodSpecBuilder().withVolumes(new VolumeBuilder().withName("volume").build()).build();
  podSpec1.setAdditionalProperty("add1", 1L);
  PodData podData1 = new PodData(podSpec1, new ObjectMetaBuilder().build());

  PodSpec podSpec2 =
      new PodSpecBuilder().withVolumes(new VolumeBuilder().withName("volume").build()).build();
  podSpec2.setAdditionalProperty("add2", 2L);
  PodData podData2 = new PodData(podSpec2, new ObjectMetaBuilder().build());

  // when
  podMerger.merge(Arrays.asList(podData1, podData2));
}
 
Example #8
Source File: PodMergerTest.java    From che with Eclipse Public License 2.0 6 votes vote down vote up
@Test
public void shouldAssignServiceAccountSharedByPods() throws Exception {
  // given
  PodSpec podSpec1 = new PodSpecBuilder().withServiceAccount("sa").build();
  podSpec1.setAdditionalProperty("add1", 1L);
  PodData podData1 = new PodData(podSpec1, new ObjectMetaBuilder().build());

  PodSpec podSpec2 = new PodSpecBuilder().withServiceAccount("sa").build();
  podSpec2.setAdditionalProperty("add2", 2L);
  PodData podData2 = new PodData(podSpec2, new ObjectMetaBuilder().build());

  // when
  Deployment merged = podMerger.merge(Arrays.asList(podData1, podData2));

  // then
  PodTemplateSpec podTemplate = merged.getSpec().getTemplate();
  String sa = podTemplate.getSpec().getServiceAccount();
  assertEquals(sa, "sa");
}
 
Example #9
Source File: FileSecretApplierTest.java    From che with Eclipse Public License 2.0 6 votes vote down vote up
@Test(
    expectedExceptions = InfrastructureException.class,
    expectedExceptionsMessageRegExp =
        "Unable to mount secret 'test_secret': It is configured to be mounted as a file but the mount path was not specified. Please define the 'che.eclipse.org/mount-path' annotation on the secret to specify it.")
public void shouldThrowExceptionWhenNoMountPathSpecifiedForFiles() throws Exception {
  Container container_match = new ContainerBuilder().withName("maven").build();

  PodSpec localSpec =
      new PodSpecBuilder().withContainers(ImmutableList.of(container_match)).build();

  when(podData.getSpec()).thenReturn(localSpec);
  Secret secret =
      new SecretBuilder()
          .withData(ImmutableMap.of("settings.xml", "random", "another.xml", "freedom"))
          .withMetadata(
              new ObjectMetaBuilder()
                  .withName("test_secret")
                  .withAnnotations(singletonMap(ANNOTATION_MOUNT_AS, "file"))
                  .withLabels(emptyMap())
                  .build())
          .build();
  when(secrets.get(any(LabelSelector.class))).thenReturn(singletonList(secret));
  secretApplier.applySecret(environment, runtimeIdentity, secret);
}
 
Example #10
Source File: PodMergerTest.java    From che with Eclipse Public License 2.0 6 votes vote down vote up
@Test
public void shouldMatchMergedPodTemplateLabelsWithDeploymentSelector() throws Exception {
  // given
  ObjectMeta podMeta1 =
      new ObjectMetaBuilder()
          .withName("ignored-1")
          .withAnnotations(ImmutableMap.of("ann1", "v1"))
          .withLabels(ImmutableMap.of("label1", "v1"))
          .build();
  podMeta1.setAdditionalProperty("add1", 1L);
  PodData podData1 = new PodData(new PodSpecBuilder().build(), podMeta1);

  // when
  Deployment merged = podMerger.merge(Collections.singletonList(podData1));

  // then
  PodTemplateSpec podTemplate = merged.getSpec().getTemplate();
  ObjectMeta podMeta = podTemplate.getMetadata();
  Map<String, String> deploymentSelector = merged.getSpec().getSelector().getMatchLabels();
  assertTrue(podMeta.getLabels().entrySet().containsAll(deploymentSelector.entrySet()));
}
 
Example #11
Source File: OpenshiftHandler.java    From dekorate with Apache License 2.0 6 votes vote down vote up
/**
 * Creates a {@link PodSpec} for the {@link OpenshiftConfig}.
 * @param config   The sesssion.
 * @return          The pod specification.
 */
public static PodSpec createPodSpec(OpenshiftConfig config, ImageConfiguration imageConfig) {
 String image = Images.getImage(imageConfig.getRegistry(), imageConfig.getGroup(), imageConfig.getName(), imageConfig.getVersion());

  return new PodSpecBuilder()
    .addNewContainer()
    .withName(config.getName())
    .withImage(image)
    .withImagePullPolicy(IF_NOT_PRESENT)
    .addNewEnv()
    .withName(KUBERNETES_NAMESPACE)
    .withNewValueFrom()
    .withNewFieldRef(null, METADATA_NAMESPACE)
    .endValueFrom()
    .endEnv()
    .addNewEnv()
    .withName(JAVA_APP_JAR)
    .withValue("/deployments/" + config.getProject().getBuildInfo().getOutputFile().getFileName().toString())
    .endEnv()
    .endContainer()
    .build();
}
 
Example #12
Source File: KubernetesHandler.java    From dekorate with Apache License 2.0 6 votes vote down vote up
/**
 * Creates a {@link PodSpec} for the {@link KubernetesConfig}.
 * @param imageConfig   The sesssion.
 * @return The pod specification.
 */
public static PodSpec createPodSpec(KubernetesConfig appConfig, ImageConfiguration imageConfig) {
 String image = Images.getImage(imageConfig.isAutoPushEnabled() ?
                                (Strings.isNullOrEmpty(imageConfig.getRegistry()) ? DEFAULT_REGISTRY : imageConfig.getRegistry())
                                : imageConfig.getRegistry(), imageConfig.getGroup(), imageConfig.getName(), imageConfig.getVersion()); 

  return new PodSpecBuilder()
    .addNewContainer()
    .withName(appConfig.getName())
    .withImage(image)
    .withImagePullPolicy(IF_NOT_PRESENT)
    .addNewEnv()
    .withName(KUBERNETES_NAMESPACE)
    .withNewValueFrom()
    .withNewFieldRef(null, METADATA_NAMESPACE)
    .endValueFrom()
    .endEnv()
    .endContainer()
    .build();
}
 
Example #13
Source File: PodMergerTest.java    From che with Eclipse Public License 2.0 6 votes vote down vote up
@Test
public void shouldAssignServiceAccountNameSharedByPods() throws Exception {
  // given
  PodSpec podSpec1 = new PodSpecBuilder().withServiceAccountName("sa").build();
  podSpec1.setAdditionalProperty("add1", 1L);
  PodData podData1 = new PodData(podSpec1, new ObjectMetaBuilder().build());

  PodSpec podSpec2 = new PodSpecBuilder().withServiceAccountName("sa").build();
  podSpec2.setAdditionalProperty("add2", 2L);
  PodData podData2 = new PodData(podSpec2, new ObjectMetaBuilder().build());

  // when
  Deployment merged = podMerger.merge(Arrays.asList(podData1, podData2));

  // then
  PodTemplateSpec podTemplate = merged.getSpec().getTemplate();
  String sa = podTemplate.getSpec().getServiceAccountName();
  assertEquals(sa, "sa");
}
 
Example #14
Source File: PodMergerTest.java    From che with Eclipse Public License 2.0 6 votes vote down vote up
@Test(expectedExceptions = ValidationException.class)
public void shouldFailServiceAccountNameDiffersInPods() throws Exception {
  // given
  PodSpec podSpec1 = new PodSpecBuilder().withServiceAccountName("sa").build();
  podSpec1.setAdditionalProperty("add1", 1L);
  PodData podData1 = new PodData(podSpec1, new ObjectMetaBuilder().build());

  PodSpec podSpec2 = new PodSpecBuilder().withServiceAccountName("sb").build();
  podSpec2.setAdditionalProperty("add2", 2L);
  PodData podData2 = new PodData(podSpec2, new ObjectMetaBuilder().build());

  // when
  Deployment merged = podMerger.merge(Arrays.asList(podData1, podData2));

  // then
  // exception is thrown
}
 
Example #15
Source File: PodMergerTest.java    From che with Eclipse Public License 2.0 6 votes vote down vote up
@Test
public void shouldGenerateContainerNamesIfCollisionHappened() throws Exception {
  // given
  PodSpec podSpec1 =
      new PodSpecBuilder().withContainers(new ContainerBuilder().withName("c").build()).build();
  PodData podData1 = new PodData(podSpec1, new ObjectMetaBuilder().build());

  PodSpec podSpec2 =
      new PodSpecBuilder().withContainers(new ContainerBuilder().withName("c").build()).build();
  PodData podData2 = new PodData(podSpec2, new ObjectMetaBuilder().build());

  // when
  Deployment merged = podMerger.merge(Arrays.asList(podData1, podData2));

  // then
  PodTemplateSpec podTemplate = merged.getSpec().getTemplate();
  List<Container> containers = podTemplate.getSpec().getContainers();
  assertEquals(containers.size(), 2);
  Container container1 = containers.get(0);
  assertEquals(container1.getName(), "c");
  Container container2 = containers.get(1);
  assertNotEquals(container2.getName(), "c");
  assertTrue(container2.getName().startsWith("c"));
}
 
Example #16
Source File: ContainerDecorator.java    From dekorate with Apache License 2.0 5 votes vote down vote up
public static ContainerFluentVisitor createNewInit() {
  return new ContainerFluentVisitor(v -> new Decorator<PodSpecBuilder>() {
      @Override
      public void visit(PodSpecBuilder b) {
        b.addToInitContainers(v);
      }
    });
}
 
Example #17
Source File: PodMergerTest.java    From che with Eclipse Public License 2.0 5 votes vote down vote up
@Test
public void shouldMergeMetasOfPodsData() throws Exception {
  // given
  ObjectMeta podMeta1 =
      new ObjectMetaBuilder()
          .withName("ignored-1")
          .withAnnotations(ImmutableMap.of("ann1", "v1"))
          .withLabels(ImmutableMap.of("label1", "v1"))
          .build();
  podMeta1.setAdditionalProperty("add1", 1L);
  PodData podData1 = new PodData(new PodSpecBuilder().build(), podMeta1);

  ObjectMeta podMeta2 =
      new ObjectMetaBuilder()
          .withName("ignored-2")
          .withAnnotations(ImmutableMap.of("ann2", "v2"))
          .withLabels(ImmutableMap.of("label2", "v2"))
          .build();
  podMeta2.setAdditionalProperty("add2", 2L);
  PodData podData2 = new PodData(new PodSpecBuilder().build(), podMeta2);

  // when
  Deployment merged = podMerger.merge(Arrays.asList(podData1, podData2));

  // then
  PodTemplateSpec podTemplate = merged.getSpec().getTemplate();
  ObjectMeta podMeta = podTemplate.getMetadata();
  verifyContainsAllFrom(podMeta, podData1.getMetadata());
  verifyContainsAllFrom(podMeta, podData2.getMetadata());
}
 
Example #18
Source File: PodMergerTest.java    From che with Eclipse Public License 2.0 5 votes vote down vote up
@Test
public void shouldGenerateInitContainerNamesIfCollisionHappened() throws Exception {
  // given
  PodSpec podSpec1 =
      new PodSpecBuilder()
          .withInitContainers(new ContainerBuilder().withName("initC").build())
          .build();
  PodData podData1 = new PodData(podSpec1, new ObjectMetaBuilder().build());

  PodSpec podSpec2 =
      new PodSpecBuilder()
          .withInitContainers(new ContainerBuilder().withName("initC").build())
          .build();
  PodData podData2 = new PodData(podSpec2, new ObjectMetaBuilder().build());
  // when
  Deployment merged = podMerger.merge(Arrays.asList(podData1, podData2));

  // then
  PodTemplateSpec podTemplate = merged.getSpec().getTemplate();
  List<Container> initContainers = podTemplate.getSpec().getInitContainers();
  assertEquals(initContainers.size(), 2);
  Container container1 = initContainers.get(0);
  assertEquals(container1.getName(), "initC");
  Container container2 = initContainers.get(1);
  assertNotEquals(container2.getName(), "initC");
  assertTrue(container2.getName().startsWith("initC"));
}
 
Example #19
Source File: PodMergerTest.java    From che with Eclipse Public License 2.0 5 votes vote down vote up
@Test
public void shouldMergeSpecsOfPodsData() throws Exception {
  // given
  PodSpec podSpec1 =
      new PodSpecBuilder()
          .withContainers(new ContainerBuilder().withName("c1").build())
          .withInitContainers(new ContainerBuilder().withName("initC1").build())
          .withVolumes(new VolumeBuilder().withName("v1").build())
          .withImagePullSecrets(new LocalObjectReferenceBuilder().withName("secret1").build())
          .build();
  podSpec1.setAdditionalProperty("add1", 1L);
  PodData podData1 = new PodData(podSpec1, new ObjectMetaBuilder().build());

  PodSpec podSpec2 =
      new PodSpecBuilder()
          .withContainers(new ContainerBuilder().withName("c2").build())
          .withInitContainers(new ContainerBuilder().withName("initC2").build())
          .withVolumes(new VolumeBuilder().withName("v2").build())
          .withImagePullSecrets(new LocalObjectReferenceBuilder().withName("secret2").build())
          .build();
  podSpec2.setAdditionalProperty("add2", 2L);
  PodData podData2 = new PodData(podSpec2, new ObjectMetaBuilder().build());

  // when
  Deployment merged = podMerger.merge(Arrays.asList(podData1, podData2));

  // then
  PodTemplateSpec podTemplate = merged.getSpec().getTemplate();
  verifyContainsAllFrom(podTemplate.getSpec(), podData1.getSpec());
  verifyContainsAllFrom(podTemplate.getSpec(), podData2.getSpec());
}
 
Example #20
Source File: AddAzureDiskVolumeDecorator.java    From dekorate with Apache License 2.0 5 votes vote down vote up
@Override
public void visit(PodSpecBuilder podSpec) {
  podSpec.addNewVolume()
    .withName(volume.getVolumeName())
    .withNewAzureDisk()
    .withKind(volume.getKind())
    .withDiskName(volume.getDiskName())
    .withDiskURI(volume.getDiskURI())
    .withFsType(volume.getFsType())
    .withCachingMode(volume.getCachingMode())
    .withReadOnly(volume.isReadOnly())
    .endAzureDisk()
    .endVolume();

}
 
Example #21
Source File: VolumeDecorator.java    From dekorate with Apache License 2.0 5 votes vote down vote up
public static VolumeFluentVisitor createNew() {
  return new VolumeFluentVisitor(v -> new Decorator<PodSpecBuilder>() {
      @Override
      public void visit(PodSpecBuilder b) {
        b.addToVolumes(v);
      }
    });
}
 
Example #22
Source File: ContainerDecorator.java    From dekorate with Apache License 2.0 5 votes vote down vote up
public static ContainerFluentVisitor createNew() {
  return new ContainerFluentVisitor(v -> new Decorator<PodSpecBuilder>() {
      @Override
      public void visit(PodSpecBuilder b) {
        b.addToContainers(v);
      }
    });
}
 
Example #23
Source File: AddSecretVolumeDecorator.java    From dekorate with Apache License 2.0 5 votes vote down vote up
@Override
public void visit(PodSpecBuilder podSpec) {
  podSpec.addNewVolume()
    .withName(volume.getVolumeName())
    .withNewSecret()
    .withSecretName(volume.getSecretName())
    .withDefaultMode(volume.getDefaultMode())
    .withOptional(volume.isOptional())
    .endSecret()
    .endVolume();

}
 
Example #24
Source File: AddConfigMapVolumeDecorator.java    From dekorate with Apache License 2.0 5 votes vote down vote up
@Override
public void visit(PodSpecBuilder podSpec) {
  podSpec.addNewVolume()
    .withName(volume.getVolumeName())
    .withNewConfigMap()
    .withName(volume.getConfigMapName())
    .withDefaultMode(volume.getDefaultMode())
    .withOptional(volume.isOptional())
    .endConfigMap()
    .endVolume();

}
 
Example #25
Source File: AddPvcVolumeDecorator.java    From dekorate with Apache License 2.0 5 votes vote down vote up
@Override
public void visit(PodSpecBuilder podSpec) {
  podSpec.addNewVolume()
    .withName(volume.getVolumeName())
    .withNewPersistentVolumeClaim()
    .withClaimName(volume.getClaimName())
    .withNewReadOnly(volume.isReadOnly())
    .endPersistentVolumeClaim()
    .endVolume();

}
 
Example #26
Source File: AddAwsElasticBlockStoreVolumeDecorator.java    From dekorate with Apache License 2.0 5 votes vote down vote up
@Override
public void visit(PodSpecBuilder podSpec) {
  podSpec.addNewVolume()
    .withName(volume.getVolumeName())
    .withNewAwsElasticBlockStore()
    .withVolumeID(volume.getVolumeId())
    .withFsType(volume.getFsType())
    .withPartition(volume.getPartition())
    .withReadOnly(volume.isReadOnly())
    .endAwsElasticBlockStore()
    .endVolume();
}
 
Example #27
Source File: AddAzureFileVolumeDecorator.java    From dekorate with Apache License 2.0 5 votes vote down vote up
@Override
public void visit(PodSpecBuilder podSpec) {
  podSpec.addNewVolume()
    .withName(volume.getVolumeName())
    .withNewAzureFile()
    .withSecretName(volume.getSecretName())
    .withShareName(volume.getShareName())
    .withReadOnly(volume.isReadOnly())
    .endAzureFile()
    .endVolume();

}
 
Example #28
Source File: PodTemplateHandler.java    From jkube with Eclipse Public License 2.0 5 votes vote down vote up
private PodSpec createPodSpec(ResourceConfig config, List<ImageConfiguration> images) {

        return new PodSpecBuilder()
            .withServiceAccountName(config.getServiceAccount())
            .withContainers(containerHandler.getContainers(config,images))
            .withVolumes(getVolumes(config))
            .build();
    }
 
Example #29
Source File: RestartPolicyRewriterTest.java    From che with Eclipse Public License 2.0 4 votes vote down vote up
private static Pod newPod(String podName, String restartPolicy, Container... containers) {
  final ObjectMeta podMetadata = new ObjectMetaBuilder().withName(podName).build();
  final PodSpec podSpec =
      new PodSpecBuilder().withRestartPolicy(restartPolicy).withContainers(containers).build();
  return new PodBuilder().withMetadata(podMetadata).withSpec(podSpec).build();
}
 
Example #30
Source File: PodCrudTest.java    From kubernetes-client with Apache License 2.0 4 votes vote down vote up
@Test
public void testPodWatchOnName() throws InterruptedException {
  KubernetesClient client = server.getClient();
  Pod pod1 = new PodBuilder().withNewMetadata().withName("pod1").addToLabels("testKey", "testValue").endMetadata().build();
  final CountDownLatch deleteLatch = new CountDownLatch(1);
  final CountDownLatch closeLatch = new CountDownLatch(1);
  final CountDownLatch editLatch = new CountDownLatch(2);
  final CountDownLatch addLatch = new CountDownLatch(1);

  pod1 = client.pods().inNamespace("ns1").create(pod1);
  Watch watch = client.pods().inNamespace("ns1").withName(pod1.getMetadata().getName()).watch(new Watcher<Pod>() {
    @Override
    public void eventReceived(Action action, Pod resource) {
      switch (action) {
        case DELETED:
          deleteLatch.countDown();
          break;
        case MODIFIED:
          editLatch.countDown();
          break;
        case ADDED:
          addLatch.countDown();
          break;
        default:
          throw new AssertionFailedError(action.toString().concat(" isn't recognised."));
      }
    }
    @Override
    public void onClose(KubernetesClientException cause) {
      closeLatch.countDown();
    }
  });

  pod1 = client.pods().inNamespace("ns1").withName(pod1.getMetadata().getName())
    .patch(new PodBuilder().withNewMetadataLike(pod1.getMetadata()).endMetadata().build());

  pod1.setSpec(new PodSpecBuilder().addNewContainer().withImage("nginx").withName("nginx").endContainer().build());

  client.pods().inNamespace("ns1").withName(pod1.getMetadata().getName()).replace(pod1);

  client.pods().inNamespace("ns1").withName(pod1.getMetadata().getName()).delete();

  client.pods().inNamespace("ns1").create(new PodBuilder().withNewMetadata().withName("pod1").addToLabels("testKey", "testValue").endMetadata().build());

  assertEquals(1, client.pods().inNamespace("ns1").list().getItems().size());
  assertTrue(addLatch.await(1, TimeUnit.MINUTES));
  assertTrue(editLatch.await(1, TimeUnit.MINUTES));
  assertTrue(deleteLatch.await(1, TimeUnit.MINUTES));

  watch.close();

  assertTrue(closeLatch.await(1, TimeUnit.MINUTES));
}