Search in sources :

Example 86 with OpenShiftClient

use of io.fabric8.openshift.client.OpenShiftClient 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 87 with OpenShiftClient

use of io.fabric8.openshift.client.OpenShiftClient 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 88 with OpenShiftClient

use of io.fabric8.openshift.client.OpenShiftClient in project fabric8 by fabric8io.

the class Controller method applyRoute.

public void applyRoute(Route entity, String sourceName) {
    OpenShiftClient openShiftClient = getOpenShiftClientOrNull();
    if (openShiftClient != null && openShiftClient.supportsOpenShiftAPIGroup(OpenShiftAPIGroups.ROUTE)) {
        String id = getName(entity);
        Objects.notNull(id, "No name for " + entity + " " + sourceName);
        String namespace = KubernetesHelper.getNamespace(entity);
        if (Strings.isNullOrBlank(namespace)) {
            namespace = getNamespace();
        }
        Route route = openShiftClient.routes().inNamespace(namespace).withName(id).get();
        if (route == null) {
            try {
                LOG.info("Creating Route " + namespace + ":" + id + " " + KubernetesHelper.summaryText(entity));
                openShiftClient.routes().inNamespace(namespace).create(entity);
            } catch (Exception e) {
                onApplyError("Failed to create Route from " + sourceName + ". " + e + ". " + entity, e);
            }
        }
    }
}
Also used : OpenShiftClient(io.fabric8.openshift.client.OpenShiftClient) Route(io.fabric8.openshift.api.model.Route) KubernetesClientException(io.fabric8.kubernetes.client.KubernetesClientException) FileNotFoundException(java.io.FileNotFoundException) OpenShiftNotAvailableException(io.fabric8.openshift.client.OpenShiftNotAvailableException) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) IOException(java.io.IOException)

Example 89 with OpenShiftClient

use of io.fabric8.openshift.client.OpenShiftClient in project fabric8 by fabric8io.

the class Controller method applyPolicyBinding.

public void applyPolicyBinding(PolicyBinding entity, String sourceName) {
    OpenShiftClient openShiftClient = getOpenShiftClientOrJenkinshift();
    if (openShiftClient != null) {
        String id = getName(entity);
        Objects.notNull(id, "No name for " + entity + " " + sourceName);
        String namespace = KubernetesHelper.getNamespace(entity);
        if (Strings.isNullOrBlank(namespace)) {
            namespace = getNamespace();
        }
        applyNamespace(namespace);
        PolicyBinding old = openShiftClient.policyBindings().inNamespace(namespace).withName(id).get();
        if (isRunning(old)) {
            if (UserConfigurationCompare.configEqual(entity, old)) {
                LOG.info("PolicyBinding has not changed so not doing anything");
            } else {
                if (isRecreateMode()) {
                    LOG.info("Deleting PolicyBinding: " + id);
                    openShiftClient.policyBindings().inNamespace(namespace).withName(id).delete();
                    doCreatePolicyBinding(entity, namespace, sourceName);
                } else {
                    LOG.info("Updating PolicyBinding from " + sourceName);
                    try {
                        String resourceVersion = KubernetesHelper.getResourceVersion(old);
                        ObjectMeta metadata = KubernetesHelper.getOrCreateMetadata(entity);
                        metadata.setNamespace(namespace);
                        metadata.setResourceVersion(resourceVersion);
                        Object answer = openShiftClient.policyBindings().inNamespace(namespace).withName(id).replace(entity);
                        logGeneratedEntity("Updated PolicyBinding: ", namespace, entity, answer);
                    } catch (Exception e) {
                        onApplyError("Failed to update PolicyBinding from " + sourceName + ". " + e + ". " + entity, e);
                    }
                }
            }
        } else {
            if (!isAllowCreate()) {
                LOG.warn("Creation disabled so not creating PolicyBinding from " + sourceName + " namespace " + namespace + " name " + getName(entity));
            } else {
                doCreatePolicyBinding(entity, namespace, sourceName);
            }
        }
    }
}
Also used : ObjectMeta(io.fabric8.kubernetes.api.model.ObjectMeta) OpenShiftClient(io.fabric8.openshift.client.OpenShiftClient) JSONObject(org.json.JSONObject) PolicyBinding(io.fabric8.openshift.api.model.PolicyBinding) KubernetesClientException(io.fabric8.kubernetes.client.KubernetesClientException) FileNotFoundException(java.io.FileNotFoundException) OpenShiftNotAvailableException(io.fabric8.openshift.client.OpenShiftNotAvailableException) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) IOException(java.io.IOException)

Example 90 with OpenShiftClient

use of io.fabric8.openshift.client.OpenShiftClient in project fabric8 by fabric8io.

the class Controller method applyRoleBinding.

public void applyRoleBinding(RoleBinding entity, String sourceName) {
    OpenShiftClient openShiftClient = getOpenShiftClientOrJenkinshift();
    if (openShiftClient != null) {
        String id = getName(entity);
        Objects.notNull(id, "No name for " + entity + " " + sourceName);
        String namespace = KubernetesHelper.getNamespace(entity);
        if (Strings.isNullOrBlank(namespace)) {
            namespace = getNamespace();
        }
        applyNamespace(namespace);
        RoleBinding old = openShiftClient.roleBindings().inNamespace(namespace).withName(id).get();
        if (isRunning(old)) {
            if (UserConfigurationCompare.configEqual(entity, old)) {
                LOG.info("RoleBinding has not changed so not doing anything");
            } else {
                if (isRecreateMode()) {
                    LOG.info("Deleting RoleBinding: " + id);
                    openShiftClient.roleBindings().inNamespace(namespace).withName(id).delete();
                    doCreateRoleBinding(entity, namespace, sourceName);
                } else {
                    LOG.info("Updating RoleBinding from " + sourceName);
                    try {
                        String resourceVersion = KubernetesHelper.getResourceVersion(old);
                        ObjectMeta metadata = KubernetesHelper.getOrCreateMetadata(entity);
                        metadata.setNamespace(namespace);
                        metadata.setResourceVersion(resourceVersion);
                        Object answer = openShiftClient.roleBindings().inNamespace(namespace).withName(id).replace(entity);
                        logGeneratedEntity("Updated RoleBinding: ", namespace, entity, answer);
                    } catch (Exception e) {
                        onApplyError("Failed to update RoleBinding from " + sourceName + ". " + e + ". " + entity, e);
                    }
                }
            }
        } else {
            if (!isAllowCreate()) {
                LOG.warn("Creation disabled so not creating RoleBinding from " + sourceName + " namespace " + namespace + " name " + getName(entity));
            } else {
                doCreateRoleBinding(entity, namespace, sourceName);
            }
        }
    }
}
Also used : ObjectMeta(io.fabric8.kubernetes.api.model.ObjectMeta) OpenShiftClient(io.fabric8.openshift.client.OpenShiftClient) JSONObject(org.json.JSONObject) RoleBinding(io.fabric8.openshift.api.model.RoleBinding) KubernetesClientException(io.fabric8.kubernetes.client.KubernetesClientException) FileNotFoundException(java.io.FileNotFoundException) OpenShiftNotAvailableException(io.fabric8.openshift.client.OpenShiftNotAvailableException) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) IOException(java.io.IOException)

Aggregations

OpenShiftClient (io.fabric8.openshift.client.OpenShiftClient)92 KubernetesClientException (io.fabric8.kubernetes.client.KubernetesClientException)31 IOException (java.io.IOException)31 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)23 FileNotFoundException (java.io.FileNotFoundException)21 DefaultOpenShiftClient (io.fabric8.openshift.client.DefaultOpenShiftClient)20 OpenShiftNotAvailableException (io.fabric8.openshift.client.OpenShiftNotAvailableException)20 Test (org.junit.Test)17 Deployment (io.fabric8.kubernetes.api.model.extensions.Deployment)16 JSONObject (org.json.JSONObject)16 NonNamespaceOperation (io.fabric8.kubernetes.client.dsl.NonNamespaceOperation)15 DeploymentConfig (io.fabric8.openshift.api.model.DeploymentConfig)15 Route (io.fabric8.openshift.api.model.Route)14 Service (io.fabric8.kubernetes.api.model.Service)12 BuildConfig (io.fabric8.openshift.api.model.BuildConfig)12 API (org.wso2.carbon.apimgt.core.models.API)12 Controller (io.fabric8.kubernetes.api.Controller)11 ObjectMeta (io.fabric8.kubernetes.api.model.ObjectMeta)10 HasMetadata (io.fabric8.kubernetes.api.model.HasMetadata)9 BeforeTest (org.testng.annotations.BeforeTest)9