-
Notifications
You must be signed in to change notification settings - Fork 6
/
eks-cluster.cfndsl.rb
321 lines (278 loc) · 10 KB
/
eks-cluster.cfndsl.rb
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
CloudFormation do
Condition('KeyNameSet', FnNot(FnEquals(Ref('KeyName'), '')))
Condition("SpotEnabled", FnNot(FnEquals(Ref('SpotPrice'), '')))
tags = []
extra_tags = external_parameters.fetch(:extra_tags, {})
extra_tags.each { |key,value| tags << { Key: FnSub(key), Value: FnSub(value) } }
IAM_Role(:EksClusterRole) {
AssumeRolePolicyDocument service_assume_role_policy('eks')
Path '/'
ManagedPolicyArns([
'arn:aws:iam::aws:policy/AmazonEKSServicePolicy',
'arn:aws:iam::aws:policy/AmazonEKSClusterPolicy'
])
}
AutoScaling_LifecycleHook(:DrainingLifecycleHook) {
AutoScalingGroupName Ref('EksNodeAutoScalingGroup')
HeartbeatTimeout 450
LifecycleTransition 'autoscaling:EC2_INSTANCE_TERMINATING'
}
Lambda_Permission(:DrainingLambdaPermission) {
Action 'lambda:InvokeFunction'
FunctionName FnGetAtt('Drainer', 'Arn')
Principal 'events.amazonaws.com'
SourceArn FnGetAtt('LifecycleEvent', 'Arn')
}
draining_lambda = external_parameters[:draining_lambda]
Events_Rule(:LifecycleEvent) {
Description FnSub("Rule for ${EnvironmentName} eks draining lifecycle hook")
State 'ENABLED'
EventPattern draining_lambda['event']['pattern']
Targets draining_lambda['event']['targets']
}
EC2_SecurityGroup(:EksClusterSecurityGroup) {
VpcId Ref('VPCId')
GroupDescription "EKS Cluster communication with worker nodes"
Tags([{ Key: 'Name', Value: FnSub("${EnvironmentName}-eks-controller")}] + tags)
Metadata({
cfn_nag: {
rules_to_suppress: [
{ id: 'F1000', reason: 'adding rules using cfn resources' }
]
}
})
}
EC2_SecurityGroup('EksNodeSecurityGroup') {
VpcId Ref('VPCId')
GroupDescription "Security group for all nodes in the cluster"
Tags([
{ Key: 'Name', Value: FnSub("${EnvironmentName}-eks-nodes") },
{ Key: FnSub("kubernetes.io/cluster/${EksCluster}"), Value: 'owned' }
] + tags)
Metadata({
cfn_nag: {
rules_to_suppress: [
{ id: 'F1000', reason: 'adding rules using cfn resources' }
]
}
})
}
EC2_SecurityGroupIngress(:NodeSecurityGroupIngress) {
DependsOn 'EksNodeSecurityGroup'
Description 'Allow node to communicate with each other'
GroupId Ref(:EksNodeSecurityGroup)
SourceSecurityGroupId Ref(:EksNodeSecurityGroup)
IpProtocol '-1'
FromPort 0
ToPort 65535
}
EC2_SecurityGroupIngress(:NodeSecurityGroupFromControlPlaneIngress) {
DependsOn 'EksNodeSecurityGroup'
Description 'Allow worker Kubelets and pods to receive communication from the cluster control plane'
GroupId Ref(:EksNodeSecurityGroup)
SourceSecurityGroupId Ref(:EksClusterSecurityGroup)
IpProtocol 'tcp'
FromPort 1025
ToPort 65535
}
EC2_SecurityGroupEgress(:ControlPlaneEgressToNodeSecurityGroup) {
DependsOn 'EksNodeSecurityGroup'
Description 'Allow the cluster control plane to communicate with worker Kubelet and pods'
GroupId Ref(:EksClusterSecurityGroup)
DestinationSecurityGroupId Ref(:EksNodeSecurityGroup)
IpProtocol 'tcp'
FromPort 1025
ToPort 65535
}
EC2_SecurityGroupIngress(:NodeSecurityGroupFromControlPlaneOn443Ingress) {
DependsOn 'EksNodeSecurityGroup'
Description 'Allow pods running extension API servers on port 443 to receive communication from cluster control plane'
GroupId Ref(:EksNodeSecurityGroup)
SourceSecurityGroupId Ref(:EksClusterSecurityGroup)
IpProtocol 'tcp'
FromPort 443
ToPort 443
}
EC2_SecurityGroupEgress(:ControlPlaneEgressToNodeSecurityGroupOn443) {
DependsOn 'EksNodeSecurityGroup'
Description 'Allow the cluster control plane to communicate with pods running extension API servers on port 443'
GroupId Ref(:EksClusterSecurityGroup)
DestinationSecurityGroupId Ref(:EksNodeSecurityGroup)
IpProtocol 'tcp'
FromPort 443
ToPort 443
}
EC2_SecurityGroupIngress(:ClusterControlPlaneSecurityGroupIngress) {
DependsOn 'EksNodeSecurityGroup'
Description 'Allow pods to communicate with the cluster API Server'
GroupId Ref(:EksClusterSecurityGroup)
SourceSecurityGroupId Ref(:EksNodeSecurityGroup)
IpProtocol 'tcp'
ToPort 443
FromPort 443
}
cluster_name = external_parameters.fetch(:cluster_name, '')
eks_version = external_parameters.fetch(:eks_version, nil)
EKS_Cluster(:EksCluster) {
Name FnSub(cluster_name) unless cluster_name.empty?
ResourcesVpcConfig({
SecurityGroupIds: [ Ref(:EksClusterSecurityGroup) ],
SubnetIds: FnSplit(',', Ref('SubnetIds'))
})
RoleArn FnGetAtt(:EksClusterRole, :Arn)
Version eks_version unless eks_version.nil?
}
iam = external_parameters[:iam]
IAM_Role(:EksNodeRole) {
AssumeRolePolicyDocument service_assume_role_policy(iam['services'])
Path '/'
ManagedPolicyArns(iam['managed_policies'])
Policies(iam_role_policies(iam['policies'])) if iam.has_key?('policies')
}
IAM_InstanceProfile(:EksNodeInstanceProfile) do
Path '/'
Roles [Ref(:EksNodeRole)]
end
# Setup userdata string
node_userdata = "#!/bin/bash\nset -o xtrace\n"
node_userdata << external_parameters.fetch(:eks_bootstrap, '')
node_userdata << userdata = external_parameters.fetch(:userdata, '')
node_userdata << cfnsignal = external_parameters.fetch(:cfnsignal, '')
launch_template_tags = [
{ Key: 'Name', Value: FnSub("${EnvironmentName}-eks-node-xx") },
{ Key: FnSub("kubernetes.io/cluster/${EksCluster}"), Value: 'owned' }
]
launch_template_tags += tags
template_data = {
SecurityGroupIds: [ Ref(:EksNodeSecurityGroup) ],
TagSpecifications: [
{ ResourceType: 'instance', Tags: launch_template_tags },
{ ResourceType: 'volume', Tags: launch_template_tags }
],
UserData: FnBase64(FnSub(node_userdata)),
IamInstanceProfile: { Name: Ref(:EksNodeInstanceProfile) },
KeyName: FnIf('KeyNameSet', Ref('KeyName'), Ref('AWS::NoValue')),
ImageId: Ref('ImageId'),
Monitoring: { Enabled: detailed_monitoring },
InstanceType: Ref('InstanceType')
}
spot = external_parameters.fetch(:spot, {})
unless spot.empty?
spot_options = {
MarketType: 'spot',
SpotOptions: {
SpotInstanceType: (defined?(spot['type']) ? spot['type'] : 'one-time'),
MaxPrice: FnSub(spot['price'])
}
}
template_data[:InstanceMarketOptions] = FnIf('SpotEnabled', spot_options, Ref('AWS::NoValue'))
end
volumes = []
volume_size = external_parameters.fetch(:volume_size, nil)
unless volume_size.nil?
volumes << {
DeviceName: '/dev/xvda',
Ebs: {
VolumeSize: volume_size
}
}
template_data[:BlockDeviceMappings] = volumes
end
EC2_LaunchTemplate(:EksNodeLaunchTemplate) {
LaunchTemplateData(template_data)
}
add_ons = external_parameters.fetch(:add_ons, {})
add_ons.each do | add_on, config |
safe_addon_name = add_on.dup.gsub!('-','') || add_on
EKS_Addon("#{safe_addon_name.capitalize}Addon") {
AddonName add_on
AddonVersion config['version']
ResolveConflicts config['resolve_conflicts'] if config.has_key?('resolve_conflicts')
ClusterName Ref(:EksCluster)
Tags tags
}
end unless add_ons.empty?
asg_tags = [
{ Key: FnSub("k8s.io/cluster/${EksCluster}"), Value: 'owned' },
{ Key: 'k8s.io/cluster-autoscaler/enabled', Value: Ref('EnableScaling') }
]
asg_tags = tags.clone.map(&:clone).concat(asg_tags).uniq.each {|tag| tag[:PropagateAtLaunch] = false }
pause_time = external_parameters.fetch(:pause_time, 'PT5M')
max_batch_size = external_parameters.fetch(:max_batch_size, '1')
AutoScaling_AutoScalingGroup(:EksNodeAutoScalingGroup) {
UpdatePolicy(:AutoScalingRollingUpdate, {
MaxBatchSize: max_batch_size,
MinInstancesInService: FnIf('SpotEnabled', 0, Ref('DesiredCapacity')),
SuspendProcesses: %w(HealthCheck ReplaceUnhealthy AZRebalance AlarmNotification ScheduledActions),
PauseTime: pause_time
})
DesiredCapacity Ref('DesiredCapacity')
MinSize Ref('MinSize')
MaxSize Ref('MaxSize')
VPCZoneIdentifiers FnSplit(',', Ref('SubnetIds'))
LaunchTemplate({
LaunchTemplateId: Ref(:EksNodeLaunchTemplate),
Version: FnGetAtt(:EksNodeLaunchTemplate, :LatestVersionNumber)
})
Tags asg_tags
}
auth_type = external_parameters.fetch(:auth_type, 'STANDARD')
node_type = external_parameters.fetch(:node_type, 'EC2_LINUX')
#provides elmer with cluster admin access
EKS_AccessEntry(:AccessEntryElmerClusterAdmin) {
Tags ([
{
Key: 'simple key',
Value: 'support'
}
])
AccessPolicies([
{
AccessScope: {
Type:'cluster'
},
PolicyArn: 'arn:aws:eks::aws:cluster-access-policy/AmazonEKSClusterAdminPolicy'
}
])
ClusterName Ref(:EksCluster)
KubernetesGroups ['cluster-admin']
PrincipalArn FnJoin('', ['arn:aws:iam::',Ref('AWS::AccountId'),':role/iamelmer/IAMElmerRole_admin'])
Type auth_type
}
# allow nodes to join the cluster, also provides access to nodes resource (console, api, k8s)
EKS_AccessEntry(:AccessEntryAdminNode) {
ClusterName Ref(:EksCluster)
PrincipalArn FnGetAtt(:EksNodeRole, 'Arn')
Type node_type
}
max_cluster_roles = external_parameters.fetch(:max_cluster_roles, 0)
max_cluster_roles.times do | cluster_role|
# cluster_admin_role_arns.split(",").each_with_index do |cluster_admin_arn, index|
EKS_AccessEntry("AccessEntryAdmin#{cluster_role}") {
AccessPolicies([
{
AccessScope: {
Type:'cluster'
},
PolicyArn: 'arn:aws:eks::aws:cluster-access-policy/AmazonEKSClusterAdminPolicy'
}
])
ClusterName Ref(:EksCluster)
KubernetesGroups ['cluster-admin']
PrincipalArn Ref("ClusterRoleAdmin#{cluster_role}")
Type auth_type
}
end
Output(:EksNodeSecurityGroup) {
Value(Ref(:EksNodeSecurityGroup))
}
Output(:EksClusterName) {
Value(Ref(:EksCluster))
}
Output(:DrainingLambdaRole) {
Value(FnGetAtt(:LambdaRoleDraining, :Arn))
}
Output(:EksNodeRole) {
Value(FnGetAtt(:EksNodeRole, :Arn))
}
end