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");
}
}
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");
}
}
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");
}
}
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;
}
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;
}
Aggregations