Java Code Examples for io.fabric8.kubernetes.api.model.PodTemplateSpec#getMetadata()

The following examples show how to use io.fabric8.kubernetes.api.model.PodTemplateSpec#getMetadata() . 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: KubernetesEnvironment.java    From che with Eclipse Public License 2.0 6 votes vote down vote up
public PodData(Deployment deployment) {
  PodTemplateSpec podTemplate = deployment.getSpec().getTemplate();

  // it is not required for PodTemplate to have name specified
  // but many of Che Server components rely that PodData has name
  // so, provision name from deployment if it is missing
  ObjectMeta podTemplateMeta = podTemplate.getMetadata();
  if (podTemplateMeta == null) {
    podTemplate.setMetadata(
        new ObjectMetaBuilder().withName(deployment.getMetadata().getName()).build());
  } else {
    if (podTemplateMeta.getName() == null) {
      podTemplateMeta.setName(deployment.getMetadata().getName());
    }
  }

  this.podSpec = podTemplate.getSpec();
  this.podMeta = podTemplate.getMetadata();
  this.role = PodRole.DEPLOYMENT;
}
 
Example 2
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 3
Source File: KubernetesResourceUtil.java    From jkube with Eclipse Public License 2.0 5 votes vote down vote up
private static void mergeMetadata(PodTemplateSpec item1, PodTemplateSpec item2) {
    if (item1 != null && item2 != null) {
        ObjectMeta metadata1 = item1.getMetadata();
        ObjectMeta metadata2 = item2.getMetadata();
        if (metadata1 == null) {
            item1.setMetadata(metadata2);
        } else if (metadata2 != null) {
            metadata1.setAnnotations(mergeMapsAndRemoveEmptyStrings(metadata2.getAnnotations(), metadata1.getAnnotations()));
            metadata1.setLabels(mergeMapsAndRemoveEmptyStrings(metadata2.getLabels(), metadata1.getLabels()));
        }
    }
}
 
Example 4
Source File: DockerimageComponentToWorkspaceApplierTest.java    From che with Eclipse Public License 2.0 5 votes vote down vote up
@Test
public void shouldBeAbleToSetupSpecificImagePullPolicy() throws DevfileException {
  ComponentImpl dockerimageComponent = new ComponentImpl();
  dockerimageComponent.setType(DOCKERIMAGE_COMPONENT_TYPE);
  dockerimageComponent.setImage("eclipse/ubuntu_jdk8:latest");
  dockerimageComponent.setMemoryLimit("1G");
  dockerimageComponentApplier =
      new DockerimageComponentToWorkspaceApplier(PROJECTS_MOUNT_PATH, "Never", k8sEnvProvisioner);

  // when
  dockerimageComponentApplier.apply(workspaceConfig, dockerimageComponent, null);

  // then
  verify(k8sEnvProvisioner)
      .provision(
          any(),
          eq(KubernetesEnvironment.TYPE),
          objectsCaptor.capture(),
          machinesCaptor.capture());
  MachineConfigImpl machineConfig = machinesCaptor.getValue().get("eclipse-ubuntu_jdk8-latest");
  assertNotNull(machineConfig);

  List<HasMetadata> objects = objectsCaptor.getValue();
  assertEquals(objects.size(), 1);
  assertTrue(objects.get(0) instanceof Deployment);
  Deployment deployment = (Deployment) objects.get(0);
  PodTemplateSpec podTemplate = deployment.getSpec().getTemplate();
  ObjectMeta podMeta = podTemplate.getMetadata();
  assertEquals(podMeta.getName(), "eclipse-ubuntu_jdk8-latest");

  Map<String, String> deploymentSelector = deployment.getSpec().getSelector().getMatchLabels();
  assertFalse(deploymentSelector.isEmpty());
  assertTrue(podMeta.getLabels().entrySet().containsAll(deploymentSelector.entrySet()));

  Container container = podTemplate.getSpec().getContainers().get(0);
  assertEquals(container.getName(), "eclipse-ubuntu_jdk8-latest");
  assertEquals(container.getImagePullPolicy(), "Never");
}
 
Example 5
Source File: DockerimageComponentToWorkspaceApplierTest.java    From che with Eclipse Public License 2.0 5 votes vote down vote up
@Test
public void shouldProvisionK8sEnvironmentWithMachineConfigFromSpecifiedDockerimageWithoutAlias()
    throws Exception {
  ComponentImpl dockerimageComponent = new ComponentImpl();
  dockerimageComponent.setType(DOCKERIMAGE_COMPONENT_TYPE);
  dockerimageComponent.setImage("eclipse/ubuntu_jdk8:latest");
  dockerimageComponent.setMemoryLimit("1G");

  // when
  dockerimageComponentApplier.apply(workspaceConfig, dockerimageComponent, null);

  // then
  verify(k8sEnvProvisioner)
      .provision(
          any(),
          eq(KubernetesEnvironment.TYPE),
          objectsCaptor.capture(),
          machinesCaptor.capture());
  MachineConfigImpl machineConfig = machinesCaptor.getValue().get("eclipse-ubuntu_jdk8-latest");
  assertNotNull(machineConfig);

  List<HasMetadata> objects = objectsCaptor.getValue();
  assertEquals(objects.size(), 1);
  assertTrue(objects.get(0) instanceof Deployment);
  Deployment deployment = (Deployment) objects.get(0);
  PodTemplateSpec podTemplate = deployment.getSpec().getTemplate();
  ObjectMeta podMeta = podTemplate.getMetadata();
  assertEquals(podMeta.getName(), "eclipse-ubuntu_jdk8-latest");

  Map<String, String> deploymentSelector = deployment.getSpec().getSelector().getMatchLabels();
  assertFalse(deploymentSelector.isEmpty());
  assertTrue(podMeta.getLabels().entrySet().containsAll(deploymentSelector.entrySet()));

  Container container = podTemplate.getSpec().getContainers().get(0);
  assertEquals(container.getName(), "eclipse-ubuntu_jdk8-latest");
  assertEquals(container.getImage(), "eclipse/ubuntu_jdk8:latest");

  assertEquals(
      Names.machineName(podTemplate.getMetadata(), container), "eclipse-ubuntu_jdk8-latest");
}
 
Example 6
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 7
Source File: Annotations.java    From strimzi-kafka-operator with Apache License 2.0 4 votes vote down vote up
public static int intAnnotation(PodTemplateSpec podSpec, String annotation, int defaultValue, String... deprecatedAnnotations) {
    ObjectMeta metadata = podSpec.getMetadata();
    String str = annotation(annotation, null, metadata, deprecatedAnnotations);
    return str != null ? parseInt(str) : defaultValue;
}
 
Example 8
Source File: Annotations.java    From strimzi-kafka-operator with Apache License 2.0 4 votes vote down vote up
public static String stringAnnotation(PodTemplateSpec podSpec, String annotation, String defaultValue, String... deprecatedAnnotations) {
    ObjectMeta metadata = podSpec.getMetadata();
    String str = annotation(annotation, null, metadata, deprecatedAnnotations);
    return str != null ? str : defaultValue;
}
 
Example 9
Source File: Annotations.java    From strimzi-kafka-operator with Apache License 2.0 4 votes vote down vote up
public static int incrementIntAnnotation(PodTemplateSpec podSpec, String annotation, int defaultValue, String... deprecatedAnnotations) {
    ObjectMeta metadata = podSpec.getMetadata();
    return incrementIntAnnotation(annotation, defaultValue, metadata, deprecatedAnnotations);
}
 
Example 10
Source File: DockerimageComponentToWorkspaceApplierTest.java    From che with Eclipse Public License 2.0 4 votes vote down vote up
@Test
public void
    shouldProvisionK8sEnvironmentWithMachineConfigAndGeneratedDeploymentForSpecifiedDockerimage()
        throws Exception {
  // given
  ComponentImpl dockerimageComponent = new ComponentImpl();
  dockerimageComponent.setType(DOCKERIMAGE_COMPONENT_TYPE);
  dockerimageComponent.setAlias("jdk");
  dockerimageComponent.setImage("eclipse/ubuntu_jdk8:latest");
  dockerimageComponent.setMemoryLimit("1G");

  // when
  dockerimageComponentApplier.apply(workspaceConfig, dockerimageComponent, null);

  // then
  verify(k8sEnvProvisioner)
      .provision(
          eq(workspaceConfig),
          eq(KubernetesEnvironment.TYPE),
          objectsCaptor.capture(),
          machinesCaptor.capture());
  MachineConfigImpl machineConfig = machinesCaptor.getValue().get("jdk");
  assertNotNull(machineConfig);

  List<HasMetadata> objects = objectsCaptor.getValue();
  assertEquals(objects.size(), 1);
  assertTrue(objects.get(0) instanceof Deployment);
  Deployment deployment = (Deployment) objects.get(0);
  PodTemplateSpec podTemplate = deployment.getSpec().getTemplate();
  ObjectMeta podMeta = podTemplate.getMetadata();
  assertEquals(podMeta.getName(), "jdk");

  Map<String, String> deploymentSelector = deployment.getSpec().getSelector().getMatchLabels();
  assertFalse(deploymentSelector.isEmpty());
  assertTrue(podMeta.getLabels().entrySet().containsAll(deploymentSelector.entrySet()));

  Container container = podTemplate.getSpec().getContainers().get(0);
  assertEquals(container.getName(), "jdk");
  assertEquals(container.getImage(), "eclipse/ubuntu_jdk8:latest");
  assertEquals(container.getImagePullPolicy(), "Always");

  assertEquals(Names.machineName(podMeta, container), "jdk");
}