diff --git a/examples/java/hello/cdk8s.yaml b/examples/java/hello/cdk8s.yaml index 442c856f86..146076240f 100644 --- a/examples/java/hello/cdk8s.yaml +++ b/examples/java/hello/cdk8s.yaml @@ -1,4 +1,4 @@ language: java -app: mvn exec:java -Dexec.mainClass="com.mycompany.app.HelloKube" +app: mvn exec:java -Dexec.mainClass="com.mycompany.app.HelloKube" imports: - - k8s \ No newline at end of file + - k8s \ No newline at end of file diff --git a/examples/java/hello/pom.xml b/examples/java/hello/pom.xml index d6c8d753ac..9170b51057 100644 --- a/examples/java/hello/pom.xml +++ b/examples/java/hello/pom.xml @@ -11,7 +11,7 @@ org.cdk8s cdk8s - 0.24.0 + 0.25.0 software.constructs @@ -22,11 +22,6 @@ com.google.guava guava 29.0-jre - - - software.amazon.jsii - jsii-runtime - 1.5.0 diff --git a/examples/java/web-service/.gitignore b/examples/java/web-service/.gitignore new file mode 100644 index 0000000000..bbbd2a2923 --- /dev/null +++ b/examples/java/web-service/.gitignore @@ -0,0 +1,6 @@ +nodes_modules/ +.settings/ +imports/ +target/ +.classpath +.project \ No newline at end of file diff --git a/examples/java/web-service/cdk8s.yaml b/examples/java/web-service/cdk8s.yaml new file mode 100644 index 0000000000..644ffb1500 --- /dev/null +++ b/examples/java/web-service/cdk8s.yaml @@ -0,0 +1,4 @@ +language: java +app: mvn exec:java -Dexec.mainClass="com.mycompany.app.HelloKube" +imports: + - k8s \ No newline at end of file diff --git a/examples/java/web-service/package.json b/examples/java/web-service/package.json new file mode 100644 index 0000000000..8cff321fa7 --- /dev/null +++ b/examples/java/web-service/package.json @@ -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.2" + }, + "devDependencies": { + "cdk8s-cli": "0.0.0" + } +} diff --git a/examples/java/web-service/pom.xml b/examples/java/web-service/pom.xml new file mode 100644 index 0000000000..371c1fde82 --- /dev/null +++ b/examples/java/web-service/pom.xml @@ -0,0 +1,35 @@ + + 4.0.0 + com.mycompany.app + my-app + jar + 1.0-SNAPSHOT + my-app + http://maven.apache.org + + + org.cdk8s + cdk8s + 0.25.0 + + + software.constructs + constructs + 2.0.2 + + + com.google.guava + guava + 29.0-jre + + + + 1.8 + 1.8 + + + src/main/java + src/test/java + + \ No newline at end of file diff --git a/examples/java/web-service/src/main/java/com/mycompany/app/HelloKube.java b/examples/java/web-service/src/main/java/com/mycompany/app/HelloKube.java new file mode 100644 index 0000000000..24b7900ba5 --- /dev/null +++ b/examples/java/web-service/src/main/java/com/mycompany/app/HelloKube.java @@ -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(); + } +} \ No newline at end of file diff --git a/examples/java/web-service/src/main/java/com/mycompany/app/WebService.java b/examples/java/web-service/src/main/java/com/mycompany/app/WebService.java new file mode 100644 index 0000000000..985d7411c4 --- /dev/null +++ b/examples/java/web-service/src/main/java/com/mycompany/app/WebService.java @@ -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 imports.k8s.IntOrString; +import imports.k8s.LabelSelector; +import imports.k8s.ObjectMeta; +import imports.k8s.PodTemplateSpec; +import imports.k8s.Service; +import imports.k8s.ServiceOptions; +import imports.k8s.ServicePort; +import imports.k8s.ServiceSpec; +import imports.k8s.DeploymentSpec; +import imports.k8s.PodSpec; +import imports.k8s.Container; +import imports.k8s.ContainerPort; +import imports.k8s.Deployment; +import imports.k8s.DeploymentOptions; + +/** + * 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 selector = new HashMap<>(); + selector.put("app", "hello-k8s"); + final List 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 containerPorts = new ArrayList<>(); + final ContainerPort containerPort = new ContainerPort.Builder() + .containerPort(containerPortNumber) + .build(); + containerPorts.add(containerPort); + final List 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); + + } +} \ No newline at end of file diff --git a/examples/java/web-service/src/main/java/com/mycompany/app/WebServiceOptions.java b/examples/java/web-service/src/main/java/com/mycompany/app/WebServiceOptions.java new file mode 100644 index 0000000000..86a8d2a435 --- /dev/null +++ b/examples/java/web-service/src/main/java/com/mycompany/app/WebServiceOptions.java @@ -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); + } + } +} \ No newline at end of file