Search in sources :

Example 41 with KubernetesClient

use of io.fabric8.kubernetes.client.KubernetesClient in project fabric8 by fabric8io.

the class Configuration method fromMap.

public static Configuration fromMap(Map<String, String> map, KubernetesClient testKubernetesClient) {
    Configuration configuration = new Configuration();
    try {
        configuration.masterUrl = getStringProperty(KUBERNETES_MASTER, map, FALLBACK_CONFIG.getMasterUrl());
        configuration.environment = getStringProperty(FABRIC8_ENVIRONMENT, map, null);
        configuration.environmentInitEnabled = getBooleanProperty(ENVIRONMENT_INIT_ENABLED, map, true);
        configuration.environmentConfigUrl = getKubernetesConfigurationUrl(map);
        configuration.environmentDependencies = Strings.splitAndTrimAsList(getStringProperty(ENVIRONMENT_DEPENDENCIES, map, ""), "\\s+");
        configuration.namespaceLazyCreateEnabled = getBooleanProperty(NAMESPACE_LAZY_CREATE_ENABLED, map, DEFAULT_NAMESPACE_LAZY_CREATE_ENABLED);
        configuration.properties = map;
        String existingNamespace = getStringProperty(NAMESPACE_TO_USE, map, null);
        configuration.sessionId = UUID.randomUUID().toString();
        configuration.namespaceCleanupConfirmationEnabled = getBooleanProperty(NAMESPACE_CLEANUP_CONFIRM_ENABLED, map, false);
        configuration.deleteAllResourcesOnExit = getBooleanProperty(NAMESPACE_DELETE_ALL_RESOURCES_ON_EXIT, map, false);
        configuration.namespaceCleanupTimeout = getLongProperty(NAMESPACE_CLEANUP_TIMEOUT, map, DEFAULT_NAMESPACE_CLEANUP_TIMEOUT);
        configuration.waitTimeout = getLongProperty(WAIT_TIMEOUT, map, DEFAULT_WAIT_TIMEOUT);
        configuration.waitPollInterval = getLongProperty(WAIT_POLL_INTERVAL, map, DEFAULT_WAIT_POLL_INTERVAL);
        configuration.waitForServiceList = Strings.splitAndTrimAsList(getStringProperty(WAIT_FOR_SERVICE_LIST, map, ""), "\\s+");
        configuration.waitForServiceConnectionEnabled = getBooleanProperty(WAIT_FOR_SERVICE_CONNECTION_ENABLED, map, DEFAULT_WAIT_FOR_SERVICE_CONNECTION_ENABLED);
        configuration.waitForServiceConnectionTimeout = getLongProperty(WAIT_FOR_SERVICE_CONNECTION_TIMEOUT, map, DEFAULT_NAMESPACE_CLEANUP_TIMEOUT);
        configuration.ansiLoggerEnabled = getBooleanProperty(ANSI_LOGGER_ENABLED, map, true);
        configuration.kubernetesDomain = getStringProperty(KUBERNETES_DOMAIN, map, "");
        configuration.gofabric8Enabled = getBooleanProperty(GOFABRIC8_ENABLED, map, false);
        configuration.createNamespaceForTest = getBooleanProperty(CREATE_NAMESPACE_FOR_TEST, map, false);
        KubernetesClient kubernetesClient = getOrCreateKubernetesClient(configuration, testKubernetesClient);
        boolean failOnMissingEnvironmentNamespace = getBooleanProperty(FAIL_ON_MISSING_ENVIRONMENT_NAMESPACE, map, false);
        String defaultDevelopNamespace = existingNamespace;
        if (Strings.isNullOrBlank(defaultDevelopNamespace)) {
            defaultDevelopNamespace = kubernetesClient.getNamespace();
        }
        String developNamespace = getStringProperty(DEVELOPMENT_NAMESPACE, map, defaultDevelopNamespace);
        configuration.kubernetesClient = kubernetesClient;
        String environmentNamespace = findNamespaceForEnvironment(configuration.environment, map, kubernetesClient, developNamespace, failOnMissingEnvironmentNamespace);
        String providedNamespace = selectNamespace(environmentNamespace, existingNamespace);
        if (configuration.createNamespaceForTest) {
            configuration.namespace = NAMESPACE_PREFIX + configuration.sessionId;
        } else {
            String namespace = Strings.isNotBlank(providedNamespace) ? providedNamespace : developNamespace;
            ;
            if (Strings.isNullOrBlank(namespace)) {
                namespace = kubernetesClient.getNamespace();
                if (Strings.isNullOrBlank(namespace)) {
                    namespace = KubernetesHelper.defaultNamespace();
                }
            }
            configuration.namespace = namespace;
        }
        // We default to "cleanup=true" when generating namespace and "cleanup=false" when using existing namespace.
        configuration.namespaceCleanupEnabled = getBooleanProperty(NAMESPACE_CLEANUP_ENABLED, map, Strings.isNullOrBlank(providedNamespace));
    } catch (Throwable t) {
        if (t instanceof RuntimeException) {
            throw (RuntimeException) t;
        } else {
            throw new RuntimeException(t);
        }
    }
    return configuration;
}
Also used : KubernetesClient(io.fabric8.kubernetes.client.KubernetesClient) DefaultKubernetesClient(io.fabric8.kubernetes.client.DefaultKubernetesClient)

Example 42 with KubernetesClient

use of io.fabric8.kubernetes.client.KubernetesClient in project fabric8 by fabric8io.

the class Configuration method findNamespaceForEnvironment.

/**
 * Resolves a logical environment name for a project, such as <code>Testing</code> to the physical projcect/team specific
 * namespace value.
 *
 * It tries to find a fabric8.yml file in this folder or a parent folder and loads it and tries to use it to find the
 * namespace for the given environment or uses environment variables to resolve the environment name -> physical namespace
 * @return the namespace
 */
private static String findNamespaceForEnvironment(String environment, Map<String, String> map, KubernetesClient kubernetesClient, String developNamespace, boolean failOnMissingEnvironmentNamespace) {
    String namespace = null;
    if (!Strings.isNullOrBlank(environment)) {
        namespace = Environments.namespaceForEnvironment(kubernetesClient, environment, developNamespace);
        if (Strings.isNotBlank(namespace)) {
            return namespace;
        }
        String basedir = System.getProperty("basedir", ".");
        File folder = new File(basedir);
        ProjectConfig projectConfig = ProjectConfigs.findFromFolder(folder);
        if (projectConfig != null) {
            LinkedHashMap<String, String> environments = projectConfig.getEnvironments();
            if (environments != null) {
                namespace = environments.get(environment);
            }
        }
        String key = environment.toLowerCase() + ".namespace";
        if (Strings.isNullOrBlank(namespace)) {
            // lets try find an environment variable or system property
            namespace = getStringProperty(key, map, null);
        }
        if (Strings.isNullOrBlank(namespace)) {
            if (failOnMissingEnvironmentNamespace) {
                throw new IllegalStateException("A fabric8 environment '" + environment + "' has been specified, but no matching namespace was found in the fabric8.yml file or '" + key + "' system property");
            } else {
                return developNamespace;
            }
        }
    }
    return namespace;
}
Also used : ProjectConfig(io.fabric8.devops.ProjectConfig) File(java.io.File)

Example 43 with KubernetesClient

use of io.fabric8.kubernetes.client.KubernetesClient in project fabric8 by fabric8io.

the class J4pClientProvider method lookup.

@Override
public Object lookup(ArquillianResource resource, Annotation... qualifiers) {
    KubernetesClient client = this.clientInstance.get();
    Session session = this.sessionInstance.get();
    JolokiaClients jolokiaClients = new JolokiaClients(client);
    String serviceName = getServiceName(qualifiers);
    String podName = getPodName(qualifiers);
    String replicationControllerName = getReplicationControllerName(qualifiers);
    if (Strings.isNotBlank(serviceName)) {
        Service service = client.services().inNamespace(session.getNamespace()).withName(serviceName).get();
        if (service != null) {
            return jolokiaClients.clientForService(service);
        }
    }
    if (Strings.isNotBlank(podName)) {
        Pod pod = client.pods().inNamespace(session.getNamespace()).withName(serviceName).get();
        if (pod != null) {
            return jolokiaClients.clientForPod(pod);
        }
    }
    if (Strings.isNotBlank(replicationControllerName)) {
        ReplicationController replicationController = client.replicationControllers().inNamespace(session.getNamespace()).withName(replicationControllerName).get();
        if (replicationController != null) {
            return jolokiaClients.clientForReplicationController(replicationController);
        }
    }
    return null;
}
Also used : KubernetesClient(io.fabric8.kubernetes.client.KubernetesClient) Pod(io.fabric8.kubernetes.api.model.Pod) ReplicationController(io.fabric8.kubernetes.api.model.ReplicationController) Service(io.fabric8.kubernetes.api.model.Service) JolokiaClients(io.fabric8.kubernetes.jolokia.JolokiaClients) Session(io.fabric8.arquillian.kubernetes.Session)

Example 44 with KubernetesClient

use of io.fabric8.kubernetes.client.KubernetesClient in project fabric8 by fabric8io.

the class PodResourceProvider method lookup.

@Override
public Object lookup(ArquillianResource resource, Annotation... qualifiers) {
    KubernetesClient client = this.clientInstance.get();
    Session session = sessionInstance.get();
    String name = getPodName(qualifiers);
    if (name != null) {
        return client.pods().inNamespace(session.getNamespace()).withName(name).get();
    }
    // Gets the first pod found that matches the labels.
    Map<String, String> labels = getLabels(qualifiers);
    PodList list = client.pods().inNamespace(session.getNamespace()).withLabels(labels).list();
    List<Pod> pods = notNullList(list.getItems());
    if (!pods.isEmpty()) {
        return pods.get(0);
    }
    return null;
}
Also used : KubernetesClient(io.fabric8.kubernetes.client.KubernetesClient) PodList(io.fabric8.kubernetes.api.model.PodList) Pod(io.fabric8.kubernetes.api.model.Pod) Session(io.fabric8.arquillian.kubernetes.Session)

Example 45 with KubernetesClient

use of io.fabric8.kubernetes.client.KubernetesClient in project fabric8 by fabric8io.

the class ReplicationControllerListResourceProvider method lookup.

@Override
public Object lookup(ArquillianResource resource, Annotation... qualifiers) {
    KubernetesClient client = this.clientInstance.get();
    Session session = sessionInstance.get();
    Map<String, String> labels = getLabels(qualifiers);
    if (labels.isEmpty()) {
        return client.replicationControllers().inNamespace(session.getNamespace()).list();
    } else {
        return client.replicationControllers().inNamespace(session.getNamespace()).withLabels(labels).list();
    }
}
Also used : KubernetesClient(io.fabric8.kubernetes.client.KubernetesClient) Session(io.fabric8.arquillian.kubernetes.Session)

Aggregations

KubernetesClient (io.fabric8.kubernetes.client.KubernetesClient)62 DefaultKubernetesClient (io.fabric8.kubernetes.client.DefaultKubernetesClient)40 OpenShiftClient (io.fabric8.openshift.client.OpenShiftClient)21 HashMap (java.util.HashMap)19 Pod (io.fabric8.kubernetes.api.model.Pod)17 Service (io.fabric8.kubernetes.api.model.Service)16 ConfigMap (io.fabric8.kubernetes.api.model.ConfigMap)14 KubernetesClientException (io.fabric8.kubernetes.client.KubernetesClientException)13 IOException (java.io.IOException)12 ReplicationController (io.fabric8.kubernetes.api.model.ReplicationController)10 File (java.io.File)10 ArrayList (java.util.ArrayList)9 HasMetadata (io.fabric8.kubernetes.api.model.HasMetadata)8 BuildConfig (io.fabric8.openshift.api.model.BuildConfig)8 Test (org.junit.Test)8 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)7 DeploymentConfig (io.fabric8.openshift.api.model.DeploymentConfig)7 Map (java.util.Map)7 MojoExecutionException (org.apache.maven.plugin.MojoExecutionException)7 Controller (io.fabric8.kubernetes.api.Controller)6