use of org.ballerinax.kubernetes.KubernetesConstants.KUBERNETES in project kubernetes by ballerinax.
the class KubernetesPlugin method process.
@Override
public void process(PackageNode packageNode) {
BLangPackage bPackage = (BLangPackage) packageNode;
KubernetesContext.getInstance().addDataHolder(bPackage.packageID, sourceDirectory.getPath());
// Get dependency jar paths
JarResolver jarResolver = KubernetesContext.getInstance().getCompilerContext().get(JAR_RESOLVER_KEY);
if (jarResolver != null) {
Set<Path> dependencyJarPaths = new HashSet<>(jarResolver.allDependencies(bPackage));
KubernetesContext.getInstance().getDataHolder(bPackage.packageID).getDockerModel().addDependencyJarPaths(dependencyJarPaths);
}
// Get the imports with alias _
List<BLangImportPackage> kubernetesImports = bPackage.getImports().stream().filter(i -> i.symbol.toString().startsWith("ballerina/kubernetes") && i.getAlias().toString().equals("_")).collect(Collectors.toList());
if (kubernetesImports.size() > 0) {
for (BLangImportPackage kubernetesImport : kubernetesImports) {
// Get the units of the file which has kubernetes import as _
List<TopLevelNode> topLevelNodes = bPackage.getCompilationUnits().stream().filter(cu -> cu.getName().equals(kubernetesImport.compUnit.getValue())).flatMap(cu -> cu.getTopLevelNodes().stream()).collect(Collectors.toList());
// Filter out the services
List<ServiceNode> serviceNodes = topLevelNodes.stream().filter(tln -> tln instanceof ServiceNode).map(tln -> (ServiceNode) tln).collect(Collectors.toList());
// Generate artifacts for services for all services
serviceNodes.forEach(sn -> process(sn, Collections.singletonList(createAnnotation("Deployment"))));
// Create Service annotation with NodePort service type
AnnotationAttachmentNode serviceAnnotation = createAnnotation("Service");
BLangRecordLiteral svcRecordLiteral = (BLangRecordLiteral) TreeBuilder.createRecordLiteralNode();
serviceAnnotation.setExpression(svcRecordLiteral);
BLangLiteral serviceTypeKey = (BLangLiteral) TreeBuilder.createLiteralExpression();
serviceTypeKey.value = ServiceAnnotationProcessor.ServiceConfiguration.serviceType.name();
serviceTypeKey.type = new BType(TypeTags.STRING, null);
BLangLiteral serviceTypeValue = new BLangLiteral();
serviceTypeValue.value = KubernetesConstants.ServiceType.NodePort.name();
serviceTypeValue.type = new BType(TypeTags.STRING, null);
BLangRecordLiteral.BLangRecordKeyValueField serviceTypeRecordField = new BLangRecordLiteral.BLangRecordKeyValueField();
serviceTypeRecordField.key = new BLangRecordLiteral.BLangRecordKey(serviceTypeKey);
serviceTypeRecordField.valueExpr = serviceTypeValue;
svcRecordLiteral.fields.add(serviceTypeRecordField);
// Filter services with 'new Listener()' and generate services
for (ServiceNode serviceNode : serviceNodes) {
Optional<? extends ExpressionNode> initListener = serviceNode.getAttachedExprs().stream().filter(aex -> aex instanceof BLangTypeInit).findAny();
if (initListener.isPresent()) {
serviceNodes.forEach(sn -> process(sn, Collections.singletonList(serviceAnnotation)));
}
}
// Get the variable names of the listeners attached to services
List<String> listenerNamesToExpose = serviceNodes.stream().map(ServiceNode::getAttachedExprs).flatMap(Collection::stream).filter(aex -> aex instanceof BLangSimpleVarRef).map(aex -> (BLangSimpleVarRef) aex).map(BLangSimpleVarRef::toString).collect(Collectors.toList());
// Generate artifacts for listeners attached to services
topLevelNodes.stream().filter(tln -> tln instanceof SimpleVariableNode).map(tln -> (SimpleVariableNode) tln).filter(listener -> listenerNamesToExpose.contains(listener.getName().getValue())).forEach(listener -> process(listener, Collections.singletonList(serviceAnnotation)));
// Generate artifacts for main functions
topLevelNodes.stream().filter(tln -> tln instanceof FunctionNode).map(tln -> (FunctionNode) tln).filter(fn -> "main".equals(fn.getName().getValue())).forEach(fn -> process(fn, Collections.singletonList(createAnnotation("Job"))));
}
}
}
Aggregations