Skip to content

Commit 9089938

Browse files
committed
Route alert buttons through Rancher proxy instead of Harvester IP
1 parent 8173c35 commit 9089938

File tree

3 files changed

+38
-28
lines changed

3 files changed

+38
-28
lines changed

modules/monitoring/README.md

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,11 @@ module "monitoring" {
2424
kubeconfig_context = "local"
2525
google_chat_webhook_url = var.google_chat_webhook_url
2626
27-
# Optional — show a "View Alert" deep-link button in each notification card.
28-
# Find this URL: Harvester UI → Add-ons → rancher-monitoring → alert-manager
29-
# alertmanager_url = "https://<harvester-ip>/api/v1/namespaces/cattle-monitoring-system/services/http:rancher-monitoring-alertmanager:9093/proxy"
27+
# Optional — adds "View Alert" and "View in Prometheus" buttons to each card.
28+
# Both URLs are routed through Rancher's authenticated proxy so users don't
29+
# need a separate session directly on the Harvester IP.
30+
# rancher_url = "https://rancher.example.com"
31+
# harvester_cluster_id = "c-xxxxx" # Rancher UI → Cluster Management → cluster row
3032
}
3133
```
3234

@@ -267,16 +269,15 @@ stored in the `calert-config` Secret and rendered by calert at runtime.
267269
└─────────────────────────────────────────────────────┘
268270
```
269271

270-
**"View Alert" button** is only rendered when `alertmanager_url` is set. It
271-
links to:
272+
Both buttons are rendered only when `rancher_url` and `harvester_cluster_id`
273+
are set. URLs are constructed at `terraform apply` time and routed through
274+
Rancher's authenticated proxy — no separate Harvester session required.
275+
272276
```text
273-
<alertmanager_url>/#/alerts?filter={alertname="<name>"}
277+
View Alert → <rancher_url>/k8s/clusters/<id>/…/rancher-monitoring-alertmanager:9093/proxy/#/alerts?filter={alertname="<name>"}
278+
View in Prometheus → <rancher_url>/k8s/clusters/<id>/…/rancher-monitoring-prometheus:9090/proxy/alerts?search=<name>
274279
```
275280

276-
**"View in Prometheus" button** is only rendered when the alert carries a
277-
`GeneratorURL` (set automatically by Prometheus when a rule fires for real;
278-
absent in synthetic test-fires).
279-
280281
### Template evaluation: Terraform vs Go
281282

282283
The card template is a Go template evaluated by calert at runtime — but it
@@ -312,7 +313,8 @@ calert ignores `${ }` delimiters.
312313

313314
| Name | Type | Default | Description |
314315
|---|---|---|---|
315-
| `alertmanager_url` | string | `""` | Alertmanager UI base URL — enables "View Alert" button. Leave empty to omit. |
316+
| `rancher_url` | string | `""` | Base URL of the Rancher server (e.g. `https://rancher.example.com`). Combined with `harvester_cluster_id` to build Rancher-authenticated proxy URLs for both buttons. Leave empty to omit buttons. |
317+
| `harvester_cluster_id` | string | `""` | Rancher cluster ID for the Harvester cluster (e.g. `c-v7gvt`). Found in Rancher UI → Cluster Management. Required when `rancher_url` is set. |
316318
| `monitoring_namespace` | string | `cattle-monitoring-system` | Namespace where rancher-monitoring runs |
317319
| `dashboards_namespace` | string | `cattle-dashboards` | Namespace where Grafana picks up dashboard ConfigMaps |
318320
| `runbook_base_url` | string | `https://wiki.internal/runbooks/harvester` | Base URL prepended to each alert's `runbook_url` annotation |

modules/monitoring/main.tf

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,15 @@ locals {
1515
# AlertmanagerConfig and template resources go in the monitoring namespace.
1616
ns = var.monitoring_namespace
1717
dns = var.dashboards_namespace
18+
19+
# Rancher-authenticated proxy base for this Harvester cluster.
20+
# e.g. https://rancher.example.com/k8s/clusters/c-v7gvt
21+
# Both button URLs are derived from this so they pass through Rancher auth
22+
# rather than hitting the Harvester IP directly (which returns 403 to users
23+
# who are not separately authenticated to Harvester).
24+
rancher_proxy_base = (var.rancher_url != "" && var.harvester_cluster_id != "") ? "${var.rancher_url}/k8s/clusters/${var.harvester_cluster_id}" : ""
25+
alertmanager_base_url = local.rancher_proxy_base != "" ? "${local.rancher_proxy_base}/api/v1/namespaces/${var.monitoring_namespace}/services/http:rancher-monitoring-alertmanager:9093/proxy" : ""
26+
prometheus_base_url = local.rancher_proxy_base != "" ? "${local.rancher_proxy_base}/api/v1/namespaces/${var.monitoring_namespace}/services/http:rancher-monitoring-prometheus:9090/proxy" : ""
1827
}
1928

2029
# ── Alertmanager config + calert (Google Chat webhook forwarder) ──────────────
@@ -63,9 +72,10 @@ locals {
6372
# Section-level headers (strings) only — card-level header objects are not
6473
# supported by Google Chat webhooks and produce blank cards.
6574
# Available functions: toUpper, Title, SortedPairs (calert v2.3.0)
66-
# var.alertmanager_url is a Terraform interpolation — baked in at apply time as
67-
# a literal string. {{.GeneratorURL}} is a Go template variable — resolved at
68-
# runtime per alert. Both coexist safely in the same heredoc.
75+
# local.alertmanager_base_url / local.prometheus_base_url are Terraform
76+
# interpolations — baked in at apply time as literal strings. The Go template
77+
# vars ({{.Labels.alertname}} etc.) are resolved at runtime per alert.
78+
# Both coexist safely in the same heredoc.
6979
calert_message_tmpl = <<-TMPL
7080
{{- define "cardsV2" -}}
7181
{
@@ -78,20 +88,12 @@ locals {
7888
{{- if ne $i 0 -}},{{- end -}}
7989
{"decoratedText": {"text": "{{ $pair.Name | Title }}: {{ $pair.Value }}"}}
8090
{{- end -}}
81-
%{~if var.alertmanager_url != ""~}
82-
,{"buttonList": {"buttons": [
83-
{"text": "View Alert", "onClick": {"openLink": {"url": "${var.alertmanager_url}/#/alerts?filter=%7Balertname%3D%22{{.Labels.alertname}}%22%7D"}}}
84-
{{- if .GeneratorURL -}}
85-
,{"text": "View in Prometheus", "onClick": {"openLink": {"url": "{{.GeneratorURL}}"}}}
86-
{{- end -}}
87-
]}}
88-
%{~else~}
89-
{{- if .GeneratorURL -}}
91+
%{~ if local.rancher_proxy_base != "" ~}
9092
,{"buttonList": {"buttons": [
91-
{"text": "View in Prometheus", "onClick": {"openLink": {"url": "{{.GeneratorURL}}"}}}
93+
{"text": "View Alert", "onClick": {"openLink": {"url": "${local.alertmanager_base_url}/#/alerts?filter=%7Balertname%3D%22{{.Labels.alertname}}%22%7D"}}},
94+
{"text": "View in Prometheus", "onClick": {"openLink": {"url": "${local.prometheus_base_url}/alerts?search={{.Labels.alertname}}"}}}
9295
]}}
93-
{{- end -}}
94-
%{~endif~}
96+
%{~ endif ~}
9597
]
9698
},
9799
{

modules/monitoring/variables.tf

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,16 @@ variable "google_chat_webhook_url" {
2121
description = "Google Chat incoming webhook URL for alert notifications."
2222
}
2323

24-
variable "alertmanager_url" {
24+
variable "rancher_url" {
2525
type = string
2626
default = ""
27-
description = "Base URL of the Alertmanager UI (e.g. https://<rancher>/api/v1/namespaces/cattle-monitoring-system/services/http:rancher-monitoring-alertmanager:9093/proxy). Used for the 'View Alert' button in Google Chat cards. Leave empty to omit the button."
27+
description = "Base URL of the Rancher server (e.g. https://rancher.example.com). Combined with harvester_cluster_id to build Rancher-authenticated proxy URLs for the 'View Alert' and 'View in Prometheus' buttons. Leave empty to omit both buttons."
28+
}
29+
30+
variable "harvester_cluster_id" {
31+
type = string
32+
default = ""
33+
description = "Rancher cluster ID for the Harvester cluster (e.g. c-v7gvt). Found in Rancher UI → Cluster Management → cluster row. Required when rancher_url is set."
2834
}
2935

3036
# ── Optional (monitoring namespaces) ─────────────────────────────────────────

0 commit comments

Comments
 (0)