Search in sources :

Example 1 with PodImagePullSecret

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

Aggregations

AbortException (hudson.AbortException)1 TaskListener (hudson.model.TaskListener)1 ContainerTemplate (org.csanchez.jenkins.plugins.kubernetes.ContainerTemplate)1 KubernetesCloud (org.csanchez.jenkins.plugins.kubernetes.KubernetesCloud)1 PodAnnotation (org.csanchez.jenkins.plugins.kubernetes.PodAnnotation)1 PodImagePullSecret (org.csanchez.jenkins.plugins.kubernetes.PodImagePullSecret)1 PodTemplate (org.csanchez.jenkins.plugins.kubernetes.PodTemplate)1 BodyInvoker (org.jenkinsci.plugins.workflow.steps.BodyInvoker)1