forked from lacework/terraform-kubernetes-agent
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lacework_cluster.tf
180 lines (151 loc) · 4.56 KB
/
lacework_cluster.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
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
locals {
cluster_config_data = templatefile("${path.module}/config_cluster.tmpl", {
lacework_cluster_name = var.lacework_cluster_name
lacework_cluster_region = var.lacework_cluster_region
lacework_cluster_type = var.lacework_cluster_type
lacework_server_url = var.lacework_server_url
})
cluster_config_name = "${var.lacework_config_name}-${random_id.cluster_config_name_tail.hex}"
merged_cluster_config = var.enable_cluster_agent ? yamlencode(merge(yamldecode(local.cluster_config_data), var.lacework_cluster_configuration)) : ""
}
resource "random_id" "cluster_config_name_tail" {
byte_length = 8
keepers = {
data = local.merged_cluster_config
}
}
resource "kubernetes_service_account" "lacework_k8s_collector" {
count = var.enable_cluster_agent ? 1 : 0
metadata {
name = "${var.lacework_agent_name}-cluster-sa"
namespace = var.namespace
}
}
resource "kubernetes_cluster_role" "lacework_k8s_collector" {
count = var.enable_cluster_agent ? 1 : 0
metadata {
name = "${var.lacework_agent_name}-cluster-role"
}
rule {
api_groups = ["*"]
resources = ["*"]
verbs = ["get", "list"]
}
}
resource "kubernetes_cluster_role_binding" "lacework_k8s_collector" {
count = var.enable_cluster_agent ? 1 : 0
metadata {
name = "${var.lacework_agent_name}-cluster-role-binding"
}
role_ref {
api_group = "rbac.authorization.k8s.io"
kind = "ClusterRole"
name = "${var.lacework_agent_name}-cluster-role"
}
subject {
kind = "ServiceAccount"
name = "${var.lacework_agent_name}-cluster-sa"
namespace = var.namespace
}
depends_on = [
kubernetes_service_account.lacework_k8s_collector,
kubernetes_cluster_role.lacework_k8s_collector
]
}
resource "kubernetes_secret" "lacework_k8s_collector" {
count = var.enable_cluster_agent ? 1 : 0
metadata {
name = local.cluster_config_name
namespace = var.namespace
labels = {
tier = "monitoring"
app = "${var.lacework_agent_name}-cluster"
}
}
data = {
"config.yaml" = local.merged_cluster_config
}
}
resource "kubernetes_deployment" "lacework_k8s_collector" {
count = var.enable_cluster_agent ? 1 : 0
metadata {
name = "${var.lacework_agent_name}-cluster"
namespace = var.namespace
labels = {
tier = "monitoring"
app = "${var.lacework_agent_name}-cluster"
}
}
spec {
selector {
match_labels = {
name = "${var.lacework_agent_name}-cluster"
}
}
template {
metadata {
labels = {
name = "${var.lacework_agent_name}-cluster"
}
annotations = {
lacework_config_version = kubernetes_secret.lacework_k8s_collector[0].metadata.0.resource_version
}
}
spec {
service_account_name = "${var.lacework_agent_name}-cluster-sa"
termination_grace_period_seconds = 20
container {
name = "${var.lacework_agent_name}-cluster"
image = var.lacework_cluster_image
image_pull_policy = var.lacework_cluster_image_pull_policy
env {
name = "LaceworkAccessToken"
value_from {
secret_key_ref {
name = "${var.lacework_agent_name}-access-token"
key = "agent-access-token"
}
}
}
resources {
requests = {
cpu = var.lacework_cluster_cpu_request
memory = var.lacework_cluster_mem_request
}
limits = {
cpu = var.lacework_cluster_cpu_limit
memory = var.lacework_cluster_mem_limit
}
}
security_context {
privileged = false
run_as_non_root = true
run_as_user = 5001
read_only_root_filesystem = true
allow_privilege_escalation = false
}
volume_mount {
name = "cfgmap"
mount_path = "/config"
}
}
volume {
name = "cfgmap"
secret {
secret_name = local.cluster_config_name
items {
key = "config.yaml"
path = "config.yaml"
}
}
}
}
}
}
depends_on = [
kubernetes_service_account.lacework_k8s_collector,
kubernetes_cluster_role.lacework_k8s_collector,
kubernetes_secret.lacework_k8s_collector,
kubernetes_cluster_role_binding.lacework_k8s_collector,
]
}