/** * Lookup namespace that is already defined in the current build. * @return The {@link NamespaceAction}. */ protected NamespaceAction getNamespaceAction() { try { return new NamespaceAction(getContext().get(Run.class)); } catch (Throwable t) { return null; } }
private String checkNamespace(KubernetesCloud kubernetesCloud, @CheckForNull NamespaceAction namespaceAction) { String namespace = null; if (!Strings.isNullOrEmpty(step.getNamespace())) { namespace = step.getNamespace(); } else if ((namespaceAction != null) && (!Strings.isNullOrEmpty(namespaceAction.getNamespace()))) { namespace = namespaceAction.getNamespace(); } else { namespace = kubernetesCloud.getNamespace(); } return namespace; }
@Override public void stop(Throwable cause) throws Exception { if (configuration.isNamespaceDestroyEnabled()) { namespaceService.destroy(namespace); } String ns = new NamespaceAction(getContext().get(Run.class)).pop(); }
@Override public boolean start() throws Exception { TaskListener listener = getContext().get(TaskListener.class); NamespaceAction namespaceAction = new NamespaceAction(getContext().get(Run.class)); sessionId = generateSessionId(); namespace = generateNamespaceId(step.getName(), step.getPrefix(), sessionId); client = getKubernetesClient(); isOpenshift = client.isAdaptable(OpenShiftClient.class); configuration = new DefaultConfigurationBuilder() .withNamespace(namespace) .withNamespaceLazyCreateEnabled(step.isNamespaceLazyCreateEnabled()) .withNamespaceDestroyEnabled(step.isNamespaceDestroyEnabled()) .withMasterUrl(client.getMasterUrl()) .build(); StreamLogger logger = new StreamLogger(listener.getLogger()); LabelProvider labelProvider = new MapLabelProvider(step.getLabels()); namespaceService = isOpenshift ? new OpenshiftNamespaceService.ImmutableOpenshiftNamespaceService(client, configuration, labelProvider, logger) : new DefaultNamespaceService.ImmutableNamespaceService(client, configuration, labelProvider, logger); if (!namespaceService.exists(namespace) && configuration.isNamespaceLazyCreateEnabled()) { namespaceService.create(namespace); } namespaceAction.push(namespace); getContext().newBodyInvoker(). withContext(EnvironmentExpander.merge(getContext().get(EnvironmentExpander.class), new NamespaceExpander(namespace))). withCallback(new NamespaceDestructionCallback(namespace, configuration, namespaceService, namespaceAction)). start(); return false; }
@Override protected String run() throws Exception { String namespace = null; try { FilePath workspace = getContext().get(FilePath.class); namespace = workspace.child(Config.KUBERNETES_NAMESPACE_PATH).readToString(); if (Utils.isNotNullOrEmpty(namespace)) { return namespace; } } catch (Throwable t) { //it might be executed outside of a `node` block in which case, we want to ignore. } NamespaceAction namespaceAction = new NamespaceAction(getContext().get(Run.class)); namespace = namespaceAction.getNamespace(); if (Utils.isNotNullOrEmpty(namespace)) { return namespace; } return step.getFallbackNamespace(); } }
NamespaceAction.push(run, namespace); return false;
@Override protected void finished(StepContext context) throws Exception { if (configuration.isNamespaceDestroyEnabled()) { namespaceService.destroy(namespace); } String ns = namespaceAction.pop(); } }
@Deprecated public NamespaceAction(Run run) { setRun(run); }
@Override public boolean start() throws Exception { TaskListener listener = getContext().get(TaskListener.class); NamespaceAction namespaceAction = new NamespaceAction(getContext().get(Run.class)); sessionId = generateSessionId(); namespace = generateNamespaceId(step.getName(), step.getPrefix(), sessionId); client = getKubernetesClient(); isOpenshift = client.isAdaptable(OpenShiftClient.class); configuration = new DefaultConfigurationBuilder() .withNamespace(namespace) .withNamespaceLazyCreateEnabled(step.isNamespaceLazyCreateEnabled()) .withNamespaceDestroyEnabled(step.isNamespaceDestroyEnabled()) .withMasterUrl(client.getMasterUrl()) .build(); StreamLogger logger = new StreamLogger(listener.getLogger()); LabelProvider labelProvider = new MapLabelProvider(step.getLabels()); namespaceService = isOpenshift ? new OpenshiftNamespaceService.ImmutableOpenshiftNamespaceService(client, configuration, labelProvider, logger) : new DefaultNamespaceService.ImmutableNamespaceService(client, configuration, labelProvider, logger); if (!namespaceService.exists(namespace) && configuration.isNamespaceLazyCreateEnabled()) { namespaceService.create(namespace); } namespaceAction.push(namespace); getContext().newBodyInvoker(). withContext(EnvironmentExpander.merge(getContext().get(EnvironmentExpander.class), new NamespaceExpander(namespace))). withCallback(new NamespaceDestructionCallback(namespace, configuration, namespaceService, namespaceAction)). start(); return false; }
@Override protected String run() throws Exception { String namespace = null; try { FilePath workspace = getContext().get(FilePath.class); namespace = workspace.child(Config.KUBERNETES_NAMESPACE_PATH).readToString(); if (Utils.isNotNullOrEmpty(namespace)) { return namespace; } } catch (Throwable t) { //it might be executed outside of a `node` block in which case, we want to ignore. } NamespaceAction namespaceAction = new NamespaceAction(getContext().get(Run.class)); namespace = namespaceAction.getNamespace(); if (Utils.isNotNullOrEmpty(namespace)) { return namespace; } return step.getFallbackNamespace(); } }
@Test /** * Step namespace should have priority over anything else. */ public void runWithStepOverriddenNamespace() throws Exception { String overriddenNamespace = testingNamespace + "-overridden-namespace"; String stepNamespace = testingNamespace + "-overridden-namespace2"; KubernetesClient client = cloud.connect(); // Run in our own testing namespace if (client.namespaces().withName(stepNamespace).get() == null) { client.namespaces().createOrReplace( new NamespaceBuilder().withNewMetadata().withName(stepNamespace).endMetadata().build()); } WorkflowJob p = r.jenkins.createProject(WorkflowJob.class, "stepOverriddenNamespace"); p.setDefinition(new CpsFlowDefinition(loadPipelineScript("runWithStepOverriddenNamespace.groovy") .replace("OVERRIDDEN_NAMESPACE", stepNamespace), true)); WorkflowRun b = p.scheduleBuild2(0).waitForStart(); assertNotNull(b); NamespaceAction.push(b, overriddenNamespace); r.assertBuildStatusSuccess(r.waitForCompletion(b)); r.assertLogContains(stepNamespace, b); }
@Override protected void finished(StepContext context) throws Exception { if (configuration.isNamespaceDestroyEnabled()) { namespaceService.destroy(namespace); } String ns = namespaceAction.pop(); } }
@Test public void runWithOverriddenNamespace() throws Exception { String overriddenNamespace = testingNamespace + "-overridden-namespace"; KubernetesClient client = cloud.connect(); // Run in our own testing namespace if (client.namespaces().withName(overriddenNamespace).get() == null) { client.namespaces().createOrReplace( new NamespaceBuilder().withNewMetadata().withName(overriddenNamespace).endMetadata().build()); } WorkflowJob p = r.jenkins.createProject(WorkflowJob.class, "overriddenNamespace"); p.setDefinition(new CpsFlowDefinition(loadPipelineScript("runWithOverriddenNamespace.groovy"), true)); WorkflowRun b = p.scheduleBuild2(0).waitForStart(); assertNotNull(b); NamespaceAction.push(b, overriddenNamespace); r.assertBuildStatusSuccess(r.waitForCompletion(b)); r.assertLogContains(overriddenNamespace, b); }
/** * Lookup namespace that is already defined in the current build. * @return The {@link NamespaceAction}. */ protected NamespaceAction getNamespaceAction() { try { return new NamespaceAction(getContext().get(Run.class)); } catch (Throwable t) { return null; } } protected URL toURL(String url) {
/** * Generates a namespace id/name if one has not been explicitly specified. * If no name or prefix is specified, it will try to determine a namespace based on the enclosing elements. * Finally it will fallback to generating one using the default prefix. * * @param name A fixed namespace name. * @param prefix The prefix to use. * @param sessionId The id of the session. * @return The name if not null or empty, else prefix-sessionId. */ protected String generateNamespaceId(String name, String prefix, String sessionId) { if (Utils.isNotNullOrEmpty(name)) { return name; } else if (Utils.isNotNullOrEmpty(prefix)) { return String.format(NAME_FORMAT, prefix, sessionId); } NamespaceAction namespaceAction = getNamespaceAction(); if (namespaceAction != null && Utils.isNotNullOrEmpty(namespaceAction.getNamespace())) { return namespaceAction.getNamespace(); } return String.format(NAME_FORMAT, DEFAULT_PREFIX, sessionId); }
@Override public void stop(Throwable cause) throws Exception { if (configuration.isNamespaceDestroyEnabled()) { namespaceService.destroy(namespace); } String ns = new NamespaceAction(getContext().get(Run.class)).pop(); }
/** * Lookup namespace that is already defined in the current build. * @return The {@link NamespaceAction}. */ protected NamespaceAction getNamespaceAction() { try { return new NamespaceAction(getContext().get(Run.class)); } catch (Throwable t) { return null; } }
/** * Generates a namespace id/name if one has not been explicitly specified. * If no name or prefix is specified, it will try to determine a namespace based on the enclosing elements. * Finally it will fallback to generating one using the default prefix. * * @param name A fixed namespace name. * @param prefix The prefix to use. * @param sessionId The id of the session. * @return The name if not null or empty, else prefix-sessionId. */ protected String generateNamespaceId(String name, String prefix, String sessionId) { if (Utils.isNotNullOrEmpty(name)) { return name; } else if (Utils.isNotNullOrEmpty(prefix)) { return String.format(NAME_FORMAT, prefix, sessionId); } NamespaceAction namespaceAction = getNamespaceAction(); if (namespaceAction != null && Utils.isNotNullOrEmpty(namespaceAction.getNamespace())) { return namespaceAction.getNamespace(); } return String.format(NAME_FORMAT, DEFAULT_PREFIX, sessionId); }
/** * Lookup namespace that is already defined in the current build. * @return The {@link NamespaceAction}. */ protected NamespaceAction getNamespaceAction() { try { return new NamespaceAction(getContext().get(Run.class)); } catch (Throwable t) { return null; } } protected URL toURL(String url) {
protected boolean isNamespaceProvided() { if (Utils.isNotNullOrEmpty(getStep().getName())) { return true; } NamespaceAction namespaceAction = getNamespaceAction(); if (namespaceAction != null && Utils.isNotNullOrEmpty(namespaceAction.getNamespace())) { return true; } return false; }