use of io.fabric8.patch.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;
}
use of io.fabric8.patch.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;
}
use of io.fabric8.patch.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();
}
}
use of io.fabric8.patch.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);
}
}
use of io.fabric8.patch.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);
}
}
Aggregations