Search in sources :

Example 1 with DeploymentModel

use of org.ballerinax.kubernetes.models.DeploymentModel 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 DeploymentModel

use of org.ballerinax.kubernetes.models.DeploymentModel in project kubernetes by ballerinax.

the class KubernetesAnnotationProcessor method processDeployment.

/**
 * Process annotations and create deployment model object.
 *
 * @param attachmentNode annotation attachment node.
 * @return Deployment model object
 */
DeploymentModel processDeployment(AnnotationAttachmentNode attachmentNode) throws KubernetesPluginException {
    DeploymentModel deploymentModel = new DeploymentModel();
    List<BLangRecordLiteral.BLangRecordKeyValue> keyValues = ((BLangRecordLiteral) ((BLangAnnotationAttachment) attachmentNode).expr).getKeyValuePairs();
    for (BLangRecordLiteral.BLangRecordKeyValue keyValue : keyValues) {
        DeploymentConfiguration deploymentConfiguration = DeploymentConfiguration.valueOf(keyValue.getKey().toString());
        String annotationValue = resolveValue(keyValue.getValue().toString());
        switch(deploymentConfiguration) {
            case name:
                deploymentModel.setName(getValidName(annotationValue));
                break;
            case labels:
                deploymentModel.setLabels(getMap(((BLangRecordLiteral) keyValue.valueExpr).keyValuePairs));
                break;
            case enableLiveness:
                deploymentModel.setEnableLiveness(annotationValue);
                break;
            case livenessPort:
                deploymentModel.setLivenessPort(Integer.parseInt(annotationValue));
                break;
            case initialDelaySeconds:
                deploymentModel.setInitialDelaySeconds(Integer.parseInt(annotationValue));
                break;
            case periodSeconds:
                deploymentModel.setPeriodSeconds(Integer.parseInt(annotationValue));
                break;
            case username:
                deploymentModel.setUsername(annotationValue);
                break;
            case env:
                deploymentModel.setEnv(getMap(((BLangRecordLiteral) keyValue.valueExpr).keyValuePairs));
                break;
            case password:
                deploymentModel.setPassword(annotationValue);
                break;
            case baseImage:
                deploymentModel.setBaseImage(annotationValue);
                break;
            case push:
                deploymentModel.setPush(Boolean.valueOf(annotationValue));
                break;
            case buildImage:
                deploymentModel.setBuildImage(Boolean.valueOf(annotationValue));
                break;
            case image:
                deploymentModel.setImage(annotationValue);
                break;
            case dockerHost:
                deploymentModel.setDockerHost(annotationValue);
                break;
            case dockerCertPath:
                deploymentModel.setDockerCertPath(annotationValue);
                break;
            case imagePullPolicy:
                deploymentModel.setImagePullPolicy(annotationValue);
                break;
            case replicas:
                deploymentModel.setReplicas(Integer.parseInt(annotationValue));
                break;
            default:
                break;
        }
    }
    return deploymentModel;
}
Also used : DeploymentModel(org.ballerinax.kubernetes.models.DeploymentModel) BLangRecordLiteral(org.wso2.ballerinalang.compiler.tree.expressions.BLangRecordLiteral)

Example 3 with DeploymentModel

use of org.ballerinax.kubernetes.models.DeploymentModel in project kubernetes by ballerinax.

the class KubernetesAnnotationProcessor method getDefaultDeploymentModel.

/**
 * Get DeploymentModel object with default values.
 *
 * @param balxFilePath ballerina file path
 * @return DeploymentModel object with default values
 */
private DeploymentModel getDefaultDeploymentModel(String balxFilePath) {
    DeploymentModel deploymentModel = new DeploymentModel();
    String balxName = KubernetesUtils.extractBalxName(balxFilePath);
    String deploymentName = balxName + "-deployment";
    deploymentModel.setName(getValidName(deploymentName));
    String namespace = KubernetesConstants.DEPLOYMENT_NAMESPACE_DEFAULT;
    deploymentModel.setNamespace(namespace);
    String imagePullPolicy = KubernetesConstants.DEPLOYMENT_IMAGE_PULL_POLICY_DEFAULT;
    deploymentModel.setImagePullPolicy(imagePullPolicy);
    String liveness = KubernetesConstants.DEPLOYMENT_LIVENESS_DISABLE;
    deploymentModel.setEnableLiveness(liveness);
    int defaultReplicas = 1;
    deploymentModel.setReplicas(defaultReplicas);
    deploymentModel.addLabel(KubernetesConstants.KUBERNETES_SELECTOR_KEY, balxName);
    deploymentModel.setEnv(getMap(null));
    deploymentModel.setImage(balxName + DOCKER_LATEST_TAG);
    deploymentModel.setBuildImage(true);
    deploymentModel.setPush(false);
    return deploymentModel;
}
Also used : DeploymentModel(org.ballerinax.kubernetes.models.DeploymentModel)

Example 4 with DeploymentModel

use of org.ballerinax.kubernetes.models.DeploymentModel in project kubernetes by ballerinax.

the class KubernetesDeploymentGeneratorTests method testServiceGenerate.

@Test
public void testServiceGenerate() {
    DeploymentModel deploymentModel = new DeploymentModel();
    deploymentModel.setName("MyDeployment");
    Map<String, String> labels = new HashMap<>();
    labels.put(KubernetesConstants.KUBERNETES_SELECTOR_KEY, "TestAPP");
    Set<Integer> ports = new HashSet<>();
    ports.add(9090);
    ports.add(9091);
    ports.add(9092);
    deploymentModel.setLabels(labels);
    deploymentModel.setImage("SampleImage:v1.0.0");
    deploymentModel.setImagePullPolicy("Always");
    deploymentModel.setReplicas(3);
    deploymentModel.setPorts(ports);
    try {
        String deploymentYAML = new DeploymentHandler(deploymentModel).generate();
        Assert.assertNotNull(deploymentYAML);
        File artifactLocation = new File("target/kubernetes");
        artifactLocation.mkdir();
        File tempFile = File.createTempFile("temp", deploymentModel.getName() + ".yaml", artifactLocation);
        KubernetesUtils.writeToFile(deploymentYAML, tempFile.getPath());
        log.info("Generated YAML: \n" + deploymentYAML);
        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 : DeploymentModel(org.ballerinax.kubernetes.models.DeploymentModel) HashMap(java.util.HashMap) DeploymentHandler(org.ballerinax.kubernetes.handlers.DeploymentHandler) IOException(java.io.IOException) KubernetesPluginException(org.ballerinax.kubernetes.exceptions.KubernetesPluginException) File(java.io.File) HashSet(java.util.HashSet) Test(org.junit.Test)

Aggregations

DeploymentModel (org.ballerinax.kubernetes.models.DeploymentModel)4 HashMap (java.util.HashMap)2 HashSet (java.util.HashSet)2 KubernetesPluginException (org.ballerinax.kubernetes.exceptions.KubernetesPluginException)2 File (java.io.File)1 IOException (java.io.IOException)1 Map (java.util.Map)1 Set (java.util.Set)1 DeploymentHandler (org.ballerinax.kubernetes.handlers.DeploymentHandler)1 ConfigMapModel (org.ballerinax.kubernetes.models.ConfigMapModel)1 IngressModel (org.ballerinax.kubernetes.models.IngressModel)1 PersistentVolumeClaimModel (org.ballerinax.kubernetes.models.PersistentVolumeClaimModel)1 SecretModel (org.ballerinax.kubernetes.models.SecretModel)1 ServiceModel (org.ballerinax.kubernetes.models.ServiceModel)1 Test (org.junit.Test)1 BLangRecordLiteral (org.wso2.ballerinalang.compiler.tree.expressions.BLangRecordLiteral)1