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);
}
}
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);
}
}
Aggregations