Skip to content

Commit

Permalink
Merge pull request #654 from Vlatombe/fix-declarative-with-missing-ns
Browse files Browse the repository at this point in the history
Setup namespace if needed and possible in declarative test
  • Loading branch information
Vlatombe authored Dec 3, 2019
2 parents 9b470e6 + bde968d commit 3c9d11d
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 10 deletions.
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

0 comments on commit 3c9d11d

Please sign in to comment.