Search in sources :

Example 91 with KubernetesClient

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

the class Util method cleanupAllMatching.

public static void cleanupAllMatching(KubernetesClient client, Session session, List<Throwable> errors, List<KubernetesList> kubeConfigs) throws MultiException {
    String sessionNamespace = session.getNamespace();
    session.getLogger().info("Removing provisioned resources in namespace " + sessionNamespace);
    /**
     * Lets use a loop to ensure we really do delete all the matching resources
     */
    for (int i = 0; i < 10; i++) {
        for (KubernetesList list : kubeConfigs) {
            List<HasMetadata> items = list.getItems();
            if (items != null) {
                for (HasMetadata item : items) {
                    cleanupItem(client, session, item, errors);
                }
            }
        }
    }
}
Also used : HasMetadata(io.fabric8.kubernetes.api.model.HasMetadata) KubernetesList(io.fabric8.kubernetes.api.model.KubernetesList)

Example 92 with KubernetesClient

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

the class Util method displaySessionStatus.

public static void displaySessionStatus(KubernetesClient client, Session session) throws MultiException {
    if (client == null) {
        session.getLogger().warn("No KubernetesClient for session: " + session.getId());
        return;
    }
    if (client.isAdaptable(OpenShiftClient.class)) {
        OpenShiftClient oClient = client.adapt(OpenShiftClient.class);
        List<DeploymentConfig> deploymentConfigs = oClient.deploymentConfigs().inNamespace(session.getNamespace()).list().getItems();
        if (deploymentConfigs == null) {
            throw new MultiException("No deployment configs found in namespace" + session.getNamespace());
        }
        for (DeploymentConfig deploymentConfig : deploymentConfigs) {
            session.getLogger().info("Deployment config:" + KubernetesHelper.getName(deploymentConfig));
        }
    } else {
        List<Deployment> deployments = client.extensions().deployments().inNamespace(session.getNamespace()).list().getItems();
        if (deployments == null) {
            throw new MultiException("No deployments found in namespace" + session.getNamespace());
        }
        for (Deployment deployment : deployments) {
            session.getLogger().info("Deployment:" + KubernetesHelper.getName(deployment));
        }
    }
    List<Pod> pods = client.pods().inNamespace(session.getNamespace()).list().getItems();
    if (pods == null) {
        throw new MultiException("No pods found in namespace" + session.getNamespace());
    }
    for (Pod pod : pods) {
        session.getLogger().info("Pod:" + KubernetesHelper.getName(pod) + " Status:" + pod.getStatus());
    }
    List<Service> svcs = client.services().inNamespace(session.getNamespace()).list().getItems();
    if (svcs == null) {
        throw new MultiException("No services found in namespace" + session.getNamespace());
    }
    for (Service service : svcs) {
        session.getLogger().info("Service:" + KubernetesHelper.getName(service) + " IP:" + getPortalIP(service) + " Port:" + getPorts(service));
    }
}
Also used : Pod(io.fabric8.kubernetes.api.model.Pod) OpenShiftClient(io.fabric8.openshift.client.OpenShiftClient) Deployment(io.fabric8.kubernetes.api.model.extensions.Deployment) Service(io.fabric8.kubernetes.api.model.Service) DeploymentConfig(io.fabric8.openshift.api.model.DeploymentConfig) MultiException(io.fabric8.utils.MultiException)

Example 93 with KubernetesClient

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

the class Util method cleanupAllResources.

public static void cleanupAllResources(KubernetesClient client, Session session, List<Throwable> errors) throws MultiException {
    String sessionNamespace = session.getNamespace();
    session.getLogger().info("Removing all resources in namespace " + sessionNamespace);
    /**
     * Lets use a loop to ensure we really do delete all the matching resources
     */
    for (int i = 0; i < 10; i++) {
        OpenShiftClient openShiftClient = new Controller(client).getOpenShiftClientOrNull();
        if (openShiftClient != null) {
            try {
                openShiftClient.deploymentConfigs().inNamespace(sessionNamespace).delete();
            } catch (KubernetesClientException e) {
                errors.add(e);
            }
            try {
                openShiftClient.routes().inNamespace(sessionNamespace).delete();
            } catch (KubernetesClientException e) {
                errors.add(e);
            }
        }
        try {
            client.extensions().deployments().inNamespace(sessionNamespace).delete();
        } catch (KubernetesClientException e) {
            errors.add(e);
        }
        try {
            client.extensions().replicaSets().inNamespace(sessionNamespace).delete();
        } catch (KubernetesClientException e) {
            errors.add(e);
        }
        try {
            client.replicationControllers().inNamespace(sessionNamespace).delete();
        } catch (KubernetesClientException e) {
            errors.add(e);
        }
        try {
            client.pods().inNamespace(sessionNamespace).delete();
        } catch (KubernetesClientException e) {
            errors.add(e);
        }
        try {
            client.extensions().ingresses().inNamespace(sessionNamespace).delete();
        } catch (KubernetesClientException e) {
            errors.add(e);
        }
        try {
            client.services().inNamespace(sessionNamespace).delete();
        } catch (KubernetesClientException e) {
            errors.add(e);
        }
        try {
            client.securityContextConstraints().withName(sessionNamespace).delete();
        } catch (KubernetesClientException e) {
            errors.add(e);
        }
        // lets see if there are any matching podList left
        List<Pod> filteredPods = notNullList(client.pods().inNamespace(sessionNamespace).list().getItems());
        if (filteredPods.isEmpty()) {
            return;
        } else {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}
Also used : Pod(io.fabric8.kubernetes.api.model.Pod) OpenShiftClient(io.fabric8.openshift.client.OpenShiftClient) Controller(io.fabric8.kubernetes.api.Controller) KubernetesClientException(io.fabric8.kubernetes.client.KubernetesClientException)

Example 94 with KubernetesClient

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

the class ConfigurationTest method testEnvironmentKeyButNoConfigMapLocalOnly.

@Ignore
public void testEnvironmentKeyButNoConfigMapLocalOnly() {
    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);
    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) Ignore(org.junit.Ignore)

Example 95 with KubernetesClient

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

the class Services method toServiceEndpointUrl.

public static List<String> toServiceEndpointUrl(String serviceId, String serviceProtocol, String servicePort) {
    List<String> endpoints = new ArrayList<>();
    KubernetesClient client = KubernetesHolder.getClient();
    String namespace = client.getNamespace();
    String actualProtocol = serviceProtocol != null ? serviceProtocol : DEFAULT_PROTO;
    Endpoints item = KubernetesHolder.getClient().endpoints().inNamespace(namespace).withName(serviceId).get();
    if (item != null) {
        for (EndpointSubset subset : item.getSubsets()) {
            for (EndpointAddress address : subset.getAddresses()) {
                for (EndpointPort endpointPort : subset.getPorts()) {
                    if (servicePort == null || servicePort.equals(endpointPort.getName())) {
                        endpoints.add(actualProtocol + "://" + address.getIp() + ":" + endpointPort.getPort());
                    }
                }
            }
        }
    }
    return endpoints;
}
Also used : Endpoints(io.fabric8.kubernetes.api.model.Endpoints) KubernetesClient(io.fabric8.kubernetes.client.KubernetesClient) EndpointSubset(io.fabric8.kubernetes.api.model.EndpointSubset) EndpointPort(io.fabric8.kubernetes.api.model.EndpointPort) ArrayList(java.util.ArrayList) EndpointAddress(io.fabric8.kubernetes.api.model.EndpointAddress)

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