Search in sources :

Example 76 with Service

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

the class ExposeEnricher method enrichService.

private void enrichService(Service service) {
    if (hasWebPort(service)) {
        ObjectMeta metadata = service.getMetadata();
        if (metadata == null) {
            metadata = new ObjectMeta();
            service.setMetadata(metadata);
        }
        Map<String, String> labels = getOrCreateLabels(service);
        if (!labels.containsKey(EXPOSE_LABEL)) {
            labels.put(EXPOSE_LABEL, "true");
            log.verbose("Adding Service label '%s:true' on service %s" + " so that it is exposed by the exposecontroller microservice." + " To disable use the maven argument: '-Dfabric8.profile=internal-microservice'", EXPOSE_LABEL, KubernetesHelper.getName(service));
        }
    }
}
Also used : ObjectMeta(io.fabric8.kubernetes.api.model.ObjectMeta)

Example 77 with Service

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

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

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

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

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