From 6fb210ef9e88c7b461a68363ddf3d9829eab0e86 Mon Sep 17 00:00:00 2001 From: Paulin Todev Date: Fri, 7 Nov 2025 17:22:27 +0000 Subject: [PATCH 1/8] Add a new mimir.alerts.kubernetes component --- .github/ISSUE_TEMPLATE/blank.yaml | 1 + .github/ISSUE_TEMPLATE/bug_report.yaml | 1 + .github/ISSUE_TEMPLATE/docs.yaml | 1 + .github/ISSUE_TEMPLATE/feature_request.yaml | 1 + .github/ISSUE_TEMPLATE/proposal.yaml | 1 + .github/workflows/integration-tests-k8s.yml | 23 + CHANGELOG.md | 2 + Makefile | 7 +- .../mimir/mimir.alerts.kubernetes.md | 494 ++++++++++++++++++ go.mod | 37 +- go.sum | 62 +-- .../tests/mimir-alerts-kubernetes/k8s_test.go | 92 ++++ .../mimir-alerts-kubernetes/kubernetes.yml | 229 ++++++++ .../tests/mimir-alerts-kubernetes/setup.sh | 13 + .../cmd/integration-tests-k8s/util/util.go | 105 ++++ internal/component/all/all.go | 1 + .../mimir/alerts/kubernetes/alerts.go | 349 +++++++++++++ .../mimir/alerts/kubernetes/alerts_test.go | 201 +++++++ .../mimir/alerts/kubernetes/debug.go | 6 + .../mimir/alerts/kubernetes/events.go | 280 ++++++++++ .../mimir/alerts/kubernetes/events_test.go | 445 ++++++++++++++++ .../mimir/alerts/kubernetes/health.go | 32 ++ .../mimir/alerts/kubernetes/types.go | 35 ++ .../mimir/rules/kubernetes/events.go | 2 +- .../mimir/rules/kubernetes/events_test.go | 2 +- .../component/mimir/rules/kubernetes/rules.go | 2 +- internal/component/mimir/util/util.go | 36 ++ .../configgen/config_gen_podmonitor.go | 8 +- .../configgen/config_gen_podmonitor_test.go | 18 +- .../operator/configgen/config_gen_probe.go | 4 +- .../configgen/config_gen_probe_test.go | 20 +- .../config_gen_servicemonitor_test.go | 4 +- internal/mimir/client/alerts.go | 45 ++ internal/mimir/client/alerts_test.go | 66 +++ internal/mimir/client/client.go | 7 +- .../testdata/alertmanager/conf.good.yml | 117 +++++ .../testdata/alertmanager/response.good.yml | 249 +++++++++ operations/helm/charts/alloy/CHANGELOG.md | 4 + operations/helm/charts/alloy/README.md | 11 +- operations/helm/charts/alloy/values.yaml | 4 + .../alloy/templates/rbac.yaml | 8 + .../clustering/alloy/templates/rbac.yaml | 8 + .../alloy/templates/rbac.yaml | 8 + .../alloy/templates/rbac.yaml | 8 + .../alloy/templates/rbac.yaml | 8 + .../alloy/templates/rbac.yaml | 8 + .../alloy/templates/rbac.yaml | 8 + .../alloy/templates/rbac.yaml | 8 + .../alloy/templates/rbac.yaml | 8 + .../alloy/templates/rbac.yaml | 8 + .../alloy/templates/rbac.yaml | 8 + .../alloy/templates/rbac.yaml | 8 + .../alloy/templates/rbac.yaml | 8 + .../alloy/templates/rbac.yaml | 8 + .../alloy/templates/rbac.yaml | 8 + .../alloy/templates/rbac.yaml | 8 + .../custom-config/alloy/templates/rbac.yaml | 8 + .../default-values/alloy/templates/rbac.yaml | 8 + .../alloy/templates/rbac.yaml | 8 + .../alloy/templates/rbac.yaml | 8 + .../alloy/templates/rbac.yaml | 8 + .../tests/envFrom/alloy/templates/rbac.yaml | 8 + .../existing-config/alloy/templates/rbac.yaml | 8 + .../tests/extra-env/alloy/templates/rbac.yaml | 8 + .../extra-manifests/alloy/templates/rbac.yaml | 8 + .../extra-ports/alloy/templates/rbac.yaml | 8 + .../faro-ingress/alloy/templates/rbac.yaml | 8 + .../alloy/templates/rbac.yaml | 8 + .../alloy/templates/rbac.yaml | 8 + .../host-alias/alloy/templates/rbac.yaml | 8 + .../initcontainers/alloy/templates/rbac.yaml | 8 + .../lifecycle-hooks/alloy/templates/rbac.yaml | 8 + .../livinessprobe/alloy/templates/rbac.yaml | 8 + .../alloy/templates/rbac.yaml | 8 + .../alloy/templates/rbac.yaml | 8 + .../alloy/templates/rbac.yaml | 8 + .../tests/nonroot/alloy/templates/rbac.yaml | 8 + .../pod_annotations/alloy/templates/rbac.yaml | 8 + .../readinessprobe/alloy/templates/rbac.yaml | 8 + .../alloy/templates/rbac.yaml | 8 + .../tests/sidecars/alloy/templates/rbac.yaml | 8 + .../alloy/templates/rbac.yaml | 8 + .../alloy/templates/rbac.yaml | 8 + .../with-digests/alloy/templates/rbac.yaml | 8 + 84 files changed, 3289 insertions(+), 80 deletions(-) create mode 100644 .github/workflows/integration-tests-k8s.yml create mode 100644 docs/sources/reference/components/mimir/mimir.alerts.kubernetes.md create mode 100644 internal/cmd/integration-tests-k8s/tests/mimir-alerts-kubernetes/k8s_test.go create mode 100644 internal/cmd/integration-tests-k8s/tests/mimir-alerts-kubernetes/kubernetes.yml create mode 100755 internal/cmd/integration-tests-k8s/tests/mimir-alerts-kubernetes/setup.sh create mode 100644 internal/cmd/integration-tests-k8s/util/util.go create mode 100644 internal/component/mimir/alerts/kubernetes/alerts.go create mode 100644 internal/component/mimir/alerts/kubernetes/alerts_test.go create mode 100644 internal/component/mimir/alerts/kubernetes/debug.go create mode 100644 internal/component/mimir/alerts/kubernetes/events.go create mode 100644 internal/component/mimir/alerts/kubernetes/events_test.go create mode 100644 internal/component/mimir/alerts/kubernetes/health.go create mode 100644 internal/component/mimir/alerts/kubernetes/types.go create mode 100644 internal/component/mimir/util/util.go create mode 100644 internal/mimir/client/alerts.go create mode 100644 internal/mimir/client/alerts_test.go create mode 100644 internal/mimir/client/testdata/alertmanager/conf.good.yml create mode 100644 internal/mimir/client/testdata/alertmanager/response.good.yml diff --git a/.github/ISSUE_TEMPLATE/blank.yaml b/.github/ISSUE_TEMPLATE/blank.yaml index 9d6245a49e8..6a7a0d1a6b0 100644 --- a/.github/ISSUE_TEMPLATE/blank.yaml +++ b/.github/ISSUE_TEMPLATE/blank.yaml @@ -78,6 +78,7 @@ body: - loki.source.syslog - loki.source.windowsevent - loki.write + - mimir.alerts.kubernetes - mimir.rules.kubernetes - otelcol.auth.basic - otelcol.auth.bearer diff --git a/.github/ISSUE_TEMPLATE/bug_report.yaml b/.github/ISSUE_TEMPLATE/bug_report.yaml index 76be2e90ebc..7fc726d80fc 100644 --- a/.github/ISSUE_TEMPLATE/bug_report.yaml +++ b/.github/ISSUE_TEMPLATE/bug_report.yaml @@ -78,6 +78,7 @@ body: - loki.source.syslog - loki.source.windowsevent - loki.write + - mimir.alerts.kubernetes - mimir.rules.kubernetes - otelcol.auth.basic - otelcol.auth.bearer diff --git a/.github/ISSUE_TEMPLATE/docs.yaml b/.github/ISSUE_TEMPLATE/docs.yaml index 407b39a686d..e69a0a34300 100644 --- a/.github/ISSUE_TEMPLATE/docs.yaml +++ b/.github/ISSUE_TEMPLATE/docs.yaml @@ -81,6 +81,7 @@ body: - loki.source.syslog - loki.source.windowsevent - loki.write + - mimir.alerts.kubernetes - mimir.rules.kubernetes - otelcol.auth.basic - otelcol.auth.bearer diff --git a/.github/ISSUE_TEMPLATE/feature_request.yaml b/.github/ISSUE_TEMPLATE/feature_request.yaml index aff80db9c0b..1f0956bd518 100644 --- a/.github/ISSUE_TEMPLATE/feature_request.yaml +++ b/.github/ISSUE_TEMPLATE/feature_request.yaml @@ -78,6 +78,7 @@ body: - loki.source.syslog - loki.source.windowsevent - loki.write + - mimir.alerts.kubernetes - mimir.rules.kubernetes - otelcol.auth.basic - otelcol.auth.bearer diff --git a/.github/ISSUE_TEMPLATE/proposal.yaml b/.github/ISSUE_TEMPLATE/proposal.yaml index 77eafe2ec25..1adf8f6b83f 100644 --- a/.github/ISSUE_TEMPLATE/proposal.yaml +++ b/.github/ISSUE_TEMPLATE/proposal.yaml @@ -78,6 +78,7 @@ body: - loki.source.syslog - loki.source.windowsevent - loki.write + - mimir.alerts.kubernetes - mimir.rules.kubernetes - otelcol.auth.basic - otelcol.auth.bearer diff --git a/.github/workflows/integration-tests-k8s.yml b/.github/workflows/integration-tests-k8s.yml new file mode 100644 index 00000000000..77789c30fb7 --- /dev/null +++ b/.github/workflows/integration-tests-k8s.yml @@ -0,0 +1,23 @@ +name: Integration Tests on Kubernetes +on: + push: + branches: + - main + pull_request: + +permissions: + contents: read + +jobs: + run_tests: + runs-on: + labels: github-hosted-ubuntu-x64-large + steps: + - name: Checkout code + uses: actions/checkout@v4 + - name: Setup Go + uses: actions/setup-go@v5 + with: + go-version-file: go.mod + - name: Run tests + run: make integration-test-k8s diff --git a/CHANGELOG.md b/CHANGELOG.md index 6f24a497b08..e65e0ae83cc 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -85,6 +85,8 @@ v1.12.0-rc.0 - The `otelcol.processor.servicegraph` component now supports defining the maximum number of buckets for generated exponential histograms. - See the upstream [core][https://github.com/open-telemetry/opentelemetry-collector/blob/v0.139.0/CHANGELOG.md] and [contrib][https://github.com/open-telemetry/opentelemetry-collector-contrib/blob/v0.139.0/CHANGELOG.md] changelogs for more details. +- A new `mimir.alerts.kubernetes` component which discovers `AlertmanagerConfig` Kubernetes resources and loads them into a Mimir instance. (@ptodev) + ### Enhancements - Add per-application rate limiting with the `strategy` attribute in the `faro.receiver` component, to prevent one application from consuming the rate limit quota of others. (@hhertout) diff --git a/Makefile b/Makefile index 9ffaffaadb1..fdba97cf948 100644 --- a/Makefile +++ b/Makefile @@ -158,7 +158,7 @@ run-alloylint: alloylint # more for packages that exclude tests via //go:build !race due to known race detection issues. The # final command runs tests for syntax module. test: - $(GO_ENV) go test $(GO_FLAGS) -race $(shell go list ./... | grep -v /integration-tests/) + $(GO_ENV) go test $(GO_FLAGS) -race $(shell go list ./... | grep -v -E '/integration-tests/|/integration-tests-k8s/') $(GO_ENV) go test $(GO_FLAGS) ./internal/static/integrations/node_exporter $(GO_ENV) cd ./syntax && go test -race ./... @@ -180,6 +180,11 @@ test-pyroscope: cd ./internal/component/pyroscope/util/internal/cmd/playground/ && \ $(GO_ENV) go build . +.PHONY: integration-test-k8s +integration-test-k8s: alloy-image + cd ./internal/cmd/integration-tests-k8s/ && \ + $(GO_ENV) go test -timeout 10m ./... + # # Targets for building binaries # diff --git a/docs/sources/reference/components/mimir/mimir.alerts.kubernetes.md b/docs/sources/reference/components/mimir/mimir.alerts.kubernetes.md new file mode 100644 index 00000000000..73355dd544b --- /dev/null +++ b/docs/sources/reference/components/mimir/mimir.alerts.kubernetes.md @@ -0,0 +1,494 @@ +--- +canonical: https://grafana.com/docs/alloy/latest/reference/components/mimir/mimir.alerts.kubernetes/ +aliases: + - ../mimir.alerts.kubernetes/ # /docs/alloy/latest/reference/components/mimir.alerts.kubernetes/ +description: Learn about mimir.alerts.kubernetes +labels: + stage: experimental + products: + - oss +title: mimir.alerts.kubernetes +--- + +# `mimir.alerts.kubernetes` + +{{< docs/shared lookup="stability/experimental.md" source="alloy" version="" >}} + +`mimir.alerts.kubernetes` discovers `AlertmanagerConfig` Kubernetes resources and loads them into a Mimir instance. + +* You can specify multiple `mimir.alerts.kubernetes` components by giving them different labels. +* [Kubernetes label selectors][] can be used to limit the `Namespace` and `AlertmanagerConfig` resources considered during reconciliation. +* Compatible with the Alertmanager APIs of Grafana Mimir, Grafana Cloud, and Grafana Enterprise Metrics. +* Compatible with the `AlertmanagerConfig` CRD from the [`prometheus-operator`][prometheus-operator]. +* This component accesses the Kubernetes REST API from [within a Pod][]. + +{{< admonition type="note" >}} +This component requires [Role-based access control (RBAC)][] to be set up in Kubernetes in order for {{< param "PRODUCT_NAME" >}} to access it via the Kubernetes REST API. + +[Role-based access control (RBAC)]: https://kubernetes.io/docs/reference/access-authn-authz/rbac/ +{{< /admonition >}} + +`mimir.alerts.kubernetes` does not support [clustering][clustered mode]. +[clustered mode]: ../../../../get-started/clustering/ + +[Kubernetes label selectors]: https://kubernetes.io/docs/concepts/overview/working-with-objects/labels/#label-selectors +[prometheus-operator]: https://prometheus-operator.dev/ +[within a Pod]: https://kubernetes.io/docs/tasks/run-application/access-api-from-pod/ + +## Usage + +```alloy +mimir.alerts.kubernetes "