Search in sources :

Example 1 with KubernetesDataHolder

use of org.ballerinax.kubernetes.models.KubernetesDataHolder in project kubernetes by ballerinax.

the class JobHandler method getDockerModel.

private DockerModel getDockerModel(JobModel jobModel) throws DockerGenException {
    final KubernetesDataHolder dataHolder = KubernetesContext.getInstance().getDataHolder();
    DockerModel dockerModel = dataHolder.getDockerModel();
    String dockerImage = jobModel.getImage();
    String imageTag = dockerImage.substring(dockerImage.lastIndexOf(":") + 1);
    dockerImage = dockerImage.substring(0, dockerImage.lastIndexOf(":"));
    dockerModel.setBaseImage(jobModel.getBaseImage());
    dockerModel.setRegistry(jobModel.getRegistry());
    dockerModel.setName(dockerImage);
    dockerModel.setTag(imageTag);
    dockerModel.setUsername(jobModel.getUsername());
    dockerModel.setPassword(jobModel.getPassword());
    dockerModel.setPush(jobModel.isPush());
    dockerModel.setCmd(jobModel.getCmd());
    dockerModel.setJarFileName(extractJarName(this.dataHolder.getUberJarPath()) + EXECUTABLE_JAR);
    dockerModel.setService(false);
    dockerModel.setDockerHost(jobModel.getDockerHost());
    dockerModel.setDockerCertPath(jobModel.getDockerCertPath());
    dockerModel.setBuildImage(jobModel.isBuildImage());
    dockerModel.setPkgId(dataHolder.getPackageID());
    dockerModel.setCopyFiles(jobModel.getCopyFiles());
    dockerModel.setUberJar(jobModel.isUberJar());
    dockerModel.setDockerConfig(jobModel.getDockerConfigPath());
    return dockerModel;
}
Also used : DockerModel(org.ballerinax.docker.generator.models.DockerModel) KubernetesDataHolder(org.ballerinax.kubernetes.models.KubernetesDataHolder)

Example 2 with KubernetesDataHolder

use of org.ballerinax.kubernetes.models.KubernetesDataHolder in project kubernetes by ballerinax.

the class KubernetesPlugin method validateDeploymentDependencies.

private void validateDeploymentDependencies() throws KubernetesPluginException {
    KubernetesContext context = KubernetesContext.getInstance();
    Map<PackageID, KubernetesDataHolder> packageToDataHolderMap = context.getPackageIDtoDataHolderMap();
    DependencyValidator dependencyValidator = new DependencyValidator();
    for (KubernetesDataHolder dataHolder : packageToDataHolderMap.values()) {
        // add other dependent deployments
        List<String> dependencies = new ArrayList<>();
        // add the current deployment as 0th element
        String currentDeployment = dataHolder.getDeploymentModel().getName();
        if (currentDeployment == null) {
            return;
        }
        dependencies.add(currentDeployment);
        Set<String> dependsOn = dataHolder.getDeploymentModel().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 : DependencyValidator(org.ballerinax.kubernetes.utils.DependencyValidator) KubernetesDataHolder(org.ballerinax.kubernetes.models.KubernetesDataHolder) ArrayList(java.util.ArrayList) PackageID(org.ballerinalang.model.elements.PackageID) KubernetesPluginException(org.ballerinax.kubernetes.exceptions.KubernetesPluginException) KubernetesContext(org.ballerinax.kubernetes.models.KubernetesContext)

Example 3 with KubernetesDataHolder

use of org.ballerinax.kubernetes.models.KubernetesDataHolder 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.");
        }
    }
}
Also used : Path(java.nio.file.Path) KubernetesDataHolder(org.ballerinax.kubernetes.models.KubernetesDataHolder) JarResolver(org.ballerinalang.compiler.JarResolver) KubernetesPluginException(org.ballerinax.kubernetes.exceptions.KubernetesPluginException)

Example 4 with KubernetesDataHolder

use of org.ballerinax.kubernetes.models.KubernetesDataHolder in project kubernetes by ballerinax.

the class HelmChartGeneratorTests method testHelmGenerate.

@Test
public void testHelmGenerate() throws KubernetesPluginException {
    DockerModel dockerModel = new DockerModel();
    Set<Integer> ports = new HashSet<>();
    ports.add(9090);
    ports.add(9091);
    ports.add(9092);
    dockerModel.setPorts(ports);
    dockerModel.setService(true);
    dockerModel.setJarFileName("hello.jar");
    dockerModel.setEnableDebug(true);
    dockerModel.setBaseImage(OPENJDK_11_JRE_SLIM_BASE);
    dockerModel.setDebugPort(5005);
    dockerModel.setBuildImage(false);
    KubernetesContext context = KubernetesContext.getInstance();
    KubernetesDataHolder dataHolder = context.getDataHolder();
    dataHolder.setDockerModel(dockerModel);
    new DockerHandler().createArtifacts();
    new HelmChartHandler().createArtifacts();
    File charYaml = dataHolder.getK8sArtifactOutputPath().resolve(module.name.toString()).resolve("hello-deployment").resolve("Chart.yaml").toFile();
    Assert.assertTrue(charYaml.exists());
    charYaml.deleteOnExit();
}
Also used : DockerModel(org.ballerinax.docker.generator.models.DockerModel) KubernetesDataHolder(org.ballerinax.kubernetes.models.KubernetesDataHolder) KubernetesContext(org.ballerinax.kubernetes.models.KubernetesContext) File(java.io.File) HashSet(java.util.HashSet) Test(org.testng.annotations.Test)

Example 5 with KubernetesDataHolder

use of org.ballerinax.kubernetes.models.KubernetesDataHolder in project kubernetes by ballerinax.

the class KubernetesUtils 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 {
    KubernetesDataHolder dataHolder = KubernetesContext.getInstance().getDataHolder();
    writeToFile(dataHolder.getK8sArtifactOutputPath(), context, outputFileName);
}
Also used : KubernetesDataHolder(org.ballerinax.kubernetes.models.KubernetesDataHolder)

Aggregations

KubernetesDataHolder (org.ballerinax.kubernetes.models.KubernetesDataHolder)7 DockerModel (org.ballerinax.docker.generator.models.DockerModel)3 File (java.io.File)2 Path (java.nio.file.Path)2 KubernetesPluginException (org.ballerinax.kubernetes.exceptions.KubernetesPluginException)2 KubernetesContext (org.ballerinax.kubernetes.models.KubernetesContext)2 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 JarResolver (org.ballerinalang.compiler.JarResolver)1 PackageID (org.ballerinalang.model.elements.PackageID)1 DeploymentModel (org.ballerinax.kubernetes.models.DeploymentModel)1 JobModel (org.ballerinax.kubernetes.models.JobModel)1 DependencyValidator (org.ballerinax.kubernetes.utils.DependencyValidator)1 Test (org.testng.annotations.Test)1