-
Notifications
You must be signed in to change notification settings - Fork 16
/
kx.ts
483 lines (439 loc) · 17.2 KB
/
kx.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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
// Copyright 2016-2019, Pulumi Corporation.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
import * as k8s from "@pulumi/kubernetes";
import * as pulumi from "@pulumi/pulumi";
import PodBuilderDeploymentSpec = types.PodBuilderDeploymentSpec;
export namespace types {
export type EnvMap = Record<string, pulumi.Input<string | k8s.types.input.core.v1.EnvVarSource>>;
export type PortMap = Record<string, pulumi.Input<number>>;
export enum ServiceType {
ClusterIP = "ClusterIP",
LoadBalancer = "LoadBalancer",
}
export type VolumeMount = {
volume: pulumi.Input<k8s.types.input.core.v1.Volume>,
destPath: pulumi.Input<string>,
srcPath?: pulumi.Input<string>,
};
export type Container = Omit<k8s.types.input.core.v1.Container, "env"|"name"|"ports"|"volumeMounts"> & {
env?: pulumi.Input<pulumi.Input<k8s.types.input.core.v1.EnvVar>[] | EnvMap>,
name?: pulumi.Input<string>,
ports?: pulumi.Input<pulumi.Input<k8s.types.input.core.v1.ContainerPort>[] | PortMap>,
volumeMounts?: pulumi.Input<pulumi.Input<k8s.types.input.core.v1.VolumeMount | VolumeMount>[]>,
};
export type PodSpec = Omit<k8s.types.input.core.v1.PodSpec, "containers"|"initContainers"> & {
containers: pulumi.Input<pulumi.Input<Container>[]>,
initContainers?: pulumi.Input<pulumi.Input<Container>[]>,
};
export type Pod = Omit<k8s.types.input.core.v1.Pod, "spec"> & {
spec: pulumi.Input<PodSpec | PodBuilder>,
};
export type DeploymentSpec = Omit<k8s.types.input.apps.v1.DeploymentSpec, "template"> & {
template: pulumi.Input<Pod>,
};
export type Deployment = Omit<k8s.types.input.apps.v1.Deployment, "spec"> & {
spec: pulumi.Input<DeploymentSpec | k8s.types.input.apps.v1.DeploymentSpec>,
};
export type ServiceSpec = Omit<k8s.types.input.core.v1.ServiceSpec, "ports"|"type"> & {
ports?: pulumi.Input<pulumi.Input<k8s.types.input.core.v1.ServicePort>[] | PortMap>,
type?: pulumi.Input<ServiceType | string>,
};
export type Service = Omit<k8s.types.input.core.v1.Service, "spec"> & {
spec: pulumi.Input<ServiceSpec>,
};
export type StatefulSetSpec = Omit<k8s.types.input.apps.v1.StatefulSetSpec, "template"> & {
template: pulumi.Input<Pod>,
};
export type StatefulSet = Omit<k8s.types.input.apps.v1.StatefulSet, "spec"> & {
spec: pulumi.Input<StatefulSetSpec | k8s.types.input.apps.v1.StatefulSetSpec>,
};
export type JobSpec = Omit<k8s.types.input.batch.v1.JobSpec, "template"> & {
template: pulumi.Input<Pod>,
};
export type Job = Omit<k8s.types.input.batch.v1.Job, "spec"> & {
spec: pulumi.Input<JobSpec | k8s.types.input.batch.v1.JobSpec>,
};
export type PodBuilderDeploymentSpec = Omit<k8s.types.input.apps.v1.DeploymentSpec, "selector"|"template">;
export type PodBuilderJobSpec = Omit<k8s.types.input.batch.v1.JobSpec, "template">;
}
function buildPodSpec(args: pulumi.Input<types.PodSpec>): pulumi.Output<k8s.types.input.core.v1.PodSpec> {
return pulumi.output<types.PodSpec>(args).apply(podSpec => {
const volumes: k8s.types.input.core.v1.Volume[] = [];
let initContainers: k8s.types.input.core.v1.Container[] = [];
if (podSpec.initContainers) {
initContainers = podSpec.initContainers.map(container => buildContainer(container, volumes));
}
const containers = podSpec.containers.map(container => buildContainer(container, volumes));
return pulumi.output({
...podSpec,
initContainers: initContainers,
containers: containers,
volumes: [
...podSpec.volumes || [],
...volumes,
],
});
});
}
function buildContainer(container: pulumi.UnwrappedObject<types.Container>, volumes: k8s.types.input.core.v1.Volume[]): k8s.types.input.core.v1.Container {
const isEnvMap = (env: any): env is pulumi.UnwrappedObject<types.EnvMap> => env.length === undefined;
const isPortMap = (ports: any): ports is pulumi.UnwrappedObject<types.PortMap> => ports.length === undefined;
const isMountObject = (object: any): object is pulumi.UnwrappedObject<types.VolumeMount> => object.hasOwnProperty("volume");
const c: pulumi.UnwrappedObject<k8s.types.input.core.v1.Container> = {
...container,
env: [],
name: "",
ports: [],
volumeMounts: [],
};
if (container.name) {
c.name = container.name;
} else {
const re = /(.*\/|^)(?<image>\w+)(:(?<tag>.*))?/;
const imageArg = container.image || "";
const result = re.exec(imageArg);
if (!result) {
throw new Error("Failed to parse image name from " + imageArg);
}
c.name = result[2];
}
const env = container.env;
if (env) {
if (isEnvMap(env)) {
Object.keys(env).forEach(key => {
const value = env[key];
if (typeof value === "string") {
c.env!.push({name: key, value: value});
} else {
c.env!.push({name: key, valueFrom: value});
}
});
} else {
c.env = env;
}
}
const ports = container.ports;
if (ports) {
if (isPortMap(ports)) {
Object.keys(ports).forEach(key => {
const value = ports[key];
c.ports!.push({name: key, containerPort: value});
});
} else {
c.ports = ports;
}
}
const volumeMounts = container.volumeMounts;
if (volumeMounts) {
volumeMounts.forEach(mount => {
if (isMountObject(mount)) {
c.volumeMounts!.push({
name: mount.volume.name,
mountPath: mount.destPath,
subPath: mount.srcPath,
});
volumes.push({
...mount.volume,
});
} else {
c.volumeMounts!.push(mount);
}
});
}
return c;
}
export class PodBuilder {
public readonly podSpec: pulumi.Output<k8s.types.input.core.v1.PodSpec>;
private readonly podName: pulumi.Output<string>;
constructor(args: types.PodSpec) {
this.podSpec = buildPodSpec(args);
this.podName = this.podSpec.containers.apply((containers: k8s.types.input.core.v1.Container[]) => {
return pulumi.output(containers[0].name);
});
}
public asDeploymentSpec(
args?: types.PodBuilderDeploymentSpec,
): pulumi.Output<k8s.types.input.apps.v1.DeploymentSpec> {
const appLabels = { app: this.podName };
const _args = args || {};
const deploymentSpec: k8s.types.input.apps.v1.DeploymentSpec = {
..._args,
selector: { matchLabels: appLabels },
replicas: _args.replicas ?? 1,
template: {
metadata: { labels: appLabels },
spec: this.podSpec,
},
};
return pulumi.output(deploymentSpec);
}
public asStatefulSetSpec(args?: {replicas?: number}): pulumi.Output<k8s.types.input.apps.v1.StatefulSetSpec> {
const appLabels = { app: this.podName };
const statefulSetSpec: k8s.types.input.apps.v1.StatefulSetSpec = {
selector: { matchLabels: appLabels },
replicas: args?.replicas ?? 1,
serviceName: "", // This will be auto-generated by kx.StatefulSet.
template: {
metadata: { labels: appLabels },
spec: this.podSpec,
},
};
return pulumi.output(statefulSetSpec);
}
public asJobSpec(args?: types.PodBuilderJobSpec): pulumi.Output<k8s.types.input.batch.v1.JobSpec> {
const appLabels = { app: this.podName };
const jobSpec: k8s.types.input.batch.v1.JobSpec = {
...args,
template: {
metadata: { labels: appLabels },
spec: this.podSpec,
},
};
return pulumi.output(jobSpec);
}
}
export class Pod extends k8s.core.v1.Pod {
constructor(name: string, args: types.Pod, opts?: pulumi.CustomResourceOptions) {
const isPodBuilder = (object: any): object is pulumi.UnwrappedObject<PodBuilder> => object.hasOwnProperty("podSpec");
const spec: pulumi.Output<k8s.types.input.core.v1.PodSpec> = pulumi.output(args.spec).apply(specArg => {
if (isPodBuilder(specArg)) {
return pulumi.output(specArg.podSpec);
} else {
return buildPodSpec(specArg);
}
});
super(name,
{
...args,
spec: spec,
},
opts);
}
}
export class Deployment extends k8s.apps.v1.Deployment {
private readonly name: string;
private readonly opts?: pulumi.CustomResourceOptions;
constructor(name: string, args: types.Deployment, opts?: pulumi.CustomResourceOptions) {
const spec: pulumi.Output<k8s.types.input.apps.v1.DeploymentSpec> = pulumi.output<types.Deployment>(args)
.apply(args => {
const podSpec = buildPodSpec(args.spec.template.spec as types.PodSpec);
return pulumi.output({
...args.spec,
template: {
...args.spec.template,
spec: podSpec,
},
});
});
super(name,
{
...args,
spec: spec,
},
opts);
this.name = name;
this.opts = opts;
}
public createService(args: types.ServiceSpec = {}) {
const serviceSpec = pulumi
.all([this.spec.template.spec.containers, args])
.apply(([containers, args]) => {
// TODO: handle merging ports from args
const ports: Record<string, number> = {};
containers.forEach(container => {
if (container.ports) {
container.ports.forEach(port => {
ports[port.name] = port.containerPort;
});
}
});
return {
...args,
ports: args.ports || ports,
selector: this.spec.selector.matchLabels,
// TODO: probably need to unwrap args.type in case it's a computed value
type: args && args.type as string,
};
});
return new Service(this.name, {
metadata: { namespace: this.metadata.namespace },
spec: serviceSpec,
}, {...this.opts, parent: this});
}
}
export class Service extends k8s.core.v1.Service {
constructor(name: string, args: types.Service, opts?: pulumi.CustomResourceOptions) {
const spec = pulumi.output(args)
.apply((args: pulumi.UnwrappedObject<types.Service>) => {
const isPortMap = (ports: any): ports is types.PortMap => ports.length === undefined;
let ports: k8s.types.input.core.v1.ServicePort[] = [];
const portsArg = args.spec.ports;
if (portsArg) {
if (isPortMap(portsArg)) {
Object.keys(portsArg).forEach(key => {
const value = portsArg[key];
ports.push({name: key, port: value});
});
} else {
ports = portsArg;
}
}
return {
...args.spec,
ports: ports,
type: args.spec.type as string,
};
});
super(name,
{
...args,
spec: spec,
},
opts);
}
/**
* Endpoint of the Service. This can be either an IP address or a hostname,
* depending on the k8s cluster provider.
*/
get endpoint(): pulumi.Output<string> {
return this.status.loadBalancer.ingress
.apply((ingress: k8s.types.output.core.v1.LoadBalancerIngress[]) => {
if (ingress.length > 0) {
return ingress[0].ip || ingress[0].hostname;
}
return "";
});
}
}
export class StatefulSet extends pulumi.ComponentResource {
private readonly name: string;
private readonly opts?: pulumi.CustomResourceOptions;
constructor(name: string, args: types.StatefulSet, opts?: pulumi.CustomResourceOptions) {
const spec: pulumi.Output<k8s.types.input.apps.v1.StatefulSetSpec> = pulumi.output<types.StatefulSet>(args)
.apply(args => {
const podSpec = buildPodSpec(args.spec.template.spec as types.PodSpec);
return pulumi.output({
...args.spec,
serviceName: `${name}-service`,
template: {
...args.spec.template,
spec: podSpec,
},
});
});
super("kubernetesx:StatefulSet", name, {...args, spec: spec}, opts);
const statefulSet = new k8s.apps.v1.StatefulSet(
name,
{...args, spec: spec},
{...opts, parent: this},
);
this.name = name;
this.opts = opts;
}
}
export class Job extends k8s.batch.v1.Job {
private readonly name: string;
private readonly opts?: pulumi.CustomResourceOptions;
constructor(name: string, args: types.Job, opts?: pulumi.CustomResourceOptions) {
const spec: pulumi.Output<k8s.types.input.batch.v1.JobSpec> = pulumi.output<types.Job>(args)
.apply(args => {
const podSpec = buildPodSpec(args.spec.template.spec as types.PodSpec);
return pulumi.output({
...args.spec,
template: {
...args.spec.template,
spec: podSpec,
},
});
});
super(name,
{
...args,
spec: spec,
},
opts);
this.name = name;
this.opts = opts;
}
}
export class PersistentVolumeClaim extends k8s.core.v1.PersistentVolumeClaim {
constructor(name: string, args: k8s.types.input.core.v1.PersistentVolumeClaim, opts?: pulumi.CustomResourceOptions) {
super(name, args, opts);
}
// TODO: define input type?
public mount(destPath: pulumi.Input<string>, srcPath?: pulumi.Input<string>): pulumi.Output<types.VolumeMount> {
return pulumi.output({
volume: {
name: this.metadata.name,
persistentVolumeClaim: {
claimName: this.metadata.name,
},
},
destPath: destPath,
srcPath: srcPath,
});
}
}
export class ConfigMap extends k8s.core.v1.ConfigMap {
constructor(name: string, args: k8s.types.input.core.v1.ConfigMap, opts?: pulumi.CustomResourceOptions) {
super(name, args, opts);
}
public mount(destPath: pulumi.Input<string>, srcPath?: pulumi.Input<string>): pulumi.Output<types.VolumeMount> {
return pulumi.output({
volume: {
name: this.metadata.name,
configMap: {
name: this.metadata.name,
// TODO: items
},
},
destPath: destPath,
srcPath: srcPath,
});
}
public asEnvValue(key: pulumi.Input<string>): pulumi.Output<k8s.types.input.core.v1.EnvVarSource> {
return pulumi.output({
configMapKeyRef: {
name: this.metadata.name,
key: key,
},
});
}
}
export class Secret extends k8s.core.v1.Secret {
constructor(name: string, args: k8s.types.input.core.v1.Secret, opts?: pulumi.CustomResourceOptions) {
super(name, args, opts);
}
public mount(destPath: pulumi.Input<string>, srcPath?: pulumi.Input<string>): pulumi.Output<types.VolumeMount> {
return pulumi.output({
volume: {
name: this.metadata.name,
secret: {
secretName: this.metadata.name,
// TODO: items
},
},
destPath: destPath,
srcPath: srcPath,
});
}
public asEnvValue(key: pulumi.Input<string>): pulumi.Output<k8s.types.input.core.v1.EnvVarSource> {
return pulumi.output({
secretKeyRef: {
name: this.metadata.name,
key: key,
},
});
}
}