Search in sources :

Example 1 with KnativeDataHolder

use of org.ballerinax.kubernetes.models.knative.KnativeDataHolder in project kubernetes by ballerinax.

the class KnativePlugin method codeGenerated.

@Override
public void codeGenerated(PackageID moduleID, Path executableJarFile) {
    KnativeContext.getInstance().setCurrentPackage(moduleID);
    KnativeDataHolder dataHolder = KnativeContext.getInstance().getDataHolder();
    dataHolder.getDockerModel().setPkgId(moduleID);
    if (dataHolder.isCanProcess()) {
        executableJarFile = executableJarFile.toAbsolutePath();
        if (executableJarFile != null && Files.exists(executableJarFile)) {
            Path parent = executableJarFile.getParent();
            // artifacts location for a single bal file.
            Path kubernetesOutputPath = parent != null ? parent.resolve(KUBERNETES) : null;
            Path dockerOutputPath = parent != null ? parent.resolve(DOCKER) : null;
            if (Files.exists(executableJarFile)) {
                // if executable came from a ballerina project
                Path projectRoot = executableJarFile;
                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));
                }
            }
            dataHolder.setUberJarPath(executableJarFile);
            dataHolder.setK8sArtifactOutputPath(kubernetesOutputPath);
            dataHolder.setDockerArtifactOutputPath(dockerOutputPath);
            KnativeArtifactManager knativeArtifactManager = new KnativeArtifactManager();
            try {
                KnativeUtils.deleteDirectory(kubernetesOutputPath);
                knativeArtifactManager.populateDeploymentModel();
                validateDeploymentDependencies();
                knativeArtifactManager.createArtifacts();
            } catch (KubernetesPluginException e) {
                String errorMessage = "module [" + moduleID + "] " + e.getMessage();
                printError(errorMessage);
                pluginLog.error(errorMessage, e);
                try {
                    KnativeUtils.deleteDirectory(kubernetesOutputPath);
                } catch (KubernetesPluginException ignored) {
                // ignored
                }
            }
        } else {
            printError("error in resolving docker generation location.");
            pluginLog.error("error in resolving docker generation location.");
        }
    }
}
Also used : Path(java.nio.file.Path) KnativeDataHolder(org.ballerinax.kubernetes.models.knative.KnativeDataHolder) KubernetesPluginException(org.ballerinax.kubernetes.exceptions.KubernetesPluginException)

Example 2 with KnativeDataHolder

use of org.ballerinax.kubernetes.models.knative.KnativeDataHolder in project kubernetes by ballerinax.

the class KnativePlugin method validateDeploymentDependencies.

private void validateDeploymentDependencies() throws KubernetesPluginException {
    KnativeContext context = KnativeContext.getInstance();
    Map<PackageID, KnativeDataHolder> packageToDataHolderMap = context.getPackageIDtoDataHolderMap();
    DependencyValidator dependencyValidator = new DependencyValidator();
    for (KnativeDataHolder dataHolder : packageToDataHolderMap.values()) {
        // add other dependent deployments
        List<String> dependencies = new ArrayList<>();
        // add the current deployment as 0th element
        String currentDeployment = dataHolder.getServiceModel().getName();
        if (currentDeployment == null) {
            return;
        }
        dependencies.add(currentDeployment);
        Set<String> dependsOn = dataHolder.getServiceModel().getDependsOn();
        for (String listenerName : dependsOn) {
            String dependentDeployment = context.getDeploymentNameFromListener(listenerName);
            if (dependentDeployment == null) {
                return;
            }
            if (!dependentDeployment.equals(currentDeployment)) {
                dependencies.add(dependentDeployment);
            } else {
                // Listener is in the same package.
                throw new KubernetesPluginException("@kubernetes:Deployment{} contains cyclic dependencies");
            }
        }
        String[] array = dependencies.toArray(new String[0]);
        if (!dependencyValidator.validateDependency(array)) {
            throw new KubernetesPluginException("@kubernetes:Deployment{} contains cyclic dependencies");
        }
    }
}
Also used : KnativeContext(org.ballerinax.kubernetes.models.knative.KnativeContext) DependencyValidator(org.ballerinax.kubernetes.utils.DependencyValidator) KnativeDataHolder(org.ballerinax.kubernetes.models.knative.KnativeDataHolder) ArrayList(java.util.ArrayList) PackageID(org.ballerinalang.model.elements.PackageID) KubernetesPluginException(org.ballerinax.kubernetes.exceptions.KubernetesPluginException)

Example 3 with KnativeDataHolder

use of org.ballerinax.kubernetes.models.knative.KnativeDataHolder in project kubernetes by ballerinax.

the class KnativeUtils method writeToFile.

/**
 * Write content to a File. Create the required directories if they don't not exists.
 *
 * @param outputDir  Artifact output path.
 * @param context    Context of the file
 * @param fileSuffix Suffix for artifact.
 * @throws IOException If an error occurs when writing to a file
 */
public static void writeToFile(Path outputDir, String context, String fileSuffix) throws IOException {
    KnativeDataHolder dataHolder = KnativeContext.getInstance().getDataHolder();
    // Priority given for job, then deployment.
    Path artifactFileName = outputDir.resolve(extractJarName(dataHolder.getUberJarPath()) + YAML);
    File newFile = artifactFileName.toFile();
    // append if file exists
    if (newFile.exists()) {
        Files.write(artifactFileName, context.getBytes(StandardCharsets.UTF_8), StandardOpenOption.APPEND);
        return;
    }
    // create required directories
    if (newFile.getParentFile().mkdirs()) {
        Files.write(artifactFileName, context.getBytes(StandardCharsets.UTF_8));
        return;
    }
    Files.write(artifactFileName, context.getBytes(StandardCharsets.UTF_8));
}
Also used : Path(java.nio.file.Path) KnativeDataHolder(org.ballerinax.kubernetes.models.knative.KnativeDataHolder) File(java.io.File)

Example 4 with KnativeDataHolder

use of org.ballerinax.kubernetes.models.knative.KnativeDataHolder in project kubernetes by ballerinax.

the class KnativeUtils method writeToFile.

/**
 * Write content to a File. Create the required directories if they don't not exists.
 *
 * @param context        context of the file
 * @param outputFileName target file path
 * @throws IOException If an error occurs when writing to a file
 */
public static void writeToFile(String context, String outputFileName) throws IOException {
    KnativeDataHolder dataHolder = KnativeContext.getInstance().getDataHolder();
    writeToFile(dataHolder.getK8sArtifactOutputPath().resolve(KNATIVE), context, outputFileName);
}
Also used : KnativeDataHolder(org.ballerinax.kubernetes.models.knative.KnativeDataHolder)

Aggregations

KnativeDataHolder (org.ballerinax.kubernetes.models.knative.KnativeDataHolder)4 Path (java.nio.file.Path)2 KubernetesPluginException (org.ballerinax.kubernetes.exceptions.KubernetesPluginException)2 File (java.io.File)1 ArrayList (java.util.ArrayList)1 PackageID (org.ballerinalang.model.elements.PackageID)1 KnativeContext (org.ballerinax.kubernetes.models.knative.KnativeContext)1 DependencyValidator (org.ballerinax.kubernetes.utils.DependencyValidator)1