-
Notifications
You must be signed in to change notification settings - Fork 881
/
echoserver.ts
163 lines (153 loc) · 4.55 KB
/
echoserver.ts
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
// Copyright 2016-2019, Pulumi Corporation. All rights reserved.
import * as eks from "@pulumi/eks";
import * as k8s from "@pulumi/kubernetes";
import * as pulumi from "@pulumi/pulumi";
// Create the echoserver workload's Service, Deployment and Ingress.
interface EchoserverArgs {
replicas: pulumi.Input<number>;
namespace: pulumi.Input<string>;
ingressClass: pulumi.Input<string>;
provider: k8s.Provider;
}
export function create(
name: string,
args: EchoserverArgs,
): k8s.core.v1.Service {
const labels = {app: name};
// Create the Service.
const service = createService(name, {
labels: labels,
namespace: args.namespace,
provider: args.provider,
});
const serviceName = service.metadata.name;
// Deploy the echoserver in the general, standard nodegroup.
const deployment = createDeployment(name, {
replicas: args.replicas,
labels: labels,
namespace: args.namespace,
provider: args.provider,
});
// Create the Ingress.
const ingress = createIngress(name, {
labels: labels,
namespace: args.namespace,
ingressClass: args.ingressClass,
serviceName: serviceName,
provider: args.provider,
});
return service;
}
interface EchoserverServiceArgs {
labels: pulumi.Input<any>;
namespace: pulumi.Input<string>;
provider: k8s.Provider;
}
export function createService(
name: string,
args: EchoserverServiceArgs,
): k8s.core.v1.Service {
return new k8s.core.v1.Service(
name,
{
metadata: {
labels: args.labels,
namespace: args.namespace,
},
spec: {
type: "ClusterIP",
ports: [{port: 80, protocol: "TCP", targetPort: "http"}],
selector: args.labels,
},
},
{
provider: args.provider,
},
);
}
interface EchoserverDeploymentArgs {
replicas: pulumi.Input<number>;
labels: pulumi.Input<any>;
namespace: pulumi.Input<string>;
provider: k8s.Provider;
}
export function createDeployment(
name: string,
args: EchoserverDeploymentArgs,
): k8s.apps.v1.Deployment {
return new k8s.apps.v1.Deployment(name,
{
metadata: {
labels: args.labels,
namespace: args.namespace,
},
spec: {
replicas: args.replicas,
selector: { matchLabels: args.labels },
template: {
metadata: { labels: args.labels, namespace: args.namespace },
spec: {
restartPolicy: "Always",
containers: [
{
name: name,
image: "gcr.io/google-containers/echoserver:1.5",
ports: [{ name: "http", containerPort: 8080 }],
},
],
},
},
},
},
{
provider: args.provider,
},
);
}
interface EchoserverIngressArgs {
labels: pulumi.Input<any>;
namespace: pulumi.Input<string>;
ingressClass: pulumi.Input<string>;
serviceName: pulumi.Input<string>;
provider: k8s.Provider;
}
export function createIngress(
name: string,
args: EchoserverIngressArgs,
): k8s.extensions.v1beta1.Ingress {
// TODO(metral): change to k8s.networking.v1beta.Ingress
// when EKS supports >= 1.14.
return new k8s.extensions.v1beta1.Ingress(
name,
{
metadata: {
labels: args.labels,
namespace: args.namespace,
annotations: {
"kubernetes.io/ingress.class": args.ingressClass,
},
},
spec: {
rules: [
{
host: "apps.example.com",
http: {
paths: [
{
path: "/echoserver",
backend: {
serviceName: args.serviceName,
servicePort: "http",
},
},
],
},
},
],
},
},
{
provider: args.provider,
},
);
}