Search in sources :

Example 16 with PodTemplateBuilder

use of org.csanchez.jenkins.plugins.kubernetes.PodTemplateBuilder in project kubernetes-plugin by jenkinsci.

the class PodTemplateBuilderTest method whenRuntimeClassNameIsSetDoNotSetDefaultNodeSelector.

@Test
public void whenRuntimeClassNameIsSetDoNotSetDefaultNodeSelector() {
    setupStubs();
    PodTemplate template = new PodTemplate();
    template.setYaml("spec:\n" + "  runtimeClassName: windows");
    Pod pod = new PodTemplateBuilder(template, slave).build();
    assertEquals("windows", pod.getSpec().getRuntimeClassName());
    assertThat(pod.getSpec().getNodeSelector(), anEmptyMap());
}
Also used : PodTemplateBuilder(org.csanchez.jenkins.plugins.kubernetes.PodTemplateBuilder) Pod(io.fabric8.kubernetes.api.model.Pod) Test(org.junit.Test)

Example 17 with PodTemplateBuilder

use of org.csanchez.jenkins.plugins.kubernetes.PodTemplateBuilder in project kubernetes-plugin by jenkinsci.

the class PodTemplateBuilderTest method yamlMergeVolumes.

@Test
public void yamlMergeVolumes() throws Exception {
    PodTemplate parent = new PodTemplate();
    parent.setYaml("apiVersion: v1\n" + "kind: Pod\n" + "metadata:\n" + "  labels:\n" + "    some-label: some-label-value\n" + "spec:\n" + "  volumes:\n" + "  - name: host-volume\n" + "    hostPath:\n" + "      path: /host/data\n");
    PodTemplate child = new PodTemplate();
    child.setYaml("spec:\n" + "  volumes:\n" + "  - name: host-volume2\n" + "    hostPath:\n" + "      path: /host/data2\n");
    child.setInheritFrom("parent");
    child.setYamlMergeStrategy(merge());
    setupStubs();
    PodTemplate result = combine(parent, child);
    Pod pod = new PodTemplateBuilder(result, slave).build();
    // inherit from parent
    assertEquals("some-label-value", pod.getMetadata().getLabels().get("some-label"));
    assertThat(pod.getSpec().getVolumes(), hasSize(3));
    Optional<Volume> hostVolume = pod.getSpec().getVolumes().stream().filter(v -> "host-volume".equals(v.getName())).findFirst();
    assertTrue(hostVolume.isPresent());
    // parent value
    assertThat(hostVolume.get().getHostPath().getPath(), equalTo("/host/data"));
    Optional<Volume> hostVolume2 = pod.getSpec().getVolumes().stream().filter(v -> "host-volume2".equals(v.getName())).findFirst();
    assertTrue(hostVolume2.isPresent());
    // child value
    assertThat(hostVolume2.get().getHostPath().getPath(), equalTo("/host/data2"));
}
Also used : Quantity(io.fabric8.kubernetes.api.model.Quantity) VolumeMount(io.fabric8.kubernetes.api.model.VolumeMount) PodSecurityContext(io.fabric8.kubernetes.api.model.PodSecurityContext) Arrays(java.util.Arrays) Issue(org.jvnet.hudson.test.Issue) ConfigMapVolume(org.csanchez.jenkins.plugins.kubernetes.volumes.ConfigMapVolume) YamlMergeStrategy(org.csanchez.jenkins.plugins.kubernetes.pod.yaml.YamlMergeStrategy) KeyValueEnvVar(org.csanchez.jenkins.plugins.kubernetes.model.KeyValueEnvVar) FlagRule(org.jvnet.hudson.test.FlagRule) DynamicPVCWorkspaceVolume(org.csanchez.jenkins.plugins.kubernetes.volumes.workspace.DynamicPVCWorkspaceVolume) ResourceRequirements(io.fabric8.kubernetes.api.model.ResourceRequirements) VolumeMountBuilder(io.fabric8.kubernetes.api.model.VolumeMountBuilder) Map(java.util.Map) Spy(org.mockito.Spy) JUnitParamsRunner(junitparams.JUnitParamsRunner) PodTemplateUtils(org.csanchez.jenkins.plugins.kubernetes.PodTemplateUtils) Jenkins(jenkins.model.Jenkins) WithoutJenkins(org.jvnet.hudson.test.WithoutJenkins) HostPathVolume(org.csanchez.jenkins.plugins.kubernetes.volumes.HostPathVolume) Logger(java.util.logging.Logger) Collectors(java.util.stream.Collectors) List(java.util.List) PodTemplateBuilder(org.csanchez.jenkins.plugins.kubernetes.PodTemplateBuilder) Optional(java.util.Optional) EmptyDirVolume(org.csanchez.jenkins.plugins.kubernetes.volumes.EmptyDirVolume) Parameters(junitparams.Parameters) EnvVar(io.fabric8.kubernetes.api.model.EnvVar) Container(io.fabric8.kubernetes.api.model.Container) Mock(org.mockito.Mock) RunWith(org.junit.runner.RunWith) Function(java.util.function.Function) ArrayList(java.util.ArrayList) Level(java.util.logging.Level) Merge(org.csanchez.jenkins.plugins.kubernetes.pod.yaml.Merge) LoggerRule(org.jvnet.hudson.test.LoggerRule) EmptyDirWorkspaceVolume(org.csanchez.jenkins.plugins.kubernetes.volumes.workspace.EmptyDirWorkspaceVolume) MockitoJUnit(org.mockito.junit.MockitoJUnit) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) TestCaseName(junitparams.naming.TestCaseName) Before(org.junit.Before) Volume(io.fabric8.kubernetes.api.model.Volume) PodVolume(org.csanchez.jenkins.plugins.kubernetes.volumes.PodVolume) Assert.assertNotNull(org.junit.Assert.assertNotNull) Matchers(org.hamcrest.Matchers) Assert.assertTrue(org.junit.Assert.assertTrue) IOUtils(org.apache.commons.compress.utils.IOUtils) Pod(io.fabric8.kubernetes.api.model.Pod) IOException(java.io.IOException) Test(org.junit.Test) Mockito(org.mockito.Mockito) TemplateEnvVar(org.csanchez.jenkins.plugins.kubernetes.model.TemplateEnvVar) Collectors.toList(java.util.stream.Collectors.toList) Assert.assertNull(org.junit.Assert.assertNull) Rule(org.junit.Rule) Matcher(org.hamcrest.Matcher) MockitoRule(org.mockito.junit.MockitoRule) JenkinsRule(org.jvnet.hudson.test.JenkinsRule) Collections(java.util.Collections) Assert.assertEquals(org.junit.Assert.assertEquals) PodTemplateBuilder(org.csanchez.jenkins.plugins.kubernetes.PodTemplateBuilder) Pod(io.fabric8.kubernetes.api.model.Pod) ConfigMapVolume(org.csanchez.jenkins.plugins.kubernetes.volumes.ConfigMapVolume) DynamicPVCWorkspaceVolume(org.csanchez.jenkins.plugins.kubernetes.volumes.workspace.DynamicPVCWorkspaceVolume) HostPathVolume(org.csanchez.jenkins.plugins.kubernetes.volumes.HostPathVolume) EmptyDirVolume(org.csanchez.jenkins.plugins.kubernetes.volumes.EmptyDirVolume) EmptyDirWorkspaceVolume(org.csanchez.jenkins.plugins.kubernetes.volumes.workspace.EmptyDirWorkspaceVolume) Volume(io.fabric8.kubernetes.api.model.Volume) PodVolume(org.csanchez.jenkins.plugins.kubernetes.volumes.PodVolume) Test(org.junit.Test)

Example 18 with PodTemplateBuilder

use of org.csanchez.jenkins.plugins.kubernetes.PodTemplateBuilder in project kubernetes-plugin by jenkinsci.

the class PodTemplateBuilderTest method testBuildFromTemplate.

@Test
@TestCaseName("{method}(directConnection={0})")
@Parameters({ "true", "false" })
public void testBuildFromTemplate(boolean directConnection) throws Exception {
    cloud.setDirectConnection(directConnection);
    PodTemplate template = new PodTemplate();
    template.setRunAsUser("1000");
    template.setRunAsGroup("1000");
    template.setSupplementalGroups("5001,5002");
    template.setHostNetwork(false);
    List<PodVolume> volumes = new ArrayList<PodVolume>();
    volumes.add(new HostPathVolume("/host/data", "/container/data"));
    volumes.add(new EmptyDirVolume("/empty/dir", false));
    template.setVolumes(volumes);
    List<ContainerTemplate> containers = new ArrayList<ContainerTemplate>();
    ContainerTemplate busyboxContainer = new ContainerTemplate("busybox", "busybox");
    busyboxContainer.setCommand("cat");
    busyboxContainer.setTtyEnabled(true);
    List<TemplateEnvVar> envVars = new ArrayList<TemplateEnvVar>();
    envVars.add(new KeyValueEnvVar("CONTAINER_ENV_VAR", "container-env-var-value"));
    busyboxContainer.setEnvVars(envVars);
    busyboxContainer.setRunAsUser("2000");
    busyboxContainer.setRunAsGroup("2000");
    containers.add(busyboxContainer);
    template.setContainers(containers);
    setupStubs();
    Pod pod = new PodTemplateBuilder(template, slave).build();
    pod.getMetadata().setLabels(Collections.singletonMap("some-label", "some-label-value"));
    validatePod(pod, false, directConnection);
    ArrayList<Long> supplementalGroups = new ArrayList<Long>();
    supplementalGroups.add(5001L);
    supplementalGroups.add(5002L);
    Map<String, Container> containersMap = toContainerMap(pod);
    PodSecurityContext securityContext = pod.getSpec().getSecurityContext();
    assertEquals(Long.valueOf(1000L), securityContext.getRunAsUser());
    assertEquals(Long.valueOf(1000L), securityContext.getRunAsGroup());
    assertEquals(supplementalGroups, securityContext.getSupplementalGroups());
    assertEquals(Long.valueOf(2000L), containersMap.get("busybox").getSecurityContext().getRunAsUser());
    assertEquals(Long.valueOf(2000L), containersMap.get("busybox").getSecurityContext().getRunAsGroup());
}
Also used : TemplateEnvVar(org.csanchez.jenkins.plugins.kubernetes.model.TemplateEnvVar) PodTemplateBuilder(org.csanchez.jenkins.plugins.kubernetes.PodTemplateBuilder) Pod(io.fabric8.kubernetes.api.model.Pod) PodSecurityContext(io.fabric8.kubernetes.api.model.PodSecurityContext) HostPathVolume(org.csanchez.jenkins.plugins.kubernetes.volumes.HostPathVolume) KeyValueEnvVar(org.csanchez.jenkins.plugins.kubernetes.model.KeyValueEnvVar) ArrayList(java.util.ArrayList) Container(io.fabric8.kubernetes.api.model.Container) EmptyDirVolume(org.csanchez.jenkins.plugins.kubernetes.volumes.EmptyDirVolume) PodVolume(org.csanchez.jenkins.plugins.kubernetes.volumes.PodVolume) Parameters(junitparams.Parameters) Test(org.junit.Test) TestCaseName(junitparams.naming.TestCaseName)

Example 19 with PodTemplateBuilder

use of org.csanchez.jenkins.plugins.kubernetes.PodTemplateBuilder in project kubernetes-plugin by jenkinsci.

the class PodTemplateBuilderTest method testOverridesFromYaml.

@Test
@TestCaseName("{method}(directConnection={0})")
@Parameters({ "true", "false" })
public void testOverridesFromYaml(boolean directConnection) throws Exception {
    cloud.setDirectConnection(directConnection);
    PodTemplate template = new PodTemplate();
    template.setYaml(loadYamlFile("pod-overrides.yaml"));
    setupStubs();
    Pod pod = new PodTemplateBuilder(template, slave).build();
    Map<String, Container> containers = toContainerMap(pod);
    assertEquals(1, containers.size());
    Container jnlp = containers.get("jnlp");
    assertThat("Wrong number of volume mounts: " + jnlp.getVolumeMounts(), jnlp.getVolumeMounts(), hasSize(1));
    PodTemplateUtilsTest.assertQuantity("2", jnlp.getResources().getLimits().get("cpu"));
    PodTemplateUtilsTest.assertQuantity("2Gi", jnlp.getResources().getLimits().get("memory"));
    PodTemplateUtilsTest.assertQuantity("200m", jnlp.getResources().getRequests().get("cpu"));
    PodTemplateUtilsTest.assertQuantity("256Mi", jnlp.getResources().getRequests().get("memory"));
    validateContainers(pod, slave, directConnection);
}
Also used : PodTemplateBuilder(org.csanchez.jenkins.plugins.kubernetes.PodTemplateBuilder) Container(io.fabric8.kubernetes.api.model.Container) Pod(io.fabric8.kubernetes.api.model.Pod) Parameters(junitparams.Parameters) Test(org.junit.Test) TestCaseName(junitparams.naming.TestCaseName)

Example 20 with PodTemplateBuilder

use of org.csanchez.jenkins.plugins.kubernetes.PodTemplateBuilder in project kubernetes-plugin by jenkinsci.

the class PodTemplateBuilderTest method yamlOverrideVolume.

@Test
public void yamlOverrideVolume() throws Exception {
    PodTemplate parent = new PodTemplate();
    parent.setYaml("apiVersion: v1\n" + "kind: Pod\n" + "metadata:\n" + "  labels:\n" + "    some-label: some-label-value\n" + "spec:\n" + "  containers:\n" + "  - name: jnlp\n" + "    volumeMounts:\n" + "    - name: host-volume\n" + "      mountPath: /etc/config\n" + "      subPath: mypath\n" + "  volumes:\n" + "  - name: host-volume\n" + "    hostPath:\n" + "      path: /host/data\n");
    PodTemplate child = new PodTemplate();
    child.setYaml("spec:\n" + "  volumes:\n" + "  - name: host-volume\n" + "    hostPath:\n" + "      path: /host/data2\n");
    child.setContainers(Collections.singletonList(new ContainerTemplate("jnlp", "image")));
    ConfigMapVolume cmVolume = new ConfigMapVolume("/etc/configmap", "my-configmap", false);
    cmVolume.setSubPath("subpath");
    child.setVolumes(Collections.singletonList(cmVolume));
    child.setInheritFrom("parent");
    child.setYamlMergeStrategy(merge());
    setupStubs();
    PodTemplate result = combine(parent, child);
    Pod pod = new PodTemplateBuilder(result, slave).build();
    // inherit from parent
    assertEquals("some-label-value", pod.getMetadata().getLabels().get("some-label"));
    Optional<Volume> maybeVolume = pod.getSpec().getVolumes().stream().filter(v -> "host-volume".equals(v.getName())).findFirst();
    assertTrue(maybeVolume.isPresent());
    // child value overrides parent value
    assertThat(maybeVolume.get().getHostPath().getPath(), equalTo("/host/data2"));
    assertThat(pod.getSpec().getContainers(), hasSize(1));
    Container container = pod.getSpec().getContainers().get(0);
    Optional<VolumeMount> maybeVolumeMount = container.getVolumeMounts().stream().filter(vm -> "host-volume".equals(vm.getName())).findFirst();
    assertTrue(maybeVolumeMount.isPresent());
    VolumeMount volumeMount = maybeVolumeMount.get();
    assertEquals("/etc/config", volumeMount.getMountPath());
    assertEquals("mypath", volumeMount.getSubPath());
    Optional<VolumeMount> maybeVolumeMountCm = container.getVolumeMounts().stream().filter(vm -> "/etc/configmap".equals(vm.getMountPath())).findFirst();
    assertTrue(maybeVolumeMountCm.isPresent());
    VolumeMount cmVolumeMount = maybeVolumeMountCm.get();
    assertEquals("subpath", cmVolumeMount.getSubPath());
}
Also used : Quantity(io.fabric8.kubernetes.api.model.Quantity) VolumeMount(io.fabric8.kubernetes.api.model.VolumeMount) PodSecurityContext(io.fabric8.kubernetes.api.model.PodSecurityContext) Arrays(java.util.Arrays) Issue(org.jvnet.hudson.test.Issue) ConfigMapVolume(org.csanchez.jenkins.plugins.kubernetes.volumes.ConfigMapVolume) YamlMergeStrategy(org.csanchez.jenkins.plugins.kubernetes.pod.yaml.YamlMergeStrategy) KeyValueEnvVar(org.csanchez.jenkins.plugins.kubernetes.model.KeyValueEnvVar) FlagRule(org.jvnet.hudson.test.FlagRule) DynamicPVCWorkspaceVolume(org.csanchez.jenkins.plugins.kubernetes.volumes.workspace.DynamicPVCWorkspaceVolume) ResourceRequirements(io.fabric8.kubernetes.api.model.ResourceRequirements) VolumeMountBuilder(io.fabric8.kubernetes.api.model.VolumeMountBuilder) Map(java.util.Map) Spy(org.mockito.Spy) JUnitParamsRunner(junitparams.JUnitParamsRunner) PodTemplateUtils(org.csanchez.jenkins.plugins.kubernetes.PodTemplateUtils) Jenkins(jenkins.model.Jenkins) WithoutJenkins(org.jvnet.hudson.test.WithoutJenkins) HostPathVolume(org.csanchez.jenkins.plugins.kubernetes.volumes.HostPathVolume) Logger(java.util.logging.Logger) Collectors(java.util.stream.Collectors) List(java.util.List) PodTemplateBuilder(org.csanchez.jenkins.plugins.kubernetes.PodTemplateBuilder) Optional(java.util.Optional) EmptyDirVolume(org.csanchez.jenkins.plugins.kubernetes.volumes.EmptyDirVolume) Parameters(junitparams.Parameters) EnvVar(io.fabric8.kubernetes.api.model.EnvVar) Container(io.fabric8.kubernetes.api.model.Container) Mock(org.mockito.Mock) RunWith(org.junit.runner.RunWith) Function(java.util.function.Function) ArrayList(java.util.ArrayList) Level(java.util.logging.Level) Merge(org.csanchez.jenkins.plugins.kubernetes.pod.yaml.Merge) LoggerRule(org.jvnet.hudson.test.LoggerRule) EmptyDirWorkspaceVolume(org.csanchez.jenkins.plugins.kubernetes.volumes.workspace.EmptyDirWorkspaceVolume) MockitoJUnit(org.mockito.junit.MockitoJUnit) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) TestCaseName(junitparams.naming.TestCaseName) Before(org.junit.Before) Volume(io.fabric8.kubernetes.api.model.Volume) PodVolume(org.csanchez.jenkins.plugins.kubernetes.volumes.PodVolume) Assert.assertNotNull(org.junit.Assert.assertNotNull) Matchers(org.hamcrest.Matchers) Assert.assertTrue(org.junit.Assert.assertTrue) IOUtils(org.apache.commons.compress.utils.IOUtils) Pod(io.fabric8.kubernetes.api.model.Pod) IOException(java.io.IOException) Test(org.junit.Test) Mockito(org.mockito.Mockito) TemplateEnvVar(org.csanchez.jenkins.plugins.kubernetes.model.TemplateEnvVar) Collectors.toList(java.util.stream.Collectors.toList) Assert.assertNull(org.junit.Assert.assertNull) Rule(org.junit.Rule) Matcher(org.hamcrest.Matcher) MockitoRule(org.mockito.junit.MockitoRule) JenkinsRule(org.jvnet.hudson.test.JenkinsRule) Collections(java.util.Collections) Assert.assertEquals(org.junit.Assert.assertEquals) ConfigMapVolume(org.csanchez.jenkins.plugins.kubernetes.volumes.ConfigMapVolume) PodTemplateBuilder(org.csanchez.jenkins.plugins.kubernetes.PodTemplateBuilder) Container(io.fabric8.kubernetes.api.model.Container) Pod(io.fabric8.kubernetes.api.model.Pod) ConfigMapVolume(org.csanchez.jenkins.plugins.kubernetes.volumes.ConfigMapVolume) DynamicPVCWorkspaceVolume(org.csanchez.jenkins.plugins.kubernetes.volumes.workspace.DynamicPVCWorkspaceVolume) HostPathVolume(org.csanchez.jenkins.plugins.kubernetes.volumes.HostPathVolume) EmptyDirVolume(org.csanchez.jenkins.plugins.kubernetes.volumes.EmptyDirVolume) EmptyDirWorkspaceVolume(org.csanchez.jenkins.plugins.kubernetes.volumes.workspace.EmptyDirWorkspaceVolume) Volume(io.fabric8.kubernetes.api.model.Volume) PodVolume(org.csanchez.jenkins.plugins.kubernetes.volumes.PodVolume) VolumeMount(io.fabric8.kubernetes.api.model.VolumeMount) Test(org.junit.Test)

Aggregations

Pod (io.fabric8.kubernetes.api.model.Pod)21 PodTemplateBuilder (org.csanchez.jenkins.plugins.kubernetes.PodTemplateBuilder)21 Test (org.junit.Test)21 Container (io.fabric8.kubernetes.api.model.Container)16 Parameters (junitparams.Parameters)13 TestCaseName (junitparams.naming.TestCaseName)13 Quantity (io.fabric8.kubernetes.api.model.Quantity)8 VolumeMount (io.fabric8.kubernetes.api.model.VolumeMount)8 VolumeMountBuilder (io.fabric8.kubernetes.api.model.VolumeMountBuilder)8 KeyValueEnvVar (org.csanchez.jenkins.plugins.kubernetes.model.KeyValueEnvVar)8 TemplateEnvVar (org.csanchez.jenkins.plugins.kubernetes.model.TemplateEnvVar)8 EnvVar (io.fabric8.kubernetes.api.model.EnvVar)7 PodSecurityContext (io.fabric8.kubernetes.api.model.PodSecurityContext)7 ResourceRequirements (io.fabric8.kubernetes.api.model.ResourceRequirements)7 ArrayList (java.util.ArrayList)7 EmptyDirVolume (org.csanchez.jenkins.plugins.kubernetes.volumes.EmptyDirVolume)7 HostPathVolume (org.csanchez.jenkins.plugins.kubernetes.volumes.HostPathVolume)7 PodVolume (org.csanchez.jenkins.plugins.kubernetes.volumes.PodVolume)7 DynamicPVCWorkspaceVolume (org.csanchez.jenkins.plugins.kubernetes.volumes.workspace.DynamicPVCWorkspaceVolume)7 EmptyDirWorkspaceVolume (org.csanchez.jenkins.plugins.kubernetes.volumes.workspace.EmptyDirWorkspaceVolume)7