Search in sources :

Example 1 with JobModel

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

the class JobHandler method createArtifacts.

@Override
public void createArtifacts() throws KubernetesPluginException {
    try {
        String balxFileName = extractJarName(dataHolder.getUberJarPath());
        JobModel jobModel = dataHolder.getJobModel();
        if (isBlank(jobModel.getName())) {
            jobModel.setName(getValidName(balxFileName) + JOB_POSTFIX);
        }
        if (isBlank(jobModel.getImage())) {
            jobModel.setImage(balxFileName + DOCKER_LATEST_TAG);
        }
        jobModel.addLabel(KubernetesConstants.KUBERNETES_SELECTOR_KEY, balxFileName);
        generate(jobModel);
        // generate dockerfile and docker image
        dataHolder.setDockerModel(getDockerModel(jobModel));
        OUT.println();
        OUT.print("\t@kubernetes:Job \t\t\t - complete 1/1");
    } catch (DockerGenException e) {
        throw new KubernetesPluginException("error occurred creating docker image.", e);
    }
}
Also used : DockerGenException(org.ballerinax.docker.generator.exceptions.DockerGenException) JobModel(org.ballerinax.kubernetes.models.JobModel) KubernetesPluginException(org.ballerinax.kubernetes.exceptions.KubernetesPluginException)

Example 2 with JobModel

use of org.ballerinax.kubernetes.models.JobModel 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 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 {
    KubernetesDataHolder dataHolder = KubernetesContext.getInstance().getDataHolder();
    Path artifactFileName = outputDir.resolve(extractJarName(dataHolder.getUberJarPath()) + fileSuffix);
    DeploymentModel deploymentModel = dataHolder.getDeploymentModel();
    JobModel jobModel = dataHolder.getJobModel();
    // Priority given for job, then deployment.
    if (jobModel != null && jobModel.isSingleYAML()) {
        artifactFileName = outputDir.resolve(extractJarName(dataHolder.getUberJarPath()) + YAML);
    } else if (jobModel == null && deploymentModel != null && deploymentModel.isSingleYAML()) {
        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) DeploymentModel(org.ballerinax.kubernetes.models.DeploymentModel) KubernetesDataHolder(org.ballerinax.kubernetes.models.KubernetesDataHolder) JobModel(org.ballerinax.kubernetes.models.JobModel) File(java.io.File)

Example 3 with JobModel

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

the class KubernetesJobGeneratorTests method testDeploymentGeneration.

@Test
public void testDeploymentGeneration() {
    JobModel jobModel = new JobModel();
    jobModel.setName(jobName);
    Map<String, String> labels = new HashMap<>();
    labels.put(KubernetesConstants.KUBERNETES_SELECTOR_KEY, selector);
    jobModel.setLabels(labels);
    jobModel.setImage(imageName);
    jobModel.setImagePullPolicy(imagePullPolicy);
    jobModel.setSingleYAML(false);
    Map<String, EnvVarValueModel> env = new HashMap<>();
    EnvVarValueModel testEnvVar = new EnvVarValueModel("ENV");
    env.put("ENV_VAR", testEnvVar);
    jobModel.setEnv(env);
    dataHolder.setJobModel(jobModel);
    try {
        new JobHandler().createArtifacts();
        File tempFile = dataHolder.getK8sArtifactOutputPath().resolve("hello_job.yaml").toFile();
        Assert.assertTrue(tempFile.exists());
        assertGeneratedYAML(tempFile);
        tempFile.deleteOnExit();
    } catch (IOException e) {
        Assert.fail("Unable to write to file");
    } catch (KubernetesPluginException e) {
        Assert.fail("Unable to generate yaml from service");
    }
}
Also used : HashMap(java.util.HashMap) EnvVarValueModel(org.ballerinax.kubernetes.models.EnvVarValueModel) JobModel(org.ballerinax.kubernetes.models.JobModel) IOException(java.io.IOException) KubernetesPluginException(org.ballerinax.kubernetes.exceptions.KubernetesPluginException) File(java.io.File) Test(org.testng.annotations.Test)

Example 4 with JobModel

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

the class JobAnnotationProcessor method processAnnotation.

public void processAnnotation(FunctionNode functionNode, AnnotationAttachmentNode attachmentNode) throws KubernetesPluginException {
    JobModel jobModel = new JobModel();
    List<BLangRecordLiteral.BLangRecordKeyValueField> keyValues = convertRecordFields(((BLangRecordLiteral) ((BLangAnnotationAttachment) attachmentNode).expr).getFields());
    for (BLangRecordLiteral.BLangRecordKeyValueField keyValue : keyValues) {
        JobConfiguration jobConfiguration = JobConfiguration.valueOf(keyValue.getKey().toString());
        switch(jobConfiguration) {
            case name:
                jobModel.setName(getValidName(getStringValue(keyValue.getValue())));
                break;
            case namespace:
                KubernetesContext.getInstance().getDataHolder().setNamespace(getStringValue(keyValue.getValue()));
                break;
            case labels:
                jobModel.setLabels(getMap(keyValue.getValue()));
                break;
            case annotations:
                jobModel.setAnnotations(getMap(keyValue.getValue()));
                break;
            case restartPolicy:
                jobModel.setRestartPolicy(KubernetesConstants.RestartPolicy.valueOf(getStringValue(keyValue.getValue())).name());
                break;
            case backoffLimit:
                jobModel.setBackoffLimit(getIntValue(keyValue.getValue()));
                break;
            case activeDeadlineSeconds:
                jobModel.setActiveDeadlineSeconds(getIntValue(keyValue.getValue()));
                break;
            case schedule:
                jobModel.setSchedule(getStringValue(keyValue.getValue()));
                break;
            case username:
                jobModel.setUsername(getStringValue(keyValue.getValue()));
                break;
            case env:
                jobModel.setEnv(getEnvVarMap(keyValue.getValue()));
                break;
            case password:
                jobModel.setPassword(getStringValue(keyValue.getValue()));
                break;
            case baseImage:
                jobModel.setBaseImage(getStringValue(keyValue.getValue()));
                break;
            case push:
                jobModel.setPush(getBooleanValue(keyValue.getValue()));
                break;
            case cmd:
                jobModel.setCmd(getStringValue(keyValue.getValue()));
                break;
            case buildImage:
                jobModel.setBuildImage(getBooleanValue(keyValue.getValue()));
                break;
            case image:
                jobModel.setImage(getStringValue(keyValue.getValue()));
                break;
            case dockerHost:
                jobModel.setDockerHost(getStringValue(keyValue.getValue()));
                break;
            case dockerCertPath:
                jobModel.setDockerCertPath(getStringValue(keyValue.getValue()));
                break;
            case imagePullPolicy:
                jobModel.setImagePullPolicy(getStringValue(keyValue.getValue()));
                break;
            case copyFiles:
                jobModel.setCopyFiles(getExternalFileMap(keyValue));
                break;
            case singleYAML:
                jobModel.setSingleYAML(getBooleanValue(keyValue.getValue()));
                break;
            case imagePullSecrets:
                jobModel.setImagePullSecrets(getImagePullSecrets(keyValue));
                break;
            case registry:
                jobModel.setRegistry(getStringValue(keyValue.getValue()));
                break;
            case nodeSelector:
                jobModel.setNodeSelector(getMap(keyValue.getValue()));
                break;
            case uberJar:
                jobModel.setUberJar(getBooleanValue(keyValue.getValue()));
                break;
            case dockerConfigPath:
                jobModel.setDockerConfigPath(getStringValue(keyValue.getValue()));
                break;
            default:
                break;
        }
    }
    String dockerHost = System.getenv(DOCKER_HOST);
    if (!isBlank(dockerHost)) {
        jobModel.setDockerHost(dockerHost);
    }
    String dockerCertPath = System.getenv(DOCKER_CERT_PATH);
    if (!isBlank(dockerCertPath)) {
        jobModel.setDockerCertPath(dockerCertPath);
    }
    KubernetesContext.getInstance().getDataHolder().setJobModel(jobModel);
}
Also used : BLangAnnotationAttachment(org.wso2.ballerinalang.compiler.tree.BLangAnnotationAttachment) JobModel(org.ballerinax.kubernetes.models.JobModel) BLangRecordLiteral(org.wso2.ballerinalang.compiler.tree.expressions.BLangRecordLiteral)

Aggregations

JobModel (org.ballerinax.kubernetes.models.JobModel)4 File (java.io.File)2 KubernetesPluginException (org.ballerinax.kubernetes.exceptions.KubernetesPluginException)2 IOException (java.io.IOException)1 Path (java.nio.file.Path)1 HashMap (java.util.HashMap)1 DockerGenException (org.ballerinax.docker.generator.exceptions.DockerGenException)1 DeploymentModel (org.ballerinax.kubernetes.models.DeploymentModel)1 EnvVarValueModel (org.ballerinax.kubernetes.models.EnvVarValueModel)1 KubernetesDataHolder (org.ballerinax.kubernetes.models.KubernetesDataHolder)1 Test (org.testng.annotations.Test)1 BLangAnnotationAttachment (org.wso2.ballerinalang.compiler.tree.BLangAnnotationAttachment)1 BLangRecordLiteral (org.wso2.ballerinalang.compiler.tree.expressions.BLangRecordLiteral)1