Search in sources :

Example 16 with DeploymentBuilder

use of io.fabric8.kubernetes.api.model.apps.DeploymentBuilder in project kubernetes by ballerinax.

the class DeploymentHandler method generate.

/**
 * Generate kubernetes deployment definition from annotation.
 *
 * @param deploymentModel @{@link DeploymentModel} definition
 * @throws KubernetesPluginException If an error occurs while generating artifact.
 */
private void generate(DeploymentModel deploymentModel) throws KubernetesPluginException {
    List<ContainerPort> containerPorts = null;
    if (deploymentModel.getPorts() != null) {
        containerPorts = populatePorts(deploymentModel.getPorts());
    }
    Container container = generateContainer(deploymentModel, containerPorts);
    Deployment deployment = new DeploymentBuilder().withNewMetadata().withName(deploymentModel.getName()).withLabels(deploymentModel.getLabels()).withAnnotations(deploymentModel.getAnnotations()).withNamespace(dataHolder.getNamespace()).endMetadata().withNewSpec().withNewSelector().withMatchLabels(deploymentModel.getLabels()).endSelector().withStrategy(deploymentModel.getStrategy()).withReplicas(deploymentModel.getReplicas()).withNewTemplate().withNewMetadata().addToLabels(deploymentModel.getLabels()).addToAnnotations(deploymentModel.getPodAnnotations()).endMetadata().withNewSpec().withServiceAccountName(deploymentModel.getServiceAccountName()).withContainers(container).withImagePullSecrets(getImagePullSecrets(deploymentModel)).withInitContainers(generateInitContainer(deploymentModel)).withVolumes(populateVolume(deploymentModel)).withTolerations(populatePodTolerations(deploymentModel.getPodTolerations())).withNodeSelector(deploymentModel.getNodeSelector()).endSpec().endTemplate().endSpec().build();
    try {
        String deploymentContent = SerializationUtils.dumpWithoutRuntimeStateAsYaml(deployment);
        KubernetesUtils.writeToFile(deploymentContent, DEPLOYMENT_FILE_POSTFIX + YAML);
    } catch (IOException e) {
        String errorMessage = "error while generating yaml file for deployment: " + deploymentModel.getName();
        throw new KubernetesPluginException(errorMessage, e);
    }
}
Also used : Container(io.fabric8.kubernetes.api.model.Container) ContainerPort(io.fabric8.kubernetes.api.model.ContainerPort) Deployment(io.fabric8.kubernetes.api.model.apps.Deployment) IOException(java.io.IOException) KubernetesPluginException(org.ballerinax.kubernetes.exceptions.KubernetesPluginException) DeploymentBuilder(io.fabric8.kubernetes.api.model.apps.DeploymentBuilder)

Aggregations

DeploymentBuilder (io.fabric8.kubernetes.api.model.extensions.DeploymentBuilder)13 Deployment (io.fabric8.kubernetes.api.model.extensions.Deployment)11 Container (io.fabric8.kubernetes.api.model.Container)6 Test (org.junit.Test)5 ContainerBuilder (io.fabric8.kubernetes.api.model.ContainerBuilder)4 HasMetadata (io.fabric8.kubernetes.api.model.HasMetadata)4 PodSpecBuilder (io.fabric8.kubernetes.api.model.PodSpecBuilder)4 PodSpec (io.fabric8.kubernetes.api.model.PodSpec)3 ContainerPort (io.fabric8.kubernetes.api.model.ContainerPort)2 EnvVarBuilder (io.fabric8.kubernetes.api.model.EnvVarBuilder)2 Deployment (io.fabric8.kubernetes.api.model.apps.Deployment)2 DeploymentBuilder (io.fabric8.kubernetes.api.model.apps.DeploymentBuilder)2 DeploymentSpec (io.fabric8.kubernetes.api.model.extensions.DeploymentSpec)2 ArrayList (java.util.ArrayList)2 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 ImmutableList (com.google.common.collect.ImmutableList)1 ImmutableMap (com.google.common.collect.ImmutableMap)1 TypedVisitor (io.fabric8.kubernetes.api.builder.TypedVisitor)1 ConfigMapVolumeSourceBuilder (io.fabric8.kubernetes.api.model.ConfigMapVolumeSourceBuilder)1 ContainerPortBuilder (io.fabric8.kubernetes.api.model.ContainerPortBuilder)1