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