From ef56d13e89bddfa074f36d00b7316cc21f266371 Mon Sep 17 00:00:00 2001 From: xlight05 Date: Thu, 21 Mar 2024 13:10:47 +0530 Subject: [PATCH] Remove windows container support --- .../docker/DockerGeneratorWindowsTests.java | 108 ------------------ .../src/test/resources/testng-integration.xml | 1 - .../src/test/resources/testng.xml | 1 - .../io/ballerina/c2c/DockerGenConstants.java | 2 - .../io/ballerina/c2c/models/DockerModel.java | 6 +- .../ballerina/c2c/utils/DockerGenerator.java | 54 +-------- 6 files changed, 4 insertions(+), 168 deletions(-) delete mode 100644 compiler-plugin-tests/src/test/java/io/ballerina/c2c/test/docker/DockerGeneratorWindowsTests.java diff --git a/compiler-plugin-tests/src/test/java/io/ballerina/c2c/test/docker/DockerGeneratorWindowsTests.java b/compiler-plugin-tests/src/test/java/io/ballerina/c2c/test/docker/DockerGeneratorWindowsTests.java deleted file mode 100644 index 1426b184..00000000 --- a/compiler-plugin-tests/src/test/java/io/ballerina/c2c/test/docker/DockerGeneratorWindowsTests.java +++ /dev/null @@ -1,108 +0,0 @@ -/* - * Copyright (c) 2018, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. - * - * WSO2 Inc. licenses this file to you under the Apache License, - * Version 2.0 (the "License"); you may not use this file except - * in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ - -package io.ballerina.c2c.test.docker; - -import io.ballerina.c2c.exceptions.DockerGenException; -import io.ballerina.c2c.models.CopyFileModel; -import io.ballerina.c2c.models.DockerModel; -import io.ballerina.c2c.utils.DockerGenerator; -import org.apache.commons.io.FileUtils; -import org.ballerinalang.model.elements.PackageID; -import org.testng.Assert; -import org.testng.annotations.AfterClass; -import org.testng.annotations.Test; -import org.wso2.ballerinalang.compiler.util.Name; - -import java.io.File; -import java.io.IOException; -import java.io.PrintStream; -import java.nio.file.Files; -import java.nio.file.Path; -import java.nio.file.Paths; -import java.util.Collections; -import java.util.HashSet; -import java.util.Set; -import java.util.stream.Collectors; - -import static io.ballerina.c2c.test.utils.DockerTestUtils.removeEnv; -import static io.ballerina.c2c.test.utils.DockerTestUtils.updateEnv; - -/** - * Docker generator tests. - */ -public class DockerGeneratorWindowsTests { - - private static final Path SOURCE_DIR_PATH = Paths.get("src", "test", "resources"); - private final PrintStream out = System.out; - - @Test - public void buildDockerImageWindowsTest() throws DockerGenException, IOException, ReflectiveOperationException { - updateEnv("BAL_DOCKER_WINDOWS", "true"); - DockerModel dockerModel = new DockerModel(); - dockerModel.setName("test-win-image"); - dockerModel.setRegistry("anuruddhal"); - dockerModel.setTag("v1"); - dockerModel.setJarFileName("hello.jar"); - dockerModel.setPorts(Collections.singleton(9090)); - dockerModel.setBuildImage(false); - dockerModel.setService(true); - Path jarFilePath = SOURCE_DIR_PATH.resolve("docker-test").resolve("hello.jar"); - Set jarFilePaths = getJarFilePaths(); - PackageID packageID = new PackageID(new Name("wso2"), new Name("bal"), new Name("1.0.0")); - dockerModel.setPkgId(packageID); - dockerModel.setDependencyJarPaths(jarFilePaths); - CopyFileModel configFile = new CopyFileModel(); - configFile.setSource(SOURCE_DIR_PATH.resolve("conf").resolve("Config.toml").toString()); - configFile.setTarget("/home/ballerina/conf/"); - configFile.setBallerinaConf(true); - CopyFileModel dataFile = new CopyFileModel(); - dataFile.setSource(SOURCE_DIR_PATH.resolve("conf").resolve("data.txt").toString()); - dataFile.setTarget("/home/ballerina/data/"); - dataFile.setBallerinaConf(true); - Set externalFiles = new HashSet<>(); - externalFiles.add(configFile); - externalFiles.add(dataFile); - dockerModel.setCopyFiles(externalFiles); - Path outputDir = SOURCE_DIR_PATH.resolve("target"); - Files.createDirectories(outputDir); - DockerGenerator handler = new DockerGenerator(dockerModel); - handler.createArtifacts(out, "\t@kubernetes:Docker \t\t\t", jarFilePath, outputDir); - removeEnv("BAL_DOCKER_WINDOWS"); - } - - @Test(dependsOnMethods = {"buildDockerImageWindowsTest"}) - public void validateDockerFile() throws IOException { - File dockerFile = SOURCE_DIR_PATH.resolve("target").resolve("Dockerfile").toFile(); - Assert.assertTrue(dockerFile.exists()); - String dockerFileContent = new String(Files.readAllBytes(dockerFile.toPath())); - Assert.assertTrue(dockerFileContent.contains("CMD java -Xdiag -cp \"hello.jar:jars/*\" " + - "'wso2.bal.1.$_init'")); - Assert.assertTrue(dockerFileContent.contains("FROM openjdk:17-windowsservercore")); - } - - private Set getJarFilePaths() throws IOException { - return Files.list(SOURCE_DIR_PATH.resolve("docker-test")).collect(Collectors.toSet()); - } - - @AfterClass - public void cleanUp() throws IOException, ReflectiveOperationException { - FileUtils.deleteDirectory(SOURCE_DIR_PATH.resolve("target").toFile()); - } - -} diff --git a/compiler-plugin-tests/src/test/resources/testng-integration.xml b/compiler-plugin-tests/src/test/resources/testng-integration.xml index a70a8e42..f8c9fff3 100644 --- a/compiler-plugin-tests/src/test/resources/testng-integration.xml +++ b/compiler-plugin-tests/src/test/resources/testng-integration.xml @@ -24,7 +24,6 @@ - diff --git a/compiler-plugin-tests/src/test/resources/testng.xml b/compiler-plugin-tests/src/test/resources/testng.xml index b67ff6dd..0cfcd102 100644 --- a/compiler-plugin-tests/src/test/resources/testng.xml +++ b/compiler-plugin-tests/src/test/resources/testng.xml @@ -29,7 +29,6 @@ - diff --git a/compiler-plugin/src/main/java/io/ballerina/c2c/DockerGenConstants.java b/compiler-plugin/src/main/java/io/ballerina/c2c/DockerGenConstants.java index c526100a..922a72e4 100644 --- a/compiler-plugin/src/main/java/io/ballerina/c2c/DockerGenConstants.java +++ b/compiler-plugin/src/main/java/io/ballerina/c2c/DockerGenConstants.java @@ -23,12 +23,10 @@ */ public class DockerGenConstants { public static final String ENABLE_DEBUG_LOGS = "BAL_DOCKER_DEBUG"; - public static final String ENABLE_WINDOWS_BUILD = "BAL_DOCKER_WINDOWS"; public static final String EXECUTABLE_JAR = ".jar"; public static final String REGISTRY_SEPARATOR = "/"; public static final String TAG_SEPARATOR = ":"; public static final String JRE_SLIM_BASE = "ballerina/jvm-runtime:2.0"; - public static final String JRE_WINDOWS_BASE_IMAGE = "openjdk:17-windowsservercore"; public static final String NATIVE_BUILDER_IMAGE = "ghcr.io/graalvm/native-image-community:17-ol8"; public static final String NATIVE_RUNTIME_BASE_IMAGE = "gcr.io/distroless/base"; public static final int MAX_BALLERINA_LAYERS = 110; diff --git a/compiler-plugin/src/main/java/io/ballerina/c2c/models/DockerModel.java b/compiler-plugin/src/main/java/io/ballerina/c2c/models/DockerModel.java index 49844e13..bfa9e9d0 100644 --- a/compiler-plugin/src/main/java/io/ballerina/c2c/models/DockerModel.java +++ b/compiler-plugin/src/main/java/io/ballerina/c2c/models/DockerModel.java @@ -40,8 +40,7 @@ @Getter @Setter public class DockerModel { - private final boolean windowsBuild = - Boolean.parseBoolean(System.getenv(DockerGenConstants.ENABLE_WINDOWS_BUILD)); + private String name; private String registry; private String tag; @@ -69,8 +68,7 @@ public DockerModel() { // Initialize with default values except for image name this.tag = "latest"; this.buildImage = true; - this.baseImage = windowsBuild ? DockerGenConstants.JRE_WINDOWS_BASE_IMAGE : - DockerGenConstants.JRE_SLIM_BASE; + this.baseImage = DockerGenConstants.JRE_SLIM_BASE; this.enableDebug = false; this.debugPort = 5005; this.externalFiles = new HashSet<>(); diff --git a/compiler-plugin/src/main/java/io/ballerina/c2c/utils/DockerGenerator.java b/compiler-plugin/src/main/java/io/ballerina/c2c/utils/DockerGenerator.java index b2f7000c..b0a299ee 100644 --- a/compiler-plugin/src/main/java/io/ballerina/c2c/utils/DockerGenerator.java +++ b/compiler-plugin/src/main/java/io/ballerina/c2c/utils/DockerGenerator.java @@ -70,11 +70,7 @@ public void createArtifacts(PrintStream outStream, String logAppender, Path jarF outputDir.resolve(this.dockerModel.getFatJarPath().getFileName())); } else { String dockerContent; - if (!isWindowsBuild()) { - dockerContent = generateDockerfile(); - } else { - dockerContent = generateThinJarWindowsDockerfile(); - } + dockerContent = generateDockerfile(); copyNativeJars(outputDir); DockerGenUtils.writeToFile(dockerContent, outputDir.resolve("Dockerfile")); Path jarLocation = outputDir.resolve(DockerGenUtils.extractJarName(jarFilePath) + EXECUTABLE_JAR); @@ -241,48 +237,6 @@ protected void appendUser(StringBuilder dockerfileContent) { } } - private String generateThinJarWindowsDockerfile() { - final String separator = "\\"; - StringBuilder dockerfileContent = new StringBuilder(); - dockerfileContent.append("# Auto Generated Dockerfile").append(LINE_SEPARATOR); - dockerfileContent.append("FROM ").append(this.dockerModel.getBaseImage()).append(LINE_SEPARATOR); - dockerfileContent.append(LINE_SEPARATOR); - dockerfileContent.append("LABEL maintainer=\"dev@ballerina.io\"").append(LINE_SEPARATOR); - dockerfileContent.append(LINE_SEPARATOR); - dockerfileContent.append("WORKDIR ").append(getWorkDir()).append(LINE_SEPARATOR); - - for (Path path : this.dockerModel.getDependencyJarPaths()) { - dockerfileContent.append("COPY ").append(path.getFileName()).append(getWorkDir()) - .append("jars").append(separator); - dockerfileContent.append(LINE_SEPARATOR); - } - dockerfileContent.append(LINE_SEPARATOR); - appendCommonCommands(dockerfileContent); - if (isBlank(this.dockerModel.getEntryPoint())) { - PackageID packageID = this.dockerModel.getPkgId(); - String mainClass = JarResolver.getQualifiedClassName(packageID.orgName.value, packageID.name.value, - packageID.version.value, MODULE_INIT_CLASS_NAME); - mainClass = "'" + mainClass + "'"; - if (this.dockerModel.isEnableDebug()) { - dockerfileContent.append("CMD java -Xdiag -agentlib:jdwp=transport=dt_socket,server=y,suspend=n," + - "address=*:").append(this.dockerModel.getDebugPort()).append(" -cp \"") - .append(this.dockerModel.getJarFileName()).append(":jars/*\" ").append(mainClass); - } else { - dockerfileContent.append("CMD java -Xdiag -cp \"").append(this.dockerModel.getJarFileName()) - .append(":jars/*\" ").append(mainClass); - } - } else { - dockerfileContent.append(this.dockerModel.getEntryPoint()); - } - dockerfileContent.append(LINE_SEPARATOR); - if (!isBlank(this.dockerModel.getCommandArg())) { - dockerfileContent.append(this.dockerModel.getCommandArg()); - } - dockerfileContent.append(LINE_SEPARATOR); - - return dockerfileContent.toString(); - } - protected void appendCommonCommands(StringBuilder dockerfileContent) { this.dockerModel.getEnv().forEach((key, value) -> dockerfileContent.append("ENV "). append(key).append("=").append(value).append(LINE_SEPARATOR)); @@ -310,11 +264,7 @@ protected void appendCommonCommands(StringBuilder dockerfileContent) { } } - private boolean isWindowsBuild() { - return Boolean.parseBoolean(System.getenv(DockerGenConstants.ENABLE_WINDOWS_BUILD)); - } - private String getWorkDir() { - return isWindowsBuild() ? "C:\\ballerina\\home\\" : "/home/ballerina"; + return "/home/ballerina"; } }