Search in sources :

Example 6 with ServiceDiscoveryException

use of org.wso2.carbon.apimgt.core.exception.ServiceDiscoveryException 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 7 with ServiceDiscoveryException

use of org.wso2.carbon.apimgt.core.exception.ServiceDiscoveryException in project carbon-apimgt by wso2.

the class ServiceDiscovererKubernetesTestCase method testInitWhileExternalTokenFileNameNotGiven.

@Test(description = "Test init method when external service account token file name is NOT given")
public void testInitWhileExternalTokenFileNameNotGiven() throws Exception {
    OpenShiftClient openShiftClient = Mockito.mock(OpenShiftClient.class);
    ServiceDiscovererKubernetes sdKubernetes = new ServiceDiscovererKubernetes();
    sdKubernetes.setClient(openShiftClient);
    try {
        sdKubernetes.initImpl(createImplParametersMap(""));
    } catch (ServiceDiscoveryException e) {
        // since pod's token is then searched, this is exception msg we get
        Assert.assertEquals(e.getCause().getMessage(), "Error while reading file /var/run/secrets/kubernetes.io/serviceaccount/token");
    }
}
Also used : OpenShiftClient(io.fabric8.openshift.client.OpenShiftClient) ServiceDiscoveryException(org.wso2.carbon.apimgt.core.exception.ServiceDiscoveryException) Test(org.testng.annotations.Test) BeforeTest(org.testng.annotations.BeforeTest)

Example 8 with ServiceDiscoveryException

use of org.wso2.carbon.apimgt.core.exception.ServiceDiscoveryException in project carbon-apimgt by wso2.

the class ServiceDiscovererKubernetesTestCase method testInitWhileExternalTokenFileNameGiven.

@Test(description = "Test init method with external service account token file name")
public void testInitWhileExternalTokenFileNameGiven() throws Exception {
    OpenShiftClient openShiftClient = Mockito.mock(OpenShiftClient.class);
    ServiceDiscovererKubernetes sdKubernetes = new ServiceDiscovererKubernetes();
    sdKubernetes.setClient(openShiftClient);
    try {
        sdKubernetes.initImpl(createImplParametersMap("TestK8Token"));
    } catch (ServiceDiscoveryException e) {
        Assert.assertEquals(e.getCause().getMessage(), "File to decrypt does not exist");
    }
}
Also used : OpenShiftClient(io.fabric8.openshift.client.OpenShiftClient) ServiceDiscoveryException(org.wso2.carbon.apimgt.core.exception.ServiceDiscoveryException) Test(org.testng.annotations.Test) BeforeTest(org.testng.annotations.BeforeTest)

Example 9 with ServiceDiscoveryException

use of org.wso2.carbon.apimgt.core.exception.ServiceDiscoveryException in project carbon-apimgt by wso2.

the class APIPublisherImpl method discoverServiceEndpoints.

/**
 * Discover and return a list of service endpoints, of all systems that were included in the config
 *
 * @return {@code List<Endpoint>}   list of all service endpoints
 * @throws APIManagementException If an error occurred while discovering services
 */
@Override
public List<Endpoint> discoverServiceEndpoints() throws APIManagementException {
    List<Endpoint> discoveredEndpointList = new ArrayList();
    try {
        ServiceDiscoveryConfigurations serviceDiscoveryConfig = ServiceDiscoveryConfigBuilder.getServiceDiscoveryConfiguration();
        // If service discovery not enabled - do not proceed
        if (!serviceDiscoveryConfig.isServiceDiscoveryEnabled()) {
            log.error("Service Discovery not enabled");
            return discoveredEndpointList;
        }
        List<ServiceDiscoveryImplConfig> implConfigList = serviceDiscoveryConfig.getImplementationsList();
        for (ServiceDiscoveryImplConfig implConfig : implConfigList) {
            // Every implConfig has two elements. The implClass and the implParameters.
            /* Get the implClass instance */
            String implClassName = implConfig.getImplClass();
            Class implClass = Class.forName(implClassName);
            ServiceDiscoverer serviceDiscoverer = (ServiceDiscoverer) implClass.newInstance();
            /* Pass the implParameters to the above instance */
            serviceDiscoverer.init(implConfig.getImplParameters());
            /*
                 * The .init() method above sets the filtering parameters (if provided)
                 * to the ServiceDiscoverer impl class instance.
                 *
                 * Let's check whether those filtering parameters : "namespace" and/or "criteria" are set,
                 * and call ServiceDiscoverer Impl class's #listServices method accordingly
                 */
            String namespaceFilter = serviceDiscoverer.getNamespaceFilter();
            Map<String, String> criteriaFilter = serviceDiscoverer.getCriteriaFilter();
            List<Endpoint> subDiscoveredEndpointList;
            if (namespaceFilter == null && criteriaFilter == null) {
                // both not set
                subDiscoveredEndpointList = serviceDiscoverer.listServices();
            } else if (namespaceFilter != null && criteriaFilter != null) {
                // both set
                subDiscoveredEndpointList = serviceDiscoverer.listServices(namespaceFilter, criteriaFilter);
            } else if (namespaceFilter != null) {
                // only "namespace" is set
                subDiscoveredEndpointList = serviceDiscoverer.listServices(namespaceFilter);
            } else {
                // remaining -> only "criteria" is set
                subDiscoveredEndpointList = serviceDiscoverer.listServices(criteriaFilter);
            }
            if (subDiscoveredEndpointList != null) {
                discoveredEndpointList.addAll(subDiscoveredEndpointList);
            }
        }
    } catch (ServiceDiscoveryException e) {
        String msg = "Error while Discovering Service Endpoints";
        throw new APIManagementException(msg, e, e.getErrorHandler());
    } catch (ClassNotFoundException | InstantiationException | IllegalAccessException e) {
        String msg = "Error while Loading Service Discovery Impl Class";
        throw new APIManagementException(msg, e, ExceptionCodes.ERROR_LOADING_SERVICE_DISCOVERY_IMPL_CLASS);
    }
    return discoveredEndpointList;
}
Also used : ServiceDiscoveryImplConfig(org.wso2.carbon.apimgt.core.configuration.models.ServiceDiscoveryImplConfig) ArrayList(java.util.ArrayList) Endpoint(org.wso2.carbon.apimgt.core.models.Endpoint) APIManagementException(org.wso2.carbon.apimgt.core.exception.APIManagementException) ServiceDiscoveryException(org.wso2.carbon.apimgt.core.exception.ServiceDiscoveryException) ServiceDiscoveryConfigurations(org.wso2.carbon.apimgt.core.configuration.models.ServiceDiscoveryConfigurations)

Aggregations

ServiceDiscoveryException (org.wso2.carbon.apimgt.core.exception.ServiceDiscoveryException)9 KubernetesClientException (io.fabric8.kubernetes.client.KubernetesClientException)5 ArrayList (java.util.ArrayList)5 Endpoint (org.wso2.carbon.apimgt.core.models.Endpoint)5 Service (io.fabric8.kubernetes.api.model.Service)4 MalformedURLException (java.net.MalformedURLException)4 OpenShiftClient (io.fabric8.openshift.client.OpenShiftClient)2 BeforeTest (org.testng.annotations.BeforeTest)2 Test (org.testng.annotations.Test)2 APIManagementException (org.wso2.carbon.apimgt.core.exception.APIManagementException)2 DefaultOpenShiftClient (io.fabric8.openshift.client.DefaultOpenShiftClient)1 ServiceDiscoveryConfigurations (org.wso2.carbon.apimgt.core.configuration.models.ServiceDiscoveryConfigurations)1 ServiceDiscoveryImplConfig (org.wso2.carbon.apimgt.core.configuration.models.ServiceDiscoveryImplConfig)1 APIMgtDAOException (org.wso2.carbon.apimgt.core.exception.APIMgtDAOException)1