Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable thanos-sidecar for archiving prometheus data to S3 #638

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions k8s/production/prometheus/release.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ spec:
sourceRef:
kind: HelmRepository
name: kube-prometheus-stack

values:
defaultRules:
create: false
Expand Down Expand Up @@ -95,6 +96,8 @@ spec:


prometheus:
thanosService:
enabled: true
ingress:
enabled: false
prometheusSpec:
Expand Down Expand Up @@ -146,6 +149,10 @@ spec:
name: grafana-additional-datasources
valuesKey: values.yaml
optional: false
# See terraform/modules/spack/prometheus.tf
- kind: ConfigMap
name: prometheus-thanos-config
valuesKey: values.yaml


# NOTE:
Expand Down
122 changes: 122 additions & 0 deletions terraform/modules/spack/prometheus.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
resource "aws_s3_bucket" "metrics_bucket" {
bucket = "spack-${var.deployment_name}-prometheus-thanos-metrics"

lifecycle {
prevent_destroy = true
}
}

# Bucket policy that prevents deletion bucket.
resource "aws_s3_bucket_policy" "metrics_bucket" {
bucket = aws_s3_bucket.metrics_bucket.id

policy = jsonencode({
"Version" : "2012-10-17",
"Statement" : [
{
"Principal" : "*"
"Effect" : "Deny",
"Action" : [
"s3:DeleteBucket",
],
"Resource" : aws_s3_bucket.metrics_bucket.arn,
}
]
})
}

resource "aws_iam_policy" "metrics_bucket" {
name = "PrometheusThanosPolicy-${var.deployment_name}"
description = "Managed by Terraform. Grants required permissions for Thanos to read/write to the Prometheus metrics bucket."

# https://docs.gitlab.com/ee/install/aws/manual_install_aws.html#create-an-iam-policy
policy = jsonencode({
"Version" : "2012-10-17",
"Statement" : [
{
"Effect" : "Allow",
"Action" : [
"s3:ListBucket",
"s3:GetObject",
"s3:DeleteObject",
"s3:PutObject",
],
"Resource" : [
aws_s3_bucket.metrics_bucket.arn,
"${aws_s3_bucket.metrics_bucket.arn}/*"
]
}
]
})
}

resource "aws_iam_role" "metrics_bucket" {
name = "PrometheusThanosRole-${var.deployment_name}"
description = "Managed by Terraform. Role for Thanos to assume so that it can access the Prometheus metrics bucket."
assume_role_policy = jsonencode({
"Version" : "2012-10-17",
"Statement" : [
{
"Effect" : "Allow",
"Principal" : {
"Federated" : module.eks.oidc_provider_arn
},
"Action" : "sts:AssumeRoleWithWebIdentity",
"Condition" : {
"StringEquals" : {
"${module.eks.oidc_provider}:aud" : "sts.amazonaws.com"
}
}
}
]
})
}

resource "aws_iam_role_policy_attachment" "metrics_bucket" {
role = aws_iam_role.metrics_bucket.name
policy_arn = aws_iam_policy.metrics_bucket.arn
}

resource "kubectl_manifest" "prometheus_thanos_secret" {
yaml_body = <<-YAML
apiVersion: v1
kind: Secret
metadata:
name: thanos-objstore
namespace: monitoring
stringData:
objstore.yaml: |
type: S3
config:
endpoint: "s3.${data.aws_region.current.name}.amazonaws.com"
bucket: "${aws_s3_bucket.metrics_bucket.id}"
insecure: false
signature_version2: false
trace:
enable: true

YAML
}

resource "kubectl_manifest" "prometheus_thanos_config_map" {
yaml_body = <<-YAML
apiVersion: v1
kind: ConfigMap
metadata:
name: prometheus-thanos-config
namespace: monitoring
data:
values.yaml: |
prometheus:
serviceAccount:
create: true
name: "prometheus-thanos-sa"
annotations:
eks.amazonaws.com/role-arn: ${aws_iam_role.metrics_bucket.arn}
prometheusSpec:
thanos:
objectStorageConfig:
name: ${kubectl_manifest.prometheus_thanos_secret.name}
key: objstore.yaml
YAML
}