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

The following examples show how to use io.fabric8.kubernetes.api.model.PodTemplateSpecBuilder. 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: 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 #2
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 #3
Source File: InitContainerHandler.java    From jkube with Eclipse Public License 2.0 6 votes vote down vote up
public void appendInitContainer(PodTemplateSpecBuilder builder, Container initContainer) {
    String name = initContainer.getName();
    Container existing = getInitContainer(builder, name);
    if (existing != null) {
        if (existing.equals(initContainer)) {
            log.warn("Trying to add init-container %s a second time. Ignoring ....", name);
            return;
        } else {
            throw new IllegalArgumentException(
                String.format("PodSpec %s already contains a different init container with name %s but can not add a second one with the same name. " +
                              "Please choose a different name for the init container",
                              builder.build().getMetadata().getName(), name));
        }
    }

    ensureSpec(builder);
    builder.editSpec().addToInitContainers(initContainer).endSpec();
}
 
Example #4
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 #5
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 #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: ImageEnricher.java    From jkube with Eclipse Public License 2.0 6 votes vote down vote up
private void updateContainers(KubernetesListBuilder builder) {
    builder.accept(new TypedVisitor<PodTemplateSpecBuilder>() {
        @Override
        public void visit(PodTemplateSpecBuilder templateBuilder) {
            PodTemplateSpecFluent.SpecNested<PodTemplateSpecBuilder> podSpec =
                templateBuilder.buildSpec() == null ? templateBuilder.withNewSpec() : templateBuilder.editSpec();

            List<Container> containers = podSpec.buildContainers();
            if (containers == null) {
                containers = new ArrayList<>();
            }
            mergeImageConfigurationWithContainerSpec(containers);
            podSpec.withContainers(containers).endSpec();
        }
    });
}
 
Example #8
Source File: InitContainerHandlerTest.java    From jkube with Eclipse Public License 2.0 5 votes vote down vote up
@Test
public void existingDifferent() {
    try {
        PodTemplateSpecBuilder builder = getPodTemplateBuilder("blub", "foo/bla");
        assertTrue(handler.hasInitContainer(builder, "blub"));
        Container initContainer = createInitContainer("blub", "foo/blub");
        handler.appendInitContainer(builder, initContainer);
        fail();
    } catch (IllegalArgumentException exp) {
        assertTrue(exp.getMessage().contains("blub"));
    }
}
 
Example #9
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 #10
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 #11
Source File: OpenshiftHandler.java    From dekorate with Apache License 2.0 5 votes vote down vote up
/**
 * Creates a {@link PodTemplateSpec} for the {@link OpenshiftConfig}.
 * @param config   The sesssion.
 * @return          The pod template specification.
 */
public PodTemplateSpec createPodTemplateSpec(OpenshiftConfig config, ImageConfiguration imageConfig, Map<String, String> labels) {
  return new PodTemplateSpecBuilder()
    .withSpec(createPodSpec(config, imageConfig))
    .withNewMetadata()
    .withLabels(labels)
    .endMetadata()
    .build();
}
 
Example #12
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 #13
Source File: InitContainerHandlerTest.java    From jkube with Eclipse Public License 2.0 5 votes vote down vote up
private void verifyBuilder(PodTemplateSpecBuilder builder, List<Container> initContainers) {
    PodTemplateSpec spec = builder.build();
    List<Container> initContainersInSpec = spec.getSpec().getInitContainers();
    if (initContainersInSpec.size() == 0) {
        assertNull(initContainers);
    } else {
        assertEquals(initContainersInSpec.size(), initContainers.size());
        for (int i = 0; i < initContainers.size(); i++) {
            assertEquals(initContainersInSpec.get(i), initContainers.get(i));
        }
    }
}
 
Example #14
Source File: InitContainerHandlerTest.java    From jkube with Eclipse Public License 2.0 5 votes vote down vote up
@Test
public void existingSame() {
    new Expectations() {{
        log.warn(anyString, withSubstring("blub"));
    }};

    PodTemplateSpecBuilder builder = getPodTemplateBuilder("blub", "foo/blub");
    assertTrue(handler.hasInitContainer(builder, "blub"));
    Container initContainer = createInitContainer("blub", "foo/blub");
    handler.appendInitContainer(builder, initContainer);
    assertTrue(handler.hasInitContainer(builder, "blub"));
    verifyBuilder(builder, Arrays.asList(initContainer));
}
 
Example #15
Source File: InitContainerHandlerTest.java    From jkube with Eclipse Public License 2.0 5 votes vote down vote up
@Test
public void removeOne() {
    PodTemplateSpecBuilder builder = getPodTemplateBuilder("bla", "foo/bla", "blub", "foo/blub");
    assertTrue(handler.hasInitContainer(builder, "bla"));
    assertTrue(handler.hasInitContainer(builder, "blub"));
    handler.removeInitContainer(builder, "bla");
    assertFalse(handler.hasInitContainer(builder, "bla"));
    assertTrue(handler.hasInitContainer(builder, "blub"));
    verifyBuilder(builder, Arrays.asList(createInitContainer("blub", "foo/blub")));
}
 
Example #16
Source File: InitContainerHandlerTest.java    From jkube with Eclipse Public License 2.0 5 votes vote down vote up
@Test
public void removeAll() {
    PodTemplateSpecBuilder builder = getPodTemplateBuilder("bla", "foo/bla");
    assertTrue(handler.hasInitContainer(builder, "bla"));
    handler.removeInitContainer(builder, "bla");
    assertFalse(handler.hasInitContainer(builder, "bla"));
    verifyBuilder(builder, null);
}
 
Example #17
Source File: InitContainerHandlerTest.java    From jkube with Eclipse Public License 2.0 5 votes vote down vote up
@Test
public void append() {
    PodTemplateSpecBuilder builder = getPodTemplateBuilder("bla", "foo/bla");
    assertFalse(handler.hasInitContainer(builder, "blub"));
    Container initContainer = createInitContainer("blub", "foo/blub");
    handler.appendInitContainer(builder, initContainer);
    assertTrue(handler.hasInitContainer(builder, "blub"));
    verifyBuilder(builder, Arrays.asList(createInitContainer("bla", "foo/bla"), initContainer));
}
 
Example #18
Source File: InitContainerHandlerTest.java    From jkube with Eclipse Public License 2.0 5 votes vote down vote up
@Test
public void simple() {
    PodTemplateSpecBuilder builder = getPodTemplateBuilder();
    assertFalse(handler.hasInitContainer(builder, "blub"));
    Container initContainer = createInitContainer("blub", "foo/blub");
    handler.appendInitContainer(builder, initContainer);
    assertTrue(handler.hasInitContainer(builder, "blub"));
    verifyBuilder(builder, Arrays.asList(initContainer));
}
 
Example #19
Source File: KubernetesEnvironmentFactoryTest.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 k8sEnv =
      k8sEnvFactory.doCreate(internalRecipe, emptyMap(), emptyList());

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

  assertEquals(k8sEnv.getPodsData().size(), 1);
  assertEquals(
      k8sEnv.getPodsData().get("deployment-test").getMetadata(), podTemplate.getMetadata());
  assertEquals(k8sEnv.getPodsData().get("deployment-test").getSpec(), podTemplate.getSpec());
}
 
Example #20
Source File: InitContainerHandler.java    From jkube with Eclipse Public License 2.0 5 votes vote down vote up
public void removeInitContainer(PodTemplateSpecBuilder builder, String initContainerName) {
    Container initContainer = getInitContainer(builder, initContainerName);
    if (initContainer != null) {
        List<Container> initContainers = builder.buildSpec().getInitContainers();
        initContainers.remove(initContainer);
        builder.editSpec().withInitContainers(initContainers).endSpec();
    }
}
 
Example #21
Source File: InitContainerHandler.java    From jkube with Eclipse Public License 2.0 5 votes vote down vote up
public Container getInitContainer(PodTemplateSpecBuilder builder, String name) {
    if (builder.hasSpec()) {
        List<Container> initContainerList = builder.buildSpec().getInitContainers();
        for(Container initContainer : initContainerList) {
            if(initContainer.getName().equals(name)) {
                return initContainer;
            }
        }
    }
    return null;
}
 
Example #22
Source File: InitContainerHandlerTest.java    From jkube with Eclipse Public License 2.0 4 votes vote down vote up
private PodTemplateSpecBuilder getPodTemplateBuilder(String ... definitions) {
    PodTemplateSpecBuilder ret = new PodTemplateSpecBuilder();
    ret.withNewMetadata().withName("test-pod-templateSpec").endMetadata().withNewSpec().withInitContainers(getInitContainerList(definitions)).endSpec();
    return ret;
}
 
Example #23
Source File: OpenShiftEnvironmentFactoryTest.java    From che with Eclipse Public License 2.0 4 votes vote down vote up
@Test
public void shouldMergeDeploymentAndPodIntoOneDeployment() 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();
  Pod pod =
      new PodBuilder()
          .withNewMetadata()
          .withName("bare-pod")
          .endMetadata()
          .withNewSpec()
          .endSpec()
          .build();
  when(k8sRecipeParser.parse(any(InternalRecipe.class))).thenReturn(asList(deployment, pod));
  Deployment merged = createEmptyDeployment("merged");
  when(podMerger.merge(any())).thenReturn(merged);

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

  // then
  verify(podMerger).merge(asList(new PodData(pod), new PodData(deployment)));
  assertEquals(k8sEnv.getPodsData().size(), 1);

  assertTrue(k8sEnv.getPodsCopy().isEmpty());

  assertEquals(k8sEnv.getDeploymentsCopy().size(), 1);
  assertEquals(k8sEnv.getDeploymentsCopy().get("merged"), merged);
}
 
Example #24
Source File: KubernetesEnvironmentFactoryTest.java    From che with Eclipse Public License 2.0 4 votes vote down vote up
@Test
public void shouldMergeDeploymentAndPodIntoOneDeployment() 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();
  Pod pod =
      new PodBuilder()
          .withNewMetadata()
          .withName("bare-pod")
          .endMetadata()
          .withNewSpec()
          .endSpec()
          .build();
  when(k8sRecipeParser.parse(any(InternalRecipe.class))).thenReturn(asList(deployment, pod));
  Deployment merged = createEmptyDeployment("merged");
  when(podMerger.merge(any())).thenReturn(merged);

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

  // then
  verify(podMerger).merge(asList(new PodData(pod), new PodData(deployment)));
  assertEquals(k8sEnv.getPodsData().size(), 1);

  assertTrue(k8sEnv.getPodsCopy().isEmpty());

  assertEquals(k8sEnv.getDeploymentsCopy().size(), 1);
  assertEquals(k8sEnv.getDeploymentsCopy().get("merged"), merged);
}
 
Example #25
Source File: PlanUtils.java    From enmasse with Apache License 2.0 4 votes vote down vote up
public static PodTemplateSpec createTemplateSpec(Map<String, String> labels, String nodeAffinityValue, String tolerationKey) {
    PodTemplateSpecBuilder builder = new PodTemplateSpecBuilder();
    if (labels != null) {
        builder.editOrNewMetadata()
                .withLabels(labels)
                .endMetadata();
    }

    if (nodeAffinityValue != null) {
        builder.editOrNewSpec()
                .editOrNewAffinity()
                .editOrNewNodeAffinity()
                .addToPreferredDuringSchedulingIgnoredDuringExecution(new PreferredSchedulingTermBuilder()
                        .withWeight(1)
                        .withNewPreference()
                        .addToMatchExpressions(new NodeSelectorRequirementBuilder()
                                .withKey("node-label-key")
                                .withOperator("In")
                                .addToValues(nodeAffinityValue)
                                .build())
                        .endPreference()
                        .build())
                .endNodeAffinity()
                .endAffinity()
                .endSpec();
    }

    if (tolerationKey != null) {
        builder.editOrNewSpec()
                .addNewToleration()
                .withKey(tolerationKey)
                .withOperator("Exists")
                .withEffect("NoSchedule")
                .endToleration()
                .endSpec();
    }

    /* TODO: Not always supported by cluster
    if (priorityClassName != null) {
        builder.editOrNewSpec()
                .withPriorityClassName(priorityClassName)
                .endSpec();
    }*/

    return builder.build();
}
 
Example #26
Source File: MetadataVisitor.java    From jkube with Eclipse Public License 2.0 4 votes vote down vote up
@Override
protected ObjectMeta getOrCreateMetadata(PodTemplateSpecBuilder item) {
    return addEmptyLabelsAndAnnotations(item::hasMetadata, item::withNewMetadata, item::editMetadata, item::buildMetadata)
            .endMetadata().buildMetadata();
}
 
Example #27
Source File: PodTemplateHandler.java    From jkube with Eclipse Public License 2.0 4 votes vote down vote up
public PodTemplateSpec getPodTemplate(ResourceConfig config, List<ImageConfiguration> images)  {
    return new PodTemplateSpecBuilder()
        .withMetadata(createPodMetaData(config))
        .withSpec(createPodSpec(config, images))
        .build();
}
 
Example #28
Source File: InitContainerHandler.java    From jkube with Eclipse Public License 2.0 4 votes vote down vote up
private void ensureSpec(PodTemplateSpecBuilder obj) {
    if (obj.buildSpec() == null) {
        obj.withNewSpec().endSpec();
    }
}
 
Example #29
Source File: InitContainerHandler.java    From jkube with Eclipse Public License 2.0 4 votes vote down vote up
public boolean hasInitContainer(PodTemplateSpecBuilder builder, String name) {
    return getInitContainer(builder, name) != null;
}