Search in sources :

Example 96 with Service

use of io.fabric8.knative.serving.v1.Service 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)

Example 97 with Service

use of io.fabric8.knative.serving.v1.Service in project fabric8 by fabric8io.

the class AbstractServiceRegistar method createServiceDefinition.

private BeanDefinitionHolder createServiceDefinition(Service service, String alias, String protocol, String port, Class type) {
    BeanDefinitionBuilder builder = BeanDefinitionBuilder.genericBeanDefinition(KubernetesServiceFactoryBean.class);
    builder.addPropertyValue("name", alias);
    builder.addPropertyValue("service", service);
    builder.addPropertyValue("port", port);
    builder.addPropertyValue("type", type.getCanonicalName());
    builder.setAutowireMode(Autowire.BY_TYPE.value());
    // Add protocol qualifier
    builder.getBeanDefinition().addQualifier(new AutowireCandidateQualifier(ServiceName.class, KubernetesHelper.getName(service)));
    builder.getBeanDefinition().addQualifier(new AutowireCandidateQualifier(Protocol.class, protocol));
    builder.getBeanDefinition().addQualifier(new AutowireCandidateQualifier(PortName.class, port != null ? port : ""));
    return new BeanDefinitionHolder(builder.getBeanDefinition(), alias);
}
Also used : BeanDefinitionBuilder(org.springframework.beans.factory.support.BeanDefinitionBuilder) ServiceName(io.fabric8.annotations.ServiceName) BeanDefinitionHolder(org.springframework.beans.factory.config.BeanDefinitionHolder) Protocol(io.fabric8.annotations.Protocol) AutowireCandidateQualifier(org.springframework.beans.factory.support.AutowireCandidateQualifier) PortName(io.fabric8.annotations.PortName)

Example 98 with Service

use of io.fabric8.knative.serving.v1.Service in project fabric8 by fabric8io.

the class ConfigCompareServiceTest method testServicesSelectorNotEqual.

@Test
public void testServicesSelectorNotEqual() throws Exception {
    Service entity1 = new ServiceBuilder().withNewMetadata().withName("foo").addToLabels("label1", "value1").addToLabels("label2", "value2").endMetadata().withNewSpec().addToSelector("label1", "value1").addToSelector("label2", "value2").addNewPort().withPort(123).withNewTargetPort(456).endPort().endSpec().build();
    Service entity2 = new ServiceBuilder().withNewMetadata().withName("foo").addToLabels("label2", "value2").addToLabels("label1", "value1").endMetadata().withNewSpec().addToSelector("label1", "value1").addToSelector("notSame", "value2").addNewPort().withPort(123).withNewTargetPort(456).endPort().endSpec().build();
    assertCompareConfig(entity1, entity2, false);
}
Also used : Service(io.fabric8.kubernetes.api.model.Service) ServiceBuilder(io.fabric8.kubernetes.api.model.ServiceBuilder) Test(org.junit.Test)

Example 99 with Service

use of io.fabric8.knative.serving.v1.Service in project fabric8 by fabric8io.

the class ConfigCompareServiceTest method testServicesPortNotEqual.

@Test
public void testServicesPortNotEqual() throws Exception {
    Service entity1 = new ServiceBuilder().withNewMetadata().withName("foo").addToLabels("label1", "value1").addToLabels("label2", "value2").endMetadata().withNewSpec().addToSelector("label1", "value1").addToSelector("label2", "value2").addNewPort().withPort(123).withNewTargetPort(456).endPort().endSpec().build();
    Service entity2 = new ServiceBuilder().withNewMetadata().withName("foo").addToLabels("label2", "value2").addToLabels("label1", "value1").endMetadata().withNewSpec().addToSelector("label1", "value1").addToSelector("label2", "value2").addNewPort().withPort(456).withNewTargetPort(456).endPort().endSpec().build();
    assertCompareConfig(entity1, entity2, false);
}
Also used : Service(io.fabric8.kubernetes.api.model.Service) ServiceBuilder(io.fabric8.kubernetes.api.model.ServiceBuilder) Test(org.junit.Test)

Example 100 with Service

use of io.fabric8.knative.serving.v1.Service in project fabric8 by fabric8io.

the class ConfigCompareServiceTest method testServicesContainerPortNotEqual.

@Test
public void testServicesContainerPortNotEqual() throws Exception {
    Service entity1 = new ServiceBuilder().withNewMetadata().withName("foo").addToLabels("label1", "value1").addToLabels("label2", "value2").endMetadata().withNewSpec().addToSelector("label1", "value1").addToSelector("label2", "value2").addNewPort().withPort(123).withNewTargetPort(456).endPort().endSpec().build();
    Service entity2 = new ServiceBuilder().withNewMetadata().withName("foo").addToLabels("label2", "value2").addToLabels("label1", "value1").endMetadata().withNewSpec().addToSelector("label1", "value1").addToSelector("label2", "value2").addNewPort().withPort(123).withNewTargetPort(555).endPort().endSpec().build();
    assertCompareConfig(entity1, entity2, false);
}
Also used : Service(io.fabric8.kubernetes.api.model.Service) ServiceBuilder(io.fabric8.kubernetes.api.model.ServiceBuilder) Test(org.junit.Test)

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