Search in sources :

Example 6 with ServiceName

use of io.fabric8.annotations.ServiceName in project carbon-apimgt by wso2.

the class KubernetesGatewayImplTestCase method createService.

/**
 * Create Service resource
 *
 * @param openShiftClient       Openshift client
 * @param nonNamespaceOperation NonNamespaceOperation instance
 */
private Service createService(OpenShiftClient openShiftClient, NonNamespaceOperation nonNamespaceOperation) {
    BaseOperation baseOperation = Mockito.mock(BaseOperation.class);
    String serviceName = ContainerBasedGatewayConstants.PRIVATE_JET_API_PREFIX + LABEL_SUFFIX + ContainerBasedGatewayConstants.CMS_SERVICE_SUFFIX;
    Mockito.when(openShiftClient.services().inNamespace(NAMESPACE)).thenReturn(nonNamespaceOperation);
    Mockito.when(nonNamespaceOperation.withName(serviceName)).thenReturn(baseOperation);
    Mockito.when(baseOperation.get()).thenReturn(null);
    Service service = Mockito.mock(Service.class, Mockito.RETURNS_DEEP_STUBS);
    return service;
}
Also used : Service(io.fabric8.kubernetes.api.model.Service) BaseOperation(io.fabric8.kubernetes.client.dsl.base.BaseOperation)

Example 7 with ServiceName

use of io.fabric8.annotations.ServiceName in project carbon-apimgt by wso2.

the class KubernetesGatewayImplTestCase method testCreateServiceResourceForKubernetesClientException.

@Test
public void testCreateServiceResourceForKubernetesClientException() throws Exception {
    OpenShiftClient openShiftClient = Mockito.mock(OpenShiftClient.class, Mockito.RETURNS_DEEP_STUBS);
    KubernetesGatewayImpl kubernetesGateway = getKubernetesGatewayImpl(openShiftClient);
    Mockito.when(openShiftClient.load(Mockito.any()).get()).thenReturn(getServiceResources());
    NonNamespaceOperation nonNamespaceOperationService = Mockito.mock(NonNamespaceOperation.class);
    BaseOperation baseOperationService = Mockito.mock(BaseOperation.class);
    String serviceName = ContainerBasedGatewayConstants.PRIVATE_JET_API_PREFIX + LABEL_SUFFIX + ContainerBasedGatewayConstants.CMS_SERVICE_SUFFIX;
    Mockito.when(openShiftClient.services().inNamespace(NAMESPACE)).thenReturn(nonNamespaceOperationService);
    Mockito.when(nonNamespaceOperationService.withName(serviceName)).thenReturn(baseOperationService);
    Mockito.when(baseOperationService.get()).thenThrow(KubernetesClientException.class);
    API api = SampleTestObjectCreator.createDefaultAPI().context("").build();
    try {
        kubernetesGateway.createContainerGateway(ContainerBasedGatewayConstants.PRIVATE_JET_API_PREFIX + LABEL_SUFFIX, api);
    } catch (ContainerBasedGatewayException e) {
        Assert.assertEquals(e.getErrorHandler(), ExceptionCodes.DEDICATED_CONTAINER_GATEWAY_CREATION_FAILED);
    }
}
Also used : OpenShiftClient(io.fabric8.openshift.client.OpenShiftClient) ContainerBasedGatewayException(org.wso2.carbon.apimgt.core.exception.ContainerBasedGatewayException) API(org.wso2.carbon.apimgt.core.models.API) BaseOperation(io.fabric8.kubernetes.client.dsl.base.BaseOperation) NonNamespaceOperation(io.fabric8.kubernetes.client.dsl.NonNamespaceOperation) Test(org.junit.Test)

Example 8 with ServiceName

use of io.fabric8.annotations.ServiceName in project fabric8-maven-plugin by fabric8io.

the class ApplyMojo method createIngressForService.

private Ingress createIngressForService(String routeDomainPostfix, String namespace, Service service) {
    Ingress ingress = null;
    String serviceName = KubernetesHelper.getName(service);
    ServiceSpec serviceSpec = service.getSpec();
    if (serviceSpec != null && Strings.isNotBlank(serviceName) && shouldCreateExternalURLForService(service, serviceName)) {
        String ingressId = serviceName;
        String host = "";
        if (Strings.isNotBlank(routeDomainPostfix)) {
            host = serviceName + "." + namespace + "." + Strings.stripPrefix(routeDomainPostfix, ".");
        }
        List<HTTPIngressPath> paths = new ArrayList<>();
        List<ServicePort> ports = serviceSpec.getPorts();
        if (ports != null) {
            for (ServicePort port : ports) {
                Integer portNumber = port.getPort();
                if (portNumber != null) {
                    HTTPIngressPath path = new HTTPIngressPathBuilder().withNewBackend().withServiceName(serviceName).withServicePort(createIntOrString(portNumber.intValue())).endBackend().build();
                    paths.add(path);
                }
            }
        }
        if (paths.isEmpty()) {
            return ingress;
        }
        ingress = new IngressBuilder().withNewMetadata().withName(ingressId).withNamespace(namespace).endMetadata().withNewSpec().addNewRule().withHost(host).withNewHttp().withPaths(paths).endHttp().endRule().endSpec().build();
        String json;
        try {
            json = KubernetesHelper.toJson(ingress);
        } catch (JsonProcessingException e) {
            json = e.getMessage() + ". object: " + ingress;
        }
        log.debug("Created ingress: " + json);
    }
    return ingress;
}
Also used : ServicePort(io.fabric8.kubernetes.api.model.ServicePort) IngressBuilder(io.fabric8.kubernetes.api.model.extensions.IngressBuilder) ServiceSpec(io.fabric8.kubernetes.api.model.ServiceSpec) ArrayList(java.util.ArrayList) Ingress(io.fabric8.kubernetes.api.model.extensions.Ingress) HTTPIngressPathBuilder(io.fabric8.kubernetes.api.model.extensions.HTTPIngressPathBuilder) KubernetesHelper.createIntOrString(io.fabric8.kubernetes.api.KubernetesHelper.createIntOrString) HTTPIngressPath(io.fabric8.kubernetes.api.model.extensions.HTTPIngressPath) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException)

Example 9 with ServiceName

use of io.fabric8.annotations.ServiceName in project fabric8-maven-plugin by fabric8io.

the class ApplyMojo method serviceHasIngressRule.

/**
 * Returns true if there is an existing ingress rule for the given service
 */
private boolean serviceHasIngressRule(List<Ingress> ingresses, Service service) {
    String serviceName = KubernetesHelper.getName(service);
    for (Ingress ingress : ingresses) {
        IngressSpec spec = ingress.getSpec();
        if (spec == null) {
            break;
        }
        List<IngressRule> rules = spec.getRules();
        if (rules == null) {
            break;
        }
        for (IngressRule rule : rules) {
            HTTPIngressRuleValue http = rule.getHttp();
            if (http == null) {
                break;
            }
            List<HTTPIngressPath> paths = http.getPaths();
            if (paths == null) {
                break;
            }
            for (HTTPIngressPath path : paths) {
                IngressBackend backend = path.getBackend();
                if (backend == null) {
                    break;
                }
                if (Objects.equals(serviceName, backend.getServiceName())) {
                    return true;
                }
            }
        }
    }
    return false;
}
Also used : IngressSpec(io.fabric8.kubernetes.api.model.extensions.IngressSpec) IngressRule(io.fabric8.kubernetes.api.model.extensions.IngressRule) HTTPIngressRuleValue(io.fabric8.kubernetes.api.model.extensions.HTTPIngressRuleValue) Ingress(io.fabric8.kubernetes.api.model.extensions.Ingress) KubernetesHelper.createIntOrString(io.fabric8.kubernetes.api.KubernetesHelper.createIntOrString) HTTPIngressPath(io.fabric8.kubernetes.api.model.extensions.HTTPIngressPath) IngressBackend(io.fabric8.kubernetes.api.model.extensions.IngressBackend)

Example 10 with ServiceName

use of io.fabric8.annotations.ServiceName in project fabric8 by fabric8io.

the class AbstractServiceRegistar method registerBeanDefinitions.

@Override
public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) {
    for (Method method : REFLECTIONS.getMethodsAnnotatedWith(Factory.class)) {
        String methodName = method.getName();
        Class sourceType = getSourceType(method);
        Class targetType = method.getReturnType();
        Class beanType = method.getDeclaringClass();
        BeanDefinitionHolder holder = createConverterBean(beanType, methodName, sourceType, targetType);
        BeanDefinitionReaderUtils.registerBeanDefinition(holder, registry);
    }
    for (Field field : REFLECTIONS.getFieldsAnnotatedWith(ServiceName.class)) {
        Class targetClass = field.getType();
        Alias alias = field.getAnnotation(Alias.class);
        ServiceName name = field.getAnnotation(ServiceName.class);
        PortName port = field.getAnnotation(PortName.class);
        Protocol protocol = field.getAnnotation(Protocol.class);
        External external = field.getAnnotation(External.class);
        String serviceName = name != null ? name.value() : null;
        // We copy the service since we are going to add properties to it.
        Service serviceInstance = new ServiceBuilder(getService(serviceName)).build();
        String servicePort = port != null ? port.value() : null;
        String serviceProtocol = protocol != null ? protocol.value() : DEFAULT_PROTOCOL;
        Boolean serviceExternal = external != null && external.value();
        String serviceAlias = alias != null ? alias.value() : createAlias(serviceName, targetClass, serviceProtocol, servicePort, serviceExternal);
        // Add annotation info as additional properties
        serviceInstance.getAdditionalProperties().put(ALIAS, serviceAlias);
        serviceInstance.getAdditionalProperties().put(PROTOCOL, serviceProtocol);
        serviceInstance.getAdditionalProperties().put(EXTERNAL, serviceExternal);
        // We don't want to add a fallback value to the attributes.
        if (port != null) {
            serviceInstance.getAdditionalProperties().put(PORT, servicePort);
        }
        BeanDefinitionHolder holder = createServiceDefinition(serviceInstance, serviceAlias, serviceProtocol, servicePort, targetClass);
        BeanDefinitionReaderUtils.registerBeanDefinition(holder, registry);
    }
}
Also used : Service(io.fabric8.kubernetes.api.model.Service) Method(java.lang.reflect.Method) PortName(io.fabric8.annotations.PortName) ServiceBuilder(io.fabric8.kubernetes.api.model.ServiceBuilder) Field(java.lang.reflect.Field) ServiceName(io.fabric8.annotations.ServiceName) Alias(io.fabric8.annotations.Alias) BeanDefinitionHolder(org.springframework.beans.factory.config.BeanDefinitionHolder) External(io.fabric8.annotations.External) Protocol(io.fabric8.annotations.Protocol)

Aggregations

Service (io.fabric8.kubernetes.api.model.Service)13 ArrayList (java.util.ArrayList)7 ServiceBuilder (io.fabric8.kubernetes.api.model.ServiceBuilder)6 OpenShiftClient (io.fabric8.openshift.client.OpenShiftClient)6 ServicePort (io.fabric8.kubernetes.api.model.ServicePort)5 Ingress (io.fabric8.kubernetes.api.model.extensions.Ingress)5 KubernetesClient (io.fabric8.kubernetes.client.KubernetesClient)5 ServiceSpec (io.fabric8.kubernetes.api.model.ServiceSpec)4 HTTPIngressPath (io.fabric8.kubernetes.api.model.extensions.HTTPIngressPath)4 Route (io.fabric8.openshift.api.model.Route)4 List (java.util.List)4 ServicePortBuilder (io.fabric8.kubernetes.api.model.ServicePortBuilder)3 BaseOperation (io.fabric8.kubernetes.client.dsl.base.BaseOperation)3 Map (java.util.Map)3 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)2 Alias (io.fabric8.annotations.Alias)2 External (io.fabric8.annotations.External)2 PortName (io.fabric8.annotations.PortName)2 Protocol (io.fabric8.annotations.Protocol)2 ServiceName (io.fabric8.annotations.ServiceName)2