Search in sources :

Example 1 with ConfigMapModel

use of org.ballerinax.kubernetes.models.knative.ConfigMapModel in project kubernetes by ballerinax.

the class KnativeConfigMapAnnotationProcessor method getBallerinaConfConfigMap.

private ConfigMapModel getBallerinaConfConfigMap(String configFilePath, String serviceName) throws KubernetesPluginException {
    // create a new config map model with ballerina conf
    ConfigMapModel configMapModel = new ConfigMapModel();
    configMapModel.setName(getValidName(serviceName) + "-ballerina-conf" + CONFIG_MAP_POSTFIX);
    configMapModel.setMountPath(BALLERINA_CONF_MOUNT_PATH);
    Path dataFilePath = Paths.get(configFilePath);
    if (!dataFilePath.isAbsolute()) {
        dataFilePath = KnativeContext.getInstance().getDataHolder().getSourceRoot().resolve(dataFilePath).normalize();
    }
    String content = new String(KnativeUtils.readFileContent(dataFilePath), StandardCharsets.UTF_8);
    Map<String, String> dataMap = new HashMap<>();
    dataMap.put(BALLERINA_CONF_FILE_NAME, content);
    configMapModel.setData(dataMap);
    configMapModel.setBallerinaConf(configFilePath);
    configMapModel.setReadOnly(false);
    return configMapModel;
}
Also used : Path(java.nio.file.Path) ConfigMapModel(org.ballerinax.kubernetes.models.knative.ConfigMapModel) HashMap(java.util.HashMap)

Example 2 with ConfigMapModel

use of org.ballerinax.kubernetes.models.knative.ConfigMapModel 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 3 with ConfigMapModel

use of org.ballerinax.kubernetes.models.knative.ConfigMapModel in project kubernetes by ballerinax.

the class KnativeServiceHandler method populateVolumeMounts.

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

Example 4 with ConfigMapModel

use of org.ballerinax.kubernetes.models.knative.ConfigMapModel in project kubernetes by ballerinax.

the class KnativeConfigMapHandler method createArtifacts.

@Override
public void createArtifacts() throws KubernetesPluginException {
    // configMap
    int count = 0;
    Collection<ConfigMapModel> configMapModels = knativeDataHolder.getConfigMapModelSet();
    if (configMapModels.size() > 0) {
        OUT.println();
    }
    for (ConfigMapModel configMapModel : configMapModels) {
        count++;
        if (!isBlank(configMapModel.getBallerinaConf())) {
            if (configMapModel.getData().size() != 1) {
                throw new KubernetesPluginException("there can be only 1 ballerina config file");
            }
            ServiceModel serviceModel = knativeDataHolder.getServiceModel();
            serviceModel.setCommandArgs(" --b7a.config.file=${CONFIG_FILE}");
            EnvVarValueModel envVarValueModel = new EnvVarValueModel(configMapModel.getMountPath() + BALLERINA_CONF_FILE_NAME);
            serviceModel.addEnv("CONFIG_FILE", envVarValueModel);
            knativeDataHolder.setServiceModel(serviceModel);
        }
        generate(configMapModel);
        OUT.print("\t@knative:ConfigMap \t\t\t - complete " + count + "/" + configMapModels.size() + "\r");
    }
}
Also used : ConfigMapModel(org.ballerinax.kubernetes.models.knative.ConfigMapModel) ServiceModel(org.ballerinax.kubernetes.models.knative.ServiceModel) EnvVarValueModel(org.ballerinax.kubernetes.models.knative.EnvVarValueModel) KubernetesPluginException(org.ballerinax.kubernetes.exceptions.KubernetesPluginException)

Example 5 with ConfigMapModel

use of org.ballerinax.kubernetes.models.knative.ConfigMapModel in project kubernetes by ballerinax.

the class KnativeServiceHandler method populateVolume.

private List<Volume> populateVolume(ServiceModel serviceModel) {
    List<Volume> volumes = new ArrayList<>();
    for (SecretModel secretModel : serviceModel.getSecretModels()) {
        Volume volume = new VolumeBuilder().withName(secretModel.getName() + VOLUME_DEFINE).withNewSecret().withSecretName(secretModel.getName()).endSecret().build();
        volumes.add(volume);
    }
    for (ConfigMapModel configMapModel : serviceModel.getConfigMapModels()) {
        Volume volume = new VolumeBuilder().withName(configMapModel.getName() + VOLUME_DEFINE).withNewConfigMap().withName(configMapModel.getName()).endConfigMap().build();
        volumes.add(volume);
    }
    return volumes;
}
Also used : ConfigMapModel(org.ballerinax.kubernetes.models.knative.ConfigMapModel) Volume(io.fabric8.kubernetes.api.model.Volume) ArrayList(java.util.ArrayList) SecretModel(org.ballerinax.kubernetes.models.knative.SecretModel) VolumeBuilder(io.fabric8.kubernetes.api.model.VolumeBuilder)

Aggregations

ConfigMapModel (org.ballerinax.kubernetes.models.knative.ConfigMapModel)5 Path (java.nio.file.Path)2 ArrayList (java.util.ArrayList)2 KubernetesPluginException (org.ballerinax.kubernetes.exceptions.KubernetesPluginException)2 SecretModel (org.ballerinax.kubernetes.models.knative.SecretModel)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 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 EnvVarValueModel (org.ballerinax.kubernetes.models.knative.EnvVarValueModel)1 ServiceModel (org.ballerinax.kubernetes.models.knative.ServiceModel)1 BLangAnnotationAttachment (org.wso2.ballerinalang.compiler.tree.BLangAnnotationAttachment)1 BLangExpression (org.wso2.ballerinalang.compiler.tree.expressions.BLangExpression)1 BLangListConstructorExpr (org.wso2.ballerinalang.compiler.tree.expressions.BLangListConstructorExpr)1 BLangRecordLiteral (org.wso2.ballerinalang.compiler.tree.expressions.BLangRecordLiteral)1