Search in sources :

Example 1 with SecretModel

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

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

the class KubernetesAnnotationProcessor method processSecureSocketAnnotation.

/**
 * Extract key-store/trust-store file location from endpoint.
 *
 * @param endpointName          Endpoint name
 * @param secureSocketKeyValues secureSocket annotation struct
 * @return List of @{@link SecretModel} objects
 */
Set<SecretModel> processSecureSocketAnnotation(String endpointName, List<BLangRecordLiteral.BLangRecordKeyValue> secureSocketKeyValues) throws KubernetesPluginException {
    Set<SecretModel> secrets = new HashSet<>();
    String keyStoreFile = null;
    String trustStoreFile = null;
    for (BLangRecordLiteral.BLangRecordKeyValue keyValue : secureSocketKeyValues) {
        // extract file paths.
        String key = keyValue.getKey().toString();
        if ("keyStore".equals(key)) {
            keyStoreFile = extractFilePath(keyValue);
        } else if ("trustStore".equals(key)) {
            trustStoreFile = extractFilePath(keyValue);
        }
    }
    if (keyStoreFile != null && trustStoreFile != null) {
        if (getMountPath(keyStoreFile).equals(getMountPath(trustStoreFile))) {
            // trust-store and key-store mount to same path
            String keyStoreContent = readSecretFile(keyStoreFile);
            String trustStoreContent = readSecretFile(trustStoreFile);
            SecretModel secretModel = new SecretModel();
            secretModel.setName(getValidName(endpointName) + "-secure-socket");
            secretModel.setMountPath(getMountPath(keyStoreFile));
            Map<String, String> dataMap = new HashMap<>();
            dataMap.put(String.valueOf(Paths.get(keyStoreFile).getFileName()), keyStoreContent);
            dataMap.put(String.valueOf(Paths.get(trustStoreFile).getFileName()), trustStoreContent);
            secretModel.setData(dataMap);
            secrets.add(secretModel);
            return secrets;
        }
    }
    if (keyStoreFile != null) {
        String keyStoreContent = readSecretFile(keyStoreFile);
        SecretModel secretModel = new SecretModel();
        secretModel.setName(getValidName(endpointName) + "-keystore");
        secretModel.setMountPath(getMountPath(keyStoreFile));
        Map<String, String> dataMap = new HashMap<>();
        dataMap.put(String.valueOf(Paths.get(keyStoreFile).getFileName()), keyStoreContent);
        secretModel.setData(dataMap);
        secrets.add(secretModel);
    }
    if (trustStoreFile != null) {
        String trustStoreContent = readSecretFile(trustStoreFile);
        SecretModel secretModel = new SecretModel();
        secretModel.setName(getValidName(endpointName) + "-truststore");
        secretModel.setMountPath(getMountPath(trustStoreFile));
        Map<String, String> dataMap = new HashMap<>();
        dataMap.put(String.valueOf(Paths.get(trustStoreFile).getFileName()), trustStoreContent);
        secretModel.setData(dataMap);
        secrets.add(secretModel);
    }
    return secrets;
}
Also used : HashMap(java.util.HashMap) SecretModel(org.ballerinax.kubernetes.models.SecretModel) BLangRecordLiteral(org.wso2.ballerinalang.compiler.tree.expressions.BLangRecordLiteral) HashSet(java.util.HashSet)

Example 3 with SecretModel

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

the class KubernetesPlugin method process.

@Override
public void process(EndpointNode endpointNode, List<AnnotationAttachmentNode> annotations) {
    String endpointName = endpointNode.getName().getValue();
    ServiceModel serviceModel = null;
    setCanProcess(true);
    for (AnnotationAttachmentNode attachmentNode : annotations) {
        String annotationKey = attachmentNode.getAnnotationName().getValue();
        try {
            switch(annotationKey) {
                case "Service":
                    serviceModel = kubernetesAnnotationProcessor.processServiceAnnotation(endpointName, attachmentNode);
                    kubernetesDataHolder.addServiceModel(endpointName, serviceModel);
                    break;
                case "Deployment":
                    kubernetesDataHolder.setDeploymentModel(kubernetesAnnotationProcessor.processDeployment(attachmentNode));
                    break;
                default:
                    break;
            }
        } catch (KubernetesPluginException e) {
            dlog.logDiagnostic(Diagnostic.Kind.ERROR, endpointNode.getPosition(), e.getMessage());
        }
    }
    List<BLangRecordLiteral.BLangRecordKeyValue> keyValues = ((BLangRecordLiteral) ((BLangEndpoint) endpointNode).configurationExpr).getKeyValuePairs();
    for (BLangRecordLiteral.BLangRecordKeyValue keyValue : keyValues) {
        String key = keyValue.getKey().toString();
        switch(key) {
            case "port":
                int port = Integer.parseInt(keyValue.getValue().toString());
                kubernetesDataHolder.addPort(port);
                if (serviceModel != null) {
                    serviceModel.setPort(port);
                }
                break;
            case "secureSocket":
                List<BLangRecordLiteral.BLangRecordKeyValue> sslKeyValues = ((BLangRecordLiteral) keyValue.valueExpr).getKeyValuePairs();
                try {
                    Set<SecretModel> secretModels = kubernetesAnnotationProcessor.processSecureSocketAnnotation(endpointName, sslKeyValues);
                    kubernetesDataHolder.addEndpointSecret(endpointName, secretModels);
                    kubernetesDataHolder.addSecrets(secretModels);
                } catch (KubernetesPluginException e) {
                    dlog.logDiagnostic(Diagnostic.Kind.ERROR, null, e.getMessage());
                }
                break;
            default:
                break;
        }
    }
}
Also used : ServiceModel(org.ballerinax.kubernetes.models.ServiceModel) KubernetesPluginException(org.ballerinax.kubernetes.exceptions.KubernetesPluginException) BLangRecordLiteral(org.wso2.ballerinalang.compiler.tree.expressions.BLangRecordLiteral) SecretModel(org.ballerinax.kubernetes.models.SecretModel) BLangEndpoint(org.wso2.ballerinalang.compiler.tree.BLangEndpoint) AnnotationAttachmentNode(org.ballerinalang.model.tree.AnnotationAttachmentNode)

Example 4 with SecretModel

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

the class DeploymentHandler method populateVolumeMounts.

private List<VolumeMount> populateVolumeMounts(DeploymentModel deploymentModel) {
    List<VolumeMount> volumeMounts = new ArrayList<>();
    for (SecretModel secretModel : deploymentModel.getSecretModels()) {
        VolumeMount volumeMount = new VolumeMountBuilder().withMountPath(secretModel.getMountPath()).withName(secretModel.getName() + "-volume").withReadOnly(secretModel.isReadOnly()).build();
        volumeMounts.add(volumeMount);
    }
    for (ConfigMapModel configMapModel : deploymentModel.getConfigMapModels()) {
        VolumeMount volumeMount = new VolumeMountBuilder().withMountPath(configMapModel.getMountPath()).withName(configMapModel.getName() + "-volume").withReadOnly(configMapModel.isReadOnly()).build();
        volumeMounts.add(volumeMount);
    }
    for (PersistentVolumeClaimModel volumeClaimModel : deploymentModel.getVolumeClaimModels()) {
        VolumeMount volumeMount = new VolumeMountBuilder().withMountPath(volumeClaimModel.getMountPath()).withName(volumeClaimModel.getName() + "-volume").withReadOnly(volumeClaimModel.isReadOnly()).build();
        volumeMounts.add(volumeMount);
    }
    return volumeMounts;
}
Also used : ConfigMapModel(org.ballerinax.kubernetes.models.ConfigMapModel) ArrayList(java.util.ArrayList) VolumeMount(io.fabric8.kubernetes.api.model.VolumeMount) PersistentVolumeClaimModel(org.ballerinax.kubernetes.models.PersistentVolumeClaimModel) SecretModel(org.ballerinax.kubernetes.models.SecretModel) VolumeMountBuilder(io.fabric8.kubernetes.api.model.VolumeMountBuilder)

Example 5 with SecretModel

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

the class DeploymentHandler method populateVolume.

private List<Volume> populateVolume(DeploymentModel deploymentModel) {
    List<Volume> volumes = new ArrayList<>();
    for (SecretModel secretModel : deploymentModel.getSecretModels()) {
        Volume volume = new VolumeBuilder().withName(secretModel.getName() + "-volume").withNewSecret().withSecretName(secretModel.getName()).endSecret().build();
        volumes.add(volume);
    }
    for (ConfigMapModel configMapModel : deploymentModel.getConfigMapModels()) {
        Volume volume = new VolumeBuilder().withName(configMapModel.getName() + "-volume").withNewConfigMap().withName(configMapModel.getName()).endConfigMap().build();
        volumes.add(volume);
    }
    for (PersistentVolumeClaimModel volumeClaimModel : deploymentModel.getVolumeClaimModels()) {
        Volume volume = new VolumeBuilder().withName(volumeClaimModel.getName() + "-volume").withNewPersistentVolumeClaim().withClaimName(volumeClaimModel.getName()).endPersistentVolumeClaim().build();
        volumes.add(volume);
    }
    return volumes;
}
Also used : ConfigMapModel(org.ballerinax.kubernetes.models.ConfigMapModel) Volume(io.fabric8.kubernetes.api.model.Volume) ArrayList(java.util.ArrayList) PersistentVolumeClaimModel(org.ballerinax.kubernetes.models.PersistentVolumeClaimModel) SecretModel(org.ballerinax.kubernetes.models.SecretModel) VolumeBuilder(io.fabric8.kubernetes.api.model.VolumeBuilder)

Aggregations

SecretModel (org.ballerinax.kubernetes.models.SecretModel)6 HashSet (java.util.HashSet)3 ConfigMapModel (org.ballerinax.kubernetes.models.ConfigMapModel)3 PersistentVolumeClaimModel (org.ballerinax.kubernetes.models.PersistentVolumeClaimModel)3 BLangRecordLiteral (org.wso2.ballerinalang.compiler.tree.expressions.BLangRecordLiteral)3 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 KubernetesPluginException (org.ballerinax.kubernetes.exceptions.KubernetesPluginException)2 ServiceModel (org.ballerinax.kubernetes.models.ServiceModel)2 Volume (io.fabric8.kubernetes.api.model.Volume)1 VolumeBuilder (io.fabric8.kubernetes.api.model.VolumeBuilder)1 VolumeMount (io.fabric8.kubernetes.api.model.VolumeMount)1 VolumeMountBuilder (io.fabric8.kubernetes.api.model.VolumeMountBuilder)1 Map (java.util.Map)1 Set (java.util.Set)1 AnnotationAttachmentNode (org.ballerinalang.model.tree.AnnotationAttachmentNode)1 DeploymentModel (org.ballerinax.kubernetes.models.DeploymentModel)1 IngressModel (org.ballerinax.kubernetes.models.IngressModel)1 BLangEndpoint (org.wso2.ballerinalang.compiler.tree.BLangEndpoint)1 BLangArrayLiteral (org.wso2.ballerinalang.compiler.tree.expressions.BLangArrayLiteral)1