Search in sources :

Example 1 with WorkspaceVolume

use of org.csanchez.jenkins.plugins.kubernetes.volumes.workspace.WorkspaceVolume in project kubernetes-plugin by jenkinsci.

the class CasCTest method assertConfiguredAsExpected.

@Override
protected void assertConfiguredAsExpected(RestartableJenkinsRule r, String configContent) {
    List<KubernetesCloud> all = r.j.jenkins.clouds.getAll(KubernetesCloud.class);
    assertThat(all, hasSize(1));
    KubernetesCloud cloud = all.get(0);
    assertNotNull(cloud);
    assertEquals(10, cloud.getContainerCap());
    assertEquals("http://jenkinshost:8080/jenkins/", cloud.getJenkinsUrl());
    assertEquals(32, cloud.getMaxRequestsPerHost());
    assertEquals("kubernetes", cloud.name);
    List<PodTemplate> templates = cloud.getTemplates();
    assertNotNull(templates);
    assertEquals(3, templates.size());
    PodTemplate podTemplate = templates.get(0);
    assertFalse(podTemplate.isHostNetwork());
    assertEquals("java", podTemplate.getLabel());
    assertEquals("default-java", podTemplate.getName());
    assertEquals(10, podTemplate.getInstanceCap());
    assertEquals(123, podTemplate.getSlaveConnectTimeout());
    assertEquals(5, podTemplate.getIdleMinutes());
    assertEquals(66, podTemplate.getActiveDeadlineSeconds());
    assertThat(podTemplate.getYamlMergeStrategy(), isA(Overrides.class));
    podTemplate = templates.get(1);
    assertFalse(podTemplate.isHostNetwork());
    assertEquals("dynamic-pvc", podTemplate.getLabel());
    assertEquals("dynamic-pvc", podTemplate.getName());
    assertThat(podTemplate.getYamlMergeStrategy(), isA(Overrides.class));
    WorkspaceVolume workspaceVolume = podTemplate.getWorkspaceVolume();
    assertNotNull(workspaceVolume);
    assertThat(workspaceVolume, isA(DynamicPVCWorkspaceVolume.class));
    DynamicPVCWorkspaceVolume dynamicPVCVolume = (DynamicPVCWorkspaceVolume) workspaceVolume;
    assertEquals("ReadWriteOnce", dynamicPVCVolume.getAccessModes());
    assertEquals("1", dynamicPVCVolume.getRequestsSize());
    assertEquals("hostpath", dynamicPVCVolume.getStorageClassName());
    podTemplate = templates.get(2);
    assertFalse(podTemplate.isHostNetwork());
    assertEquals("test", podTemplate.getLabel());
    assertEquals("test", podTemplate.getName());
    assertThat(podTemplate.getYamlMergeStrategy(), isA(Merge.class));
    List<ContainerTemplate> containers = podTemplate.getContainers();
    assertNotNull(containers);
    assertEquals(1, containers.size());
    ContainerTemplate container = containers.get(0);
    assertEquals("cat", container.getArgs());
    assertEquals("/bin/sh -c", container.getCommand());
    assertEquals("maven:3.6.3-jdk-8", container.getImage());
    ContainerLivenessProbe livenessProbe = container.getLivenessProbe();
    assertEquals(1, livenessProbe.getFailureThreshold());
    assertEquals(2, livenessProbe.getInitialDelaySeconds());
    assertEquals(3, livenessProbe.getPeriodSeconds());
    assertEquals(4, livenessProbe.getSuccessThreshold());
    assertEquals(5, livenessProbe.getTimeoutSeconds());
    assertEquals("maven", container.getName());
    assertTrue(container.isTtyEnabled());
    assertEquals("/src", container.getWorkingDir());
}
Also used : DynamicPVCWorkspaceVolume(org.csanchez.jenkins.plugins.kubernetes.volumes.workspace.DynamicPVCWorkspaceVolume) ContainerTemplate(org.csanchez.jenkins.plugins.kubernetes.ContainerTemplate) Merge(org.csanchez.jenkins.plugins.kubernetes.pod.yaml.Merge) WorkspaceVolume(org.csanchez.jenkins.plugins.kubernetes.volumes.workspace.WorkspaceVolume) DynamicPVCWorkspaceVolume(org.csanchez.jenkins.plugins.kubernetes.volumes.workspace.DynamicPVCWorkspaceVolume) KubernetesCloud(org.csanchez.jenkins.plugins.kubernetes.KubernetesCloud) Overrides(org.csanchez.jenkins.plugins.kubernetes.pod.yaml.Overrides) ContainerLivenessProbe(org.csanchez.jenkins.plugins.kubernetes.ContainerLivenessProbe) PodTemplate(org.csanchez.jenkins.plugins.kubernetes.PodTemplate)

Example 2 with WorkspaceVolume

use of org.csanchez.jenkins.plugins.kubernetes.volumes.workspace.WorkspaceVolume in project kubernetes-plugin by jenkinsci.

the class PodTemplateUtils method combine.

/**
 * 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) {
    if (template == null) {
        throw new IllegalArgumentException("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 = isNullOrEmpty(template.getNodeSelector()) ? parent.getNodeSelector() : template.getNodeSelector();
    String serviceAccount = isNullOrEmpty(template.getServiceAccount()) ? parent.getServiceAccount() : template.getServiceAccount();
    String schedulerName = isNullOrEmpty(template.getSchedulerName()) ? parent.getSchedulerName() : template.getSchedulerName();
    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
    List<NodeProperty<?>> nodeProperties = new ArrayList<>(parent.getNodeProperties());
    nodeProperties.addAll(template.getNodeProperties());
    PodTemplate podTemplate = new PodTemplate(template.getId());
    podTemplate.setName(name);
    podTemplate.setNamespace(!isNullOrEmpty(template.getNamespace()) ? template.getNamespace() : parent.getNamespace());
    podTemplate.setLabel(label);
    podTemplate.setNodeSelector(nodeSelector);
    podTemplate.setServiceAccount(serviceAccount);
    podTemplate.setSchedulerName(schedulerName);
    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(nodeProperties);
    podTemplate.setNodeUsageMode(nodeUsageMode);
    podTemplate.setYamlMergeStrategy(template.getYamlMergeStrategy());
    podTemplate.setInheritFrom(!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(!isNullOrEmpty(template.getServiceAccount()) ? template.getServiceAccount() : parent.getServiceAccount());
    podTemplate.setSchedulerName(!isNullOrEmpty(template.getSchedulerName()) ? template.getSchedulerName() : parent.getSchedulerName());
    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());
    if (template.isHostNetworkSet()) {
        podTemplate.setHostNetwork(template.isHostNetwork());
    } else if (parent.isHostNetworkSet()) {
        podTemplate.setHostNetwork(parent.isHostNetwork());
    }
    List<String> yamls = new ArrayList<>(parent.getYamls());
    yamls.addAll(template.getYamls());
    podTemplate.setYamls(yamls);
    podTemplate.setListener(template.getListener());
    LOGGER.log(Level.FINEST, "Pod templates combined: {0}", podTemplate);
    return podTemplate;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) Quantity(io.fabric8.kubernetes.api.model.Quantity) VolumeMount(io.fabric8.kubernetes.api.model.VolumeMount) StringUtils(org.apache.commons.lang.StringUtils) Label(hudson.model.Label) EnvFromSource(io.fabric8.kubernetes.api.model.EnvFromSource) SpecNested(io.fabric8.kubernetes.api.model.PodFluent.SpecNested) NodeProperty(hudson.slaves.NodeProperty) Matcher(java.util.regex.Matcher) ResourceRequirements(io.fabric8.kubernetes.api.model.ResourceRequirements) ByteArrayInputStream(java.io.ByteArrayInputStream) Collectors.toMap(java.util.stream.Collectors.toMap) PodBuilder(io.fabric8.kubernetes.api.model.PodBuilder) Map(java.util.Map) NonNull(edu.umd.cs.findbugs.annotations.NonNull) ContainerBuilder(io.fabric8.kubernetes.api.model.ContainerBuilder) DefaultKubernetesClient(io.fabric8.kubernetes.client.DefaultKubernetesClient) KubernetesClientException(io.fabric8.kubernetes.client.KubernetesClientException) Collection(java.util.Collection) Set(java.util.Set) Logger(java.util.logging.Logger) Collectors(java.util.stream.Collectors) BinaryOperator(java.util.function.BinaryOperator) Node(hudson.model.Node) Objects(java.util.Objects) List(java.util.List) Stream(java.util.stream.Stream) ObjectMeta(io.fabric8.kubernetes.api.model.ObjectMeta) DEFAULT_WORKING_DIR(org.csanchez.jenkins.plugins.kubernetes.ContainerTemplate.DEFAULT_WORKING_DIR) Optional(java.util.Optional) Util.replaceMacro(hudson.Util.replaceMacro) Pattern(java.util.regex.Pattern) SuppressFBWarnings(edu.umd.cs.findbugs.annotations.SuppressFBWarnings) EnvVar(io.fabric8.kubernetes.api.model.EnvVar) Container(io.fabric8.kubernetes.api.model.Container) HashMap(java.util.HashMap) WorkspaceVolume(org.csanchez.jenkins.plugins.kubernetes.volumes.workspace.WorkspaceVolume) Function(java.util.function.Function) LocalObjectReference(io.fabric8.kubernetes.api.model.LocalObjectReference) ArrayList(java.util.ArrayList) Level(java.util.logging.Level) LinkedHashMap(java.util.LinkedHashMap) Serialization(io.fabric8.kubernetes.client.utils.Serialization) PodSpec(io.fabric8.kubernetes.api.model.PodSpec) LinkedList(java.util.LinkedList) LinkedHashSet(java.util.LinkedHashSet) Volume(io.fabric8.kubernetes.api.model.Volume) Util(hudson.Util) PodVolume(org.csanchez.jenkins.plugins.kubernetes.volumes.PodVolume) UTF_8(java.nio.charset.StandardCharsets.UTF_8) Pod(io.fabric8.kubernetes.api.model.Pod) IOException(java.io.IOException) Toleration(io.fabric8.kubernetes.api.model.Toleration) MetadataNested(io.fabric8.kubernetes.api.model.PodFluent.MetadataNested) TemplateEnvVar(org.csanchez.jenkins.plugins.kubernetes.model.TemplateEnvVar) Collectors.toList(java.util.stream.Collectors.toList) Nullable(edu.umd.cs.findbugs.annotations.Nullable) KubernetesClient(io.fabric8.kubernetes.client.KubernetesClient) CheckForNull(edu.umd.cs.findbugs.annotations.CheckForNull) Collections(java.util.Collections) InputStream(java.io.InputStream) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Node(hudson.model.Node) ArrayList(java.util.ArrayList) NodeProperty(hudson.slaves.NodeProperty) WorkspaceVolume(org.csanchez.jenkins.plugins.kubernetes.volumes.workspace.WorkspaceVolume) PodVolume(org.csanchez.jenkins.plugins.kubernetes.volumes.PodVolume)

Example 3 with WorkspaceVolume

use of org.csanchez.jenkins.plugins.kubernetes.volumes.workspace.WorkspaceVolume in project kubernetes-plugin by jenkinsci.

the class PodTemplateUtils method combine.

/**
 * Combines a Pod with its parent.
 * @param parent        The parent Pod (nullable).
 * @param template      The child Pod
 */
public static Pod combine(Pod parent, Pod template) {
    if (template == null) {
        throw new IllegalArgumentException("Pod template should not be null");
    }
    if (parent == null) {
        return template;
    }
    LOGGER.finest(() -> "Combining pods, parent: " + Serialization.asYaml(parent) + " template: " + Serialization.asYaml(template));
    Map<String, String> nodeSelector = mergeMaps(parent.getSpec().getNodeSelector(), template.getSpec().getNodeSelector());
    String serviceAccount = isNullOrEmpty(template.getSpec().getServiceAccount()) ? parent.getSpec().getServiceAccount() : template.getSpec().getServiceAccount();
    String serviceAccountName = isNullOrEmpty(template.getSpec().getServiceAccountName()) ? parent.getSpec().getServiceAccountName() : template.getSpec().getServiceAccountName();
    String schedulerName = isNullOrEmpty(template.getSpec().getSchedulerName()) ? parent.getSpec().getSchedulerName() : template.getSpec().getSchedulerName();
    Boolean hostNetwork = template.getSpec().getHostNetwork() != null ? template.getSpec().getHostNetwork() : parent.getSpec().getHostNetwork();
    Map<String, String> podAnnotations = mergeMaps(parent.getMetadata().getAnnotations(), template.getMetadata().getAnnotations());
    Map<String, String> podLabels = mergeMaps(parent.getMetadata().getLabels(), template.getMetadata().getLabels());
    Set<LocalObjectReference> imagePullSecrets = new LinkedHashSet<>();
    imagePullSecrets.addAll(parent.getSpec().getImagePullSecrets());
    imagePullSecrets.addAll(template.getSpec().getImagePullSecrets());
    // Containers
    List<Container> combinedContainers = combineContainers(parent.getSpec().getContainers(), template.getSpec().getContainers());
    // Init containers
    List<Container> combinedInitContainers = combineContainers(parent.getSpec().getInitContainers(), template.getSpec().getInitContainers());
    // Volumes
    List<Volume> combinedVolumes = combineVolumes(parent.getSpec().getVolumes(), template.getSpec().getVolumes());
    // Tolerations
    List<Toleration> combinedTolerations = new LinkedList<>();
    Optional.ofNullable(parent.getSpec().getTolerations()).ifPresent(combinedTolerations::addAll);
    Optional.ofNullable(template.getSpec().getTolerations()).ifPresent(combinedTolerations::addAll);
    // WorkspaceVolume workspaceVolume = template.isCustomWorkspaceVolumeEnabled() && template.getWorkspaceVolume() != null ? template.getWorkspaceVolume() : parent.getWorkspaceVolume();
    // Tool location node properties
    // List<ToolLocationNodeProperty> toolLocationNodeProperties = new ArrayList<>();
    // toolLocationNodeProperties.addAll(parent.getNodeProperties());
    // toolLocationNodeProperties.addAll(template.getNodeProperties());
    MetadataNested<PodBuilder> metadataBuilder = // 
    new PodBuilder(parent).withNewMetadataLike(parent.getMetadata()).withAnnotations(podAnnotations).withLabels(podLabels);
    if (!isNullOrEmpty(template.getMetadata().getName())) {
        metadataBuilder.withName(template.getMetadata().getName());
    }
    if (!isNullOrEmpty(template.getMetadata().getNamespace())) {
        metadataBuilder.withNamespace(template.getMetadata().getNamespace());
    }
    SpecNested<PodBuilder> specBuilder = // 
    metadataBuilder.endMetadata().withNewSpecLike(// 
    parent.getSpec()).withNodeSelector(// 
    nodeSelector).withServiceAccount(// 
    serviceAccount).withServiceAccountName(// 
    serviceAccountName).withSchedulerName(schedulerName).withHostNetwork(// 
    hostNetwork).withContainers(// 
    combinedContainers).withInitContainers(// 
    combinedInitContainers).withVolumes(// 
    combinedVolumes).withTolerations(// 
    combinedTolerations).withImagePullSecrets(new ArrayList<>(imagePullSecrets));
    // Security context
    if (template.getSpec().getSecurityContext() != null || parent.getSpec().getSecurityContext() != null) {
        specBuilder.editOrNewSecurityContext().withRunAsUser(template.getSpec().getSecurityContext() != null && template.getSpec().getSecurityContext().getRunAsUser() != null ? template.getSpec().getSecurityContext().getRunAsUser() : (parent.getSpec().getSecurityContext() != null && parent.getSpec().getSecurityContext().getRunAsUser() != null ? parent.getSpec().getSecurityContext().getRunAsUser() : null)).withRunAsGroup(template.getSpec().getSecurityContext() != null && template.getSpec().getSecurityContext().getRunAsGroup() != null ? template.getSpec().getSecurityContext().getRunAsGroup() : (parent.getSpec().getSecurityContext() != null && parent.getSpec().getSecurityContext().getRunAsGroup() != null ? parent.getSpec().getSecurityContext().getRunAsGroup() : null)).endSecurityContext();
    }
    // podTemplate.setLabel(label);
    // podTemplate.setEnvVars(combineEnvVars(parent, template));
    // podTemplate.setWorkspaceVolume(workspaceVolume);
    // podTemplate.setNodeProperties(toolLocationNodeProperties);
    // podTemplate.setNodeUsageMode(nodeUsageMode);
    // podTemplate.setYaml(template.getYaml() == null ? parent.getYaml() : template.getYaml());
    Pod pod = specBuilder.endSpec().build();
    LOGGER.finest(() -> "Pods combined: " + Serialization.asYaml(pod));
    return pod;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) Pod(io.fabric8.kubernetes.api.model.Pod) PodBuilder(io.fabric8.kubernetes.api.model.PodBuilder) LinkedList(java.util.LinkedList) Container(io.fabric8.kubernetes.api.model.Container) WorkspaceVolume(org.csanchez.jenkins.plugins.kubernetes.volumes.workspace.WorkspaceVolume) Volume(io.fabric8.kubernetes.api.model.Volume) PodVolume(org.csanchez.jenkins.plugins.kubernetes.volumes.PodVolume) LocalObjectReference(io.fabric8.kubernetes.api.model.LocalObjectReference) Toleration(io.fabric8.kubernetes.api.model.Toleration)

Aggregations

WorkspaceVolume (org.csanchez.jenkins.plugins.kubernetes.volumes.workspace.WorkspaceVolume)3 Container (io.fabric8.kubernetes.api.model.Container)2 LocalObjectReference (io.fabric8.kubernetes.api.model.LocalObjectReference)2 Pod (io.fabric8.kubernetes.api.model.Pod)2 PodBuilder (io.fabric8.kubernetes.api.model.PodBuilder)2 Toleration (io.fabric8.kubernetes.api.model.Toleration)2 Volume (io.fabric8.kubernetes.api.model.Volume)2 LinkedHashSet (java.util.LinkedHashSet)2 LinkedList (java.util.LinkedList)2 PodVolume (org.csanchez.jenkins.plugins.kubernetes.volumes.PodVolume)2 CheckForNull (edu.umd.cs.findbugs.annotations.CheckForNull)1 NonNull (edu.umd.cs.findbugs.annotations.NonNull)1 Nullable (edu.umd.cs.findbugs.annotations.Nullable)1 SuppressFBWarnings (edu.umd.cs.findbugs.annotations.SuppressFBWarnings)1 Util (hudson.Util)1 Util.replaceMacro (hudson.Util.replaceMacro)1 Label (hudson.model.Label)1 Node (hudson.model.Node)1 NodeProperty (hudson.slaves.NodeProperty)1 ContainerBuilder (io.fabric8.kubernetes.api.model.ContainerBuilder)1