Search in sources :

Example 1 with KUBERNETES

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"))));
        }
    }
}
Also used : JAR_RESOLVER_KEY(org.ballerinalang.compiler.JarResolver.JAR_RESOLVER_KEY) AnnotationProcessorFactory(org.ballerinax.kubernetes.processors.AnnotationProcessorFactory) LoggerFactory(org.slf4j.LoggerFactory) SupportedAnnotationPackages(org.ballerinalang.compiler.plugins.SupportedAnnotationPackages) BLangImportPackage(org.wso2.ballerinalang.compiler.tree.BLangImportPackage) Flag(org.ballerinalang.model.elements.Flag) Map(java.util.Map) TopLevelNode(org.ballerinalang.model.tree.TopLevelNode) Path(java.nio.file.Path) KUBERNETES(org.ballerinax.kubernetes.KubernetesConstants.KUBERNETES) BLangSimpleVarRef(org.wso2.ballerinalang.compiler.tree.expressions.BLangSimpleVarRef) JarResolver(org.ballerinalang.compiler.JarResolver) DOCKER(org.ballerinax.kubernetes.KubernetesConstants.DOCKER) KubernetesUtils(org.ballerinax.kubernetes.utils.KubernetesUtils) Collection(java.util.Collection) PackageID(org.ballerinalang.model.elements.PackageID) Set(java.util.Set) Collectors(java.util.stream.Collectors) KubernetesUtils.printError(org.ballerinax.kubernetes.utils.KubernetesUtils.printError) List(java.util.List) KubernetesDataHolder(org.ballerinax.kubernetes.models.KubernetesDataHolder) Optional(java.util.Optional) DependencyValidator(org.ballerinax.kubernetes.utils.DependencyValidator) PackageNode(org.ballerinalang.model.tree.PackageNode) KubernetesContext(org.ballerinax.kubernetes.models.KubernetesContext) SimpleVariableNode(org.ballerinalang.model.tree.SimpleVariableNode) BLangTypeInit(org.wso2.ballerinalang.compiler.tree.expressions.BLangTypeInit) DockerGenUtils.extractJarName(org.ballerinax.docker.generator.utils.DockerGenUtils.extractJarName) BLangRecordLiteral(org.wso2.ballerinalang.compiler.tree.expressions.BLangRecordLiteral) DiagnosticLog(org.ballerinalang.util.diagnostic.DiagnosticLog) ArrayList(java.util.ArrayList) SourceDirectory(org.wso2.ballerinalang.compiler.SourceDirectory) TypeTags(org.wso2.ballerinalang.compiler.util.TypeTags) HashSet(java.util.HashSet) AbstractCompilerPlugin(org.ballerinalang.compiler.plugins.AbstractCompilerPlugin) ServiceAnnotationProcessor(org.ballerinax.kubernetes.processors.ServiceAnnotationProcessor) TreeBuilder(org.ballerinalang.model.TreeBuilder) ExpressionNode(org.ballerinalang.model.tree.expressions.ExpressionNode) BType(org.wso2.ballerinalang.compiler.semantics.model.types.BType) DiagnosticSeverity(io.ballerina.tools.diagnostics.DiagnosticSeverity) KubernetesPluginException(org.ballerinax.kubernetes.exceptions.KubernetesPluginException) BLangPackage(org.wso2.ballerinalang.compiler.tree.BLangPackage) Logger(org.slf4j.Logger) Files(java.nio.file.Files) KubernetesUtils.createAnnotation(org.ballerinax.kubernetes.utils.KubernetesUtils.createAnnotation) FunctionNode(org.ballerinalang.model.tree.FunctionNode) AnnotationAttachmentNode(org.ballerinalang.model.tree.AnnotationAttachmentNode) ServiceNode(org.ballerinalang.model.tree.ServiceNode) BLangLiteral(org.wso2.ballerinalang.compiler.tree.expressions.BLangLiteral) Collections(java.util.Collections) CompilerContext(org.wso2.ballerinalang.compiler.util.CompilerContext) ServiceNode(org.ballerinalang.model.tree.ServiceNode) JarResolver(org.ballerinalang.compiler.JarResolver) BLangTypeInit(org.wso2.ballerinalang.compiler.tree.expressions.BLangTypeInit) BLangSimpleVarRef(org.wso2.ballerinalang.compiler.tree.expressions.BLangSimpleVarRef) BType(org.wso2.ballerinalang.compiler.semantics.model.types.BType) BLangRecordLiteral(org.wso2.ballerinalang.compiler.tree.expressions.BLangRecordLiteral) HashSet(java.util.HashSet) AnnotationAttachmentNode(org.ballerinalang.model.tree.AnnotationAttachmentNode) Path(java.nio.file.Path) BLangLiteral(org.wso2.ballerinalang.compiler.tree.expressions.BLangLiteral) BLangImportPackage(org.wso2.ballerinalang.compiler.tree.BLangImportPackage) FunctionNode(org.ballerinalang.model.tree.FunctionNode) BLangPackage(org.wso2.ballerinalang.compiler.tree.BLangPackage) SimpleVariableNode(org.ballerinalang.model.tree.SimpleVariableNode) Collection(java.util.Collection) TopLevelNode(org.ballerinalang.model.tree.TopLevelNode)

Aggregations

DiagnosticSeverity (io.ballerina.tools.diagnostics.DiagnosticSeverity)1 Files (java.nio.file.Files)1 Path (java.nio.file.Path)1 ArrayList (java.util.ArrayList)1 Collection (java.util.Collection)1 Collections (java.util.Collections)1 HashSet (java.util.HashSet)1 List (java.util.List)1 Map (java.util.Map)1 Optional (java.util.Optional)1 Set (java.util.Set)1 Collectors (java.util.stream.Collectors)1 JarResolver (org.ballerinalang.compiler.JarResolver)1 JAR_RESOLVER_KEY (org.ballerinalang.compiler.JarResolver.JAR_RESOLVER_KEY)1 AbstractCompilerPlugin (org.ballerinalang.compiler.plugins.AbstractCompilerPlugin)1 SupportedAnnotationPackages (org.ballerinalang.compiler.plugins.SupportedAnnotationPackages)1 TreeBuilder (org.ballerinalang.model.TreeBuilder)1 Flag (org.ballerinalang.model.elements.Flag)1 PackageID (org.ballerinalang.model.elements.PackageID)1 AnnotationAttachmentNode (org.ballerinalang.model.tree.AnnotationAttachmentNode)1