Search in sources :

Example 81 with Service

use of io.fabric8.patch.Service in project fabric8-maven-plugin by fabric8io.

the class KubernetesResourceUtilTest method readWholeDir.

@Test
public void readWholeDir() throws IOException {
    ResourceVersioning v = new ResourceVersioning().withCoreVersion("v2").withExtensionsVersion("extensions/v2");
    KubernetesListBuilder builder = KubernetesResourceUtil.readResourceFragmentsFrom(v, "pong", new File(fabric8Dir, "read-dir").listFiles());
    KubernetesList list = builder.build();
    assertEquals(2, list.getItems().size());
    for (HasMetadata item : list.getItems()) {
        assertTrue("Service".equals(item.getKind()) || "ReplicationController".equals(item.getKind()));
        assertEquals("pong", item.getMetadata().getName());
        assertEquals("v2", item.getApiVersion());
    }
}
Also used : KubernetesListBuilder(io.fabric8.kubernetes.api.model.KubernetesListBuilder) HasMetadata(io.fabric8.kubernetes.api.model.HasMetadata) KubernetesList(io.fabric8.kubernetes.api.model.KubernetesList) File(java.io.File) Test(org.junit.Test)

Example 82 with Service

use of io.fabric8.patch.Service in project fabric8-maven-plugin by fabric8io.

the class OpenshiftBuildService method build.

@Override
public void build(ImageConfiguration imageConfig) throws Fabric8ServiceException {
    try {
        ImageName imageName = new ImageName(imageConfig.getName());
        File dockerTar = createBuildArchive(imageConfig);
        KubernetesListBuilder builder = new KubernetesListBuilder();
        // Check for buildconfig / imagestream and create them if necessary
        String buildName = updateOrCreateBuildConfig(config, client, builder, imageConfig);
        checkOrCreateImageStream(config, client, builder, getImageStreamName(imageName));
        applyResourceObjects(config, client, builder);
        // Start the actual build
        Build build = startBuild(client, dockerTar, buildName);
        // Wait until the build finishes
        waitForOpenShiftBuildToComplete(client, build);
        // Create a file with generated image streams
        addImageStreamToFile(getImageStreamFile(config), imageName, client);
    } catch (Fabric8ServiceException e) {
        throw e;
    } catch (Exception ex) {
        throw new Fabric8ServiceException("Unable to build the image using the OpenShift build service", ex);
    }
}
Also used : ImageName(io.fabric8.maven.docker.util.ImageName) Fabric8ServiceException(io.fabric8.maven.core.service.Fabric8ServiceException) Build(io.fabric8.openshift.api.model.Build) File(java.io.File) Fabric8ServiceException(io.fabric8.maven.core.service.Fabric8ServiceException) KubernetesClientException(io.fabric8.kubernetes.client.KubernetesClientException) IOException(java.io.IOException) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException)

Example 83 with Service

use of io.fabric8.patch.Service 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 84 with Service

use of io.fabric8.patch.Service in project fabric8-maven-plugin by fabric8io.

the class ApplyMojo method shouldCreateExternalURLForService.

/**
 * Should we try to create an external URL for the given service?
 * <p/>
 * By default lets ignore the kubernetes services and any service which does not expose ports 80 and 443
 *
 * @return true if we should create an OpenShift Route for this service.
 */
private boolean shouldCreateExternalURLForService(Service service, String id) {
    if ("kubernetes".equals(id) || "kubernetes-ro".equals(id)) {
        return false;
    }
    Set<Integer> ports = KubernetesHelper.getPorts(service);
    log.debug("Service " + id + " has ports: " + ports);
    if (ports.size() == 1) {
        String type = null;
        ServiceSpec spec = service.getSpec();
        if (spec != null) {
            type = spec.getType();
            if (Objects.equals(type, "LoadBalancer")) {
                return true;
            }
        }
        log.info("Not generating route for service " + id + " type is not LoadBalancer: " + type);
        return false;
    } else {
        log.info("Not generating route for service " + id + " as only single port services are supported. Has ports: " + ports);
        return false;
    }
}
Also used : ServiceSpec(io.fabric8.kubernetes.api.model.ServiceSpec) KubernetesHelper.createIntOrString(io.fabric8.kubernetes.api.KubernetesHelper.createIntOrString)

Example 85 with Service

use of io.fabric8.patch.Service in project fabric8-maven-plugin by fabric8io.

the class ApplyMojo method createRouteForService.

private Route createRouteForService(String routeDomainPostfix, String namespace, Service service) {
    Route route = null;
    String id = KubernetesHelper.getName(service);
    if (Strings.isNotBlank(id) && hasExactlyOneService(service, id)) {
        route = new Route();
        String routeId = id;
        KubernetesHelper.setName(route, namespace, routeId);
        RouteSpec routeSpec = new RouteSpec();
        RouteTargetReference objectRef = new RouteTargetReferenceBuilder().withName(id).build();
        // objectRef.setNamespace(namespace);
        routeSpec.setTo(objectRef);
        if (!Strings.isNullOrBlank(routeDomainPostfix)) {
            String host = Strings.stripSuffix(Strings.stripSuffix(id, "-service"), ".");
            routeSpec.setHost(host + "." + Strings.stripPrefix(routeDomainPostfix, "."));
        } else {
            routeSpec.setHost("");
        }
        route.setSpec(routeSpec);
        String json;
        try {
            json = KubernetesHelper.toJson(route);
        } catch (JsonProcessingException e) {
            json = e.getMessage() + ". object: " + route;
        }
        log.debug("Created route: " + json);
    }
    return route;
}
Also used : RouteTargetReference(io.fabric8.openshift.api.model.RouteTargetReference) RouteTargetReferenceBuilder(io.fabric8.openshift.api.model.RouteTargetReferenceBuilder) KubernetesHelper.createIntOrString(io.fabric8.kubernetes.api.KubernetesHelper.createIntOrString) RouteSpec(io.fabric8.openshift.api.model.RouteSpec) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) Route(io.fabric8.openshift.api.model.Route)

Aggregations

Service (io.fabric8.kubernetes.api.model.Service)100 Test (org.junit.Test)78 IOException (java.io.IOException)35 ArrayList (java.util.ArrayList)35 HashMap (java.util.HashMap)33 File (java.io.File)28 OpenShiftClient (io.fabric8.openshift.client.OpenShiftClient)26 ServiceBuilder (io.fabric8.kubernetes.api.model.ServiceBuilder)24 Deployment (io.fabric8.kubernetes.api.model.extensions.Deployment)22 Map (java.util.Map)19 Pod (io.fabric8.kubernetes.api.model.Pod)18 ServicePort (io.fabric8.kubernetes.api.model.ServicePort)18 KubernetesClientException (io.fabric8.kubernetes.client.KubernetesClientException)18 ConfigMap (io.fabric8.kubernetes.api.model.ConfigMap)17 HasMetadata (io.fabric8.kubernetes.api.model.HasMetadata)17 List (java.util.List)17 KubernetesClient (io.fabric8.kubernetes.client.KubernetesClient)15 Reconciliation (io.strimzi.controller.cluster.Reconciliation)15 ConfigMapOperator (io.strimzi.controller.cluster.operator.resource.ConfigMapOperator)15 ServiceOperator (io.strimzi.controller.cluster.operator.resource.ServiceOperator)15