use of org.ballerinalang.compiler.JarResolver in project kubernetes by ballerinax.
the class KubernetesPlugin method codeGenerated.
@Override
public void codeGenerated(PackageID moduleID, Path executableJarFile) {
KubernetesContext.getInstance().setCurrentPackage(moduleID);
KubernetesDataHolder dataHolder = KubernetesContext.getInstance().getDataHolder();
dataHolder.setPackageID(moduleID);
if (dataHolder.isCanProcess()) {
executableJarFile = executableJarFile.toAbsolutePath();
if (null != executableJarFile.getParent() && Files.exists(executableJarFile.getParent())) {
// artifacts location for a single bal file.
Path kubernetesOutputPath = executableJarFile.getParent().resolve(KUBERNETES);
Path dockerOutputPath = executableJarFile.getParent().resolve(DOCKER);
if (null != executableJarFile.getParent().getParent().getParent() && Files.exists(executableJarFile.getParent().getParent().getParent())) {
// if executable came from a ballerina project
Path projectRoot = executableJarFile.getParent().getParent().getParent();
if (Files.exists(projectRoot.resolve("Ballerina.toml"))) {
dataHolder.setProject(true);
kubernetesOutputPath = projectRoot.resolve("target").resolve(KUBERNETES).resolve(extractJarName(executableJarFile));
dockerOutputPath = projectRoot.resolve("target").resolve(DOCKER).resolve(extractJarName(executableJarFile));
}
}
if (!dataHolder.getDockerModel().isUberJar()) {
JarResolver jarResolver = KubernetesContext.getInstance().getCompilerContext().get(JAR_RESOLVER_KEY);
executableJarFile = jarResolver.moduleJar(moduleID);
}
dataHolder.setUberJarPath(executableJarFile);
dataHolder.setK8sArtifactOutputPath(kubernetesOutputPath);
dataHolder.setDockerArtifactOutputPath(dockerOutputPath);
ArtifactManager artifactManager = new ArtifactManager();
try {
KubernetesUtils.deleteDirectory(kubernetesOutputPath);
artifactManager.populateDeploymentModel();
validateDeploymentDependencies();
artifactManager.createArtifacts();
} catch (KubernetesPluginException e) {
String errorMessage = "module [" + moduleID + "] " + e.getMessage();
printError(errorMessage);
pluginLog.error(errorMessage, e);
try {
KubernetesUtils.deleteDirectory(kubernetesOutputPath);
} catch (KubernetesPluginException ignored) {
// ignored
}
}
} else {
printError("error in resolving docker generation location.");
pluginLog.error("error in resolving docker generation location.");
}
}
}
use of org.ballerinalang.compiler.JarResolver 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