Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Setup namespace if needed and possible in declarative test #654

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@
import java.util.concurrent.ExecutionException;
import java.util.logging.Level;

import io.fabric8.kubernetes.api.model.NamespaceBuilder;
import io.fabric8.kubernetes.client.KubernetesClient;
import org.csanchez.jenkins.plugins.kubernetes.ContainerEnvVar;
import org.csanchez.jenkins.plugins.kubernetes.ContainerTemplate;
import org.csanchez.jenkins.plugins.kubernetes.KubernetesCloud;
Expand Down Expand Up @@ -182,4 +184,11 @@ private static void setEnvVariables(PodTemplate podTemplate) {
podTemplate.getContainers().get(0)
.setEnvVars(asList(containerEnvVariable, containerEnvVariableLegacy, containerSecretEnvVariable));
}

protected void createNamespaceIfNotExist(KubernetesClient client, String namespace) {
if (client.namespaces().withName(namespace).get() == null) {
client.namespaces().createOrReplace(
new NamespaceBuilder().withNewMetadata().withName(namespace).endMetadata().build());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ public void declarativeFromYaml() throws Exception {
@Issue("JENKINS-51610")
@Test
public void declarativeWithNamespaceFromYaml() throws Exception {
createNamespaceIfNotExist(cloud.connect(), "kubernetes-plugin-test-overridden-namespace");
assertNotNull(createJobThenScheduleRun());
r.assertBuildStatusSuccess(r.waitForCompletion(b));
r.assertLogContains("Apache Maven 3.3.9", b);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,8 @@ public class KubernetesPipelineOverridenNamespaceTest extends AbstractKubernetes
public void runWithCloudOverriddenNamespace() throws Exception {
String overriddenNamespace = testingNamespace + "-overridden-namespace";
cloud.setNamespace(overriddenNamespace);
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());
}
createNamespaceIfNotExist(cloud.connect(), overriddenNamespace);

assertNotNull(createJobThenScheduleRun());

Expand All @@ -37,12 +33,8 @@ public void runWithStepOverriddenNamespace() throws Exception {
String overriddenNamespace = testingNamespace + "-overridden-namespace";
String stepNamespace = testingNamespace + "-overridden-namespace2";
cloud.setNamespace(overriddenNamespace);
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());
}
createNamespaceIfNotExist(cloud.connect(), stepNamespace);

Map<String, String> env = new HashMap<>();
env.put("OVERRIDDEN_NAMESPACE", stepNamespace);
Expand Down