Search in sources :

Example 1 with KubernetesPluginException

use of org.ballerinax.kubernetes.exceptions.KubernetesPluginException in project kubernetes by ballerinax.

the class KubernetesAnnotationProcessor method createArtifacts.

/**
 * Generate kubernetes artifacts.
 *
 * @param kubernetesDataHolder Kubernetes data holder object
 * @param balxFilePath         ballerina file path
 * @param outputDir            output directory to save artifacts
 * @throws KubernetesPluginException if an error ocurrs while generating artifacts
 */
void createArtifacts(KubernetesDataHolder kubernetesDataHolder, String balxFilePath, String outputDir) throws KubernetesPluginException {
    DeploymentModel deploymentModel = kubernetesDataHolder.getDeploymentModel();
    if (deploymentModel == null) {
        deploymentModel = getDefaultDeploymentModel(balxFilePath);
    }
    kubernetesDataHolder.setDeploymentModel(deploymentModel);
    deploymentModel.setPorts(kubernetesDataHolder.getPorts());
    deploymentModel.setPodAutoscalerModel(kubernetesDataHolder.getPodAutoscalerModel());
    deploymentModel.setSecretModels(kubernetesDataHolder.getSecrets());
    deploymentModel.setConfigMapModels(kubernetesDataHolder.getConfigMaps());
    deploymentModel.setVolumeClaimModels(kubernetesDataHolder.getPersistentVolumeClaims());
    // Service
    Collection<ServiceModel> serviceModels = kubernetesDataHolder.getEndpointToServiceModelMap().values();
    int count = 0;
    for (ServiceModel serviceModel : serviceModels) {
        count++;
        generateService(serviceModel, balxFilePath, outputDir);
        out.print("@kubernetes:Service \t\t\t - complete " + count + "/" + serviceModels.size() + "\r");
    }
    // ingress
    count = 0;
    Map<IngressModel, Set<String>> ingressModels = kubernetesDataHolder.getIngressToEndpointMap();
    if (ingressModels.size() > 0) {
        out.println();
    }
    int size = ingressModels.size();
    Map<String, ServiceModel> endpointMap = kubernetesDataHolder.getEndpointToServiceModelMap();
    Iterator<Map.Entry<IngressModel, Set<String>>> iterator = ingressModels.entrySet().iterator();
    Map<String, Set<SecretModel>> secretModelsMap = kubernetesDataHolder.getSecretModels();
    while (iterator.hasNext()) {
        Map.Entry<IngressModel, Set<String>> pair = iterator.next();
        IngressModel ingressModel = pair.getKey();
        Set<String> endpoints = pair.getValue();
        for (String endpointName : endpoints) {
            ServiceModel serviceModel = endpointMap.get(endpointName);
            ingressModel.setServiceName(serviceModel.getName());
            ingressModel.setServicePort(serviceModel.getPort());
            if (secretModelsMap.get(endpointName) != null && secretModelsMap.get(endpointName).size() != 0) {
                ingressModel.setEnableTLS(true);
            }
        }
        generateIngress(ingressModel, balxFilePath, outputDir);
        count++;
        out.print("@kubernetes:Ingress \t\t\t - complete " + count + "/" + size + "\r");
        iterator.remove();
    }
    // secret
    count = 0;
    Collection<SecretModel> secretModels = kubernetesDataHolder.getSecrets();
    if (secretModels.size() > 0) {
        out.println();
    }
    for (SecretModel secretModel : secretModels) {
        count++;
        generateSecrets(secretModel, balxFilePath, outputDir);
        out.print("@kubernetes:Secret \t\t\t - complete " + count + "/" + secretModels.size() + "\r");
    }
    // configMap
    count = 0;
    Collection<ConfigMapModel> configMapModels = kubernetesDataHolder.getConfigMaps();
    if (configMapModels.size() > 0) {
        out.println();
    }
    for (ConfigMapModel configMapModel : configMapModels) {
        count++;
        if (configMapModel.isBallerinaConf()) {
            if (configMapModel.getData().size() != 1) {
                throw new KubernetesPluginException("There can be only 1 ballerina config file");
            }
            deploymentModel.setCommandArgs(" --config ${CONFIG_FILE} ");
            deploymentModel.addEnv("CONFIG_FILE", configMapModel.getMountPath() + File.separator + configMapModel.getData().keySet().iterator().next());
        }
        generateConfigMaps(configMapModel, balxFilePath, outputDir);
        out.print("@kubernetes:ConfigMap \t\t\t - complete " + count + "/" + configMapModels.size() + "\r");
    }
    // volume mount
    count = 0;
    Collection<PersistentVolumeClaimModel> volumeClaims = kubernetesDataHolder.getPersistentVolumeClaims();
    if (volumeClaims.size() > 0) {
        out.println();
    }
    for (PersistentVolumeClaimModel claimModel : volumeClaims) {
        count++;
        generatePersistentVolumeClaim(claimModel, balxFilePath, outputDir);
        out.print("@kubernetes:VolumeClaim \t\t - complete " + count + "/" + volumeClaims.size() + "\r");
    }
    out.println();
    generateDeployment(deploymentModel, balxFilePath, outputDir);
    out.println();
    out.println("@kubernetes:Deployment \t\t\t - complete 1/1");
    out.println();
    printKubernetesInstructions(outputDir);
}
Also used : HashSet(java.util.HashSet) Set(java.util.Set) PersistentVolumeClaimModel(org.ballerinax.kubernetes.models.PersistentVolumeClaimModel) KubernetesPluginException(org.ballerinax.kubernetes.exceptions.KubernetesPluginException) SecretModel(org.ballerinax.kubernetes.models.SecretModel) IngressModel(org.ballerinax.kubernetes.models.IngressModel) ConfigMapModel(org.ballerinax.kubernetes.models.ConfigMapModel) DeploymentModel(org.ballerinax.kubernetes.models.DeploymentModel) ServiceModel(org.ballerinax.kubernetes.models.ServiceModel) HashMap(java.util.HashMap) Map(java.util.Map)

Example 2 with KubernetesPluginException

use of org.ballerinax.kubernetes.exceptions.KubernetesPluginException in project kubernetes by ballerinax.

the class KubernetesAnnotationProcessor method generateConfigMaps.

private void generateConfigMaps(ConfigMapModel configMapModel, String balxFilePath, String outputDir) throws KubernetesPluginException {
    String balxFileName = KubernetesUtils.extractBalxName(balxFilePath);
    String configMapContent = new ConfigMapHandler(configMapModel).generate();
    try {
        KubernetesUtils.writeToFile(configMapContent, outputDir + File.separator + balxFileName + CONFIG_MAP_FILE_POSTFIX + YAML);
    } catch (IOException e) {
        throw new KubernetesPluginException("Error while writing config map content", e);
    }
}
Also used : IOException(java.io.IOException) KubernetesPluginException(org.ballerinax.kubernetes.exceptions.KubernetesPluginException) ConfigMapHandler(org.ballerinax.kubernetes.handlers.ConfigMapHandler)

Example 3 with KubernetesPluginException

use of org.ballerinax.kubernetes.exceptions.KubernetesPluginException in project kubernetes by ballerinax.

the class DeploymentHandler method generate.

/**
 * Generate kubernetes deployment definition from annotation.
 *
 * @return Generated kubernetes @{@link Deployment} definition
 * @throws KubernetesPluginException If an error occurs while generating artifact.
 */
public String generate() 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()).withNamespace(deploymentModel.getNamespace()).withLabels(deploymentModel.getLabels()).endMetadata().withNewSpec().withReplicas(deploymentModel.getReplicas()).withNewTemplate().withNewMetadata().addToLabels(deploymentModel.getLabels()).endMetadata().withNewSpec().withContainers(container).withVolumes(populateVolume(deploymentModel)).endSpec().endTemplate().endSpec().build();
    try {
        return SerializationUtils.dumpWithoutRuntimeStateAsYaml(deployment);
    } catch (JsonProcessingException e) {
        String errorMessage = "Error while parsing 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.extensions.Deployment) KubernetesPluginException(org.ballerinax.kubernetes.exceptions.KubernetesPluginException) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) DeploymentBuilder(io.fabric8.kubernetes.api.model.extensions.DeploymentBuilder)

Example 4 with KubernetesPluginException

use of org.ballerinax.kubernetes.exceptions.KubernetesPluginException in project kubernetes by ballerinax.

the class KubernetesIngressGeneratorTests method testIngressGenerator.

@Test
public void testIngressGenerator() {
    IngressModel ingressModel = new IngressModel();
    ingressModel.setName("MyIngress");
    ingressModel.setHostname("abc.com");
    ingressModel.setPath("/helloworld");
    ingressModel.setServicePort(9090);
    ingressModel.setIngressClass("nginx");
    ingressModel.setServiceName("HelloWorldService");
    Map<String, String> labels = new HashMap<>();
    labels.put(KubernetesConstants.KUBERNETES_SELECTOR_KEY, "TestAPP");
    ingressModel.setLabels(labels);
    IngressHandler kubernetesIngressGenerator = new IngressHandler(ingressModel);
    try {
        String ingressYaml = kubernetesIngressGenerator.generate();
        Assert.assertNotNull(ingressYaml);
        File artifactLocation = new File("target/kubernetes");
        artifactLocation.mkdir();
        File tempFile = File.createTempFile("temp", ingressModel.getName() + ".yaml", artifactLocation);
        KubernetesUtils.writeToFile(ingressYaml, tempFile.getPath());
        log.info("Generated YAML: \n" + ingressYaml);
        Assert.assertTrue(tempFile.exists());
    // tempFile.deleteOnExit();
    } catch (IOException e) {
        Assert.fail("Unable to write to file");
    } catch (KubernetesPluginException e) {
        Assert.fail("Unable to generate yaml from ingress");
    }
}
Also used : IngressModel(org.ballerinax.kubernetes.models.IngressModel) HashMap(java.util.HashMap) IngressHandler(org.ballerinax.kubernetes.handlers.IngressHandler) IOException(java.io.IOException) KubernetesPluginException(org.ballerinax.kubernetes.exceptions.KubernetesPluginException) File(java.io.File) Test(org.junit.Test)

Example 5 with KubernetesPluginException

use of org.ballerinax.kubernetes.exceptions.KubernetesPluginException in project kubernetes by ballerinax.

the class KubernetesServiceGeneratorTests method testServiceGenerate.

@Test
public void testServiceGenerate() {
    ServiceModel serviceModel = new ServiceModel();
    serviceModel.setName("MyService");
    serviceModel.setPort(9090);
    serviceModel.setServiceType("NodePort");
    serviceModel.setSelector("MyAPP");
    Map<String, String> labels = new HashMap<>();
    labels.put(KubernetesConstants.KUBERNETES_SELECTOR_KEY, "TestAPP");
    serviceModel.setLabels(labels);
    ServiceHandler kubernetesServiceGenerator = new ServiceHandler(serviceModel);
    try {
        String serviceYAML = kubernetesServiceGenerator.generate();
        Assert.assertNotNull(serviceYAML);
        File artifactLocation = new File("target/kubernetes");
        artifactLocation.mkdir();
        File tempFile = File.createTempFile("temp", serviceModel.getName() + ".yaml", artifactLocation);
        KubernetesUtils.writeToFile(serviceYAML, tempFile.getPath());
        log.info("Generated YAML: \n" + serviceYAML);
        Assert.assertTrue(tempFile.exists());
    // tempFile.deleteOnExit();
    } catch (IOException e) {
        Assert.fail("Unable to write to file");
    } catch (KubernetesPluginException e) {
        Assert.fail("Unable to generate yaml from service");
    }
}
Also used : ServiceModel(org.ballerinax.kubernetes.models.ServiceModel) HashMap(java.util.HashMap) ServiceHandler(org.ballerinax.kubernetes.handlers.ServiceHandler) IOException(java.io.IOException) KubernetesPluginException(org.ballerinax.kubernetes.exceptions.KubernetesPluginException) File(java.io.File) Test(org.junit.Test)

Aggregations

KubernetesPluginException (org.ballerinax.kubernetes.exceptions.KubernetesPluginException)20 IOException (java.io.IOException)13 HashMap (java.util.HashMap)6 File (java.io.File)4 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)3 ServiceModel (org.ballerinax.kubernetes.models.ServiceModel)3 Test (org.junit.Test)3 HashSet (java.util.HashSet)2 AnnotationAttachmentNode (org.ballerinalang.model.tree.AnnotationAttachmentNode)2 DeploymentHandler (org.ballerinax.kubernetes.handlers.DeploymentHandler)2 IngressHandler (org.ballerinax.kubernetes.handlers.IngressHandler)2 ServiceHandler (org.ballerinax.kubernetes.handlers.ServiceHandler)2 DeploymentModel (org.ballerinax.kubernetes.models.DeploymentModel)2 IngressModel (org.ballerinax.kubernetes.models.IngressModel)2 SecretModel (org.ballerinax.kubernetes.models.SecretModel)2 Container (io.fabric8.kubernetes.api.model.Container)1 ContainerPort (io.fabric8.kubernetes.api.model.ContainerPort)1 PersistentVolumeClaim (io.fabric8.kubernetes.api.model.PersistentVolumeClaim)1 PersistentVolumeClaimBuilder (io.fabric8.kubernetes.api.model.PersistentVolumeClaimBuilder)1 Quantity (io.fabric8.kubernetes.api.model.Quantity)1