-
Notifications
You must be signed in to change notification settings - Fork 2
/
eks.tf
133 lines (100 loc) · 3.29 KB
/
eks.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
locals {
worker_role = <<EOF
- rolearn: ${aws_iam_role.eks_worker.arn}
username: system:node:{{EC2PrivateDNSName}}
groups:
- system:bootstrappers
- system:nodes
EOF
add_configmap_roles = join("\n",
[ for role in var.add_configmap_roles :
templatefile("${path.module}/resources/configmap_aws_auth.tpl",
{
iam_entity = "role"
arn = role.role_arn,
k8s_user = role.k8s_user,
k8s_groups = role.k8s_groups
}
) ]
)
add_configmap_users = join("\n",
[ for user in var.add_configmap_users :
templatefile("${path.module}/resources/configmap_aws_auth.tpl",
{
iam_entity = "user"
arn = user.user_arn,
k8s_user = user.k8s_user,
k8s_groups = user.k8s_groups
}
) ]
)
}
resource "aws_eks_cluster" "main" {
name = var.cluster_name
version = var.cluster_version
role_arn = aws_iam_role.eks_master.arn
enabled_cluster_log_types = var.enabled_cluster_log_types
vpc_config {
subnet_ids = var.subnets_ids
security_group_ids = [aws_security_group.eks_master.id]
endpoint_private_access = var.eks_api_private
endpoint_public_access = var.eks_api_private ? false : true
}
tags = merge({Name="${var.cluster_name}-master"}, var.eks_tags)
}
resource "time_sleep" "wait_20_seconds" {
depends_on = [aws_eks_cluster.main]
create_duration = "20s"
}
resource "kubernetes_config_map" "aws_auth" {
count = var.aws_auth_ignore_changes ? 1 : 0
metadata {
name = "aws-auth"
namespace = "kube-system"
}
data = {
mapRoles = var.add_configmap_roles == [] ? local.worker_role : format("${local.worker_role}%s", local.add_configmap_roles)
mapUsers = local.add_configmap_users
}
lifecycle {
ignore_changes = [data]
}
depends_on = [time_sleep.wait_20_seconds]
}
resource "kubernetes_config_map" "aws_auth_without_ignore" {
count = var.aws_auth_ignore_changes ? 0 : 1
metadata {
name = "aws-auth"
namespace = "kube-system"
}
data = {
mapRoles = var.add_configmap_roles == [] ? local.worker_role : format("${local.worker_role}%s", local.add_configmap_roles)
mapUsers = local.add_configmap_users
}
depends_on = [time_sleep.wait_20_seconds]
}
# Addons
resource "aws_eks_addon" "this" {
for_each = var.eks_addons
cluster_name = var.cluster_name
addon_name = each.key
addon_version = each.value["version"]
configuration_values = try(jsonencode(each.value["configuration_values"]), null)
service_account_role_arn = try(each.value["service_account_role_arn"], null)
resolve_conflicts = try(each.value["resolve_conflicts"], "OVERWRITE")
}
# IRSA
data "tls_certificate" "cert" {
count = var.enable_irsa ? 1 : 0
url = aws_eks_cluster.main.identity[0].oidc[0].issuer
}
resource "aws_iam_openid_connect_provider" "oidc_provider" {
count = var.enable_irsa ? 1 : 0
client_id_list = distinct(compact(concat(["sts.amazonaws.com"], var.openid_connect_audiences)))
thumbprint_list = concat(data.tls_certificate.cert[0].certificates[*].sha1_fingerprint, var.custom_oidc_thumbprints)
url = aws_eks_cluster.main.identity[0].oidc[0].issuer
tags = merge(
{ Name = "${var.cluster_name}-eks-irsa" },
var.eks_tags
)
}