Search in sources :

Example 6 with KubernetesClientException

use of io.fabric8.kubernetes.client.KubernetesClientException in project carbon-apimgt by wso2.

the class ServiceDiscovererKubernetes method listServices.

/**
 * {@inheritDoc}
 */
@Override
public List<Endpoint> listServices(Map<String, String> criteria) throws ServiceDiscoveryException {
    List<Endpoint> endpointList = new ArrayList<>();
    if (client != null) {
        log.debug("Looking for services, with the specified labels, in all namespaces");
        try {
            // namespace has to be set to null to check all allowed namespaces
            List<Service> serviceList = client.services().inNamespace(null).withLabels(criteria).list().getItems();
            addServicesToEndpointList(serviceList, endpointList);
        } catch (KubernetesClientException | MalformedURLException e) {
            String msg = "Error occurred while trying to list services using Kubernetes client";
            throw new ServiceDiscoveryException(msg, e, ExceptionCodes.ERROR_WHILE_TRYING_TO_DISCOVER_SERVICES);
        } catch (NoSuchMethodError e) {
            String msg = "Filtering criteria in the deployment yaml includes unwanted characters";
            throw new ServiceDiscoveryException(msg, e, ExceptionCodes.ERROR_WHILE_TRYING_TO_DISCOVER_SERVICES);
        }
    }
    return endpointList;
}
Also used : MalformedURLException(java.net.MalformedURLException) Endpoint(org.wso2.carbon.apimgt.core.models.Endpoint) ArrayList(java.util.ArrayList) Service(io.fabric8.kubernetes.api.model.Service) ServiceDiscoveryException(org.wso2.carbon.apimgt.core.exception.ServiceDiscoveryException) KubernetesClientException(io.fabric8.kubernetes.client.KubernetesClientException)

Example 7 with KubernetesClientException

use of io.fabric8.kubernetes.client.KubernetesClientException in project carbon-apimgt by wso2.

the class ServiceDiscovererKubernetes method initImpl.

/**
 * Initializes OpenShiftClient (extended KubernetesClient) and sets the necessary parameters
 *
 * @param implParameters implementation parameters added by the super class #initImpl(java.util.Map) method
 * @throws ServiceDiscoveryException if an error occurs while initializing the client
 */
@Override
public void initImpl(Map<String, String> implParameters) throws ServiceDiscoveryException {
    try {
        setClient(new DefaultOpenShiftClient(buildConfig(implParameters)));
    } catch (KubernetesClientException | APIMgtDAOException e) {
        String msg = "Error occurred while creating Kubernetes client";
        throw new ServiceDiscoveryException(msg, e, ExceptionCodes.ERROR_INITIALIZING_SERVICE_DISCOVERY);
    } catch (ArrayIndexOutOfBoundsException e) {
        String msg = "Error occurred while reading filtering criteria from the configuration";
        throw new ServiceDiscoveryException(msg, e, ExceptionCodes.ERROR_INITIALIZING_SERVICE_DISCOVERY);
    }
    includeClusterIP = Boolean.parseBoolean(implParameters.get(INCLUDE_CLUSTER_IPS));
    includeExternalNameTypeServices = Boolean.parseBoolean(implParameters.get(INCLUDE_EXTERNAL_NAME_SERVICES));
}
Also used : APIMgtDAOException(org.wso2.carbon.apimgt.core.exception.APIMgtDAOException) ServiceDiscoveryException(org.wso2.carbon.apimgt.core.exception.ServiceDiscoveryException) DefaultOpenShiftClient(io.fabric8.openshift.client.DefaultOpenShiftClient) KubernetesClientException(io.fabric8.kubernetes.client.KubernetesClientException)

Example 8 with KubernetesClientException

use of io.fabric8.kubernetes.client.KubernetesClientException in project carbon-apimgt by wso2.

the class KubernetesGatewayImpl method createServiceResource.

/**
 * Create a service in cms
 *
 * @param serviceTemplate Service template as a String
 * @param serviceName     Name of the service
 * @throws ContainerBasedGatewayException if failed to create a service
 */
private void createServiceResource(String serviceTemplate, String serviceName) throws ContainerBasedGatewayException {
    HasMetadata resource = getResourcesFromTemplate(serviceTemplate);
    try {
        if (resource instanceof Service) {
            // check whether there are existing service already
            if (client.services().inNamespace(namespace).withName(serviceName).get() == null) {
                log.debug("Deploying in CMS type: {} and the Service resource definition: {} ", cmsType, serviceTemplate);
                Service service = (Service) resource;
                Service result = client.services().inNamespace(namespace).create(service);
                log.info("Created Service : " + result.getMetadata().getName() + " in Namespace : " + result.getMetadata().getNamespace() + " in " + cmsType);
            } else {
                log.info("There exist a service with the same name in " + cmsType + ". Service name : " + serviceName);
            }
        } else {
            throw new ContainerBasedGatewayException("Loaded Resource is not a Service in " + cmsType + "! " + resource, ExceptionCodes.LOADED_RESOURCE_DEFINITION_IS_NOT_VALID);
        }
    } catch (KubernetesClientException e) {
        throw new ContainerBasedGatewayException("Error while creating container based gateway service in " + cmsType + "!", e, ExceptionCodes.DEDICATED_CONTAINER_GATEWAY_CREATION_FAILED);
    }
}
Also used : HasMetadata(io.fabric8.kubernetes.api.model.HasMetadata) Service(io.fabric8.kubernetes.api.model.Service) ContainerBasedGatewayException(org.wso2.carbon.apimgt.core.exception.ContainerBasedGatewayException) KubernetesClientException(io.fabric8.kubernetes.client.KubernetesClientException)

Example 9 with KubernetesClientException

use of io.fabric8.kubernetes.client.KubernetesClientException in project carbon-apimgt by wso2.

the class KubernetesGatewayImpl method removeContainerBasedGateway.

/**
 * @see ContainerBasedGatewayGenerator#removeContainerBasedGateway(String, API) (String)
 */
@Override
public void removeContainerBasedGateway(String label, API api) throws ContainerBasedGatewayException {
    try {
        client.services().inNamespace(namespace).withLabel(ContainerBasedGatewayConstants.GATEWAY, label).delete();
        client.extensions().deployments().inNamespace(namespace).withLabel(ContainerBasedGatewayConstants.GATEWAY, label).delete();
        client.extensions().ingresses().inNamespace(namespace).withLabel(ContainerBasedGatewayConstants.GATEWAY, label).delete();
        log.info(String.format("Completed deleting the container gateway related %s deployment, service and " + "ingress resources.", cmsType));
    } catch (KubernetesClientException e) {
        throw new ContainerBasedGatewayException("Error while removing container based gateway", e, ExceptionCodes.CONTAINER_GATEWAY_REMOVAL_FAILED);
    }
}
Also used : ContainerBasedGatewayException(org.wso2.carbon.apimgt.core.exception.ContainerBasedGatewayException) KubernetesClientException(io.fabric8.kubernetes.client.KubernetesClientException)

Example 10 with KubernetesClientException

use of io.fabric8.kubernetes.client.KubernetesClientException in project carbon-apimgt by wso2.

the class KubernetesGatewayImpl method initImpl.

/**
 * @see ContainerBasedGatewayGenerator#initImpl(Map)
 */
@Override
void initImpl(Map<String, String> implParameters) throws ContainerBasedGatewayException {
    try {
        setValues(implParameters);
        setClient(new DefaultOpenShiftClient(buildConfig()));
    } catch (KubernetesClientException e) {
        String msg = "Error occurred while creating Default Openshift Client";
        throw new ContainerBasedGatewayException(msg, e, ExceptionCodes.ERROR_INITIALIZING_DEDICATED_CONTAINER_BASED_GATEWAY);
    }
}
Also used : ContainerBasedGatewayException(org.wso2.carbon.apimgt.core.exception.ContainerBasedGatewayException) DefaultOpenShiftClient(io.fabric8.openshift.client.DefaultOpenShiftClient) KubernetesClientException(io.fabric8.kubernetes.client.KubernetesClientException)

Aggregations

KubernetesClientException (io.fabric8.kubernetes.client.KubernetesClientException)10 Service (io.fabric8.kubernetes.api.model.Service)5 ContainerBasedGatewayException (org.wso2.carbon.apimgt.core.exception.ContainerBasedGatewayException)5 ServiceDiscoveryException (org.wso2.carbon.apimgt.core.exception.ServiceDiscoveryException)5 MalformedURLException (java.net.MalformedURLException)4 ArrayList (java.util.ArrayList)4 Endpoint (org.wso2.carbon.apimgt.core.models.Endpoint)4 HasMetadata (io.fabric8.kubernetes.api.model.HasMetadata)3 DefaultOpenShiftClient (io.fabric8.openshift.client.DefaultOpenShiftClient)2 Deployment (io.fabric8.kubernetes.api.model.extensions.Deployment)1 Ingress (io.fabric8.kubernetes.api.model.extensions.Ingress)1 APIMgtDAOException (org.wso2.carbon.apimgt.core.exception.APIMgtDAOException)1