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());
}
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;
}
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));
}
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;
}
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;
}
Aggregations