Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 3 additions & 6 deletions examples/java/hello/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,7 @@
<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>
<directory>imports/src/main/resources</directory>
Comment thread
campionfellin marked this conversation as resolved.
Outdated
</resource>
</resources>
<plugins>
Expand All @@ -64,7 +61,7 @@
</goals>
<configuration>
<sources>
<source>imports/src/main/k8s/main/java</source>
<source>imports/</source>
</sources>
</configuration>
</execution>
Expand All @@ -76,7 +73,7 @@
<version>2.12.4</version>
<configuration>
<additionalClasspathElements>
<additionalClasspathElement>imports/src/main/k8s/main/java</additionalClasspathElement>
<additionalClasspathElement>imports/</additionalClasspathElement>
</additionalClasspathElements>
</configuration>
</plugin>
Expand Down
6 changes: 6 additions & 0 deletions examples/java/web-service/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
nodes_modules/
.settings/
imports/
target/
.classpath
.project
4 changes: 4 additions & 0 deletions examples/java/web-service/cdk8s.yaml
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
18 changes: 18 additions & 0 deletions examples/java/web-service/package.json
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"
}
}
82 changes: 82 additions & 0 deletions examples/java/web-service/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
<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/resources</directory>
Comment thread
campionfellin marked this conversation as resolved.
Outdated
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>3.1.0</version>
<executions>
<execution>
<phase>generate-sources</phase>
<goals>
<goal>add-source</goal>
</goals>
<configuration>
<sources>
<source>imports/</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/</additionalClasspathElement>
</additionalClasspathElements>
</configuration>
</plugin>
</plugins>
</build>
</project>
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();
}
}
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;
Comment thread
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);

}
}
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);
}
}
}