-
Notifications
You must be signed in to change notification settings - Fork 1
/
cluster-autoscaler.tf
152 lines (138 loc) · 5.45 KB
/
cluster-autoscaler.tf
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
resource "helm_release" "cluster_autoscaler" {
depends_on = [
var.module_depends_on
]
count = var.cluster_autoscaler_enabled ? 1 - local.argocd_enabled : 0
name = local.cluster_autoscaler_name
repository = local.cluster_autoscaler_chart_repository
chart = local.cluster_autoscaler_chart
version = var.cluster_autoscaler_chart_version
namespace = local.namespace
timeout = 1200
dynamic "set" {
for_each = local.cluster_autoscaler_conf
content {
name = set.key
value = set.value
}
}
}
module "iam_assumable_role_admin" {
source = "terraform-aws-modules/iam/aws//modules/iam-assumable-role-with-oidc"
version = "~> v3.6.0"
create_role = var.cluster_autoscaler_enabled
role_name = "${var.cluster_name}_cluster-autoscaler"
provider_url = replace(data.aws_eks_cluster.this.identity[0].oidc[0].issuer, "https://", "")
role_policy_arns = [aws_iam_policy.cluster_autoscaler[0].arn]
oidc_fully_qualified_subjects = ["system:serviceaccount:${local.namespace}:aws-cluster-autoscaler"]
tags = var.tags
}
resource "aws_iam_policy" "cluster_autoscaler" {
depends_on = [
var.module_depends_on
]
count = var.cluster_autoscaler_enabled ? 1 : 0
name_prefix = "cluster-autoscaler"
description = "EKS cluster-autoscaler policy for cluster ${data.aws_eks_cluster.this.id}"
policy = data.aws_iam_policy_document.cluster_autoscaler.json
}
data "aws_iam_policy_document" "cluster_autoscaler" {
statement {
sid = "clusterAutoscalerAll"
effect = "Allow"
actions = [
"autoscaling:DescribeAutoScalingGroups",
"autoscaling:DescribeAutoScalingInstances",
"autoscaling:DescribeLaunchConfigurations",
"autoscaling:DescribeTags",
"ec2:DescribeLaunchTemplateVersions",
]
resources = ["*"]
}
statement {
sid = "clusterAutoscalerOwn"
effect = "Allow"
actions = [
"autoscaling:SetDesiredCapacity",
"autoscaling:TerminateInstanceInAutoScalingGroup",
"autoscaling:UpdateAutoScalingGroup",
]
resources = ["*"]
condition {
test = "StringEquals"
variable = "autoscaling:ResourceTag/kubernetes.io/cluster/${data.aws_eks_cluster.this.id}"
values = ["owned"]
}
condition {
test = "StringEquals"
variable = "autoscaling:ResourceTag/k8s.io/cluster-autoscaler/enabled"
values = ["true"]
}
}
}
resource "local_file" "cluster_autoscaler" {
count = var.cluster_autoscaler_enabled ? local.argocd_enabled : 0
content = yamlencode(local.cluster_autoscaler_app)
filename = "${var.argocd.path}/${local.cluster_autoscaler_name}.yaml"
}
locals {
cluster_autoscaler_chart_repository = "https://kubernetes.github.io/autoscaler"
cluster_autoscaler_name = "aws-cluster-autoscaler"
cluster_autoscaler_chart = "cluster-autoscaler"
cluster_autoscaler_app = {
"apiVersion" = "argoproj.io/v1alpha1"
"kind" = "Application"
"metadata" = {
"name" = local.cluster_autoscaler_name
"namespace" = var.argocd.namespace
}
"spec" = {
"destination" = {
"namespace" = local.namespace
"server" = "https://kubernetes.default.svc"
}
"project" = "default"
"source" = {
"repoURL" = local.cluster_autoscaler_chart_repository
"targetRevision" = var.cluster_autoscaler_chart_version
"chart" = local.cluster_autoscaler_chart
"helm" = {
"parameters" = values({
for key, value in local.cluster_autoscaler_conf :
key => {
"name" = key
"value" = tostring(value)
}
})
}
}
"syncPolicy" = {
"automated" = {
"prune" = true
"selfHeal" = true
}
"syncOptions" = {
"createNamespace" = true
}
}
}
}
cluster_autoscaler_conf = merge(local.cluster_autoscaler_conf_defaults, var.cluster_autoscaler_conf)
cluster_autoscaler_conf_defaults = {
"cloudProvider" = "aws"
"image.repository" = "us.gcr.io/k8s-artifacts-prod/autoscaling/cluster-autoscaler"
"image.tag" = "v${var.cluster_version}.1" # Make sure it matches the version of the cluster
"autoDiscovery.clusterName" = var.cluster_name,
"autoDiscovery.enabled" = true
"awsRegion" = data.aws_region.current.name,
"extraArgs.balance-similar-node-groups" = true,
"extraArgs.scale-down-enabled" = true,
"rbac.create" = true,
"rbac.serviceAccount.annotations.eks\\.amazonaws\\.com/role-arn" = module.iam_assumable_role_admin.this_iam_role_arn
"rbac.pspEnabled" = true,
"resources.limits.cpu" = "200m",
"resources.limits.memory" = "500Mi",
"resources.requests.cpu" = "100m",
"resources.requests.memory" = "500Mi",
}
}