forked from cdk8s-team/cdk8s
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathWebService.java
More file actions
103 lines (90 loc) · 3.41 KB
/
WebService.java
File metadata and controls
103 lines (90 loc) · 3.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
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;
/**
* 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);
}
}