Search in sources :

Example 1 with ContainerTemplate

use of org.csanchez.jenkins.plugins.kubernetes.ContainerTemplate 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 ContainerTemplate

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

the class PodTemplateStepExecution method start.

@Override
public boolean start() throws Exception {
    KubernetesCloud cloud = resolveCloud();
    Run<?, ?> run = getContext().get(Run.class);
    if (cloud.isUsageRestricted()) {
        checkAccess(run, cloud);
    }
    PodTemplateContext podTemplateContext = getContext().get(PodTemplateContext.class);
    String parentTemplates = podTemplateContext != null ? podTemplateContext.getName() : null;
    String label = step.getLabel();
    if (label == null) {
        label = labelify(run.getExternalizableId());
    }
    // Let's generate a random name based on the user specified to make sure that we don't have
    // issues with concurrent builds, or messing with pre-existing configuration
    String randString = RandomStringUtils.random(5, "bcdfghjklmnpqrstvwxz0123456789");
    String stepName = step.getName();
    if (stepName == null) {
        stepName = label;
    }
    String name = String.format(NAME_FORMAT, stepName, randString);
    String namespace = checkNamespace(cloud, podTemplateContext);
    newTemplate = new PodTemplate();
    newTemplate.setName(name);
    newTemplate.setNamespace(namespace);
    if (step.getInheritFrom() == null) {
        newTemplate.setInheritFrom(PodTemplateUtils.emptyToNull(parentTemplates));
    } else {
        newTemplate.setInheritFrom(PodTemplateUtils.emptyToNull(step.getInheritFrom()));
    }
    newTemplate.setInstanceCap(step.getInstanceCap());
    newTemplate.setIdleMinutes(step.getIdleMinutes());
    newTemplate.setSlaveConnectTimeout(step.getSlaveConnectTimeout());
    newTemplate.setLabel(label);
    newTemplate.setEnvVars(step.getEnvVars());
    newTemplate.setVolumes(step.getVolumes());
    if (step.getWorkspaceVolume() != null) {
        newTemplate.setWorkspaceVolume(step.getWorkspaceVolume());
    }
    newTemplate.setContainers(step.getContainers());
    newTemplate.setNodeSelector(step.getNodeSelector());
    newTemplate.setNodeUsageMode(step.getNodeUsageMode());
    newTemplate.setServiceAccount(step.getServiceAccount());
    newTemplate.setSchedulerName(step.getSchedulerName());
    newTemplate.setRunAsUser(step.getRunAsUser());
    newTemplate.setRunAsGroup(step.getRunAsGroup());
    if (step.getHostNetwork() != null) {
        newTemplate.setHostNetwork(step.getHostNetwork());
    }
    newTemplate.setAnnotations(step.getAnnotations());
    TaskListener listener = getContext().get(TaskListener.class);
    newTemplate.setListener(listener);
    newTemplate.setYamlMergeStrategy(step.getYamlMergeStrategy());
    if (run != null) {
        String url = cloud.getJenkinsUrlOrNull();
        if (url != null) {
            newTemplate.getAnnotations().add(new PodAnnotation("buildUrl", url + run.getUrl()));
            newTemplate.getAnnotations().add(new PodAnnotation("runUrl", run.getUrl()));
        }
    }
    newTemplate.setImagePullSecrets(step.getImagePullSecrets().stream().map(x -> new PodImagePullSecret(x)).collect(toList()));
    newTemplate.setYaml(step.getYaml());
    if (step.isShowRawYamlSet()) {
        newTemplate.setShowRawYaml(step.isShowRawYaml());
    }
    newTemplate.setPodRetention(step.getPodRetention());
    if (step.getActiveDeadlineSeconds() != 0) {
        newTemplate.setActiveDeadlineSeconds(step.getActiveDeadlineSeconds());
    }
    for (ContainerTemplate container : newTemplate.getContainers()) {
        if (!PodTemplateUtils.validateContainerName(container.getName())) {
            throw new AbortException(Messages.RFC1123_error(container.getName()));
        }
    }
    Collection<String> errors = PodTemplateUtils.validateYamlContainerNames(newTemplate.getYamls());
    if (!errors.isEmpty()) {
        throw new AbortException(Messages.RFC1123_error(String.join(", ", errors)));
    }
    if (VERBOSE) {
        listener.getLogger().println("Registering template with id=" + newTemplate.getId() + ",label=" + newTemplate.getLabel());
    }
    cloud.addDynamicTemplate(newTemplate);
    BodyInvoker invoker = getContext().newBodyInvoker().withContexts(step, new PodTemplateContext(namespace, name)).withCallback(new PodTemplateCallback(newTemplate));
    if (step.getLabel() == null) {
        invoker.withContext(EnvironmentExpander.merge(getContext().get(EnvironmentExpander.class), EnvironmentExpander.constant(Collections.singletonMap("POD_LABEL", label))));
    }
    invoker.start();
    return false;
}
Also used : PodAnnotation(org.csanchez.jenkins.plugins.kubernetes.PodAnnotation) PodImagePullSecret(org.csanchez.jenkins.plugins.kubernetes.PodImagePullSecret) KubernetesCloud(org.csanchez.jenkins.plugins.kubernetes.KubernetesCloud) ContainerTemplate(org.csanchez.jenkins.plugins.kubernetes.ContainerTemplate) TaskListener(hudson.model.TaskListener) BodyInvoker(org.jenkinsci.plugins.workflow.steps.BodyInvoker) PodTemplate(org.csanchez.jenkins.plugins.kubernetes.PodTemplate) AbortException(hudson.AbortException)

Example 3 with ContainerTemplate

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

the class KubernetesPipelineTest method podTemplateWithMultipleLabels.

@Test
public void podTemplateWithMultipleLabels() throws Exception {
    PodTemplate pt = new PodTemplate();
    pt.setName("podTemplate");
    pt.setLabel("label1 label2");
    ContainerTemplate jnlp = new ContainerTemplate("jnlp", "jenkins/inbound-agent:4.3-4-alpine");
    pt.setContainers(Collections.singletonList(jnlp));
    cloud.addTemplate(pt);
    SemaphoreStep.waitForStart("pod/1", b);
    Map<String, String> labels = getLabels(cloud, this, name);
    labels.put("jenkins/label", "label1_label2");
    KubernetesSlave node = r.jenkins.getNodes().stream().filter(KubernetesSlave.class::isInstance).map(KubernetesSlave.class::cast).findAny().get();
    assertTrue(node.getAssignedLabels().containsAll(Label.parse("label1 label2")));
    PodList pods = cloud.connect().pods().withLabels(labels).list();
    assertThat("Expected one pod with labels " + labels + " but got: " + pods.getItems().stream().map(Pod::getMetadata).map(ObjectMeta::getName).collect(Collectors.toList()), pods.getItems(), hasSize(1));
    SemaphoreStep.success("pod/1", null);
    r.assertBuildStatusSuccess(r.waitForCompletion(b));
}
Also used : ContainerTemplate(org.csanchez.jenkins.plugins.kubernetes.ContainerTemplate) PodList(io.fabric8.kubernetes.api.model.PodList) Pod(io.fabric8.kubernetes.api.model.Pod) KubernetesSlave(org.csanchez.jenkins.plugins.kubernetes.KubernetesSlave) PodTemplate(org.csanchez.jenkins.plugins.kubernetes.PodTemplate) Test(org.junit.Test)

Example 4 with ContainerTemplate

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

the class AbstractKubernetesPipelineTest method buildBusyboxTemplate.

private PodTemplate buildBusyboxTemplate(String label) {
    // Create a busybox template
    PodTemplate podTemplate = new PodTemplate();
    podTemplate.setLabel(label);
    podTemplate.setTerminationGracePeriodSeconds(0L);
    ContainerTemplate containerTemplate = new ContainerTemplate("busybox", "busybox", "cat", "");
    containerTemplate.setTtyEnabled(true);
    podTemplate.getContainers().add(containerTemplate);
    setEnvVariables(podTemplate);
    return podTemplate;
}
Also used : ContainerTemplate(org.csanchez.jenkins.plugins.kubernetes.ContainerTemplate) PodTemplate(org.csanchez.jenkins.plugins.kubernetes.PodTemplate)

Example 5 with ContainerTemplate

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

the class RestartPipelineTest method buildBusyboxTemplate.

private PodTemplate buildBusyboxTemplate(String label) {
    // Create a busybox template
    PodTemplate podTemplate = new PodTemplate();
    podTemplate.setLabel(label);
    podTemplate.setTerminationGracePeriodSeconds(0L);
    ContainerTemplate containerTemplate = new ContainerTemplate("busybox", "busybox", "cat", "");
    containerTemplate.setTtyEnabled(true);
    podTemplate.getContainers().add(containerTemplate);
    setEnvVariables(podTemplate);
    return podTemplate;
}
Also used : ContainerTemplate(org.csanchez.jenkins.plugins.kubernetes.ContainerTemplate) PodTemplate(org.csanchez.jenkins.plugins.kubernetes.PodTemplate)

Aggregations

ContainerTemplate (org.csanchez.jenkins.plugins.kubernetes.ContainerTemplate)6 PodTemplate (org.csanchez.jenkins.plugins.kubernetes.PodTemplate)6 KubernetesCloud (org.csanchez.jenkins.plugins.kubernetes.KubernetesCloud)2 Test (org.junit.Test)2 AbortException (hudson.AbortException)1 TaskListener (hudson.model.TaskListener)1 Pod (io.fabric8.kubernetes.api.model.Pod)1 PodList (io.fabric8.kubernetes.api.model.PodList)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1 ContainerLivenessProbe (org.csanchez.jenkins.plugins.kubernetes.ContainerLivenessProbe)1 KubernetesSlave (org.csanchez.jenkins.plugins.kubernetes.KubernetesSlave)1 PodAnnotation (org.csanchez.jenkins.plugins.kubernetes.PodAnnotation)1 PodImagePullSecret (org.csanchez.jenkins.plugins.kubernetes.PodImagePullSecret)1 Merge (org.csanchez.jenkins.plugins.kubernetes.pod.yaml.Merge)1 Overrides (org.csanchez.jenkins.plugins.kubernetes.pod.yaml.Overrides)1 DynamicPVCWorkspaceVolume (org.csanchez.jenkins.plugins.kubernetes.volumes.workspace.DynamicPVCWorkspaceVolume)1 WorkspaceVolume (org.csanchez.jenkins.plugins.kubernetes.volumes.workspace.WorkspaceVolume)1 WorkflowJob (org.jenkinsci.plugins.workflow.job.WorkflowJob)1 WorkflowRun (org.jenkinsci.plugins.workflow.job.WorkflowRun)1 BodyInvoker (org.jenkinsci.plugins.workflow.steps.BodyInvoker)1