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

The following examples show how to use io.fabric8.kubernetes.api.model.PodTemplateSpec. 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
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 #2
Source File: PodTemplateHandlerTest.java    From jkube with Eclipse Public License 2.0 6 votes vote down vote up
@Test
public void podWithoutEmptyTypeTemplateHandlerTest(){
    ContainerHandler containerHandler = getContainerHandler();

    PodTemplateHandler podTemplateHandler = new PodTemplateHandler(containerHandler);

    //empty type
    VolumeConfig volumeConfig1 = VolumeConfig.builder().name("test").mounts(mounts).build();
    volumes1.clear();
    volumes1.add(volumeConfig1);

    ResourceConfig config = ResourceConfig.builder()
            .imagePullPolicy("IfNotPresent")
            .controllerName("testing")
            .serviceAccount("test-account")
            .replicas(5)
            .volumes(volumes1)
            .build();

    PodTemplateSpec podTemplateSpec = podTemplateHandler.getPodTemplate(config,images);

    //Assertion
    assertEquals("test-account",podTemplateSpec.getSpec().getServiceAccountName());
    assertTrue(podTemplateSpec.getSpec().getVolumes().isEmpty());
    assertNotNull(podTemplateSpec.getSpec().getContainers());
}
 
Example #3
Source File: PodTemplateHandlerTest.java    From jkube with Eclipse Public License 2.0 6 votes vote down vote up
@Test
public void podWithEmotyVolumeTemplateHandlerTest(){
    ContainerHandler containerHandler = getContainerHandler();

    PodTemplateHandler podTemplateHandler = new PodTemplateHandler(containerHandler);
    //Pod with empty Volume Config and wihtout ServiceAccount
    ResourceConfig config = ResourceConfig.builder()
            .imagePullPolicy("IfNotPresent")
            .controllerName("testing")
            .replicas(5)
            .volumes(volumes1)
            .build();

    PodTemplateSpec podTemplateSpec = podTemplateHandler.getPodTemplate(config,images);

    //Assertion
    assertNull(podTemplateSpec.getSpec().getServiceAccountName());
    assertTrue(podTemplateSpec.getSpec().getVolumes().isEmpty());
    assertNotNull(podTemplateSpec.getSpec().getContainers());
}
 
Example #4
Source File: DeploymentConfigHandler.java    From jkube with Eclipse Public License 2.0 6 votes vote down vote up
private DeploymentConfigSpec createDeploymentConfigSpec(ResourceConfig config, List<ImageConfiguration> images, Long openshiftDeployTimeoutSeconds, Boolean imageChangeTrigger, Boolean enableAutomaticTrigger, Boolean isOpenshiftBuildStrategy, List<String> generatedContainers) {
    DeploymentConfigSpecBuilder specBuilder = new DeploymentConfigSpecBuilder();

    PodTemplateSpec podTemplateSpec = podTemplateHandler.getPodTemplate(config,images);

    specBuilder.withReplicas(config.getReplicas())
            .withTemplate(podTemplateSpec)
            .addNewTrigger().withType("ConfigChange").endTrigger();

    if (openshiftDeployTimeoutSeconds != null && openshiftDeployTimeoutSeconds > 0) {
        specBuilder.withNewStrategy().withType("Rolling").
                withNewRollingParams().withTimeoutSeconds(openshiftDeployTimeoutSeconds).endRollingParams().endStrategy();
    }

    return specBuilder.build();
}
 
Example #5
Source File: KubeUtil.java    From enmasse with Apache License 2.0 6 votes vote down vote up
public static void applyCpuMemory(PodTemplateSpec podTemplateSpec, String cpu, String memory) {
    Map<String, Quantity> resources = new LinkedHashMap<>();
    if (cpu != null) {
        resources.put("cpu", new Quantity(cpu));
    }
    if (memory != null) {
        resources.put("memory", new Quantity(memory));
    }

    for (Container container : podTemplateSpec.getSpec().getContainers()) {
        if (container.getResources() == null) {
            container.setResources(new ResourceRequirements());
        }

        if (container.getResources().getRequests() == null) {
            container.getResources().setRequests(new LinkedHashMap<>());
        }
        container.getResources().getRequests().putAll(resources);

        if (container.getResources().getLimits() == null) {
            container.getResources().setLimits(new LinkedHashMap<>());
        }

        container.getResources().getLimits().putAll(resources);
    }
}
 
Example #6
Source File: KubeUtilTest.java    From enmasse with Apache License 2.0 6 votes vote down vote up
@Test
public void appliesSecurityContext() {

    PodTemplateSpec actual = new PodTemplateSpecBuilder()
            .withNewSpec()
            .endSpec()
            .build();

    final long runAsGroup = 1000L;
    final long fsGroup = 1234L;
    PodTemplateSpec desired = new PodTemplateSpecBuilder()
            .withNewSpec()
            .withNewSecurityContext()
            .withRunAsGroup(runAsGroup)
            .withFsGroup(fsGroup)
            .endSecurityContext()
            .endSpec()
            .build();

    KubeUtil.applyPodTemplate(actual, desired);

    assertThat(actual.getSpec().getSecurityContext().getFsGroup(), equalTo(fsGroup));
    assertThat(actual.getSpec().getSecurityContext().getRunAsGroup(), equalTo(runAsGroup));
}
 
Example #7
Source File: KubeUtilTest.java    From enmasse with Apache License 2.0 6 votes vote down vote up
@Test
public void appliesContainerEnvVarToPodTemplate() {
    Container actualContainer = new ContainerBuilder()
            .withName("foo")
            .withEnv(FOO1_ENVVAR, FOO2_ENVAR).build();
    Container desiredContainer = new ContainerBuilder()
            .withName("foo")
            .withEnv(FOO1_UPD_ENVVAR, FOO3_ENVVAR).build();

    PodTemplateSpec actual = doApplyContainers(actualContainer, desiredContainer);

    Container container = actual.getSpec().getContainers().get(0);
    List<EnvVar> probe = container.getEnv();
    assertThat(probe.size(), equalTo(3));
    assertThat(probe, containsInAnyOrder(FOO1_UPD_ENVVAR, FOO2_ENVAR, FOO3_ENVVAR));
}
 
Example #8
Source File: KubeUtilTest.java    From enmasse with Apache License 2.0 6 votes vote down vote up
@Test
public void appliesInitContainerEnvVarToPodTemplate() {
    Container actualContainer = new ContainerBuilder()
            .withName("foo")
            .withEnv(FOO1_ENVVAR).build();
    Container desiredContainer = new ContainerBuilder()
            .withName("foo")
            .withEnv(FOO1_UPD_ENVVAR).build();

    PodTemplateSpec actual = doApplyInitContainers(actualContainer, desiredContainer);

    Container container = actual.getSpec().getInitContainers().get(0);
    List<EnvVar> probe = container.getEnv();
    assertThat(probe.size(), equalTo(1));
    assertThat(probe, containsInAnyOrder(FOO1_UPD_ENVVAR));
}
 
Example #9
Source File: KubeUtilTest.java    From enmasse with Apache License 2.0 6 votes vote down vote up
@Test
public void appliesContainerLivenessProbeSettingsToPodTemplate() {
    Container actualContainer = new ContainerBuilder()
            .withName("foo")
            .withLivenessProbe(new ProbeBuilder()
                    .withInitialDelaySeconds(1)
                    .withPeriodSeconds(2)
                    .withFailureThreshold(4).build()).build();
    Container desiredContainer = new ContainerBuilder()
            .withName("foo")
            .withLivenessProbe(new ProbeBuilder()
                    .withInitialDelaySeconds(10)
                    .withSuccessThreshold(80).build()).build();
    PodTemplateSpec actual = doApplyContainers(actualContainer, desiredContainer);

    Container container = actual.getSpec().getContainers().get(0);
    Probe probe = container.getLivenessProbe();
    assertThat(probe.getInitialDelaySeconds(), equalTo(10));
    assertThat(probe.getPeriodSeconds(), equalTo(2));
    assertThat(probe.getFailureThreshold(), equalTo(4));
    assertThat(probe.getSuccessThreshold(), equalTo(80));
}
 
Example #10
Source File: KubeUtilTest.java    From enmasse with Apache License 2.0 6 votes vote down vote up
private PodTemplateSpec doApplyContainers(Container actualContainer, Container desiredContainer) {
    PodTemplateSpec actual = new PodTemplateSpecBuilder()
            .withNewSpec()
            .addToContainers(actualContainer)
            .endSpec()
            .build();

    PodTemplateSpec desired = new PodTemplateSpecBuilder()
            .withNewSpec()
            .addToContainers(desiredContainer)
            .endSpec()
            .build();

    KubeUtil.applyPodTemplate(actual, desired);

    return actual;
}
 
Example #11
Source File: KubeUtilTest.java    From enmasse with Apache License 2.0 6 votes vote down vote up
private PodTemplateSpec doApplyInitContainers(Container actualContainer, Container desiredContainer) {
    PodTemplateSpec actual = new PodTemplateSpecBuilder()
            .withNewSpec()
            .addToInitContainers(actualContainer)
            .endSpec()
            .build();

    PodTemplateSpec desired = new PodTemplateSpecBuilder()
            .withNewSpec()
            .addToInitContainers(desiredContainer)
            .endSpec()
            .build();

    KubeUtil.applyPodTemplate(actual, desired);

    return actual;
}
 
Example #12
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 #13
Source File: KubernetesEnvironmentFactoryTest.java    From che with Eclipse Public License 2.0 6 votes vote down vote up
@Test
public void shouldUseDeploymentNameAsPodTemplateNameIfItIsMissing() throws Exception {
  // given
  PodTemplateSpec podTemplate = new PodTemplateSpecBuilder().withNewSpec().endSpec().build();
  Deployment deployment =
      new DeploymentBuilder()
          .withNewMetadata()
          .withName("deployment-test")
          .endMetadata()
          .withNewSpec()
          .withTemplate(podTemplate)
          .endSpec()
          .build();
  when(k8sRecipeParser.parse(any(InternalRecipe.class))).thenReturn(asList(deployment));

  // when
  final KubernetesEnvironment k8sEnv =
      k8sEnvFactory.doCreate(internalRecipe, emptyMap(), emptyList());

  // then
  Deployment deploymentTest = k8sEnv.getDeploymentsCopy().get("deployment-test");
  assertNotNull(deploymentTest);
  PodTemplateSpec resultPodTemplate = deploymentTest.getSpec().getTemplate();
  assertEquals(resultPodTemplate.getMetadata().getName(), "deployment-test");
}
 
Example #14
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 #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: 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 #17
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 #18
Source File: OpenShiftEnvironmentFactoryTest.java    From che with Eclipse Public License 2.0 6 votes vote down vote up
@Test
public void shouldUseDeploymentNameAsPodTemplateNameIfItIsMissing() throws Exception {
  // given
  PodTemplateSpec podTemplate = new PodTemplateSpecBuilder().withNewSpec().endSpec().build();
  Deployment deployment =
      new DeploymentBuilder()
          .withNewMetadata()
          .withName("deployment-test")
          .endMetadata()
          .withNewSpec()
          .withTemplate(podTemplate)
          .endSpec()
          .build();
  when(k8sRecipeParser.parse(any(InternalRecipe.class))).thenReturn(asList(deployment));

  // when
  final KubernetesEnvironment k8sEnv =
      osEnvFactory.doCreate(internalRecipe, emptyMap(), emptyList());

  // then
  Deployment deploymentTest = k8sEnv.getDeploymentsCopy().get("deployment-test");
  assertNotNull(deploymentTest);
  PodTemplateSpec resultPodTemplate = deploymentTest.getSpec().getTemplate();
  assertEquals(resultPodTemplate.getMetadata().getName(), "deployment-test");
}
 
Example #19
Source File: DockerImageWatcher.java    From jkube with Eclipse Public License 2.0 6 votes vote down vote up
private boolean updateImageName(HasMetadata entity, PodTemplateSpec template, String imagePrefix, String imageName) {
    boolean answer = false;
    PodSpec spec = template.getSpec();
    if (spec != null) {
        List<Container> containers = spec.getContainers();
        if (containers != null) {
            for (Container container : containers) {
                String image = container.getImage();
                if (image != null && image.startsWith(imagePrefix)) {
                    container.setImage(imageName);
                    log.info("Updating " + KubernetesHelper.getKind(entity) + " " + KubernetesHelper.getName(entity) + " to use image: " + imageName);
                    answer = true;
                }
            }
        }
    }
    return answer;
}
 
Example #20
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 #21
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 #22
Source File: OpenShiftEnvironmentFactoryTest.java    From che with Eclipse Public License 2.0 5 votes vote down vote up
@Test
public void addDeploymentsWhenRecipeContainsThem() throws Exception {
  // given
  PodTemplateSpec podTemplate =
      new PodTemplateSpecBuilder()
          .withNewMetadata()
          .withName("deployment-pod")
          .endMetadata()
          .withNewSpec()
          .endSpec()
          .build();
  Deployment deployment =
      new DeploymentBuilder()
          .withNewMetadata()
          .withName("deployment-test")
          .endMetadata()
          .withNewSpec()
          .withTemplate(podTemplate)
          .endSpec()
          .build();

  when(k8sRecipeParser.parse(any(InternalRecipe.class))).thenReturn(singletonList(deployment));

  // when
  final KubernetesEnvironment osEnv =
      osEnvFactory.doCreate(internalRecipe, emptyMap(), emptyList());

  // then
  assertEquals(osEnv.getDeploymentsCopy().size(), 1);
  assertEquals(osEnv.getDeploymentsCopy().get("deployment-test"), deployment);

  assertEquals(osEnv.getPodsData().size(), 1);
  assertEquals(
      osEnv.getPodsData().get("deployment-test").getMetadata(), podTemplate.getMetadata());
  assertEquals(osEnv.getPodsData().get("deployment-test").getSpec(), podTemplate.getSpec());
}
 
Example #23
Source File: KubeUtilTest.java    From enmasse with Apache License 2.0 5 votes vote down vote up
@Test
public void appliesContainerReadinessProbeSettingsToPodTemplate() {
    Container actualContainer = new ContainerBuilder()
            .withName("foo")
            .withReadinessProbe(new ProbeBuilder()
                    .withInitialDelaySeconds(1).build()).build();
    Container desiredContainer = new ContainerBuilder()
            .withName("foo")
            .withReadinessProbe(new ProbeBuilder()
                    .withInitialDelaySeconds(10).build()).build();
    PodTemplateSpec actual = doApplyContainers(actualContainer, desiredContainer);

    Container container = actual.getSpec().getContainers().get(0);
    assertThat(container.getReadinessProbe().getInitialDelaySeconds(), equalTo(10));
}
 
Example #24
Source File: DeploymentConfigEnricher.java    From jkube with Eclipse Public License 2.0 5 votes vote down vote up
private DeploymentConfigSpec getDeploymentConfigSpec(Integer replicas, Integer revisionHistoryLimit, LabelSelector selector, PodTemplateSpec podTemplateSpec, String strategyType) {
    DeploymentConfigSpecBuilder specBuilder = new DeploymentConfigSpecBuilder();
    if (replicas != null) {
        specBuilder.withReplicas(replicas);
    }
    if (revisionHistoryLimit != null) {
        specBuilder.withRevisionHistoryLimit(revisionHistoryLimit);
    }

    if (selector  != null) {
        Map<String, String> matchLabels = selector.getMatchLabels();
        if (matchLabels != null && !matchLabels.isEmpty()) {
            specBuilder.withSelector(matchLabels);
        }
    }
    if (podTemplateSpec != null) {
        specBuilder.withTemplate(podTemplateSpec);
        PodSpec podSpec = podTemplateSpec.getSpec();
        Objects.requireNonNull(podSpec, "No PodSpec for PodTemplate:" + podTemplateSpec);
        Objects.requireNonNull(podSpec, "No containers for PodTemplate.spec: " + podTemplateSpec);
    }
    DeploymentStrategy deploymentStrategy = getDeploymentStrategy(strategyType);
    if (deploymentStrategy != null) {
        specBuilder.withStrategy(deploymentStrategy);
    }

    if(enableAutomaticTrigger.equals(Boolean.TRUE)) {
        specBuilder.addNewTrigger().withType("ConfigChange").endTrigger();
    }

    return specBuilder.build();
}
 
Example #25
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 #26
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 #27
Source File: KubernetesHandler.java    From dekorate with Apache License 2.0 5 votes vote down vote up
/**
 * Creates a {@link PodTemplateSpec} for the {@link KubernetesConfig}.
 * @param appConfig   The sesssion.
 * @return          The pod template specification.
 */
public static PodTemplateSpec createPodTemplateSpec(KubernetesConfig appConfig, ImageConfiguration imageConfig) {
  return new PodTemplateSpecBuilder()
    .withSpec(createPodSpec(appConfig, imageConfig))
    .withNewMetadata()
    .withLabels(createLabels(appConfig))
    .endMetadata()
    .build();
}
 
Example #28
Source File: KubeUtilTest.java    From enmasse with Apache License 2.0 5 votes vote down vote up
@Test
public void appliesContainerOrderIgnored() {
    Container actualFooContainer = new ContainerBuilder()
            .withName("foo").build();
    Container actualBarContainer = new ContainerBuilder()
            .withName("bar").build();

    Map<String, Quantity> widgets = Collections.singletonMap("widgets", new QuantityBuilder().withAmount("10").build());
    ResourceRequirements resources = new ResourceRequirementsBuilder().withLimits(widgets).build();
    Container desiredFooContainer = new ContainerBuilder()
            .withName("foo")
            .withResources(resources).build();

    PodTemplateSpec actual1 = new PodTemplateSpecBuilder()
            .withNewSpec()
            .addToContainers(actualBarContainer, actualFooContainer)
            .endSpec()
            .build();

    PodTemplateSpec desired = new PodTemplateSpecBuilder()
            .withNewSpec()
            .addToContainers(desiredFooContainer)
            .endSpec()
            .build();

    KubeUtil.applyPodTemplate(actual1, desired);

    PodTemplateSpec actual = actual1;

    Container barContainer = actual.getSpec().getContainers().get(0);
    assertThat(barContainer.getName(), equalTo("bar"));
    assertThat(barContainer.getResources(), nullValue());

    Container fooContainer = actual.getSpec().getContainers().get(1);
    assertThat(fooContainer.getName(), equalTo("foo"));
    assertThat(fooContainer.getResources(), equalTo(resources));
}
 
Example #29
Source File: DockerimageComponentToWorkspaceApplierTest.java    From che with Eclipse Public License 2.0 5 votes vote down vote up
@Test
public void shouldProvisionSpecifiedEnvVars() throws Exception {
  // given
  ComponentImpl dockerimageComponent = new ComponentImpl();
  dockerimageComponent.setAlias("jdk");
  dockerimageComponent.setType(DOCKERIMAGE_COMPONENT_TYPE);
  dockerimageComponent.setImage("eclipse/ubuntu_jdk8:latest");
  dockerimageComponent.setMemoryLimit("1G");
  dockerimageComponent.setEnv(singletonList(new EnvImpl("envName", "envValue")));

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

  // then
  verify(k8sEnvProvisioner)
      .provision(
          eq(workspaceConfig),
          eq(KubernetesEnvironment.TYPE),
          objectsCaptor.capture(),
          machinesCaptor.capture());
  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();
  assertEquals(podTemplate.getSpec().getContainers().size(), 1);
  Container container = podTemplate.getSpec().getContainers().get(0);
  int env = container.getEnv().size();
  assertEquals(env, 1);
  EnvVar containerEnvVar = container.getEnv().get(0);
  assertEquals(containerEnvVar.getName(), "envName");
  assertEquals(containerEnvVar.getValue(), "envValue");
}
 
Example #30
Source File: KubeUtilTest.java    From enmasse with Apache License 2.0 5 votes vote down vote up
@Test
public void appliesContainerResourcesToPodTemplate() {
    Container actualContainer = new ContainerBuilder()
            .withName("foo").build();
    Map<String, Quantity> widgets = Collections.singletonMap("widgets", new QuantityBuilder().withAmount("10").build());
    ResourceRequirements resources = new ResourceRequirementsBuilder().withLimits(widgets).build();
    Container desiredContainer = new ContainerBuilder()
            .withName("foo")
            .withResources(resources).build();

    PodTemplateSpec actual = doApplyContainers(actualContainer, desiredContainer);

    Container container = actual.getSpec().getContainers().get(0);
    assertThat(container.getResources(), equalTo(resources));
}