-
Notifications
You must be signed in to change notification settings - Fork 37
Add openshift support for c2c #846
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
154 changes: 154 additions & 0 deletions
154
compiler-plugin-tests/src/test/java/io/ballerina/c2c/test/samples/OpenshiftSampleTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,154 @@ | ||
| /* | ||
| * Copyright (c) 2025, 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.samples; | ||
|
|
||
| import io.ballerina.c2c.KubernetesConstants; | ||
| import io.ballerina.c2c.exceptions.KubernetesPluginException; | ||
| import io.ballerina.c2c.test.utils.KubernetesTestUtils; | ||
| import io.ballerina.c2c.utils.KubernetesUtils; | ||
| import io.fabric8.kubernetes.api.model.Container; | ||
| import io.fabric8.kubernetes.api.model.HasMetadata; | ||
| import io.fabric8.kubernetes.api.model.Service; | ||
| import io.fabric8.kubernetes.api.model.apps.Deployment; | ||
| import io.fabric8.kubernetes.client.KubernetesClient; | ||
| import io.fabric8.kubernetes.client.KubernetesClientBuilder; | ||
| import org.testng.Assert; | ||
| import org.testng.annotations.AfterClass; | ||
| import org.testng.annotations.BeforeClass; | ||
| import org.testng.annotations.Test; | ||
|
|
||
| import java.io.File; | ||
| import java.io.FileInputStream; | ||
| import java.io.IOException; | ||
| import java.nio.file.Path; | ||
| import java.nio.file.Paths; | ||
| import java.util.List; | ||
|
|
||
| import static io.ballerina.c2c.KubernetesConstants.DOCKER; | ||
| import static io.ballerina.c2c.KubernetesConstants.OPENSHIFT; | ||
| import static io.ballerina.c2c.test.utils.KubernetesTestUtils.deployK8s; | ||
| import static io.ballerina.c2c.test.utils.KubernetesTestUtils.getEntryPoint; | ||
| import static io.ballerina.c2c.test.utils.KubernetesTestUtils.getExposedPorts; | ||
| import static io.ballerina.c2c.test.utils.KubernetesTestUtils.loadImage; | ||
|
|
||
| /** | ||
| * Test cases for openshift sample. | ||
| */ | ||
| public class OpenshiftSampleTest extends SampleTest { | ||
|
|
||
| private static final Path SOURCE_DIR_PATH = SAMPLE_DIR.resolve("openshift-yaml-with-ballerina-project"); | ||
| private static final Path DOCKER_TARGET_PATH = | ||
| SOURCE_DIR_PATH.resolve("target").resolve(DOCKER).resolve("hello"); | ||
| private static final Path OPENSHIFT_TARGET_PATH = | ||
| SOURCE_DIR_PATH.resolve("target").resolve(OPENSHIFT).resolve("hello"); | ||
| private static final String DOCKER_IMAGE = "anuruddhal/hello-api:1.0.0"; | ||
| private static final Path INGRESS_PATH = | ||
| Paths.get("src", "test", "resources", "openshift-yaml-with-ballerina-project"); | ||
| private Deployment deployment; | ||
| private Service service; | ||
|
|
||
| @BeforeClass | ||
| public void compileSample() throws IOException, InterruptedException { | ||
| Assert.assertEquals(KubernetesTestUtils.compileBallerinaProject(SOURCE_DIR_PATH) | ||
| , 0); | ||
| File artifactYaml = OPENSHIFT_TARGET_PATH.resolve("hello.yaml").toFile(); | ||
| Assert.assertTrue(artifactYaml.exists()); | ||
| KubernetesClient client = new KubernetesClientBuilder().build(); | ||
| List<HasMetadata> k8sItems = client.load(new FileInputStream(artifactYaml)).items(); | ||
| for (HasMetadata data : k8sItems) { | ||
| switch (data.getKind()) { | ||
| case "Deployment": | ||
| deployment = (Deployment) data; | ||
| break; | ||
| case "Service": | ||
| service = (Service) data; | ||
| break; | ||
| case "Secret": | ||
| case "HorizontalPodAutoscaler": | ||
| break; | ||
| default: | ||
| Assert.fail("Unexpected k8s resource found: " + data.getKind()); | ||
| break; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| public void validateDeployment() { | ||
| Assert.assertNotNull(deployment); | ||
| Assert.assertEquals(deployment.getMetadata().getName(), "hello-deployment"); | ||
| Assert.assertEquals(deployment.getSpec().getReplicas().intValue(), 1); | ||
| Assert.assertEquals(deployment.getMetadata().getLabels().get(KubernetesConstants | ||
| .KUBERNETES_SELECTOR_KEY), "hello"); | ||
| Assert.assertEquals(deployment.getSpec().getTemplate().getSpec().getContainers().size(), 1); | ||
|
|
||
| // Assert Containers | ||
| Container container = deployment.getSpec().getTemplate().getSpec().getContainers().get(0); | ||
| Assert.assertEquals(container.getImage(), DOCKER_IMAGE); | ||
| Assert.assertEquals(container.getPorts().size(), 1); | ||
| } | ||
|
|
||
| @Test | ||
| public void validateService() { | ||
| Assert.assertNotNull(service); | ||
| Assert.assertEquals(1, service.getMetadata().getLabels().size()); | ||
| Assert.assertEquals("hello-svc", service.getMetadata().getName()); | ||
| Assert.assertEquals("ClusterIP", service.getSpec().getType()); | ||
| Assert.assertEquals(1, service.getSpec().getPorts().size()); | ||
| Assert.assertEquals(9090, service.getSpec().getPorts().get(0).getPort().intValue()); | ||
| Assert.assertEquals(9090, service.getSpec().getPorts().get(0).getTargetPort().getIntVal().intValue()); | ||
| Assert.assertEquals("TCP", service.getSpec().getPorts().get(0).getProtocol()); | ||
| Assert.assertEquals("port-1-hello-sv", service.getSpec().getPorts().get(0).getName()); | ||
| } | ||
|
|
||
| @Test | ||
| public void validateDockerfile() { | ||
| File dockerFile = DOCKER_TARGET_PATH.resolve("Dockerfile").toFile(); | ||
| Assert.assertTrue(dockerFile.exists()); | ||
| } | ||
|
|
||
| @Test | ||
| public void validateDockerImage() { | ||
| List<String> ports = getExposedPorts(DOCKER_IMAGE); | ||
| Assert.assertEquals(ports.size(), 1); | ||
| Assert.assertEquals(ports.get(0), "9090/tcp"); | ||
| // Validate ballerina.conf in run command | ||
| Assert.assertEquals(getEntryPoint(DOCKER_IMAGE).toString(), "[java, -Xdiag, -cp, " + | ||
| "hello-hello-0.0.1.jar:jars/*, hello.hello.0.$_init]"); | ||
| } | ||
|
|
||
| @Test(groups = {"integration"}) | ||
| public void deploySample() throws IOException, InterruptedException { | ||
| Assert.assertEquals(0, loadImage(DOCKER_IMAGE)); | ||
| Assert.assertEquals(0, deployK8s(OPENSHIFT_TARGET_PATH)); | ||
| Assert.assertEquals(0, deployK8s(INGRESS_PATH)); | ||
| Assert.assertTrue(KubernetesTestUtils.validateService( | ||
| "http://c2c.deployment.test/helloWorld/sayHello", | ||
| "Hello, World from service helloWorld ! \n")); | ||
| KubernetesTestUtils.deleteK8s(OPENSHIFT_TARGET_PATH); | ||
| KubernetesTestUtils.deleteK8s(INGRESS_PATH); | ||
| } | ||
|
|
||
| @AfterClass | ||
| public void cleanUp() throws KubernetesPluginException, IOException, InterruptedException { | ||
| KubernetesUtils.deleteDirectory(OPENSHIFT_TARGET_PATH); | ||
| KubernetesUtils.deleteDirectory(DOCKER_TARGET_PATH); | ||
| KubernetesTestUtils.deleteDockerImage(DOCKER_IMAGE); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
80 changes: 80 additions & 0 deletions
80
compiler-plugin/src/main/java/io/ballerina/c2c/handlers/BuildConfigHandler.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,80 @@ | ||
| /* | ||
| * Copyright (c) 2025, 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.handlers; | ||
|
|
||
| import io.ballerina.c2c.KubernetesConstants; | ||
| import io.ballerina.c2c.diagnostics.NullLocation; | ||
| import io.ballerina.c2c.exceptions.KubernetesPluginException; | ||
| import io.ballerina.c2c.util.C2CDiagnosticCodes; | ||
| import io.ballerina.c2c.utils.KubernetesUtils; | ||
| import io.ballerina.tools.diagnostics.Diagnostic; | ||
| import io.fabric8.openshift.api.model.BuildConfig; | ||
| import io.fabric8.openshift.api.model.BuildConfigBuilder; | ||
|
|
||
| import java.io.IOException; | ||
|
|
||
| /** | ||
| * Generates kubernetes deployment from annotations. | ||
| */ | ||
| public class BuildConfigHandler extends AbstractArtifactHandler { | ||
|
|
||
|
|
||
| @Override | ||
| public void createArtifacts() throws KubernetesPluginException { | ||
| BuildConfig buildConfig = new BuildConfigBuilder() | ||
| .withApiVersion("build.openshift.io/v1") | ||
| .withKind("BuildConfig") | ||
| .withNewMetadata() | ||
| .withName("dockerfile-binary-build") | ||
| .endMetadata() | ||
| .withNewSpec() | ||
| .withNewSource() | ||
| .withType("Binary") | ||
| .endSource() | ||
| .withNewStrategy() | ||
| .withType("Docker") | ||
| .withNewDockerStrategy() | ||
| .withNoCache(false) | ||
| .endDockerStrategy() | ||
| .endStrategy() | ||
| .withNewOutput() | ||
| .withNewTo() | ||
| .withKind("ImageStreamTag") | ||
| .withName("dockerfile-app:latest") | ||
| .endTo() | ||
| .endOutput() | ||
| .endSpec() | ||
| .build(); | ||
| String outputFileName = KubernetesConstants.BUILD_CONFIG_FILE_POSTFIX + KubernetesConstants.YAML; | ||
| try { | ||
| String buildConfigYAML = KubernetesUtils.asYaml(buildConfig); | ||
| if (dataHolder.isSingleYaml()) { | ||
| outputFileName = buildConfig.getMetadata().getName() + KubernetesConstants.YAML; | ||
| } | ||
| OUT.println("\t@openshift:BuildConfig"); | ||
| KubernetesUtils.writeToFile(dataHolder.getOpenshiftArtifactOutputPath(), buildConfigYAML, outputFileName); | ||
| } catch (IOException e) { | ||
| Diagnostic diagnostic = C2CDiagnosticCodes.createDiagnostic(C2CDiagnosticCodes.ARTIFACT_GEN_FAILED, | ||
| new NullLocation(), "buildConfig", outputFileName); | ||
| throw new KubernetesPluginException(diagnostic); | ||
| } | ||
| } | ||
|
|
||
| } | ||
|
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.