org.csanchez.jenkins.plugins.kubernetes.volumes.PodVolume Java Examples

The following examples show how to use org.csanchez.jenkins.plugins.kubernetes.volumes.PodVolume. 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: KubernetesCloudTest.java    From kubernetes-plugin with Apache License 2.0 6 votes vote down vote up
@Test
public void testInheritance() {

    ContainerTemplate jnlp = new ContainerTemplate("jnlp", "jnlp:1");
    ContainerTemplate maven = new ContainerTemplate("maven", "maven:1");
    maven.setTtyEnabled(true);
    maven.setCommand("cat");

    PodVolume podVolume = new EmptyDirVolume("/some/path", true);
    PodTemplate parent = new PodTemplate();
    parent.setName("parent");
    parent.setLabel("parent");
    parent.setContainers(Arrays.asList(jnlp));
    parent.setVolumes(Arrays.asList(podVolume));

    ContainerTemplate maven2 = new ContainerTemplate("maven", "maven:2");
    PodTemplate withNewMavenVersion = new PodTemplate();
    withNewMavenVersion.setContainers(Arrays.asList(maven2));

    PodTemplate result = PodTemplateUtils.combine(parent, withNewMavenVersion);
}
 
Example #2
Source File: WithPodStep.java    From kubernetes-pipeline-plugin with Apache License 2.0 5 votes vote down vote up
@DataBoundConstructor
public WithPodStep(String name, List<ContainerTemplate> containers, Map<String, String> envVars, List<PodVolume> volumes, String serviceAccount, String nodeSelector, String workingDir, Map<String, String> labels) {
    this.name = name;
    this.containers = containers != null ? containers : Collections.emptyList();
    this.envVars = envVars != null ? asPodEnvars(envVars) : Collections.emptyList();
    this.volumes = volumes != null ? volumes : Collections.emptyList();
    this.serviceAccount = serviceAccount;
    this.nodeSelector = nodeSelector;
    this.workingDir = Strings.isNullOrEmpty(workingDir) ? ContainerTemplate.DEFAULT_WORKING_DIR : workingDir;
    this.labels = labels != null ? labels : Collections.emptyMap();
}
 
Example #3
Source File: VolumeCasCTest.java    From kubernetes-plugin with Apache License 2.0 5 votes vote down vote up
@Override
void _verify(PodVolume volume) {
    assertThat(volume, instanceOf(NfsVolume.class));
    NfsVolume d = (NfsVolume) volume;
    assertEquals("serverAddress", d.getServerAddress());
    assertEquals("/path",d.getServerPath());
}
 
Example #4
Source File: VolumeCasCTest.java    From kubernetes-plugin with Apache License 2.0 5 votes vote down vote up
@Override
protected void assertConfiguredAsExpected(RestartableJenkinsRule r, String configContent) {
    KubernetesCloud cloud = r.j.jenkins.clouds.get(KubernetesCloud.class);
    assertNotNull(cloud);
    List<PodTemplate> templates = cloud.getTemplates();
    assertNotNull(templates);
    assertEquals(1, templates.size());
    PodTemplate podTemplate = templates.get(0);
    List<PodVolume> volumes = podTemplate.getVolumes();
    assertEquals(1, volumes.size());
    strategy.verify(volumes.get(0));

}
 
Example #5
Source File: KubernetesSlaveTest.java    From kubernetes-plugin with Apache License 2.0 5 votes vote down vote up
@WithoutJenkins
@Test
public void testGetSlaveName() {
    List<? extends PodVolume> volumes = Collections.emptyList();
    List<ContainerTemplate> containers = Collections.emptyList();

    assertRegex(KubernetesSlave.getSlaveName(new PodTemplate("image", volumes)), "^jenkins-agent-[0-9a-z]{5}$");
    assertRegex(KubernetesSlave.getSlaveName(new PodTemplate("", volumes, containers)), "^jenkins-agent-[0-9a-z]{5}$");
    assertRegex(KubernetesSlave.getSlaveName(new PodTemplate("a name", volumes, containers)),
            ("^a-name-[0-9a-z]{5}$"));
    assertRegex(KubernetesSlave.getSlaveName(new PodTemplate("an_other_name", volumes, containers)),
            ("^an-other-name-[0-9a-z]{5}$"));
    assertRegex(KubernetesSlave.getSlaveName(new PodTemplate("whatever...", volumes, containers)), ("jenkins-agent-[0-9a-z]{5}"));
}
 
Example #6
Source File: PodTemplate.java    From kubernetes-plugin with Apache License 2.0 5 votes vote down vote up
@Nonnull
public List<PodVolume> getVolumes() {
    if (volumes == null) {
        return Collections.emptyList();
    }
    return volumes;
}
 
Example #7
Source File: PodTemplate.java    From kubernetes-plugin with Apache License 2.0 5 votes vote down vote up
@DataBoundSetter
public void setVolumes(@Nonnull List<PodVolume> items) {
    synchronized (this.volumes) {
        this.volumes.clear();
        this.volumes.addAll(items);
    }
}
 
Example #8
Source File: PodTemplate.java    From kubernetes-plugin with Apache License 2.0 5 votes vote down vote up
@Deprecated
PodTemplate(String name, String image, List<? extends PodVolume> volumes) {
    this(name, volumes, Collections.emptyList());
    if (image != null) {
        getContainers().add(new ContainerTemplate(name, image));
    }
}
 
Example #9
Source File: PodTemplateBuilder.java    From kubernetes-plugin with Apache License 2.0 5 votes vote down vote up
private List<VolumeMount> getContainerVolumeMounts(Collection<VolumeMount> volumeMounts, String workingDir) {
    List<VolumeMount> containerMounts = new ArrayList<>(volumeMounts);
    if (!Strings.isNullOrEmpty(workingDir) && !PodVolume.volumeMountExists(workingDir, volumeMounts)) {
        containerMounts.add(getDefaultVolumeMount(workingDir));
    }
    return containerMounts;
}
 
Example #10
Source File: KubernetesFacade.java    From kubernetes-pipeline-plugin with Apache License 2.0 5 votes vote down vote up
public static boolean hasWorkspaceMount(String workspace, List<PodVolume> volumes) {
        for (PodVolume volume : volumes) {
            if (volume.getMountPath().equals(workspace)) {
                return true;
            }
        }
    return false;
}
 
Example #11
Source File: PodTemplate.java    From kubernetes-plugin with Apache License 2.0 4 votes vote down vote up
@Deprecated
public PodTemplate(String image, List<? extends PodVolume> volumes) {
    this(null, image, volumes);
}
 
Example #12
Source File: PodTemplate.java    From kubernetes-plugin with Apache License 2.0 4 votes vote down vote up
@Restricted(NoExternalUse.class) // testing only
PodTemplate(String name, List<? extends PodVolume> volumes, List<? extends ContainerTemplate> containers) {
    this.name = name;
    this.volumes.addAll(volumes);
    this.containers.addAll(containers);
}
 
Example #13
Source File: PodTemplateStep.java    From kubernetes-plugin with Apache License 2.0 4 votes vote down vote up
@DataBoundSetter
public void setVolumes(List<PodVolume> volumes) {
    this.volumes = volumes;
}
 
Example #14
Source File: PodTemplateStep.java    From kubernetes-plugin with Apache License 2.0 4 votes vote down vote up
public List<PodVolume> getVolumes() {
    return volumes;
}
 
Example #15
Source File: PodTemplateUtils.java    From kubernetes-plugin with Apache License 2.0 4 votes vote down vote up
/**
 * Combines a {@link PodTemplate} with its parent.
 * @param parent        The parent container template (nullable).
 * @param template      The actual container template
 * @return              The combined container template.
 */
public static PodTemplate combine(PodTemplate parent, PodTemplate template) {
    Preconditions.checkNotNull(template, "Pod template should not be null");
    if (parent == null) {
        return template;
    }

    LOGGER.log(Level.FINEST, "Combining pod templates, parent: {0}", parent);
    LOGGER.log(Level.FINEST, "Combining pod templates, template: {0}", template);

    String name = template.getName();
    String label = template.getLabel();
    String nodeSelector = Strings.isNullOrEmpty(template.getNodeSelector()) ? parent.getNodeSelector() : template.getNodeSelector();
    String serviceAccount = Strings.isNullOrEmpty(template.getServiceAccount()) ? parent.getServiceAccount() : template.getServiceAccount();
    Node.Mode nodeUsageMode = template.getNodeUsageMode() == null ? parent.getNodeUsageMode() : template.getNodeUsageMode();

    Set<PodAnnotation> podAnnotations = new LinkedHashSet<>();
    podAnnotations.addAll(template.getAnnotations());
    podAnnotations.addAll(parent.getAnnotations());

    Set<PodImagePullSecret> imagePullSecrets = new LinkedHashSet<>();
    imagePullSecrets.addAll(parent.getImagePullSecrets());
    imagePullSecrets.addAll(template.getImagePullSecrets());

    Map<String, ContainerTemplate> combinedContainers = new HashMap<>();
    Map<String, PodVolume> combinedVolumes = new HashMap<>();

    //Containers
    Map<String, ContainerTemplate> parentContainers = parent.getContainers().stream().collect(toMap(c -> c.getName(), c -> c));
    combinedContainers.putAll(parentContainers);
    combinedContainers.putAll(template.getContainers().stream().collect(toMap(c -> c.getName(), c -> combine(parentContainers.get(c.getName()), c))));

    //Volumes
    Map<String, PodVolume> parentVolumes = parent.getVolumes().stream().collect(toMap(v -> v.getMountPath(), v -> v));
    combinedVolumes.putAll(parentVolumes);
    combinedVolumes.putAll(template.getVolumes().stream().collect(toMap(v -> v.getMountPath(), v -> v)));

    WorkspaceVolume workspaceVolume = WorkspaceVolume.merge(parent.getWorkspaceVolume(), template.getWorkspaceVolume());

    //Tool location node properties
    PodTemplateToolLocation toolLocationNodeProperties = parent.getNodeProperties();
    toolLocationNodeProperties.addAll(template.getNodeProperties());

    PodTemplate podTemplate = new PodTemplate();
    podTemplate.setName(name);
    podTemplate.setNamespace(!Strings.isNullOrEmpty(template.getNamespace()) ? template.getNamespace() : parent.getNamespace());
    podTemplate.setLabel(label);
    podTemplate.setNodeSelector(nodeSelector);
    podTemplate.setServiceAccount(serviceAccount);
    podTemplate.setEnvVars(combineEnvVars(parent, template));
    podTemplate.setContainers(new ArrayList<>(combinedContainers.values()));
    podTemplate.setWorkspaceVolume(workspaceVolume);
    podTemplate.setVolumes(new ArrayList<>(combinedVolumes.values()));
    podTemplate.setImagePullSecrets(new ArrayList<>(imagePullSecrets));
    podTemplate.setAnnotations(new ArrayList<>(podAnnotations));
    podTemplate.setNodeProperties(toolLocationNodeProperties);
    podTemplate.setNodeUsageMode(nodeUsageMode);
    podTemplate.setYamlMergeStrategy(template.getYamlMergeStrategy());
    podTemplate.setInheritFrom(!Strings.isNullOrEmpty(template.getInheritFrom()) ?
                               template.getInheritFrom() : parent.getInheritFrom());

    podTemplate.setInstanceCap(template.getInstanceCap() != Integer.MAX_VALUE ?
                               template.getInstanceCap() : parent.getInstanceCap());

    podTemplate.setSlaveConnectTimeout(template.getSlaveConnectTimeout() != PodTemplate.DEFAULT_SLAVE_JENKINS_CONNECTION_TIMEOUT ?
                                       template.getSlaveConnectTimeout() : parent.getSlaveConnectTimeout());

    podTemplate.setIdleMinutes(template.getIdleMinutes() != 0 ?
                               template.getIdleMinutes() : parent.getIdleMinutes());

    podTemplate.setActiveDeadlineSeconds(template.getActiveDeadlineSeconds() != 0 ?
                                         template.getActiveDeadlineSeconds() : parent.getActiveDeadlineSeconds());


    podTemplate.setServiceAccount(!Strings.isNullOrEmpty(template.getServiceAccount()) ?
                                  template.getServiceAccount() : parent.getServiceAccount());

    podTemplate.setPodRetention(template.getPodRetention());
    podTemplate.setShowRawYaml(template.isShowRawYamlSet() ? template.isShowRawYaml() : parent.isShowRawYaml());

    podTemplate.setRunAsUser(template.getRunAsUser() != null ? template.getRunAsUser() : parent.getRunAsUser());
    podTemplate.setRunAsGroup(template.getRunAsGroup() != null ? template.getRunAsGroup() : parent.getRunAsGroup());

    podTemplate.setSupplementalGroups(template.getSupplementalGroups() != null ? template.getSupplementalGroups() : parent.getSupplementalGroups());

    podTemplate.setHostNetwork(template.isHostNetworkSet() ? template.isHostNetwork() : parent.isHostNetwork());

    List<String> yamls = new ArrayList<>(parent.getYamls());
    yamls.addAll(template.getYamls());
    podTemplate.setYamls(yamls);

    LOGGER.log(Level.FINEST, "Pod templates combined: {0}", podTemplate);
    return podTemplate;
}
 
Example #16
Source File: VolumeCasCTest.java    From kubernetes-plugin with Apache License 2.0 4 votes vote down vote up
void verify(PodVolume volume) {
    _verify(volume);
    assertEquals("/mnt/path", volume.getMountPath());
}
 
Example #17
Source File: VolumeCasCTest.java    From kubernetes-plugin with Apache License 2.0 4 votes vote down vote up
@Override
void _verify(PodVolume volume) {
    assertThat(volume, instanceOf(EmptyDirVolume.class));
    EmptyDirVolume d = (EmptyDirVolume) volume;
    assertEquals(memory, d.getMemory());
}
 
Example #18
Source File: VolumeCasCTest.java    From kubernetes-plugin with Apache License 2.0 4 votes vote down vote up
@Override
void _verify(PodVolume volume) {
    assertThat(volume, instanceOf(ConfigMapVolume.class));
    ConfigMapVolume d = (ConfigMapVolume) volume;
    assertEquals("my-configmap", d.getConfigMapName());
}
 
Example #19
Source File: VolumeCasCTest.java    From kubernetes-plugin with Apache License 2.0 4 votes vote down vote up
@Override
void _verify(PodVolume volume) {
    assertThat(volume, instanceOf(HostPathVolume.class));
    HostPathVolume d = (HostPathVolume) volume;
    assertEquals("/host/path", d.getHostPath());
}
 
Example #20
Source File: WithPodStep.java    From kubernetes-pipeline-plugin with Apache License 2.0 4 votes vote down vote up
public List<PodVolume> getVolumes() {
    return volumes;
}
 
Example #21
Source File: VolumeCasCTest.java    From kubernetes-plugin with Apache License 2.0 4 votes vote down vote up
@Override
void _verify(PodVolume volume) {
    assertThat(volume, instanceOf(PersistentVolumeClaim.class));
    PersistentVolumeClaim d = (PersistentVolumeClaim) volume;
    assertEquals("my-claim", d.getClaimName());
}
 
Example #22
Source File: KubernetesCloudTest.java    From configuration-as-code-plugin with MIT License 4 votes vote down vote up
@Test
@ConfiguredWithReadme("kubernetes/README.md")
public void configure_kubernetes_cloud() throws Exception {
    final KubernetesCloud cloud = j.jenkins.clouds.get(KubernetesCloud.class);
    assertNotNull(cloud);
    assertEquals("advanced-k8s-config", cloud.name);
    assertEquals("https://avanced-k8s-config:443", cloud.getServerUrl());
    assertEquals("serverCertificate", cloud.getServerCertificate());
    assertTrue(cloud.isSkipTlsVerify());
    assertEquals("default", cloud.getNamespace());
    assertEquals("http://jenkins/", cloud.getJenkinsUrl());
    assertEquals("advanced-k8s-credentials", cloud.getCredentialsId());
    assertEquals("jenkinsTunnel", cloud.getJenkinsTunnel());
    assertEquals(42, cloud.getContainerCap());
    assertEquals(5, cloud.getRetentionTimeout());
    assertEquals(10, cloud.getConnectTimeout());
    assertEquals(20, cloud.getReadTimeout());
    assertEquals("64", cloud.getMaxRequestsPerHostStr());

    final List<PodTemplate> templates = cloud.getTemplates();
    assertEquals(2, templates.size());
    final PodTemplate template = templates.get(0);
    assertEquals("test", template.getName());
    assertEquals("serviceAccount", template.getServiceAccount());
    assertEquals(1234, template.getInstanceCap());
    assertEquals("label", template.getLabel());

    final List<PodVolume> volumes = template.getVolumes();
    assertEquals(1, volumes.size());
    final PodVolume volume = volumes.get(0);
    assertTrue(volume instanceof HostPathVolume);
    assertEquals("mountPath", volume.getMountPath());
    assertEquals("hostPath", ((HostPathVolume)volume).getHostPath());

    final List<TemplateEnvVar> envVars = template.getEnvVars();
    assertEquals(1, envVars.size());
    final KeyValueEnvVar envVar = (KeyValueEnvVar) envVars.get(0);
    assertEquals("FOO", envVar.getKey());
    assertEquals("BAR", envVar.getValue());

    final PodTemplate template1 = templates.get(1);
    assertEquals("k8s-slave", template1.getName());
    assertEquals("default", template1.getNamespace());
    assertEquals(Mode.EXCLUSIVE, template1.getNodeUsageMode());
}
 
Example #23
Source File: PodTemplateBuilderTest.java    From kubernetes-plugin with Apache License 2.0 4 votes vote down vote up
@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).withSlave(slave).build();
    pod.getMetadata().setLabels(ImmutableMap.of("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());
}
 
Example #24
Source File: VolumeCasCTest.java    From kubernetes-plugin with Apache License 2.0 votes vote down vote up
abstract void _verify(PodVolume volume);