This repository has been archived by the owner on Nov 8, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathworkload.tfaws.w
159 lines (131 loc) · 4.29 KB
/
workload.tfaws.w
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
bring "./api.w" as api;
bring "./tfaws-eks.w" as eks;
bring "cdk8s-plus-27" as plus;
bring "cdk8s" as cdk8s;
bring "cdktf" as cdktf;
bring "./tfaws-ecr.w" as ecr;
bring "./utils.w" as utils;
bring "@cdktf/provider-kubernetes" as k8s;
bring "@cdktf/provider-helm" as helm;
pub class Workload_tfaws impl api.IWorkload {
internalUrl: str?;
publicUrl: str?;
init(props: api.WorkloadProps) {
let cluster = eks.Cluster.getOrCreate(this);
let var image = props.image;
let var deps = MutArray<cdktf.ITerraformDependable>[];
if utils.isPath(props.image) {
let hash = utils.resolveContentHash(this, props) ?? props.image;
let appDir = utils.entrypointDir(this);
let repository = new ecr.Repository(
name: props.name,
directory: appDir + "/" + props.image,
tag: hash
);
image = repository.image;
for d in repository.deps {
deps.push(d);
}
}
class _Chart extends cdk8s.Chart {
name: str;
init(props: api.WorkloadProps) {
let env = props.env ?? {};
let envVariables = MutMap<plus.EnvValue>{};
for k in env.keys() {
if let v = env.get(k) {
envVariables.set(k, plus.EnvValue.fromValue(v));
}
}
let ports = MutArray<plus.ContainerPort>[];
if let port = props.port {
ports.push({ number: port });
}
let var readiness: plus.Probe? = nil;
if let x = props.readiness {
if let port = props.port {
readiness = plus.Probe.fromHttpGet(x, port: port);
} else {
throw "Cannot setup readiness probe without a `port`";
}
}
let deployment = new plus.Deployment(
replicas: props.replicas,
metadata: {
name: props.name
},
);
deployment.addContainer(
image: "{{ .Values.image }}",
envVariables: envVariables.copy(),
ports: ports.copy(),
readiness: readiness,
args: props.args,
securityContext: {
ensureNonRoot: false,
}
);
let isPublic = props.public ?? false;
let var serviceType: plus.ServiceType? = nil;
if isPublic {
serviceType = plus.ServiceType.NODE_PORT;
}
let service = deployment.exposeViaService(
name: props.name,
serviceType: serviceType,
);
if isPublic {
new plus.Ingress(
metadata: {
name: props.name,
annotations: {
"kubernetes.io/ingress.class": "alb",
"alb.ingress.kubernetes.io/scheme": "internet-facing",
"alb.ingress.kubernetes.io/target-type": "ip",
"alb.ingress.kubernetes.io/healthcheck-protocol": "HTTP",
"alb.ingress.kubernetes.io/healthcheck-port": "traffic-port",
"alb.ingress.kubernetes.io/healthcheck-path": "/",
}
},
defaultBackend: plus.IngressBackend.fromService(service),
);
}
this.name = props.name;
}
pub toHelm(): str {
return _Chart.toHelmChart(this);
}
extern "./helm.js" pub static toHelmChart(chart: cdk8s.Chart): str;
}
let chart = new _Chart(props);
let release = new helm.release.Release(
provider: cluster.helmProvider(),
dependsOn: deps.copy(),
name: props.name,
chart: chart.toHelm(),
values: ["image: ${image}"],
);
if let port = props.port {
this.internalUrl = "http://${props.name}:${props.port}";
}
// if "public" is set, lookup the address from the ingress resource created by the helm chart
// and assign to `publicUrl`.
if props.public ?? false {
let ingress = new k8s.dataKubernetesIngressV1.DataKubernetesIngressV1(
provider: cluster.kubernetesProvider(),
dependsOn: [release],
metadata: {
name: props.name
}
);
let hostname = ingress.status.get(0).loadBalancer.get(0).ingress.get(0).hostname;
this.publicUrl = "http://${hostname}";
}
}
pub getPublicUrl(): str? {
return this.publicUrl;
}
pub getInternalUrl(): str? {
return this.internalUrl;
}
}