-
Notifications
You must be signed in to change notification settings - Fork 312
chore: java web-service example #233
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 1 commit
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
771b5e7
chore: java web-service example
campionfellin 0a08857
fix: java example pom.xml resources and classpath
campionfellin 90d88ae
Merge branch 'master' into javaexamples
campionfellin 3111075
fix: update pom.xml imports for java examples
campionfellin 8c41009
fix: simplify pom.xml a bit more
campionfellin 0de535d
fix: simplify pom.xml more
campionfellin 723b133
Merge branch 'master' into javaexamples
campionfellin f8dab77
fix: reduce pom.xml and fix java imports
campionfellin c859be4
Merge branch 'master' into javaexamples
campionfellin 80f268e
Merge branch 'master' into javaexamples
campionfellin f8200fc
fix: update examples
campionfellin c828dd5
Merge branch 'master' into javaexamples
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| nodes_modules/ | ||
| .settings/ | ||
| imports/ | ||
| target/ | ||
| .classpath | ||
| .project |
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,4 @@ | ||
| language: java | ||
| app: mvn exec:java -Dexec.mainClass="com.mycompany.app.HelloKube" | ||
| imports: | ||
| - k8s |
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,18 @@ | ||
| { | ||
| "name": "web-service-java", | ||
| "version": "0.0.0", | ||
| "license": "Apache-2.0", | ||
| "private": true, | ||
| "scripts": { | ||
| "build": "cdk8s import --language java", | ||
| "synth": "mvn install && mvn compile && cdk8s synth", | ||
| "test": "echo 'no test for java yet.'" | ||
| }, | ||
| "dependencies": { | ||
| "cdk8s": "0.0.0", | ||
| "constructs": "^2.0.1" | ||
| }, | ||
| "devDependencies": { | ||
| "cdk8s-cli": "0.0.0" | ||
| } | ||
| } |
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,85 @@ | ||
| <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
| xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> | ||
| <modelVersion>4.0.0</modelVersion> | ||
| <groupId>com.mycompany.app</groupId> | ||
| <artifactId>my-app</artifactId> | ||
| <packaging>jar</packaging> | ||
| <version>1.0-SNAPSHOT</version> | ||
| <name>my-app</name> | ||
| <url>http://maven.apache.org</url> | ||
| <dependencies> | ||
| <dependency> | ||
| <groupId>junit</groupId> | ||
| <artifactId>junit</artifactId> | ||
| <version>3.8.1</version> | ||
| <scope>test</scope> | ||
| </dependency> | ||
| <!-- https://mvnrepository.com/artifact/org.cdk8s/cdk8s --> | ||
| <dependency> | ||
| <groupId>org.cdk8s</groupId> | ||
| <artifactId>cdk8s</artifactId> | ||
| <version>0.24.0</version> | ||
| </dependency> | ||
| <dependency> | ||
| <groupId>software.constructs</groupId> | ||
| <artifactId>constructs</artifactId> | ||
| <version>2.0.1</version> | ||
| </dependency> | ||
| <dependency> | ||
| <groupId>com.google.guava</groupId> | ||
| <artifactId>guava</artifactId> | ||
| <version>29.0-jre</version> | ||
| </dependency> | ||
| <dependency> | ||
| <groupId>software.amazon.jsii</groupId> | ||
| <artifactId>jsii-runtime</artifactId> | ||
| <version>1.5.0</version> | ||
| </dependency> | ||
| </dependencies> | ||
| <properties> | ||
| <maven.compiler.source>1.8</maven.compiler.source> | ||
| <maven.compiler.target>1.8</maven.compiler.target> | ||
| </properties> | ||
| <build> | ||
| <sourceDirectory>src/main/java</sourceDirectory> | ||
| <testSourceDirectory>src/test/java</testSourceDirectory> | ||
| <resources> | ||
| <resource> | ||
| <directory>imports/src/main/k8s/main/java</directory> | ||
| </resource> | ||
| <resource> | ||
| <directory>imports/src/main/k8s/main/resources</directory> | ||
| </resource> | ||
| </resources> | ||
| <plugins> | ||
| <plugin> | ||
| <groupId>org.codehaus.mojo</groupId> | ||
| <artifactId>build-helper-maven-plugin</artifactId> | ||
| <version>3.0.0</version> | ||
| <executions> | ||
| <execution> | ||
| <phase>generate-sources</phase> | ||
| <goals> | ||
| <goal>add-source</goal> | ||
| </goals> | ||
| <configuration> | ||
| <sources> | ||
| <source>imports/src/main/k8s/main/java</source> | ||
| </sources> | ||
| </configuration> | ||
| </execution> | ||
| </executions> | ||
| </plugin> | ||
| <plugin> | ||
| <groupId>org.apache.maven.plugins</groupId> | ||
| <artifactId>maven-surefire-plugin</artifactId> | ||
| <version>2.12.4</version> | ||
| <configuration> | ||
| <additionalClasspathElements> | ||
| <additionalClasspathElement>imports/src/main/k8s/main/java</additionalClasspathElement> | ||
| </additionalClasspathElements> | ||
| </configuration> | ||
| </plugin> | ||
| </plugins> | ||
| </build> | ||
| </project> | ||
42 changes: 42 additions & 0 deletions
42
examples/java/web-service/src/main/java/com/mycompany/app/HelloKube.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,42 @@ | ||
| package com.mycompany.app; | ||
|
|
||
| import software.constructs.Construct; | ||
|
|
||
| import org.cdk8s.App; | ||
| import org.cdk8s.Chart; | ||
| import org.cdk8s.ChartOptions; | ||
|
|
||
| /** | ||
| * Hello world! | ||
| */ | ||
| public class HelloKube extends Chart | ||
| { | ||
|
|
||
| public HelloKube(final Construct scope, final String id) { | ||
| this(scope, id, null); | ||
| } | ||
|
|
||
| public HelloKube(final Construct scope, final String id, final ChartOptions options) { | ||
| super(scope, id, options); | ||
|
|
||
| final WebServiceOptions helloOptions = new WebServiceOptions.Builder() | ||
| .image("paulbouwer/hello-kubernetes:1.7") | ||
| .replicas(2) | ||
| .build(); | ||
|
|
||
| new WebService(this, "hello", helloOptions); | ||
|
|
||
| final WebServiceOptions ghostOptions = new WebServiceOptions.Builder() | ||
| .image("ghost") | ||
| .containerPort(2368) | ||
| .build(); | ||
|
|
||
| new WebService(this, "ghost", ghostOptions); | ||
| } | ||
|
|
||
| public static void main(String[] args) { | ||
| final App app = new App(); | ||
| new HelloKube(app, "web-service-java"); | ||
| app.synth(); | ||
| } | ||
| } |
103 changes: 103 additions & 0 deletions
103
examples/java/web-service/src/main/java/com/mycompany/app/WebService.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,103 @@ | ||
| package com.mycompany.app; | ||
|
|
||
| import software.constructs.Construct; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
| import java.util.HashMap; | ||
| import java.util.Map; | ||
| import java.util.Optional; | ||
|
|
||
| import org.cdk8s.App; | ||
| import org.cdk8s.Chart; | ||
| import org.cdk8s.ChartOptions; | ||
|
|
||
| import k8s.IntOrString; | ||
| import k8s.LabelSelector; | ||
| import k8s.ObjectMeta; | ||
| import k8s.PodTemplateSpec; | ||
| import k8s.Service; | ||
| import k8s.ServiceOptions; | ||
| import k8s.ServicePort; | ||
| import k8s.ServiceSpec; | ||
| import k8s.DeploymentSpec; | ||
| import k8s.PodSpec; | ||
| import k8s.Container; | ||
| import k8s.ContainerPort; | ||
| import k8s.Deployment; | ||
| import k8s.DeploymentOptions; | ||
|
campionfellin marked this conversation as resolved.
Outdated
|
||
|
|
||
| /** | ||
| * Hello world! | ||
| */ | ||
| public class WebService extends Construct | ||
| { | ||
|
|
||
| public WebService(final Construct scope, final String id) { | ||
| this(scope, id, null); | ||
| } | ||
|
|
||
| public WebService(final Construct scope, final String id, final WebServiceOptions options) { | ||
| super(scope, id); | ||
|
|
||
| final int portNumber = Optional.of(options.getPort()).orElse(80); | ||
| final int containerPortNumber = Optional.of(options.getContainerPort()).orElse(8080); | ||
| final int replicas = Optional.of(options.getReplicas()).orElse(1); | ||
| final String image = options.getImage(); | ||
|
|
||
| // Defining a LoadBalancer Service | ||
| final String serviceType = "LoadBalancer"; | ||
| final Map<String, String> selector = new HashMap<>(); | ||
| selector.put("app", "hello-k8s"); | ||
| final List<ServicePort> servicePorts = new ArrayList<>(); | ||
| final ServicePort servicePort = new ServicePort.Builder() | ||
| .port(portNumber) | ||
| .targetPort(IntOrString.fromNumber(containerPortNumber)) | ||
| .build(); | ||
| servicePorts.add(servicePort); | ||
| final ServiceSpec serviceSpec = new ServiceSpec.Builder() | ||
| .type(serviceType) | ||
| .selector(selector) | ||
| .ports(servicePorts) | ||
| .build(); | ||
| final ServiceOptions serviceOptions = new ServiceOptions.Builder() | ||
| .spec(serviceSpec) | ||
| .build(); | ||
|
|
||
| new Service(this, "service", serviceOptions); | ||
|
|
||
| // Defining a Deployment | ||
| final LabelSelector labelSelector = new LabelSelector.Builder().matchLabels(selector).build(); | ||
| final ObjectMeta objectMeta = new ObjectMeta.Builder().labels(selector).build(); | ||
| final List<ContainerPort> containerPorts = new ArrayList<>(); | ||
| final ContainerPort containerPort = new ContainerPort.Builder() | ||
| .containerPort(containerPortNumber) | ||
| .build(); | ||
| containerPorts.add(containerPort); | ||
| final List<Container> containers = new ArrayList<>(); | ||
| final Container container = new Container.Builder() | ||
| .name("web") | ||
| .image(image) | ||
| .ports(containerPorts) | ||
| .build(); | ||
| containers.add(container); | ||
| final PodSpec podSpec = new PodSpec.Builder() | ||
| .containers(containers) | ||
| .build(); | ||
| final PodTemplateSpec podTemplateSpec = new PodTemplateSpec.Builder() | ||
| .metadata(objectMeta) | ||
| .spec(podSpec) | ||
| .build(); | ||
| final DeploymentSpec deploymentSpec = new DeploymentSpec.Builder() | ||
| .replicas(1) | ||
| .selector(labelSelector) | ||
| .template(podTemplateSpec) | ||
| .build(); | ||
| final DeploymentOptions deploymentOptions = new DeploymentOptions.Builder() | ||
| .spec(deploymentSpec) | ||
| .build(); | ||
|
|
||
| new Deployment(this, "deployment", deploymentOptions); | ||
|
|
||
| } | ||
| } | ||
65 changes: 65 additions & 0 deletions
65
examples/java/web-service/src/main/java/com/mycompany/app/WebServiceOptions.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,65 @@ | ||
| package com.mycompany.app; | ||
|
|
||
| import software.constructs.Construct; | ||
|
|
||
| public class WebServiceOptions | ||
| { | ||
| private String image; | ||
| private int replicas; | ||
| private int port; | ||
| private int containerPort; | ||
|
|
||
| public WebServiceOptions(final String image, final int replicas, final int port, final int containerPort) { | ||
| this.image = image; | ||
| this.replicas = replicas; | ||
| this.port = port; | ||
| this.containerPort = containerPort; | ||
| } | ||
|
|
||
| public String getImage() { | ||
| return this.image; | ||
| } | ||
|
|
||
| public int getReplicas() { | ||
| return this.replicas; | ||
| } | ||
|
|
||
| public int getPort() { | ||
| return this.port; | ||
| } | ||
|
|
||
| public int getContainerPort() { | ||
| return this.containerPort; | ||
| } | ||
|
|
||
| public static final class Builder { | ||
| private String image; | ||
| private int replicas = 1; | ||
| private int port = 80; | ||
| private int containerPort = 8080; | ||
|
|
||
| public Builder image(String image) { | ||
| this.image = image; | ||
| return this; | ||
| } | ||
|
|
||
| public Builder replicas(int replicas) { | ||
| this.replicas = replicas; | ||
| return this; | ||
| } | ||
|
|
||
| public Builder port(int port) { | ||
| this.port = port; | ||
| return this; | ||
| } | ||
|
|
||
| public Builder containerPort(int containerPort) { | ||
| this.containerPort = containerPort; | ||
| return this; | ||
| } | ||
|
|
||
| public WebServiceOptions build() { | ||
| return new WebServiceOptions(image, replicas, port, containerPort); | ||
| } | ||
| } | ||
| } |
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.