Search in sources :

Example 1 with EnvVarValueModel

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

the class SecretHandler method createArtifacts.

@Override
public void createArtifacts() throws KubernetesPluginException {
    // secret
    int count = 0;
    Collection<SecretModel> secretModels = dataHolder.getSecretModelSet();
    if (secretModels.size() > 0) {
        OUT.println();
    }
    for (SecretModel secretModel : secretModels) {
        count++;
        if (!isBlank(secretModel.getBallerinaConf())) {
            if (secretModel.getData().size() != 1) {
                throw new KubernetesPluginException("there can be only 1 ballerina config file");
            }
            DeploymentModel deploymentModel = dataHolder.getDeploymentModel();
            deploymentModel.setCommandArgs(" --b7a.config.file=${CONFIG_FILE}");
            EnvVarValueModel envVarValueModel = new EnvVarValueModel(secretModel.getMountPath() + BALLERINA_CONF_FILE_NAME);
            deploymentModel.addEnv("CONFIG_FILE", envVarValueModel);
            dataHolder.setDeploymentModel(deploymentModel);
        }
        generate(secretModel);
        OUT.print("\t@kubernetes:Secret \t\t\t - complete " + count + "/" + secretModels.size() + "\r");
    }
}
Also used : DeploymentModel(org.ballerinax.kubernetes.models.DeploymentModel) EnvVarValueModel(org.ballerinax.kubernetes.models.EnvVarValueModel) KubernetesPluginException(org.ballerinax.kubernetes.exceptions.KubernetesPluginException) SecretModel(org.ballerinax.kubernetes.models.SecretModel)

Example 2 with EnvVarValueModel

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

the class KubernetesHPAGeneratorTests method testHPAGenerate.

@Test
public void testHPAGenerate() {
    DeploymentModel deploymentModel = new DeploymentModel();
    deploymentModel.setName(deploymentName);
    Map<String, String> labels = new HashMap<>();
    labels.put(KubernetesConstants.KUBERNETES_SELECTOR_KEY, selector);
    deploymentModel.addPort(9090);
    deploymentModel.addPort(9091);
    deploymentModel.addPort(9092);
    deploymentModel.setLabels(labels);
    ProbeModel probeModel = new ProbeModel();
    probeModel.setPort(9090);
    deploymentModel.setLivenessProbe(probeModel);
    deploymentModel.setSingleYAML(false);
    Map<String, EnvVarValueModel> env = new HashMap<>();
    EnvVarValueModel testEnvVar = new EnvVarValueModel("ENV");
    env.put("ENV_VAR", testEnvVar);
    deploymentModel.setEnv(env);
    PodAutoscalerModel podAutoscalerModel = new PodAutoscalerModel();
    podAutoscalerModel.setName(hpaName);
    podAutoscalerModel.setCpuPercentage(cpuPercentage);
    podAutoscalerModel.setMaxReplicas(maxReplicas);
    podAutoscalerModel.setMinReplicas(minReplicas);
    podAutoscalerModel.setDeployment(deploymentName);
    podAutoscalerModel.setLabels(labels);
    deploymentModel.setPodAutoscalerModel(podAutoscalerModel);
    dataHolder.setPodAutoscalerModel(podAutoscalerModel);
    dataHolder.setDeploymentModel(deploymentModel);
    try {
        new HPAHandler().createArtifacts();
        File tempFile = dataHolder.getK8sArtifactOutputPath().resolve("hello_hpa.yaml").toFile();
        Assert.assertTrue(tempFile.exists());
        assertGeneratedYAML(tempFile);
        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) ProbeModel(org.ballerinax.kubernetes.models.ProbeModel) EnvVarValueModel(org.ballerinax.kubernetes.models.EnvVarValueModel) IOException(java.io.IOException) KubernetesPluginException(org.ballerinax.kubernetes.exceptions.KubernetesPluginException) PodAutoscalerModel(org.ballerinax.kubernetes.models.PodAutoscalerModel) File(java.io.File) Test(org.testng.annotations.Test)

Example 3 with EnvVarValueModel

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

the class ConfigMapHandler method createArtifacts.

@Override
public void createArtifacts() throws KubernetesPluginException {
    // configMap
    int count = 0;
    Collection<ConfigMapModel> configMapModels = dataHolder.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");
            }
            DeploymentModel deploymentModel = dataHolder.getDeploymentModel();
            deploymentModel.setCommandArgs(" --b7a.config.file=${CONFIG_FILE}");
            EnvVarValueModel envVarValueModel = new EnvVarValueModel(configMapModel.getMountPath() + BALLERINA_CONF_FILE_NAME);
            deploymentModel.addEnv("CONFIG_FILE", envVarValueModel);
            dataHolder.setDeploymentModel(deploymentModel);
        }
        generate(configMapModel);
        OUT.print("\t@kubernetes:ConfigMap \t\t\t - complete " + count + "/" + configMapModels.size() + "\r");
    }
}
Also used : ConfigMapModel(org.ballerinax.kubernetes.models.ConfigMapModel) DeploymentModel(org.ballerinax.kubernetes.models.DeploymentModel) EnvVarValueModel(org.ballerinax.kubernetes.models.EnvVarValueModel) KubernetesPluginException(org.ballerinax.kubernetes.exceptions.KubernetesPluginException)

Example 4 with EnvVarValueModel

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

the class KubernetesUtils 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) {
        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 = (BLangRecordLiteral.BLangRecordKeyValueField) 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.EnvVarValueModel) LinkedHashMap(java.util.LinkedHashMap) BLangSimpleVarRef(org.wso2.ballerinalang.compiler.tree.expressions.BLangSimpleVarRef) BLangRecordLiteral(org.wso2.ballerinalang.compiler.tree.expressions.BLangRecordLiteral)

Example 5 with EnvVarValueModel

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

the class KubernetesUtils 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.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)

Aggregations

EnvVarValueModel (org.ballerinax.kubernetes.models.EnvVarValueModel)7 KubernetesPluginException (org.ballerinax.kubernetes.exceptions.KubernetesPluginException)5 DeploymentModel (org.ballerinax.kubernetes.models.DeploymentModel)4 File (java.io.File)3 IOException (java.io.IOException)3 HashMap (java.util.HashMap)3 Test (org.testng.annotations.Test)3 ProbeModel (org.ballerinax.kubernetes.models.ProbeModel)2 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