forked from particuleio/terraform-kubernetes-addons
-
Notifications
You must be signed in to change notification settings - Fork 0
/
thanos.tf
379 lines (341 loc) · 12.7 KB
/
thanos.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
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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
locals {
thanos = merge(
local.helm_defaults,
{
name = local.helm_dependencies[index(local.helm_dependencies.*.name, "thanos")].name
chart = local.helm_dependencies[index(local.helm_dependencies.*.name, "thanos")].name
repository = local.helm_dependencies[index(local.helm_dependencies.*.name, "thanos")].repository
chart_version = local.helm_dependencies[index(local.helm_dependencies.*.name, "thanos")].version
namespace = "monitoring"
create_iam_resources_irsa = true
iam_policy_override = null
create_ns = false
enabled = false
default_network_policy = true
default_global_requests = false
default_global_limits = false
create_bucket = false
bucket = "thanos-store-${var.cluster-name}"
bucket_force_destroy = false
generate_ca = false
trusted_ca_content = null
name_prefix = "${var.cluster-name}-thanos"
},
var.thanos
)
values_thanos = <<-VALUES
receive:
enabled: false
pdb:
create: true
minAvailable: 1
serviceAccount:
annotations:
eks.amazonaws.com/role-arn: "${local.thanos["enabled"] && local.thanos["create_iam_resources_irsa"] ? module.iam_assumable_role_thanos.iam_role_arn : ""}"
metrics:
enabled: true
serviceMonitor:
enabled: ${local.kube-prometheus-stack["enabled"] ? "true" : "false"}
query:
extraFlags:
- --query.timeout=5m
- --query.lookback-delta=15m
- --query.replica-label=rule_replica
replicaCount: 2
replicaLabel:
- prometheus_replica
enabled: true
dnsDiscovery:
enabled: true
sidecarsService: ${local.kube-prometheus-stack["name"]}-thanos-discovery
sidecarsNamespace: "${local.kube-prometheus-stack["namespace"]}"
pdb:
create: true
minAvailable: 1
stores: ${jsonencode(concat([for k, v in local.thanos-tls-querier : "dnssrv+_grpc._tcp.${v["name"]}-query-grpc.${local.thanos["namespace"]}.svc.cluster.local"], [for k, v in local.thanos-storegateway : "dnssrv+_grpc._tcp.${v["name"]}-storegateway.${local.thanos["namespace"]}.svc.cluster.local"]))}
queryFrontend:
extraFlags:
- --query-frontend.compress-responses
- --query-range.split-interval=12h
- --labels.split-interval=12h
- --query-range.max-retries-per-request=10
- --labels.max-retries-per-request=10
- --query-frontend.log-queries-longer-than=10s
replicaCount: 2
enabled: true
pdb:
create: true
minAvailable: 1
compactor:
extraFlags:
- --deduplication.replica-label=prometheus_replica
- --deduplication.replica-label=rule_replica
strategyType: Recreate
enabled: true
serviceAccount:
annotations:
eks.amazonaws.com/role-arn: "${local.thanos["enabled"] && local.thanos["create_iam_resources_irsa"] ? module.iam_assumable_role_thanos.iam_role_arn : ""}"
storegateway:
extraFlags:
- --ignore-deletion-marks-delay=24h
replicaCount: 2
enabled: true
serviceAccount:
annotations:
eks.amazonaws.com/role-arn: "${local.thanos["enabled"] && local.thanos["create_iam_resources_irsa"] ? module.iam_assumable_role_thanos.iam_role_arn : ""}"
pdb:
create: true
minAvailable: 1
VALUES
values_thanos_caching = <<-VALUES
queryFrontend:
extraFlags:
- --query-frontend.compress-responses
- --query-range.split-interval=12h
- --labels.split-interval=12h
- --query-range.max-retries-per-request=10
- --labels.max-retries-per-request=10
- --query-frontend.log-queries-longer-than=10s
- |-
--query-range.response-cache-config="config":
"addresses":
- "dnssrv+_memcache._tcp.${local.thanos-memcached["name"]}.${local.thanos-memcached["namespace"]}.svc.cluster.local"
"dns_provider_update_interval": "10s"
"max_async_buffer_size": 10000
"max_async_concurrency": 20
"max_get_multi_batch_size": 0
"max_get_multi_concurrency": 100
"max_idle_connections": 100
"timeout": "500ms"
"type": "memcached"
- |-
--labels.response-cache-config="config":
"addresses":
- "dnssrv+_memcache._tcp.${local.thanos-memcached["name"]}.${local.thanos-memcached["namespace"]}.svc.cluster.local"
"dns_provider_update_interval": "10s"
"max_async_buffer_size": 10000
"max_async_concurrency": 20
"max_get_multi_batch_size": 0
"max_get_multi_concurrency": 100
"max_idle_connections": 100
"timeout": "500ms"
"type": "memcached"
storegateway:
extraFlags:
- --ignore-deletion-marks-delay=24h
- |-
--index-cache.config="config":
"addresses":
- "dnssrv+_memcache._tcp.${local.thanos-memcached["name"]}.${local.thanos-memcached["namespace"]}.svc.cluster.local"
"dns_provider_update_interval": "10s"
"max_async_buffer_size": 10000
"max_async_concurrency": 20
"max_get_multi_batch_size": 0
"max_get_multi_concurrency": 100
"max_idle_connections": 100
"max_item_size": "1MiB"
"timeout": "500ms"
"type": "memcached"
- |-
--store.caching-bucket.config="blocks_iter_ttl": "5m"
"chunk_object_attrs_ttl": "24h"
"chunk_subrange_size": 16000
"chunk_subrange_ttl": "24h"
"config":
"addresses":
- "dnssrv+_memcache._tcp.${local.thanos-memcached["name"]}.${local.thanos-memcached["namespace"]}.svc.cluster.local"
"dns_provider_update_interval": "10s"
"max_async_buffer_size": 10000
"max_async_concurrency": 20
"max_get_multi_batch_size": 0
"max_get_multi_concurrency": 100
"max_idle_connections": 100
"max_item_size": "1MiB"
"timeout": "500ms"
"max_chunks_get_range_requests": 3
"metafile_content_ttl": "24h"
"metafile_doesnt_exist_ttl": "15m"
"metafile_exists_ttl": "2h"
"metafile_max_size": "1MiB"
"type": "memcached"
VALUES
values_store_config = <<-VALUES
objstoreConfig:
type: S3
config:
bucket: ${local.thanos["bucket"]}
region: ${data.aws_region.current.name}
endpoint: s3.${data.aws_region.current.name}.amazonaws.com
sse_config:
type: "SSE-S3"
VALUES
values_thanos_global_requests = <<-VALUES
query:
resources:
requests:
cpu: 25m
memory: 32Mi
queryFrontend:
resources:
requests:
cpu: 25m
memory: 32Mi
compactor:
resources:
requests:
cpu: 50m
memory: 258Mi
storegateway:
resources:
requests:
cpu: 25m
memory: 64Mi
VALUES
values_thanos_global_limits = <<-VALUES
query:
resources:
limits:
memory: 128Mi
queryFrontend:
resources:
limits:
memory: 64Mi
compactor:
resources:
limits:
memory: 2Gi
storegateway:
resources:
limits:
memory: 1Gi
VALUES
}
module "iam_assumable_role_thanos" {
source = "terraform-aws-modules/iam/aws//modules/iam-assumable-role-with-oidc"
version = "~> 5.0"
create_role = local.thanos["enabled"] && local.thanos["create_iam_resources_irsa"]
role_name = local.thanos["name_prefix"]
provider_url = replace(var.eks["cluster_oidc_issuer_url"], "https://", "")
role_policy_arns = local.thanos["enabled"] && local.thanos["create_iam_resources_irsa"] ? [aws_iam_policy.thanos[0].arn] : []
number_of_role_policy_arns = 1
oidc_subjects_with_wildcards = ["system:serviceaccount:${local.thanos["namespace"]}:${local.thanos["name"]}-*"]
tags = local.tags
}
resource "aws_iam_policy" "thanos" {
count = local.thanos["enabled"] && local.thanos["create_iam_resources_irsa"] ? 1 : 0
name = local.thanos["name_prefix"]
policy = local.thanos["iam_policy_override"] == null ? data.aws_iam_policy_document.thanos.json : local.thanos["iam_policy_override"]
tags = local.tags
}
data "aws_iam_policy_document" "thanos" {
statement {
effect = "Allow"
actions = [
"s3:ListBucket"
]
resources = ["arn:${local.arn-partition}:s3:::${local.thanos["bucket"]}"]
}
statement {
effect = "Allow"
actions = [
"s3:*Object"
]
resources = ["arn:${local.arn-partition}:s3:::${local.thanos["bucket"]}/*"]
}
}
module "thanos_bucket" {
create_bucket = local.thanos["enabled"] && local.thanos["create_bucket"]
source = "terraform-aws-modules/s3-bucket/aws"
version = "~> 3.0"
block_public_acls = true
block_public_policy = true
restrict_public_buckets = true
ignore_public_acls = true
force_destroy = local.thanos["bucket_force_destroy"]
bucket = local.thanos["bucket"]
acl = "private"
server_side_encryption_configuration = {
rule = {
apply_server_side_encryption_by_default = {
sse_algorithm = "AES256"
}
}
}
tags = local.tags
}
resource "kubernetes_namespace" "thanos" {
count = local.thanos["enabled"] && local.thanos["create_ns"] ? 1 : 0
metadata {
labels = {
name = local.thanos["namespace"]
"${local.labels_prefix}/component" = "monitoring"
}
name = local.thanos["namespace"]
}
}
resource "helm_release" "thanos" {
count = local.thanos["enabled"] ? 1 : 0
repository = local.thanos["repository"]
name = local.thanos["name"]
chart = local.thanos["chart"]
version = local.thanos["chart_version"]
timeout = local.thanos["timeout"]
force_update = local.thanos["force_update"]
recreate_pods = local.thanos["recreate_pods"]
wait = local.thanos["wait"]
atomic = local.thanos["atomic"]
cleanup_on_fail = local.thanos["cleanup_on_fail"]
dependency_update = local.thanos["dependency_update"]
disable_crd_hooks = local.thanos["disable_crd_hooks"]
disable_webhooks = local.thanos["disable_webhooks"]
render_subchart_notes = local.thanos["render_subchart_notes"]
replace = local.thanos["replace"]
reset_values = local.thanos["reset_values"]
reuse_values = local.thanos["reuse_values"]
skip_crds = local.thanos["skip_crds"]
verify = local.thanos["verify"]
values = compact([
local.values_thanos,
local.values_store_config,
local.thanos["default_global_requests"] ? local.values_thanos_global_requests : null,
local.thanos["default_global_limits"] ? local.values_thanos_global_limits : null,
local.thanos-memcached["enabled"] ? local.values_thanos_caching : null,
local.thanos["extra_values"]
])
namespace = local.thanos["create_ns"] ? kubernetes_namespace.thanos.*.metadata.0.name[count.index] : local.thanos["namespace"]
depends_on = [
helm_release.kube-prometheus-stack,
helm_release.thanos-memcached
]
}
resource "tls_private_key" "thanos-tls-querier-ca-key" {
count = local.thanos["generate_ca"] ? 1 : 0
algorithm = "ECDSA"
ecdsa_curve = "P384"
}
resource "tls_self_signed_cert" "thanos-tls-querier-ca-cert" {
count = local.thanos["generate_ca"] ? 1 : 0
private_key_pem = tls_private_key.thanos-tls-querier-ca-key[0].private_key_pem
is_ca_certificate = true
subject {
common_name = var.cluster-name
organization = var.cluster-name
}
validity_period_hours = 87600
allowed_uses = [
"cert_signing"
]
}
resource "kubernetes_secret" "thanos-ca" {
count = local.thanos["enabled"] && (local.thanos["generate_ca"] || local.thanos["trusted_ca_content"] != null) ? 1 : 0
metadata {
name = "${local.thanos["name"]}-ca"
namespace = local.thanos["create_ns"] ? kubernetes_namespace.thanos.*.metadata.0.name[count.index] : local.thanos["namespace"]
}
data = {
"ca.crt" = local.thanos["generate_ca"] ? tls_self_signed_cert.thanos-tls-querier-ca-cert[count.index].cert_pem : local.thanos["trusted_ca_content"]
}
}
output "thanos_ca" {
value = element(concat(tls_self_signed_cert.thanos-tls-querier-ca-cert[*].cert_pem, [""]), 0)
}