Search in sources :

Example 11 with ServiceBuilder

use of io.fabric8.knative.serving.v1.ServiceBuilder in project keycloak by keycloak.

the class RealmImportE2EIT method testWorkingRealmImport.

@Test
public void testWorkingRealmImport() {
    Log.info(((operatorDeployment == OperatorDeployment.remote) ? "Remote " : "Local ") + "Run Test :" + namespace);
    // Arrange
    k8sclient.load(getClass().getResourceAsStream("/example-postgres.yaml")).inNamespace(namespace).createOrReplace();
    k8sclient.load(getClass().getResourceAsStream("/example-keycloak.yml")).inNamespace(namespace).createOrReplace();
    k8sclient.services().inNamespace(namespace).create(new ServiceBuilder().withNewMetadata().withName(KEYCLOAK_SERVICE_NAME).withNamespace(namespace).endMetadata().withNewSpec().withSelector(Map.of("app", "keycloak")).addNewPort().withPort(KEYCLOAK_PORT).endPort().endSpec().build());
    // Act
    k8sclient.load(getClass().getResourceAsStream("/example-realm.yaml")).inNamespace(namespace).createOrReplace();
    // Assert
    var crSelector = k8sclient.resources(KeycloakRealmImport.class).inNamespace(namespace).withName("example-count0-kc");
    Awaitility.await().atMost(3, MINUTES).pollDelay(5, SECONDS).ignoreExceptions().untilAsserted(() -> {
        var conditions = crSelector.get().getStatus().getConditions();
        assertThat(getCondition(conditions, DONE).getStatus()).isFalse();
        assertThat(getCondition(conditions, STARTED).getStatus()).isTrue();
        assertThat(getCondition(conditions, HAS_ERRORS).getStatus()).isFalse();
    });
    Awaitility.await().atMost(3, MINUTES).pollDelay(5, SECONDS).ignoreExceptions().untilAsserted(() -> {
        var conditions = crSelector.get().getStatus().getConditions();
        assertThat(getCondition(conditions, DONE).getStatus()).isTrue();
        assertThat(getCondition(conditions, STARTED).getStatus()).isFalse();
        assertThat(getCondition(conditions, HAS_ERRORS).getStatus()).isFalse();
    });
    String url = "http://" + KEYCLOAK_SERVICE_NAME + "." + namespace + ":" + KEYCLOAK_PORT + "/realms/count0";
    Awaitility.await().atMost(5, MINUTES).untilAsserted(() -> {
        try {
            Log.info("Starting curl Pod to test if the realm is available");
            Pod curlPod = k8sclient.run().inNamespace(namespace).withRunConfig(new RunConfigBuilder().withArgs("-s", "-o", "/dev/null", "-w", "%{http_code}", url).withName("curl").withImage("curlimages/curl:7.78.0").withRestartPolicy("Never").build()).done();
            Log.info("Waiting for curl Pod to finish running");
            Awaitility.await().atMost(2, MINUTES).until(() -> {
                String phase = k8sclient.pods().inNamespace(namespace).withName("curl").get().getStatus().getPhase();
                return phase.equals("Succeeded") || phase.equals("Failed");
            });
            String curlOutput = k8sclient.pods().inNamespace(namespace).withName(curlPod.getMetadata().getName()).getLog();
            Log.info("Output from curl: '" + curlOutput + "'");
            assertThat(curlOutput).isEqualTo("200");
        } catch (KubernetesClientException ex) {
            throw new AssertionError(ex);
        } finally {
            Log.info("Deleting curl Pod");
            k8sclient.pods().inNamespace(namespace).withName("curl").delete();
            Awaitility.await().atMost(1, MINUTES).until(() -> k8sclient.pods().inNamespace(namespace).withName("curl").get() == null);
        }
    });
}
Also used : RunConfigBuilder(io.fabric8.kubernetes.client.extended.run.RunConfigBuilder) Pod(io.fabric8.kubernetes.api.model.Pod) ServiceBuilder(io.fabric8.kubernetes.api.model.ServiceBuilder) KubernetesClientException(io.fabric8.kubernetes.client.KubernetesClientException) QuarkusTest(io.quarkus.test.junit.QuarkusTest) Test(org.junit.jupiter.api.Test)

Example 12 with ServiceBuilder

use of io.fabric8.knative.serving.v1.ServiceBuilder in project kubernetes by ballerinax.

the class ServiceHandler method generate.

/**
 * Generate kubernetes service definition from annotation.
 *
 * @throws KubernetesPluginException If an error occurs while generating artifact.
 */
private void generate(ServiceModel serviceModel) throws KubernetesPluginException {
    if (null == serviceModel.getPortName()) {
        serviceModel.setPortName(serviceModel.getProtocol() + "-" + serviceModel.getName());
    }
    ServicePortBuilder servicePortBuilder = new ServicePortBuilder().withName(serviceModel.getPortName()).withProtocol(KubernetesConstants.KUBERNETES_SVC_PROTOCOL).withPort(serviceModel.getPort()).withNewTargetPort(serviceModel.getTargetPort());
    if (serviceModel.getNodePort() > 0) {
        servicePortBuilder.withNodePort(serviceModel.getNodePort());
    }
    Service service = new ServiceBuilder().withNewMetadata().withName(serviceModel.getName()).withNamespace(dataHolder.getNamespace()).addToLabels(serviceModel.getLabels()).endMetadata().withNewSpec().withPorts(servicePortBuilder.build()).addToSelector(KubernetesConstants.KUBERNETES_SELECTOR_KEY, serviceModel.getSelector()).withSessionAffinity(serviceModel.getSessionAffinity()).withType(serviceModel.getServiceType()).endSpec().build();
    try {
        String serviceYAML = SerializationUtils.dumpWithoutRuntimeStateAsYaml(service);
        KubernetesUtils.writeToFile(serviceYAML, SVC_FILE_POSTFIX + YAML);
    } catch (IOException e) {
        String errorMessage = "error while generating yaml file for service: " + serviceModel.getName();
        throw new KubernetesPluginException(errorMessage, e);
    }
}
Also used : ServicePortBuilder(io.fabric8.kubernetes.api.model.ServicePortBuilder) Service(io.fabric8.kubernetes.api.model.Service) IOException(java.io.IOException) KubernetesPluginException(org.ballerinax.kubernetes.exceptions.KubernetesPluginException) ServiceBuilder(io.fabric8.kubernetes.api.model.ServiceBuilder)

Example 13 with ServiceBuilder

use of io.fabric8.knative.serving.v1.ServiceBuilder in project fabric8-maven-plugin by fabric8io.

the class ServiceHandler method getServices.

public List<Service> getServices(List<ServiceConfig> services) {
    ArrayList<Service> ret = new ArrayList<>();
    for (ServiceConfig service : services) {
        ServiceBuilder serviceBuilder = new ServiceBuilder().withNewMetadata().withName(service.getName()).withAnnotations(getAnnotations(service)).withLabels(getLabels(service)).endMetadata();
        ServiceFluent.SpecNested<ServiceBuilder> serviceSpecBuilder = serviceBuilder.withNewSpec();
        List<ServicePort> servicePorts = new ArrayList<>();
        for (ServiceConfig.Port port : service.getPorts()) {
            ServicePort servicePort = new ServicePortBuilder().withName(port.getName()).withProtocol(port.getProtocol() != null ? port.getProtocol().name() : "TCP").withTargetPort(new IntOrString(port.getTargetPort())).withPort(port.getPort()).withNodePort(port.getNodePort()).build();
            servicePorts.add(servicePort);
        }
        if (!servicePorts.isEmpty()) {
            serviceSpecBuilder.withPorts(servicePorts);
        }
        if (service.isHeadless()) {
            serviceSpecBuilder.withClusterIP("None");
        }
        if (!Strings.isNullOrBlank(service.getType())) {
            serviceSpecBuilder.withType(service.getType());
        }
        serviceSpecBuilder.endSpec();
        if (service.isHeadless() || !servicePorts.isEmpty()) {
            ret.add(serviceBuilder.build());
        }
    }
    return ret;
}
Also used : ServiceConfig(io.fabric8.maven.core.config.ServiceConfig)

Example 14 with ServiceBuilder

use of io.fabric8.knative.serving.v1.ServiceBuilder in project strimzi by strimzi.

the class AbstractModel method createHeadlessService.

protected Service createHeadlessService(String name, List<ServicePort> ports, Map<String, String> annotations) {
    Service service = new ServiceBuilder().withNewMetadata().withName(name).withLabels(getLabelsWithName(name)).withNamespace(namespace).withAnnotations(annotations).endMetadata().withNewSpec().withType("ClusterIP").withClusterIP("None").withSelector(getLabelsWithName()).withPorts(ports).endSpec().build();
    log.trace("Created headless service {}", service);
    return service;
}
Also used : Service(io.fabric8.kubernetes.api.model.Service) ServiceBuilder(io.fabric8.kubernetes.api.model.ServiceBuilder)

Example 15 with ServiceBuilder

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

ServiceBuilder (io.fabric8.kubernetes.api.model.ServiceBuilder)39 Service (io.fabric8.kubernetes.api.model.Service)32 Test (org.junit.Test)14 ServicePortBuilder (io.fabric8.kubernetes.api.model.ServicePortBuilder)11 ServicePort (io.fabric8.kubernetes.api.model.ServicePort)7 ArrayList (java.util.ArrayList)7 ServiceListBuilder (io.fabric8.kubernetes.api.model.ServiceListBuilder)5 EndpointsListBuilder (io.fabric8.kubernetes.api.model.EndpointsListBuilder)4 Pod (io.fabric8.kubernetes.api.model.Pod)4 ServiceSpec (io.fabric8.kubernetes.api.model.ServiceSpec)4 VolumeBuilder (io.fabric8.kubernetes.api.model.VolumeBuilder)4 KubernetesClient (io.fabric8.kubernetes.client.KubernetesClient)4 RouteBuilder (io.fabric8.openshift.api.model.RouteBuilder)4 Map (java.util.Map)4 Container (io.fabric8.kubernetes.api.model.Container)3 IOException (java.io.IOException)3 KubernetesPluginException (org.ballerinax.kubernetes.exceptions.KubernetesPluginException)3 Alias (io.fabric8.annotations.Alias)2 External (io.fabric8.annotations.External)2 PortName (io.fabric8.annotations.PortName)2