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 pathtfaws-eks.w
237 lines (204 loc) · 7.15 KB
/
tfaws-eks.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
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
bring aws;
bring cloud;
bring "constructs" as c;
bring "cdktf" as cdktf;
bring "@cdktf/provider-helm" as helm;
bring "@cdktf/provider-kubernetes" as eks;
bring "./tfaws-vpc.w" as vpc;
bring "./values.w" as values;
bring "./aws.w" as aws_info;
struct ClusterAttributes {
name: str;
certificate: str;
endpoint: str;
}
interface ICluster extends std.IResource {
attributes(): ClusterAttributes;
kubernetesProvider(): cdktf.TerraformProvider;
helmProvider(): cdktf.TerraformProvider;
}
pub class ClusterBase impl ICluster {
pub attributes(): ClusterAttributes { throw "Not implemented"; }
pub kubernetesProvider(): cdktf.TerraformProvider {
let stack = cdktf.TerraformStack.of(this);
let singletonKey = "WingKubernetesProvider";
let attributes = this.attributes();
let existing = stack.node.tryFindChild(singletonKey);
if existing? {
return unsafeCast(existing);
}
// setup the "kubernetes" terraform provider
return new eks.provider.KubernetesProvider(
host: attributes.endpoint,
clusterCaCertificate: cdktf.Fn.base64decode(attributes.certificate),
exec: {
apiVersion: "client.authentication.k8s.io/v1beta1",
args: ["eks", "get-token", "--cluster-name", attributes.name],
command: "aws",
}
) as singletonKey in stack;
}
pub helmProvider(): cdktf.TerraformProvider {
let stack = cdktf.TerraformStack.of(this);
let singletonKey = "WingHelmProvider";
let attributes = this.attributes();
let existing = stack.node.tryFindChild(singletonKey);
if existing? {
return unsafeCast(existing);
}
return new helm.provider.HelmProvider(kubernetes: {
host: attributes.endpoint,
clusterCaCertificate: cdktf.Fn.base64decode(attributes.certificate),
exec: {
apiVersion: "client.authentication.k8s.io/v1beta1",
args: ["eks", "get-token", "--cluster-name", attributes.name],
command: "aws",
}
}) as singletonKey in stack;
}
}
class ClusterRef extends ClusterBase impl ICluster {
_attributes: ClusterAttributes;
init(attributes: ClusterAttributes) {
this._attributes = attributes;
}
pub attributes(): ClusterAttributes {
return this._attributes;
}
}
pub class Cluster extends ClusterBase impl ICluster {
/** singleton */
pub static getOrCreate(scope: std.IResource): ICluster {
let stack = cdktf.TerraformStack.of(scope);
let uid = "WingEksCluster";
let existing: ICluster? = unsafeCast(stack.node.tryFindChild(uid));
let newCluster = (): ICluster => {
if let attrs = Cluster.tryGetClusterAttributes() {
return new ClusterRef(attrs) as uid in stack;
} else {
let clusterName = "wing-eks-${std.Node.of(scope).addr.substring(0, 6)}";
return new Cluster(clusterName) as uid in stack;
}
};
return existing ?? newCluster();
}
static tryGetClusterAttributes(): ClusterAttributes? {
if !values.Values.has("eks.cluster_name") {
return nil;
}
return ClusterAttributes {
name: values.Values.get("eks.cluster_name"),
certificate: values.Values.get("eks.certificate"),
endpoint: values.Values.get("eks.endpoint"),
};
}
_attributes: ClusterAttributes;
_oidcProviderArn: str;
vpc: vpc.Vpc;
init(clusterName: str) {
let privateSubnetTags = MutMap<str>{};
privateSubnetTags.set("kubernetes.io/role/internal-elb", "1");
privateSubnetTags.set("kubernetes.io/cluster/${clusterName}", "shared");
let publicSubnetTags = MutMap<str>{};
publicSubnetTags.set("kubernetes.io/role/elb", "1");
publicSubnetTags.set("kubernetes.io/cluster/${clusterName}", "shared");
this.vpc = new vpc.Vpc(
privateSubnetTags: privateSubnetTags.copy(),
publicSubnetTags: publicSubnetTags.copy(),
);
let cluster = new cdktf.TerraformHclModule(
source: "terraform-aws-modules/eks/aws",
version: "19.17.1",
variables: {
cluster_name: clusterName,
cluster_version: "1.27",
cluster_endpoint_public_access: true,
cluster_addons: {
"kube-proxy": {},
"vpc-cni": {},
coredns: {
configuration_values: cdktf.Fn.jsonencode({
computeType: "Fargate"
})
},
},
vpc_id: this.vpc.id,
subnet_ids: this.vpc.privateSubnets,
create_cluster_security_group: false,
create_node_security_group: false,
fargate_profiles: {
default: {
name: "default",
selectors: [
{ namespace: "kube-system" },
{ namespace: "default" },
]
}
},
}
) as "eks";
this._attributes = {
name: clusterName,
certificate: cluster.get("cluster_certificate_authority_data"),
endpoint: cluster.get("cluster_endpoint"),
};
this._oidcProviderArn = cluster.get("oidc_provider_arn");
// output the cluster name
new cdktf.TerraformOutput(value: this._attributes.name, description: "eks.cluster_name") as "eks.cluster_name";
new cdktf.TerraformOutput(value: this._attributes.certificate, description: "eks.certificate") as "eks.certificate";
new cdktf.TerraformOutput(value: this._attributes.endpoint, description: "eks.endpoint") as "eks.endpoint";
// install the LB controller to support ingress
this.addLoadBalancerController();
}
pub attributes(): ClusterAttributes {
return this._attributes;
}
addLoadBalancerController() {
let awsInfo = aws_info.Aws.getOrCreate(this);
let serviceAccountName = "aws-load-balancer-controller";
let lbRole = new cdktf.TerraformHclModule(
source: "terraform-aws-modules/iam/aws//modules/iam-role-for-service-accounts-eks",
variables: {
role_name: "eks-lb-role-${this.node.addr}",
attach_load_balancer_controller_policy: true,
oidc_providers: {
main: {
provider_arn: this._oidcProviderArn,
namespace_service_accounts: ["kube-system:${serviceAccountName}"],
}
}
}
) as "lb_role";
// install the k8s terraform provider
let serviceAccount = new eks.serviceAccount.ServiceAccount(
provider: this.kubernetesProvider(),
metadata: {
name: serviceAccountName,
namespace: "kube-system",
labels: {
"app.kubernetes.io/name" => serviceAccountName,
"app.kubernetes.io/component"=> "controller"
},
annotations: {
"eks.amazonaws.com/role-arn" => lbRole.get("iam_role_arn"),
"eks.amazonaws.com/sts-regional-endpoints" => "true"
},
}
);
new helm.release.Release(
provider: this.helmProvider(),
name: "aws-load-balancer-controller",
repository: "https://aws.github.io/eks-charts",
chart: "aws-load-balancer-controller",
namespace: "kube-system",
dependsOn: [serviceAccount],
set: [
{ name: "region", value: awsInfo.region() },
{ name: "vpcId", value: this.vpc.id },
{ name: "serviceAccount.create", value: "false" },
{ name: "serviceAccount.name", value: serviceAccountName },
{ name: "clusterName", value: this._attributes.name },
]
);
}
}