Search in sources :

Example 16 with Service

use of io.fabric8.knative.serving.v1.Service in project carbon-apimgt by wso2.

the class ServiceDiscovererKubernetes method listServices.

/**
 * {@inheritDoc}
 */
@Override
public List<Endpoint> listServices() throws ServiceDiscoveryException {
    List<Endpoint> endpointList = new ArrayList<>();
    if (client != null) {
        log.debug("Looking for services in all namespaces");
        try {
            List<Service> serviceList = client.services().inNamespace(null).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);
        }
    }
    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 17 with Service

use of io.fabric8.knative.serving.v1.Service in project carbon-apimgt by wso2.

the class ServiceDiscovererKubernetes method listServices.

/**
 * {@inheritDoc}
 */
@Override
public List<Endpoint> listServices(String namespace) throws ServiceDiscoveryException {
    List<Endpoint> endpointList = new ArrayList<>();
    if (client != null) {
        log.debug("Looking for services in namespace {}", namespace);
        try {
            List<Service> serviceList = client.services().inNamespace(namespace).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);
        }
    }
    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 18 with Service

use of io.fabric8.knative.serving.v1.Service in project carbon-apimgt by wso2.

the class ServiceDiscovererKubernetes method buildConfig.

/**
 * Builds the Config required by DefaultOpenShiftClient
 * Also sets the system properties
 * (1) to not refer .kube/config file and
 * (2) the client to use service account procedure to get authenticated and authorised
 *
 * @return {@link io.fabric8.kubernetes.client.Config} object to build the client
 * @throws ServiceDiscoveryException if an error occurs while building the config using externally stored token
 */
private Config buildConfig(Map<String, String> implParameters) throws ServiceDiscoveryException, APIMgtDAOException {
    System.setProperty(TRY_KUBE_CONFIG, "false");
    System.setProperty(TRY_SERVICE_ACCOUNT, "true");
    /*
         *  Common to both situations,
         *      - Token found inside APIM pod
         *      - Token stored in APIM resources/security folder }
         */
    ConfigBuilder configBuilder = new ConfigBuilder().withMasterUrl(implParameters.get(MASTER_URL)).withCaCertFile(implParameters.get(CA_CERT_PATH));
    /*
         *  Check if a service account token File Name is given in the configuration
         *      - if not : assume APIM is running inside a pod and look for the pod's token
         */
    String externalSATokenFileName = implParameters.get(EXTERNAL_SA_TOKEN_FILE_NAME);
    if (StringUtils.isEmpty(externalSATokenFileName)) {
        log.debug("Looking for service account token in " + POD_MOUNTED_SA_TOKEN_FILE_PATH);
        String podMountedSAToken = APIFileUtils.readFileContentAsText(implParameters.get(POD_MOUNTED_SA_TOKEN_FILE_PATH));
        return configBuilder.withOauthToken(podMountedSAToken).build();
    } else {
        log.info("Using externally stored service account token");
        return configBuilder.withOauthToken(resolveToken("encrypted" + externalSATokenFileName)).build();
    }
}
Also used : ConfigBuilder(io.fabric8.kubernetes.client.ConfigBuilder)

Example 19 with Service

use of io.fabric8.knative.serving.v1.Service in project carbon-apimgt by wso2.

the class KubernetesGatewayImpl method createDeploymentResource.

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

Example 20 with Service

use of io.fabric8.knative.serving.v1.Service in project carbon-apimgt by wso2.

the class KubernetesGatewayImpl method createIngressResource.

/**
 * Create an Ingress resource in cms
 *
 * @param ingressTemplate Ingress template as a String
 * @param ingressName     Name of the ingress
 * @throws ContainerBasedGatewayException if failed to create a service
 */
private void createIngressResource(String ingressTemplate, String ingressName) throws ContainerBasedGatewayException {
    HasMetadata resource = getResourcesFromTemplate(ingressTemplate);
    try {
        if (resource instanceof Ingress) {
            // check whether there are existing service already
            if (client.extensions().ingresses().inNamespace(namespace).withName(ingressName).get() == null) {
                log.debug("Deploying in CMS type: {} and the Ingress resource definition: {} ", cmsType, ingressTemplate);
                Ingress ingress = (Ingress) resource;
                Ingress result = client.extensions().ingresses().inNamespace(namespace).create(ingress);
                log.info("Created Ingress : " + result.getMetadata().getName() + " in Namespace : " + result.getMetadata().getNamespace() + " in " + cmsType);
            } else {
                log.info("There exist an ingress with the same name in " + cmsType + ". Ingress name : " + ingressName);
            }
        } 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 ingress in " + cmsType + "!", e, ExceptionCodes.DEDICATED_CONTAINER_GATEWAY_CREATION_FAILED);
    }
}
Also used : HasMetadata(io.fabric8.kubernetes.api.model.HasMetadata) Ingress(io.fabric8.kubernetes.api.model.extensions.Ingress) ContainerBasedGatewayException(org.wso2.carbon.apimgt.core.exception.ContainerBasedGatewayException) KubernetesClientException(io.fabric8.kubernetes.client.KubernetesClientException)

Aggregations

Service (io.fabric8.kubernetes.api.model.Service)142 Test (org.junit.Test)93 File (java.io.File)60 IOException (java.io.IOException)54 ArrayList (java.util.ArrayList)54 HasMetadata (io.fabric8.kubernetes.api.model.HasMetadata)48 KubernetesClient (io.fabric8.kubernetes.client.KubernetesClient)43 HashMap (java.util.HashMap)41 ServiceBuilder (io.fabric8.kubernetes.api.model.ServiceBuilder)38 DefaultKubernetesClient (io.fabric8.kubernetes.client.DefaultKubernetesClient)33 OpenShiftClient (io.fabric8.openshift.client.OpenShiftClient)33 Pod (io.fabric8.kubernetes.api.model.Pod)31 KubernetesClientException (io.fabric8.kubernetes.client.KubernetesClientException)30 ServicePort (io.fabric8.kubernetes.api.model.ServicePort)29 Deployment (io.fabric8.kubernetes.api.model.extensions.Deployment)25 List (java.util.List)25 Map (java.util.Map)25 FileInputStream (java.io.FileInputStream)22 ConfigMap (io.fabric8.kubernetes.api.model.ConfigMap)20 ServicePortBuilder (io.fabric8.kubernetes.api.model.ServicePortBuilder)20