Search in sources :

Example 1 with BLangListConstructorExpr

use of org.wso2.ballerinalang.compiler.tree.expressions.BLangListConstructorExpr in project kubernetes by ballerinax.

the class ResourceQuotaAnnotationPreprocessor method processResourceQuotaAnnotation.

private void processResourceQuotaAnnotation(BLangAnnotationAttachment attachmentNode) throws KubernetesPluginException {
    Set<ResourceQuotaModel> resourceQuotaModels = new HashSet<>();
    List<BLangRecordLiteral.BLangRecordKeyValueField> keyValues = convertRecordFields(((BLangRecordLiteral) attachmentNode.expr).getFields());
    for (BLangRecordLiteral.BLangRecordKeyValueField keyValue : keyValues) {
        List<BLangExpression> secretAnnotation = ((BLangListConstructorExpr) keyValue.valueExpr).exprs;
        for (BLangExpression bLangExpression : secretAnnotation) {
            ResourceQuotaModel resourceQuotaModel = new ResourceQuotaModel();
            List<BLangRecordLiteral.BLangRecordKeyValueField> annotationValues = convertRecordFields(((BLangRecordLiteral) bLangExpression).getFields());
            for (BLangRecordLiteral.BLangRecordKeyValueField annotation : annotationValues) {
                ResourceQuotaConfig resourceQuotaConfig = ResourceQuotaConfig.valueOf(annotation.getKey().toString());
                switch(resourceQuotaConfig) {
                    case name:
                        resourceQuotaModel.setName(getValidName(getStringValue(annotation.getValue())));
                        break;
                    case labels:
                        resourceQuotaModel.setLabels(getMap(annotation.getValue()));
                        break;
                    case annotations:
                        resourceQuotaModel.setAnnotations(getMap(annotation.getValue()));
                        break;
                    case hard:
                        resourceQuotaModel.setHard(getMap(annotation.getValue()));
                        break;
                    case scopes:
                        resourceQuotaModel.setScopes(new HashSet<>(getList(annotation.getValue())));
                        break;
                    default:
                        break;
                }
            }
            resourceQuotaModels.add(resourceQuotaModel);
        }
    }
    KubernetesContext.getInstance().getDataHolder().setResourceQuotaModels(resourceQuotaModels);
}
Also used : ResourceQuotaModel(org.ballerinax.kubernetes.models.ResourceQuotaModel) BLangListConstructorExpr(org.wso2.ballerinalang.compiler.tree.expressions.BLangListConstructorExpr) BLangRecordLiteral(org.wso2.ballerinalang.compiler.tree.expressions.BLangRecordLiteral) BLangExpression(org.wso2.ballerinalang.compiler.tree.expressions.BLangExpression) HashSet(java.util.HashSet)

Example 2 with BLangListConstructorExpr

use of org.wso2.ballerinalang.compiler.tree.expressions.BLangListConstructorExpr in project kubernetes by ballerinax.

the class IstioGatewayAnnotationProcessor method processIstioGatewayServerAnnotation.

/**
 * Process server field of @istio:Gateway annotation.
 *
 * @param gatewayModel The gateway model.
 * @param serversField List of servers of the gateway.
 * @throws KubernetesPluginException Unable to process annotation
 */
private void processIstioGatewayServerAnnotation(IstioGatewayModel gatewayModel, BLangListConstructorExpr serversField) throws KubernetesPluginException {
    List<IstioServerModel> servers = new LinkedList<>();
    for (ExpressionNode serverRecord : serversField.getExpressions()) {
        if (serverRecord instanceof BLangRecordLiteral) {
            BLangRecordLiteral serverFieldRecord = (BLangRecordLiteral) serverRecord;
            IstioServerModel server = new IstioServerModel();
            for (BLangRecordLiteral.BLangRecordKeyValueField serverField : convertRecordFields(serverFieldRecord.getFields())) {
                switch(ServerConfig.valueOf(serverField.getKey().toString())) {
                    case port:
                        BLangRecordLiteral portRecord = (BLangRecordLiteral) serverField.getValue();
                        processIstioGatewayPortAnnotation(server, convertRecordFields(portRecord.getFields()));
                        break;
                    case hosts:
                        server.setHosts(getList(serverField.getValue()));
                        break;
                    case tls:
                        BLangRecordLiteral tlsRecord = (BLangRecordLiteral) serverField.getValue();
                        processIstioGatewayTLSAnnotation(server, convertRecordFields(tlsRecord.getFields()));
                        break;
                    default:
                        throw new KubernetesPluginException("unknown field found for istio gateway server.");
                }
            }
            servers.add(server);
        }
    }
    gatewayModel.setServers(servers);
}
Also used : ExpressionNode(org.ballerinalang.model.tree.expressions.ExpressionNode) IstioServerModel(org.ballerinax.kubernetes.models.istio.IstioServerModel) KubernetesPluginException(org.ballerinax.kubernetes.exceptions.KubernetesPluginException) BLangRecordLiteral(org.wso2.ballerinalang.compiler.tree.expressions.BLangRecordLiteral) LinkedList(java.util.LinkedList)

Example 3 with BLangListConstructorExpr

use of org.wso2.ballerinalang.compiler.tree.expressions.BLangListConstructorExpr in project kubernetes by ballerinax.

the class KnativeConfigMapAnnotationProcessor method processConfigMaps.

private void processConfigMaps(IdentifierNode nodeID, AnnotationAttachmentNode attachmentNode) throws KubernetesPluginException {
    Set<ConfigMapModel> configMapModels = new HashSet<>();
    List<BLangRecordLiteral.BLangRecordKeyValueField> keyValues = convertRecordFields(((BLangRecordLiteral) ((BLangAnnotationAttachment) attachmentNode).expr).getFields());
    for (BLangRecordLiteral.BLangRecordKeyValueField keyValue : keyValues) {
        String key = keyValue.getKey().toString();
        switch(key) {
            case "configMaps":
                List<BLangExpression> configAnnotation = ((BLangListConstructorExpr) keyValue.valueExpr).exprs;
                for (BLangExpression bLangExpression : configAnnotation) {
                    ConfigMapModel configMapModel = new ConfigMapModel();
                    List<BLangRecordLiteral.BLangRecordKeyValueField> annotationValues = convertRecordFields(((BLangRecordLiteral) bLangExpression).getFields());
                    for (BLangRecordLiteral.BLangRecordKeyValueField annotation : annotationValues) {
                        ConfigMapMountConfig volumeMountConfig = ConfigMapMountConfig.valueOf(annotation.getKey().toString());
                        switch(volumeMountConfig) {
                            case name:
                                configMapModel.setName(getValidName(getStringValue(annotation.getValue())));
                                break;
                            case labels:
                                configMapModel.setLabels(getMap(keyValue.getValue()));
                                break;
                            case annotations:
                                configMapModel.setAnnotations(getMap(keyValue.getValue()));
                                break;
                            case mountPath:
                                // validate mount path is not set to ballerina home or ballerina runtime
                                final Path mountPath = Paths.get(getStringValue(annotation.getValue()));
                                final Path homePath = Paths.get(BALLERINA_HOME);
                                final Path runtimePath = Paths.get(BALLERINA_RUNTIME);
                                final Path confPath = Paths.get(BALLERINA_CONF_MOUNT_PATH);
                                if (mountPath.equals(homePath)) {
                                    throw new KubernetesPluginException("@kubernetes:ConfigMap{} mount path " + "cannot be ballerina home: " + BALLERINA_HOME);
                                }
                                if (mountPath.equals(runtimePath)) {
                                    throw new KubernetesPluginException("@kubernetes:ConfigMap{} mount path " + "cannot be ballerina runtime: " + BALLERINA_RUNTIME);
                                }
                                if (mountPath.equals(confPath)) {
                                    throw new KubernetesPluginException("@kubernetes:ConfigMap{} mount path " + "cannot be ballerina conf file mount " + "path: " + BALLERINA_CONF_MOUNT_PATH);
                                }
                                configMapModel.setMountPath(getStringValue(annotation.getValue()));
                                break;
                            case data:
                                List<BLangExpression> data = ((BLangListConstructorExpr) annotation.valueExpr).exprs;
                                configMapModel.setData(getDataForConfigMap(data));
                                break;
                            case readOnly:
                                configMapModel.setReadOnly(getBooleanValue(annotation.getValue()));
                                break;
                            default:
                                break;
                        }
                    }
                    if (isBlank(configMapModel.getName())) {
                        configMapModel.setName(getValidName(nodeID.getValue()) + CONFIG_MAP_POSTFIX);
                    }
                    if (configMapModel.getData() != null && configMapModel.getData().size() > 0) {
                        configMapModels.add(configMapModel);
                    }
                }
                break;
            case "conf":
                // create a new config map model with ballerina conf and add it to data holder.
                configMapModels.add(getBallerinaConfConfigMap(keyValue.getValue().toString(), nodeID.getValue()));
                break;
            default:
                break;
        }
    }
    KnativeContext.getInstance().getDataHolder().addConfigMaps(configMapModels);
}
Also used : Path(java.nio.file.Path) BLangAnnotationAttachment(org.wso2.ballerinalang.compiler.tree.BLangAnnotationAttachment) KubernetesPluginException(org.ballerinax.kubernetes.exceptions.KubernetesPluginException) ConfigMapModel(org.ballerinax.kubernetes.models.knative.ConfigMapModel) BLangListConstructorExpr(org.wso2.ballerinalang.compiler.tree.expressions.BLangListConstructorExpr) BLangRecordLiteral(org.wso2.ballerinalang.compiler.tree.expressions.BLangRecordLiteral) BLangExpression(org.wso2.ballerinalang.compiler.tree.expressions.BLangExpression) HashSet(java.util.HashSet)

Example 4 with BLangListConstructorExpr

use of org.wso2.ballerinalang.compiler.tree.expressions.BLangListConstructorExpr in project kubernetes by ballerinax.

the class ConfigMapAnnotationProcessor method processConfigMaps.

private void processConfigMaps(IdentifierNode nodeID, AnnotationAttachmentNode attachmentNode) throws KubernetesPluginException {
    Set<ConfigMapModel> configMapModels = new HashSet<>();
    List<BLangRecordLiteral.BLangRecordKeyValueField> keyValues = convertRecordFields(((BLangRecordLiteral) ((BLangAnnotationAttachment) attachmentNode).expr).getFields());
    for (BLangRecordLiteral.BLangRecordKeyValueField keyValue : keyValues) {
        String key = keyValue.getKey().toString();
        switch(key) {
            case "configMaps":
                List<BLangExpression> configAnnotation = ((BLangListConstructorExpr) keyValue.valueExpr).exprs;
                for (BLangExpression bLangExpression : configAnnotation) {
                    ConfigMapModel configMapModel = new ConfigMapModel();
                    List<BLangRecordLiteral.BLangRecordKeyValueField> annotationValues = convertRecordFields(((BLangRecordLiteral) bLangExpression).getFields());
                    for (BLangRecordLiteral.BLangRecordKeyValueField annotation : annotationValues) {
                        ConfigMapMountConfig volumeMountConfig = ConfigMapMountConfig.valueOf(annotation.getKey().toString());
                        switch(volumeMountConfig) {
                            case name:
                                configMapModel.setName(getValidName(getStringValue(annotation.getValue())));
                                break;
                            case labels:
                                configMapModel.setLabels(getMap(keyValue.getValue()));
                                break;
                            case annotations:
                                configMapModel.setAnnotations(getMap(keyValue.getValue()));
                                break;
                            case mountPath:
                                // validate mount path is not set to ballerina home or ballerina runtime
                                final Path mountPath = Paths.get(getStringValue(annotation.getValue()));
                                final Path homePath = Paths.get(BALLERINA_HOME);
                                final Path runtimePath = Paths.get(BALLERINA_RUNTIME);
                                final Path confPath = Paths.get(BALLERINA_CONF_MOUNT_PATH);
                                if (mountPath.equals(homePath)) {
                                    throw new KubernetesPluginException("@kubernetes:ConfigMap{} mount path " + "cannot be ballerina home: " + BALLERINA_HOME);
                                }
                                if (mountPath.equals(runtimePath)) {
                                    throw new KubernetesPluginException("@kubernetes:ConfigMap{} mount path " + "cannot be ballerina runtime: " + BALLERINA_RUNTIME);
                                }
                                if (mountPath.equals(confPath)) {
                                    throw new KubernetesPluginException("@kubernetes:ConfigMap{} mount path " + "cannot be ballerina conf file mount " + "path: " + BALLERINA_CONF_MOUNT_PATH);
                                }
                                configMapModel.setMountPath(getStringValue(annotation.getValue()));
                                break;
                            case data:
                                List<BLangExpression> data = ((BLangListConstructorExpr) annotation.valueExpr).exprs;
                                configMapModel.setData(getDataForConfigMap(data));
                                break;
                            case readOnly:
                                configMapModel.setReadOnly(getBooleanValue(annotation.getValue()));
                                break;
                            case defaultMode:
                                configMapModel.setDefaultMode(getIntValue(annotation.getValue()));
                                break;
                            default:
                                break;
                        }
                    }
                    if (isBlank(configMapModel.getName())) {
                        configMapModel.setName(getValidName(nodeID.getValue()) + CONFIG_MAP_POSTFIX);
                    }
                    if (configMapModel.getData() != null && configMapModel.getData().size() > 0) {
                        configMapModels.add(configMapModel);
                    }
                }
                break;
            case "conf":
                // create a new config map model with ballerina conf and add it to data holder.
                configMapModels.add(getBallerinaConfConfigMap(keyValue.getValue().toString(), nodeID.getValue()));
                break;
            default:
                break;
        }
    }
    KubernetesContext.getInstance().getDataHolder().addConfigMaps(configMapModels);
}
Also used : Path(java.nio.file.Path) BLangAnnotationAttachment(org.wso2.ballerinalang.compiler.tree.BLangAnnotationAttachment) KubernetesPluginException(org.ballerinax.kubernetes.exceptions.KubernetesPluginException) ConfigMapModel(org.ballerinax.kubernetes.models.ConfigMapModel) BLangListConstructorExpr(org.wso2.ballerinalang.compiler.tree.expressions.BLangListConstructorExpr) BLangRecordLiteral(org.wso2.ballerinalang.compiler.tree.expressions.BLangRecordLiteral) BLangExpression(org.wso2.ballerinalang.compiler.tree.expressions.BLangExpression) HashSet(java.util.HashSet)

Example 5 with BLangListConstructorExpr

use of org.wso2.ballerinalang.compiler.tree.expressions.BLangListConstructorExpr in project kubernetes by ballerinax.

the class SecretAnnotationProcessor method processSecret.

private void processSecret(IdentifierNode nodeID, AnnotationAttachmentNode attachmentNode) throws KubernetesPluginException {
    Set<SecretModel> secrets = new HashSet<>();
    List<BLangRecordLiteral.BLangRecordKeyValueField> keyValues = convertRecordFields(((BLangRecordLiteral) ((BLangAnnotationAttachment) attachmentNode).expr).getFields());
    for (BLangRecordLiteral.BLangRecordKeyValueField keyValue : keyValues) {
        String key = keyValue.getKey().toString();
        switch(key) {
            case "secrets":
                List<BLangExpression> configAnnotation = ((BLangListConstructorExpr) keyValue.valueExpr).exprs;
                for (BLangExpression bLangExpression : configAnnotation) {
                    SecretModel secretModel = new SecretModel();
                    List<BLangRecordLiteral.BLangRecordKeyValueField> annotationValues = convertRecordFields(((BLangRecordLiteral) bLangExpression).getFields());
                    for (BLangRecordLiteral.BLangRecordKeyValueField annotation : annotationValues) {
                        SecretMountConfig volumeMountConfig = SecretMountConfig.valueOf(annotation.getKey().toString());
                        switch(volumeMountConfig) {
                            case name:
                                secretModel.setName(getValidName(getStringValue(annotation.getValue())));
                                break;
                            case labels:
                                secretModel.setLabels(getMap(keyValue.getValue()));
                                break;
                            case annotations:
                                secretModel.setAnnotations(getMap(keyValue.getValue()));
                                break;
                            case mountPath:
                                // validate mount path is not set to ballerina home or ballerina runtime
                                final Path mountPath = Paths.get(getStringValue(annotation.getValue()));
                                final Path homePath = Paths.get(BALLERINA_HOME);
                                final Path runtimePath = Paths.get(BALLERINA_RUNTIME);
                                final Path confPath = Paths.get(BALLERINA_CONF_MOUNT_PATH);
                                if (mountPath.equals(homePath)) {
                                    throw new KubernetesPluginException("@kubernetes:Secret{} mount path " + "cannot be ballerina home: " + BALLERINA_HOME);
                                }
                                if (mountPath.equals(runtimePath)) {
                                    throw new KubernetesPluginException("@kubernetes:Secret{} mount path " + "cannot be ballerina runtime: " + BALLERINA_RUNTIME);
                                }
                                if (mountPath.equals(confPath)) {
                                    throw new KubernetesPluginException("@kubernetes:Secret{} mount path " + "cannot be ballerina conf file mount " + "path: " + BALLERINA_CONF_MOUNT_PATH);
                                }
                                secretModel.setMountPath(getStringValue(annotation.getValue()));
                                break;
                            case data:
                                List<BLangExpression> data = ((BLangListConstructorExpr) annotation.valueExpr).exprs;
                                secretModel.setData(getDataForSecret(data));
                                break;
                            case readOnly:
                                secretModel.setReadOnly(getBooleanValue(annotation.getValue()));
                                break;
                            case defaultMode:
                                secretModel.setDefaultMode(getIntValue(annotation.getValue()));
                                break;
                            default:
                                break;
                        }
                    }
                    if (isBlank(secretModel.getName())) {
                        secretModel.setName(getValidName(nodeID.getValue()) + SECRET_POSTFIX);
                    }
                    if (secretModel.getData() != null && secretModel.getData().size() > 0) {
                        secrets.add(secretModel);
                    }
                }
                break;
            case "conf":
                // create a new secret model with ballerina conf and add it to data holder.
                secrets.add(getBallerinaConfSecret(keyValue.getValue().toString(), nodeID.getValue()));
                break;
            default:
                break;
        }
    }
    KubernetesContext.getInstance().getDataHolder().addSecrets(secrets);
}
Also used : Path(java.nio.file.Path) BLangAnnotationAttachment(org.wso2.ballerinalang.compiler.tree.BLangAnnotationAttachment) KubernetesPluginException(org.ballerinax.kubernetes.exceptions.KubernetesPluginException) SecretModel(org.ballerinax.kubernetes.models.SecretModel) BLangListConstructorExpr(org.wso2.ballerinalang.compiler.tree.expressions.BLangListConstructorExpr) BLangRecordLiteral(org.wso2.ballerinalang.compiler.tree.expressions.BLangRecordLiteral) BLangExpression(org.wso2.ballerinalang.compiler.tree.expressions.BLangExpression) HashSet(java.util.HashSet)

Aggregations

BLangListConstructorExpr (org.wso2.ballerinalang.compiler.tree.expressions.BLangListConstructorExpr)18 BLangExpression (org.wso2.ballerinalang.compiler.tree.expressions.BLangExpression)16 BLangRecordLiteral (org.wso2.ballerinalang.compiler.tree.expressions.BLangRecordLiteral)15 KubernetesPluginException (org.ballerinax.kubernetes.exceptions.KubernetesPluginException)13 HashSet (java.util.HashSet)12 LinkedList (java.util.LinkedList)7 BLangAnnotationAttachment (org.wso2.ballerinalang.compiler.tree.BLangAnnotationAttachment)5 ExpressionNode (org.ballerinalang.model.tree.expressions.ExpressionNode)4 Path (java.nio.file.Path)3 CopyFileModel (org.ballerinax.docker.generator.models.CopyFileModel)2 IstioHttpRoute (org.ballerinax.kubernetes.models.istio.IstioHttpRoute)2 IntOrString (io.fabric8.kubernetes.api.model.IntOrString)1 ConfigMapModel (org.ballerinax.kubernetes.models.ConfigMapModel)1 PersistentVolumeClaimModel (org.ballerinax.kubernetes.models.PersistentVolumeClaimModel)1 PodTolerationModel (org.ballerinax.kubernetes.models.PodTolerationModel)1 ResourceQuotaModel (org.ballerinax.kubernetes.models.ResourceQuotaModel)1 SecretModel (org.ballerinax.kubernetes.models.SecretModel)1 ServiceAccountTokenModel (org.ballerinax.kubernetes.models.ServiceAccountTokenModel)1 IstioDestination (org.ballerinax.kubernetes.models.istio.IstioDestination)1 IstioDestinationWeight (org.ballerinax.kubernetes.models.istio.IstioDestinationWeight)1