org.csanchez.jenkins.plugins.kubernetes.KubernetesCloud Java Examples

The following examples show how to use org.csanchez.jenkins.plugins.kubernetes.KubernetesCloud. 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: AbstractStepExecution.java    From kubernetes-pipeline-plugin with Apache License 2.0 6 votes vote down vote up
/**
 * Obtains a {@link KubernetesClient} either from the configured {@link Cloud} or a default instance.
 * @return
 * @throws AbortException
 */
protected KubernetesClient getKubernetesClient() throws AbortException {

    Cloud cloud = Jenkins.getInstance().getCloud(getStep().getCloud());
    if (cloud == null) {
        LOGGER.warning("Cloud does not exist: [" + getStep().getCloud() + "]. Falling back to default KubernetesClient.");
    } else if (!(cloud instanceof KubernetesCloud)) {
        LOGGER.warning("Cloud is not a Kubernetes cloud: [" + getStep().getCloud() + "]. Falling back to default KubernetesClient.");
    } else {
        KubernetesCloud kubernetesCloud = (KubernetesCloud) cloud;
        try {
            String json = Serialization.asJson(kubernetesCloud.connect().getConfiguration());
            return DefaultKubernetesClient.fromConfig(json);
        } catch (Throwable t) {
            LOGGER.warning("Could not connect to cloud: [" + getStep().getCloud() + "]. Falling back to default KubernetesClient.");
        }
    }
    return new DefaultKubernetesClient();
}
 
Example #2
Source File: PodTemplateStepExecution.java    From kubernetes-plugin with Apache License 2.0 6 votes vote down vote up
@Override
/**
 * Remove the template after step is done
 */
protected void finished(StepContext context) throws Exception {
    Cloud cloud = Jenkins.get().getCloud(cloudName);
    if (cloud == null) {
        LOGGER.log(Level.WARNING, "Cloud {0} no longer exists, cannot delete pod template {1}",
                new Object[] { cloudName, podTemplate.getName() });
        return;
    }
    if (cloud instanceof KubernetesCloud) {
        LOGGER.log(Level.INFO, "Removing pod template {1} from cloud {0}",
                new Object[] { cloud.name, podTemplate.getName() });
        KubernetesCloud kubernetesCloud = (KubernetesCloud) cloud;
        kubernetesCloud.removeDynamicTemplate(podTemplate);
    } else {
        LOGGER.log(Level.WARNING, "Cloud is not a KubernetesCloud: {0} {1}",
                new String[] { cloud.name, cloud.getClass().getName() });
    }
}
 
Example #3
Source File: Default.java    From kubernetes-plugin with Apache License 2.0 5 votes vote down vote up
@Override
public boolean shouldDeletePod(KubernetesCloud cloud, Pod pod) {
    PodRetention parent = cloud.getPodRetention();
    if (!(parent instanceof Default)) {
        return parent.shouldDeletePod(cloud, pod);
    }
    return true;
}
 
Example #4
Source File: Default.java    From kubernetes-plugin with Apache License 2.0 5 votes vote down vote up
@Override
@SuppressWarnings("rawtypes")
public boolean filter(Object context, Descriptor descriptor) {
    if (context instanceof KubernetesCloud.DescriptorImpl && descriptor instanceof DescriptorImpl) {
        return false;
    }
    return true;
}
 
Example #5
Source File: WorkspaceVolumeCasCTest.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);
    strategy.verify(podTemplate.getWorkspaceVolume());

}
 
Example #6
Source File: OnFailure.java    From kubernetes-plugin with Apache License 2.0 5 votes vote down vote up
@Override
public boolean shouldDeletePod(KubernetesCloud cloud, Pod pod) {
    if (pod == null || pod.getStatus() == null) {
        return false;
    }
    boolean hasErrors = pod.getStatus().getPhase().toLowerCase().matches("(failed|unknown)");
    return !hasErrors;
}
 
Example #7
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 #8
Source File: PodTemplateStepExecution.java    From kubernetes-plugin with Apache License 2.0 5 votes vote down vote up
/**
 * Check if the current Job is permitted to use the cloud.
 *
 * @param run
 * @param kubernetesCloud
 * @throws AbortException
 *             in case the Job has not been authorized to use the
 *             kubernetesCloud
 */
private void checkAccess(Run<?, ?> run, KubernetesCloud kubernetesCloud) throws AbortException {
    Job<?, ?> job = run.getParent(); // Return the associated Job for this Build
    ItemGroup<?> parent = job.getParent(); // Get the Parent of the Job (which might be a Folder)

    Set<String> allowedClouds = new HashSet<>();
    KubernetesFolderProperty.collectAllowedClouds(allowedClouds, parent);
    if (!allowedClouds.contains(kubernetesCloud.name)) {
        throw new AbortException(String.format("Not authorized to use Kubernetes cloud: %s", step.getCloud()));
    }
}
 
Example #9
Source File: PodTemplateStepExecution.java    From kubernetes-plugin with Apache License 2.0 5 votes vote down vote up
private String checkNamespace(KubernetesCloud kubernetesCloud, @CheckForNull PodTemplateContext podTemplateContext) {
    String namespace = null;
    if (!Strings.isNullOrEmpty(step.getNamespace())) {
        namespace = step.getNamespace();
    } else if (podTemplateContext != null && !Strings.isNullOrEmpty(podTemplateContext.getNamespace())) {
        namespace = podTemplateContext.getNamespace();
    } else {
        namespace = kubernetesCloud.getNamespace();
    }
    return namespace;
}
 
Example #10
Source File: EnvVarCasCTest.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<TemplateEnvVar> envVars = podTemplate.getEnvVars();
    assertEquals(1, envVars.size());
    strategy._verify(envVars.get(0));

}
 
Example #11
Source File: PodTemplateMap.java    From kubernetes-plugin with Apache License 2.0 5 votes vote down vote up
/**
 * Adds a template for the corresponding cloud instance.
 * @param cloud The cloud instance.
 * @param podTemplate The pod template to add.
 */
public void addTemplate(@Nonnull KubernetesCloud cloud, @Nonnull PodTemplate podTemplate) {
    synchronized (this.map) {
        List<PodTemplate> list = getOrCreateTemplateList(cloud);
        list.add(podTemplate);
        map.put(cloud.name, list);
    }
}
 
Example #12
Source File: PodTemplateStepExecutionTest.java    From kubernetes-plugin with Apache License 2.0 4 votes vote down vote up
@Before
public void configureCloud() throws Exception {
    cloud = new KubernetesCloud("kubernetes");
    r.jenkins.clouds.add(cloud);
}
 
Example #13
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 #14
Source File: CasCTest.java    From kubernetes-plugin with Apache License 2.0 4 votes vote down vote up
@Override
protected void assertConfiguredAsExpected(RestartableJenkinsRule r, String configContent) {
    KubernetesCloud cloud = r.j.jenkins.clouds.get(KubernetesCloud.class);
    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);
    assertEquals(false, 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);
    assertEquals(false, 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);
    assertEquals(false, 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());
    assertEquals(true, container.isTtyEnabled());
    assertEquals("/src", container.getWorkingDir());

}
 
Example #15
Source File: PodRetentionTest.java    From kubernetes-plugin with Apache License 2.0 4 votes vote down vote up
@Before
public void setUp() {
    this.cloud = new KubernetesCloud("kubernetes");
    this.pod = new Pod();
}
 
Example #16
Source File: NoDelayProvisionerStrategy.java    From kubernetes-plugin with Apache License 2.0 4 votes vote down vote up
@Override
public NodeProvisioner.StrategyDecision apply(NodeProvisioner.StrategyState strategyState) {
    if (DISABLE_NODELAY_PROVISING) {
        LOGGER.log(Level.FINE, "Provisioning not complete, NoDelayProvisionerStrategy is disabled");
        return NodeProvisioner.StrategyDecision.CONSULT_REMAINING_STRATEGIES;
    }

    final Label label = strategyState.getLabel();

    LoadStatistics.LoadStatisticsSnapshot snapshot = strategyState.getSnapshot();
    int availableCapacity =
            snapshot.getAvailableExecutors()   // live executors
                    + snapshot.getConnectingExecutors()  // executors present but not yet connected
                    + strategyState.getPlannedCapacitySnapshot()     // capacity added by previous strategies from previous rounds
                    + strategyState.getAdditionalPlannedCapacity();  // capacity added by previous strategies _this round_
    int currentDemand = snapshot.getQueueLength();
    LOGGER.log(Level.FINE, "Available capacity={0}, currentDemand={1}",
            new Object[]{availableCapacity, currentDemand});
    if (availableCapacity < currentDemand) {
        List<Cloud> jenkinsClouds = new ArrayList<>(Jenkins.get().clouds);
        Collections.shuffle(jenkinsClouds);
        for (Cloud cloud : jenkinsClouds) {
            int workloadToProvision = currentDemand - availableCapacity;
            if (!(cloud instanceof KubernetesCloud)) continue;
            if (!cloud.canProvision(label)) continue;
            for (CloudProvisioningListener cl : CloudProvisioningListener.all()) {
                if (cl.canProvision(cloud, strategyState.getLabel(), workloadToProvision) != null) {
                    continue;
                }
            }
            Collection<NodeProvisioner.PlannedNode> plannedNodes = cloud.provision(label, workloadToProvision);
            LOGGER.log(Level.FINE, "Planned {0} new nodes", plannedNodes.size());
            fireOnStarted(cloud, strategyState.getLabel(), plannedNodes);
            strategyState.recordPendingLaunches(plannedNodes);
            availableCapacity += plannedNodes.size();
            LOGGER.log(Level.FINE, "After provisioning, available capacity={0}, currentDemand={1}", new Object[]{availableCapacity, currentDemand});
            break;
        }
    }
    if (availableCapacity >= currentDemand) {
        LOGGER.log(Level.FINE, "Provisioning completed");
        return NodeProvisioner.StrategyDecision.PROVISIONING_COMPLETED;
    } else {
        LOGGER.log(Level.FINE, "Provisioning not complete, consulting remaining strategies");
        return NodeProvisioner.StrategyDecision.CONSULT_REMAINING_STRATEGIES;
    }
}
 
Example #17
Source File: PodTemplateMap.java    From kubernetes-plugin with Apache License 2.0 4 votes vote down vote up
@Nonnull
@Override
public List<PodTemplate> getList(@Nonnull KubernetesCloud cloud) {
    return PodTemplateMap.get().getTemplates(cloud);
}
 
Example #18
Source File: PodTemplateMap.java    From kubernetes-plugin with Apache License 2.0 4 votes vote down vote up
public void removeTemplate(@Nonnull KubernetesCloud cloud, @Nonnull PodTemplate podTemplate) {
    synchronized (this.map) {
        getOrCreateTemplateList(cloud).remove(podTemplate);
    }
}
 
Example #19
Source File: PodTemplateMap.java    From kubernetes-plugin with Apache License 2.0 4 votes vote down vote up
private List<PodTemplate> getOrCreateTemplateList(@Nonnull KubernetesCloud cloud) {
    List<PodTemplate> podTemplates = map.get(cloud.name);
    return podTemplates == null ? new CopyOnWriteArrayList<>() : podTemplates;
}
 
Example #20
Source File: PodTemplateStepExecution.java    From kubernetes-plugin with Apache License 2.0 4 votes vote down vote up
@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(Strings.emptyToNull(parentTemplates));
    } else {
        newTemplate.setInheritFrom(Strings.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.setRunAsUser(step.getRunAsUser());
    newTemplate.setRunAsGroup(step.getRunAsGroup());
    if (step.getHostNetwork() != null) {
        newTemplate.setHostNetwork(step.getHostNetwork());
    }
    newTemplate.setAnnotations(step.getAnnotations());
    newTemplate.setListener(getContext().get(TaskListener.class));
    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)));
    }

    // Note that after JENKINS-51248 this must be a single label atom, not a space-separated list, unlike PodTemplate.label generally.
    if (!PodTemplateUtils.validateLabel(newTemplate.getLabel())) {
        throw new AbortException(Messages.label_error(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;
}
 
Example #21
Source File: Never.java    From kubernetes-plugin with Apache License 2.0 4 votes vote down vote up
@Override
public boolean shouldDeletePod(KubernetesCloud cloud, Pod pod) {
    return true;
}
 
Example #22
Source File: Always.java    From kubernetes-plugin with Apache License 2.0 4 votes vote down vote up
@Override
public boolean shouldDeletePod(KubernetesCloud cloud, Pod pod) {
    return false;
}
 
Example #23
Source File: PodTemplateMap.java    From kubernetes-plugin with Apache License 2.0 2 votes vote down vote up
/**
 * Returns a read-only view of the templates available for the corresponding cloud instance.
 * @param cloud The kubernetes cloud instance for which templates are needed
 * @return a read-only view of the templates available for the corresponding cloud instance.
 */
@Nonnull
public List<PodTemplate> getTemplates(@Nonnull KubernetesCloud cloud) {
    return Collections.unmodifiableList(getOrCreateTemplateList(cloud));
}
 
Example #24
Source File: PodRetention.java    From kubernetes-plugin with Apache License 2.0 2 votes vote down vote up
/**
 * Determines if a slave pod should be deleted after the Jenkins build completes.
 * 
 * @param cloud - the {@link KubernetesCloud} the slave pod belongs to.
 * @param pod - the {@link Pod} running the Jenkins build.
 * 
 * @return <code>true</code> if the slave pod should be deleted.
 */
public abstract boolean shouldDeletePod(KubernetesCloud cloud, Pod pod);