Search in sources :

Example 1 with EnvVarValueModel

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

the class KnativeUtils method populateEnvVar.

/**
 * Get a list of environment variables.
 *
 * @param envMap Map of Environment variables
 * @return List of env vars
 */
public static List<EnvVar> populateEnvVar(Map<String, EnvVarValueModel> envMap) {
    List<EnvVar> envVars = new ArrayList<>();
    if (envMap == null) {
        return envVars;
    }
    envMap.forEach((k, v) -> {
        EnvVar envVar = null;
        if (v.getValue() != null) {
            envVar = new EnvVarBuilder().withName(k).withValue(v.getValue()).build();
        } else if (v.getValueFrom() instanceof EnvVarValueModel.FieldRef) {
            EnvVarValueModel.FieldRef fieldRefModel = (EnvVarValueModel.FieldRef) v.getValueFrom();
            ObjectFieldSelector fieldRef = new ObjectFieldSelectorBuilder().withFieldPath(fieldRefModel.getFieldPath()).build();
            EnvVarSource envVarSource = new EnvVarSourceBuilder().withFieldRef(fieldRef).build();
            envVar = new EnvVarBuilder().withName(k).withValueFrom(envVarSource).build();
        } else if (v.getValueFrom() instanceof EnvVarValueModel.SecretKeyRef) {
            EnvVarValueModel.SecretKeyRef secretKeyRefModel = (EnvVarValueModel.SecretKeyRef) v.getValueFrom();
            SecretKeySelector secretRef = new SecretKeySelectorBuilder().withName(secretKeyRefModel.getName()).withKey(secretKeyRefModel.getKey()).build();
            EnvVarSource envVarSource = new EnvVarSourceBuilder().withSecretKeyRef(secretRef).build();
            envVar = new EnvVarBuilder().withName(k).withValueFrom(envVarSource).build();
        } else if (v.getValueFrom() instanceof EnvVarValueModel.ResourceFieldRef) {
            EnvVarValueModel.ResourceFieldRef resourceFieldRefModel = (EnvVarValueModel.ResourceFieldRef) v.getValueFrom();
            ResourceFieldSelector resourceFieldRef = new ResourceFieldSelectorBuilder().withContainerName(resourceFieldRefModel.getContainerName()).withResource(resourceFieldRefModel.getResource()).build();
            EnvVarSource envVarSource = new EnvVarSourceBuilder().withResourceFieldRef(resourceFieldRef).build();
            envVar = new EnvVarBuilder().withName(k).withValueFrom(envVarSource).build();
        } else if (v.getValueFrom() instanceof EnvVarValueModel.ConfigMapKeyValue) {
            EnvVarValueModel.ConfigMapKeyValue configMapKeyValue = (EnvVarValueModel.ConfigMapKeyValue) v.getValueFrom();
            ConfigMapKeySelector configMapKey = new ConfigMapKeySelectorBuilder().withKey(configMapKeyValue.getKey()).withName(configMapKeyValue.getName()).build();
            EnvVarSource envVarSource = new EnvVarSourceBuilder().withConfigMapKeyRef(configMapKey).build();
            envVar = new EnvVarBuilder().withName(k).withValueFrom(envVarSource).build();
        }
        if (envVar != null) {
            envVars.add(envVar);
        }
    });
    return envVars;
}
Also used : ConfigMapKeySelectorBuilder(io.fabric8.kubernetes.api.model.ConfigMapKeySelectorBuilder) ArrayList(java.util.ArrayList) EnvVarValueModel(org.ballerinax.kubernetes.models.knative.EnvVarValueModel) ObjectFieldSelectorBuilder(io.fabric8.kubernetes.api.model.ObjectFieldSelectorBuilder) ResourceFieldSelectorBuilder(io.fabric8.kubernetes.api.model.ResourceFieldSelectorBuilder) EnvVarBuilder(io.fabric8.kubernetes.api.model.EnvVarBuilder) SecretKeySelector(io.fabric8.kubernetes.api.model.SecretKeySelector) ObjectFieldSelector(io.fabric8.kubernetes.api.model.ObjectFieldSelector) EnvVarSourceBuilder(io.fabric8.kubernetes.api.model.EnvVarSourceBuilder) EnvVar(io.fabric8.kubernetes.api.model.EnvVar) ResourceFieldSelector(io.fabric8.kubernetes.api.model.ResourceFieldSelector) SecretKeySelectorBuilder(io.fabric8.kubernetes.api.model.SecretKeySelectorBuilder) EnvVarSource(io.fabric8.kubernetes.api.model.EnvVarSource) ConfigMapKeySelector(io.fabric8.kubernetes.api.model.ConfigMapKeySelector)

Example 2 with EnvVarValueModel

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

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

the class KnativeUtils method getEnvVarMap.

/**
 * Convert environment variable values into a map for deployment model.
 *
 * @param envVarValues Value of env field of Deployment annotation.
 * @return A map of env var models.
 */
public static Map<String, EnvVarValueModel> getEnvVarMap(BLangExpression envVarValues) throws KubernetesPluginException {
    Map<String, EnvVarValueModel> envVarMap = new LinkedHashMap<>();
    if (envVarValues.getKind() == NodeKind.RECORD_LITERAL_EXPR) {
        if (envVarValues instanceof BLangRecordLiteral) {
            for (BLangRecordLiteral.BLangRecordKeyValueField envVar : convertRecordFields(((BLangRecordLiteral) envVarValues).getFields())) {
                String envVarName = envVar.getKey().toString();
                EnvVarValueModel envVarValue = null;
                if (envVar.getValue().getKind() == NodeKind.LITERAL) {
                    // Value is a string
                    envVarValue = new EnvVarValueModel(getStringValue(envVar.getValue()));
                } else if (envVar.getValue().getKind() == NodeKind.RECORD_LITERAL_EXPR) {
                    BLangRecordLiteral valueFrom = (BLangRecordLiteral) envVar.getValue();
                    BLangRecordLiteral.BLangRecordKeyValueField bRefType = convertRecordFields(valueFrom.getFields()).get(0);
                    BLangSimpleVarRef refType = (BLangSimpleVarRef) bRefType.getKey();
                    switch(refType.variableName.toString()) {
                        case "fieldRef":
                            BLangRecordLiteral.BLangRecordKeyValueField fieldRefValue = convertRecordFields(((BLangRecordLiteral) bRefType.getValue()).getFields()).get(0);
                            EnvVarValueModel.FieldRef fieldRefModel = new EnvVarValueModel.FieldRef();
                            fieldRefModel.setFieldPath(getStringValue(fieldRefValue.getValue()));
                            envVarValue = new EnvVarValueModel(fieldRefModel);
                            break;
                        case "secretKeyRef":
                            EnvVarValueModel.SecretKeyRef secretKeyRefModel = new EnvVarValueModel.SecretKeyRef();
                            for (BLangRecordLiteral.BLangRecordKeyValueField secretKeyRefFields : convertRecordFields(((BLangRecordLiteral) bRefType.getValue()).getFields())) {
                                if (secretKeyRefFields.getKey().toString().equals("key")) {
                                    secretKeyRefModel.setKey(getStringValue(secretKeyRefFields.getValue()));
                                } else if (secretKeyRefFields.getKey().toString().equals("name")) {
                                    secretKeyRefModel.setName(getStringValue(secretKeyRefFields.getValue()));
                                }
                            }
                            envVarValue = new EnvVarValueModel(secretKeyRefModel);
                            break;
                        case "resourceFieldRef":
                            EnvVarValueModel.ResourceFieldRef resourceFieldRefModel = new EnvVarValueModel.ResourceFieldRef();
                            for (BLangRecordLiteral.BLangRecordKeyValueField resourceFieldRefFields : convertRecordFields(((BLangRecordLiteral) bRefType.getValue()).getFields())) {
                                if (resourceFieldRefFields.getKey().toString().equals("containerName")) {
                                    resourceFieldRefModel.setContainerName(getStringValue(resourceFieldRefFields.getValue()));
                                } else if (resourceFieldRefFields.getKey().toString().equals("resource")) {
                                    resourceFieldRefModel.setResource(getStringValue(resourceFieldRefFields.getValue()));
                                }
                            }
                            envVarValue = new EnvVarValueModel(resourceFieldRefModel);
                            break;
                        case "configMapKeyRef":
                            EnvVarValueModel.ConfigMapKeyValue configMapKeyRefModel = new EnvVarValueModel.ConfigMapKeyValue();
                            for (BLangRecordLiteral.BLangRecordKeyValueField configMapKeyRefFields : convertRecordFields(((BLangRecordLiteral) bRefType.getValue()).getFields())) {
                                if (configMapKeyRefFields.getKey().toString().equals("key")) {
                                    configMapKeyRefModel.setKey(getStringValue(configMapKeyRefFields.getValue()));
                                } else if (configMapKeyRefFields.getKey().toString().equals("name")) {
                                    configMapKeyRefModel.setName(getStringValue(configMapKeyRefFields.getValue()));
                                }
                            }
                            envVarValue = new EnvVarValueModel(configMapKeyRefModel);
                            break;
                        default:
                            break;
                    }
                }
                envVarMap.put(envVarName, envVarValue);
            }
        }
    }
    return envVarMap;
}
Also used : EnvVarValueModel(org.ballerinax.kubernetes.models.knative.EnvVarValueModel) LinkedHashMap(java.util.LinkedHashMap) BLangSimpleVarRef(org.wso2.ballerinalang.compiler.tree.expressions.BLangSimpleVarRef) BLangRecordLiteral(org.wso2.ballerinalang.compiler.tree.expressions.BLangRecordLiteral)

Aggregations

EnvVarValueModel (org.ballerinax.kubernetes.models.knative.EnvVarValueModel)3 ConfigMapKeySelector (io.fabric8.kubernetes.api.model.ConfigMapKeySelector)1 ConfigMapKeySelectorBuilder (io.fabric8.kubernetes.api.model.ConfigMapKeySelectorBuilder)1 EnvVar (io.fabric8.kubernetes.api.model.EnvVar)1 EnvVarBuilder (io.fabric8.kubernetes.api.model.EnvVarBuilder)1 EnvVarSource (io.fabric8.kubernetes.api.model.EnvVarSource)1 EnvVarSourceBuilder (io.fabric8.kubernetes.api.model.EnvVarSourceBuilder)1 ObjectFieldSelector (io.fabric8.kubernetes.api.model.ObjectFieldSelector)1 ObjectFieldSelectorBuilder (io.fabric8.kubernetes.api.model.ObjectFieldSelectorBuilder)1 ResourceFieldSelector (io.fabric8.kubernetes.api.model.ResourceFieldSelector)1 ResourceFieldSelectorBuilder (io.fabric8.kubernetes.api.model.ResourceFieldSelectorBuilder)1 SecretKeySelector (io.fabric8.kubernetes.api.model.SecretKeySelector)1 SecretKeySelectorBuilder (io.fabric8.kubernetes.api.model.SecretKeySelectorBuilder)1 ArrayList (java.util.ArrayList)1 LinkedHashMap (java.util.LinkedHashMap)1 KubernetesPluginException (org.ballerinax.kubernetes.exceptions.KubernetesPluginException)1 ConfigMapModel (org.ballerinax.kubernetes.models.knative.ConfigMapModel)1 ServiceModel (org.ballerinax.kubernetes.models.knative.ServiceModel)1 BLangRecordLiteral (org.wso2.ballerinalang.compiler.tree.expressions.BLangRecordLiteral)1 BLangSimpleVarRef (org.wso2.ballerinalang.compiler.tree.expressions.BLangSimpleVarRef)1