Search in sources :

Example 41 with ServiceBuilder

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

the class ServiceHandler method generatePrometheusService.

/**
 * Generate kubernetes service definition for Prometheus.
 *
 * @throws KubernetesPluginException If an error occurs while generating artifact.
 */
private void generatePrometheusService(ServiceModel serviceModel) throws KubernetesPluginException {
    ServicePortBuilder servicePortBuilder = new ServicePortBuilder().withName(serviceModel.getProtocol() + "-prometheus-" + serviceModel.getName()).withProtocol(KubernetesConstants.KUBERNETES_SVC_PROTOCOL).withPort(serviceModel.getPrometheusModel().getPort()).withNewTargetPort(serviceModel.getPrometheusModel().getPort());
    if (serviceModel.getPrometheusModel().getNodePort() > 0) {
        servicePortBuilder.withNodePort(serviceModel.getNodePort());
    }
    Service service = new ServiceBuilder().withNewMetadata().withName(serviceModel.getName() + "-prometheus").withNamespace(dataHolder.getNamespace()).addToLabels(serviceModel.getLabels()).endMetadata().withNewSpec().withPorts(servicePortBuilder.build()).addToSelector(KubernetesConstants.KUBERNETES_SELECTOR_KEY, serviceModel.getSelector()).withSessionAffinity(serviceModel.getSessionAffinity()).withType(serviceModel.getPrometheusModel().getServiceType()).endSpec().build();
    try {
        String serviceYAML = SerializationUtils.dumpWithoutRuntimeStateAsYaml(service);
        KubernetesUtils.writeToFile(serviceYAML, "_prometheus" + SVC_FILE_POSTFIX + YAML);
    } catch (IOException e) {
        String errorMessage = "error while generating yaml file for prometheus 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 42 with ServiceBuilder

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

the class KnativeServiceHandler method generate.

/**
 * Generate kubernetes deployment definition from annotation.
 *
 * @param serviceModel @{@link ServiceModel} definition
 * @throws KubernetesPluginException If an error occurs while generating artifact.
 */
private void generate(ServiceModel serviceModel) throws KubernetesPluginException {
    List<ContainerPort> containerPorts = null;
    if (serviceModel.getPorts() != null) {
        containerPorts = populatePorts(serviceModel.getPorts());
    }
    Container container = generateContainer(serviceModel, containerPorts);
    Service knativeSvc = new ServiceBuilder().withNewMetadata().withName(serviceModel.getName()).withNamespace(knativeDataHolder.getNamespace()).withAnnotations(serviceModel.getAnnotations()).withLabels(serviceModel.getLabels()).endMetadata().withNewSpec().withNewTemplate().withNewSpec().withContainerConcurrency((long) serviceModel.getContainerConcurrency()).withTimeoutSeconds((long) serviceModel.getTimeoutSeconds()).withContainers(container).withInitContainers(generateInitContainer(serviceModel)).withVolumes(populateVolume(serviceModel)).endSpec().endTemplate().endSpec().build();
    try {
        String knativeSvcContent = SerializationUtils.dumpWithoutRuntimeStateAsYaml(knativeSvc);
        KnativeUtils.writeToFile(knativeSvcContent, KNATIVE_SVC_FILE_POSTFIX + YAML);
    } catch (IOException e) {
        String errorMessage = "error while generating yaml file for deployment: " + serviceModel.getName();
        throw new KubernetesPluginException(errorMessage, e);
    }
}
Also used : Container(io.fabric8.kubernetes.api.model.Container) ContainerPort(io.fabric8.kubernetes.api.model.ContainerPort) Service(io.fabric8.knative.serving.v1.Service) IOException(java.io.IOException) KubernetesPluginException(org.ballerinax.kubernetes.exceptions.KubernetesPluginException) ServiceBuilder(io.fabric8.knative.serving.v1.ServiceBuilder)

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