-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #786 from xlight05/fix_graalvm_no_cloud
Fix GraalVM default base image
- Loading branch information
Showing
8 changed files
with
174 additions
and
7 deletions.
There are no files selected for viewing
99 changes: 99 additions & 0 deletions
99
compiler-plugin-tests/src/test/java/io/ballerina/c2c/test/samples/NativeJobTest.java
This file contains 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,99 @@ | ||
/* | ||
* Copyright (c) 2024, 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.q | ||
* 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 com.github.dockerjava.api.command.InspectImageResponse; | ||
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.batch.v1.Job; | ||
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.IOException; | ||
import java.nio.charset.StandardCharsets; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.util.List; | ||
|
||
import static io.ballerina.c2c.KubernetesConstants.DOCKER; | ||
import static io.ballerina.c2c.KubernetesConstants.KUBERNETES; | ||
import static io.ballerina.c2c.test.utils.KubernetesTestUtils.getDockerImage; | ||
|
||
/** | ||
* Test cases for sample 4. | ||
*/ | ||
public class NativeJobTest extends SampleTest { | ||
|
||
private static final Path SOURCE_DIR_PATH = SAMPLE_DIR.resolve("kubernetes-native-job"); | ||
private static final Path DOCKER_TARGET_PATH = SOURCE_DIR_PATH.resolve("target").resolve(DOCKER) | ||
.resolve("hello"); | ||
private static final Path KUBERNETES_TARGET_PATH = SOURCE_DIR_PATH.resolve("target").resolve(KUBERNETES) | ||
.resolve("hello"); | ||
private static final String DOCKER_IMAGE = "xlight-hello-0.0.1:latest"; | ||
|
||
@BeforeClass | ||
public void compileSample() throws IOException, InterruptedException { | ||
|
||
Assert.assertEquals(KubernetesTestUtils.compileBallerinaProject(SOURCE_DIR_PATH), 0); | ||
File dockerFile = DOCKER_TARGET_PATH.resolve("Dockerfile").toFile(); | ||
Assert.assertTrue(dockerFile.exists()); | ||
InspectImageResponse imageInspect = getDockerImage(DOCKER_IMAGE); | ||
Assert.assertNotNull(imageInspect.getConfig()); | ||
} | ||
|
||
@Test | ||
public void validateDockerfile() throws IOException { | ||
|
||
File dockerFile = DOCKER_TARGET_PATH.resolve("Dockerfile").toFile(); | ||
Assert.assertTrue(dockerFile.exists()); | ||
String content = Files.readString(dockerFile.toPath(), StandardCharsets.UTF_8); | ||
Assert.assertTrue(content.contains("RUN native-image -jar hello.jar -H:Name=hello --no-fallback " + | ||
"-H:+StaticExecutableWithDynamicLibC")); | ||
Assert.assertTrue(content.contains("FROM ghcr.io/graalvm/native-image-community:17-ol8 as build")); | ||
Assert.assertTrue(content.contains("FROM gcr.io/distroless/base")); | ||
} | ||
|
||
@Test | ||
public void validateJob() throws IOException { | ||
|
||
File jobYAML = KUBERNETES_TARGET_PATH.resolve("hello.yaml").toFile(); | ||
Job job = KubernetesTestUtils.loadYaml(jobYAML); | ||
Assert.assertEquals(job.getMetadata().getName(), "xlight-hello-0-job"); | ||
List<Container> containers = job.getSpec().getTemplate().getSpec().getContainers(); | ||
Assert.assertEquals(containers.size(), 1); | ||
Container container = containers.get(0); | ||
Assert.assertEquals(container.getImage(), DOCKER_IMAGE); | ||
Assert.assertEquals(job.getSpec().getTemplate().getSpec() | ||
.getRestartPolicy(), KubernetesConstants.RestartPolicy.OnFailure.name()); | ||
} | ||
|
||
@AfterClass | ||
public void cleanUp() throws KubernetesPluginException { | ||
|
||
KubernetesUtils.deleteDirectory(KUBERNETES_TARGET_PATH); | ||
KubernetesUtils.deleteDirectory(DOCKER_TARGET_PATH); | ||
KubernetesTestUtils.deleteDockerImage(DOCKER_IMAGE); | ||
} | ||
} |
This file contains 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 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 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 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 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,9 @@ | ||
[package] | ||
org = "xlight" | ||
name= "hello" | ||
version = "0.0.1" | ||
|
||
[build-options] | ||
observabilityIncluded = true | ||
cloud = "k8s" | ||
graalvm = true |
This file contains 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,52 @@ | ||
## Kubernetes Job | ||
|
||
- This sample shows how to run a ballerina application as a job using graalvm in Kubernetes. | ||
|
||
### How to write: | ||
This segment shows how a c2c segment is mapped into cloud element. | ||
|
||
1. `Ballerina code` segment | ||
```bal | ||
@cloud:Task | ||
public function main(string... args) { | ||
io:println("hello world"); | ||
} | ||
``` | ||
2. Kubernetes YAML file segment | ||
```yaml | ||
apiVersion: "batch/v1" | ||
kind: "Job" | ||
metadata: | ||
name: "anjana-main-tas-job" | ||
spec: | ||
template: | ||
spec: | ||
containers: | ||
- image: "anjana-main_task-0.1.0:latest" | ||
name: "anjana-main-tas-job" | ||
restartPolicy: "OnFailure" | ||
|
||
``` | ||
|
||
|
||
### How to run: | ||
|
||
1. Build the Ballerina file. | ||
```bash | ||
$> bal build --cloud=k8s hello_world_job.bal | ||
Compiling source | ||
hello_world_job.bal | ||
|
||
Generating executable | ||
|
||
Generating artifacts... | ||
|
||
@kubernetes:Job - complete 1/1 | ||
@kubernetes:Docker - complete 2/2 | ||
|
||
Execute the below command to deploy the Kubernetes artifacts: | ||
kubectl apply -f /Users/luheerathan/luhee/Ballerina-Project-Files/Test/c2c-test/examples/kubernetes-job/kubernetes | ||
|
||
hello_world_job.jar | ||
``` |
This file contains 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,7 @@ | ||
import ballerina/io; | ||
import ballerina/cloud; | ||
|
||
@cloud:Task | ||
public function main(string... args) { | ||
io:println("hello world"); | ||
} |