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