Search in sources :

Example 81 with Namespace

use of io.fabric8.kubernetes.api.model.Namespace in project fabric8 by fabric8io.

the class Util method waitUntilWeCanDestroyNamespace.

protected static void waitUntilWeCanDestroyNamespace(Session session) {
    final Logger log = session.getLogger();
    String confirmDestroy = Systems.getEnvVarOrSystemProperty(Constants.NAMESPACE_CLEANUP_CONFIRM_ENABLED, "false");
    if (Objects.equal(confirmDestroy, "true")) {
        showErrorsBeforePause(session);
        System.out.println();
        System.out.println("Waiting to destroy the namespace.");
        System.out.println("Please type: [Q] to terminate the namespace.");
        while (true) {
            try {
                int ch = System.in.read();
                if (ch < 0 || ch == 'Q') {
                    System.out.println("\nStopping...");
                    break;
                } else {
                    System.out.println("Found character: " + Character.toString((char) ch));
                }
            } catch (IOException e) {
                log.warn("Failed to read from input. " + e);
                break;
            }
        }
    } else {
        String timeoutText = Systems.getEnvVarOrSystemProperty(Constants.NAMESPACE_CLEANUP_TIMEOUT, "0");
        Long timeout = null;
        if (Strings.isNotBlank(timeoutText)) {
            try {
                timeout = Long.parseLong(timeoutText);
            } catch (NumberFormatException e) {
                log.warn("Failed to parse timeout value '" + timeoutText + "' for $Constants.NAMESPACE_CLEANUP_TIMEOUT. " + e);
            }
        }
        if (timeout != null && timeout > 0L) {
            showErrorsBeforePause(session);
            System.out.println();
            System.out.println("Sleeping for " + timeout + " seconds until destroying the namespace");
            try {
                Thread.sleep(timeout * 1000);
            } catch (InterruptedException e) {
                log.info("Interupted sleeping to GC the namespace: " + e);
            }
        }
    }
    System.out.println("Now destroying the Fabric8 Arquillian test case namespace");
}
Also used : IOException(java.io.IOException) Logger(io.fabric8.arquillian.kubernetes.log.Logger)

Example 82 with Namespace

use of io.fabric8.kubernetes.api.model.Namespace in project fabric8 by fabric8io.

the class ConfigurationTest method testNamespaceFoundFromConfigMap.

@Test
public void testNamespaceFoundFromConfigMap() {
    String devNamespace = "myproject";
    String environmentKey = "testing";
    String testNamespace = "myproject-testing";
    Map<String, String> data = new HashMap<>();
    data.put(environmentKey, "    name: Testing\n" + "    namespace: " + testNamespace + "\n" + "    order: 0");
    server.expect().withPath("/api/v1/namespaces/" + devNamespace + "/configmaps/fabric8-environments").andReturn(200, new ConfigMapBuilder().withNewMetadata().withName("fabric8-environments").endMetadata().withData(data).build()).once();
    Map<String, String> map = new HashMap<>();
    map.put(FABRIC8_ENVIRONMENT, environmentKey);
    map.put(DEVELOPMENT_NAMESPACE, devNamespace);
    Configuration configuration = Configuration.fromMap(map, getKubernetesClient());
    assertEquals(testNamespace, configuration.getNamespace());
    assertTrue(configuration.isAnsiLoggerEnabled());
    assertTrue(configuration.isEnvironmentInitEnabled());
    assertTrue(configuration.isNamespaceLazyCreateEnabled());
    assertFalse(configuration.isNamespaceCleanupEnabled());
    assertFalse(configuration.isCreateNamespaceForTest());
}
Also used : HashMap(java.util.HashMap) ConfigMapBuilder(io.fabric8.kubernetes.api.model.ConfigMapBuilder) Test(org.junit.Test)

Example 83 with Namespace

use of io.fabric8.kubernetes.api.model.Namespace in project fabric8 by fabric8io.

the class ConfigurationTest method testEnvironmentKeyButNoConfigMap.

@Test
public void testEnvironmentKeyButNoConfigMap() {
    String devNamespace = "myproject";
    String environmentKey = "testing";
    String testNamespace = devNamespace;
    Map<String, String> data = new HashMap<>();
    data.put("staging", "    name: Staging\n" + "    namespace: myproject-staging\n" + "    order: 0");
    server.expect().withPath("/api/v1/namespaces/" + devNamespace + "/configmaps/fabric8-environments").andReturn(404, "Not found").once();
    Map<String, String> map = new HashMap<>();
    map.put(FABRIC8_ENVIRONMENT, environmentKey);
    map.put(DEVELOPMENT_NAMESPACE, devNamespace);
    KubernetesClient kubernetesClient = getKubernetesClient();
    Config config = new Config();
    config.setNamespace(devNamespace);
    config.setMasterUrl(kubernetesClient.getMasterUrl().toString());
    DefaultKubernetesClient clientWithDefaultNamespace = new DefaultKubernetesClient(config);
    Configuration configuration = Configuration.fromMap(map, clientWithDefaultNamespace);
    assertEquals(testNamespace, configuration.getNamespace());
    assertTrue(configuration.isAnsiLoggerEnabled());
    assertTrue(configuration.isEnvironmentInitEnabled());
    assertTrue(configuration.isNamespaceLazyCreateEnabled());
    assertFalse(configuration.isNamespaceCleanupEnabled());
    assertFalse(configuration.isCreateNamespaceForTest());
}
Also used : KubernetesClient(io.fabric8.kubernetes.client.KubernetesClient) DefaultKubernetesClient(io.fabric8.kubernetes.client.DefaultKubernetesClient) HashMap(java.util.HashMap) Config(io.fabric8.kubernetes.client.Config) DefaultKubernetesClient(io.fabric8.kubernetes.client.DefaultKubernetesClient) Test(org.junit.Test)

Example 84 with Namespace

use of io.fabric8.kubernetes.api.model.Namespace in project fabric8 by fabric8io.

the class ConfigurationTest method testFailIfEnvironmentNamespaceNotFoundFromConfigMap.

@Test(expected = IllegalStateException.class)
public void testFailIfEnvironmentNamespaceNotFoundFromConfigMap() {
    String devNamespace = "myproject";
    String environmentKey = "testing";
    Map<String, String> data = new HashMap<>();
    data.put("staging", "    name: Staging\n" + "    namespace: myproject-staging\n" + "    order: 0");
    server.expect().withPath("/api/v1/namespaces/" + devNamespace + "/configmaps/fabric8-environments").andReturn(200, new ConfigMapBuilder().withNewMetadata().withName("fabric8-environments").endMetadata().withData(data).build()).once();
    Map<String, String> map = new HashMap<>();
    map.put(FABRIC8_ENVIRONMENT, environmentKey);
    map.put(DEVELOPMENT_NAMESPACE, devNamespace);
    map.put(FAIL_ON_MISSING_ENVIRONMENT_NAMESPACE, "true");
    Configuration.fromMap(map, getKubernetesClient());
}
Also used : HashMap(java.util.HashMap) ConfigMapBuilder(io.fabric8.kubernetes.api.model.ConfigMapBuilder) Test(org.junit.Test)

Example 85 with Namespace

use of io.fabric8.kubernetes.api.model.Namespace in project fabric8 by fabric8io.

the class ConfigurationTest method testNamespaceNotFoundFromConfigMap.

@Ignore
public void testNamespaceNotFoundFromConfigMap() {
    String devNamespace = "myproject";
    String environmentKey = "testing";
    String testNamespace = devNamespace;
    Map<String, String> data = new HashMap<>();
    data.put("staging", "    name: Staging\n" + "    namespace: myproject-staging\n" + "    order: 0");
    server.expect().withPath("/api/v1/namespaces/" + devNamespace + "/configmaps/fabric8-environments").andReturn(200, new ConfigMapBuilder().withNewMetadata().withName("fabric8-environments").endMetadata().withData(data).build()).once();
    Map<String, String> map = new HashMap<>();
    map.put(FABRIC8_ENVIRONMENT, environmentKey);
    map.put(DEVELOPMENT_NAMESPACE, devNamespace);
    Configuration configuration = Configuration.fromMap(map, kubernetesClient);
    assertEquals(testNamespace, configuration.getNamespace());
    assertTrue(configuration.isAnsiLoggerEnabled());
    assertTrue(configuration.isEnvironmentInitEnabled());
    assertTrue(configuration.isNamespaceLazyCreateEnabled());
    assertFalse(configuration.isNamespaceCleanupEnabled());
    assertFalse(configuration.isCreateNamespaceForTest());
}
Also used : HashMap(java.util.HashMap) ConfigMapBuilder(io.fabric8.kubernetes.api.model.ConfigMapBuilder) Ignore(org.junit.Ignore)

Aggregations

IOException (java.io.IOException)81 Test (org.junit.Test)78 KubernetesClientException (io.fabric8.kubernetes.client.KubernetesClientException)74 ConfigMap (io.fabric8.kubernetes.api.model.ConfigMap)65 OpenShiftClient (io.fabric8.openshift.client.OpenShiftClient)60 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)59 HashMap (java.util.HashMap)59 KubernetesClient (io.fabric8.kubernetes.client.KubernetesClient)58 FileNotFoundException (java.io.FileNotFoundException)49 DefaultKubernetesClient (io.fabric8.kubernetes.client.DefaultKubernetesClient)48 OpenShiftNotAvailableException (io.fabric8.openshift.client.OpenShiftNotAvailableException)48 JSONObject (org.json.JSONObject)44 Service (io.fabric8.kubernetes.api.model.Service)38 Pod (io.fabric8.kubernetes.api.model.Pod)36 ArrayList (java.util.ArrayList)32 File (java.io.File)25 ReplicationController (io.fabric8.kubernetes.api.model.ReplicationController)24 BuildConfig (io.fabric8.openshift.api.model.BuildConfig)24 Map (java.util.Map)22 ConfigMapBuilder (io.fabric8.kubernetes.api.model.ConfigMapBuilder)21