From 861cadfd369a98671484ceb0426793d4b39c272c Mon Sep 17 00:00:00 2001 From: Suraj Deshmukh Date: Mon, 14 Sep 2020 19:53:12 +0530 Subject: [PATCH] prometheus-operator: Add external_url This commit adds the feature removed sometime back called `external_url` this allows the user to choose where(URL) the prometheus is exposed. Signed-off-by: Suraj Deshmukh --- .../components/prometheus-operator.md | 1 + .../prometheus-operator/component.go | 25 +++++++++++++++++++ .../prometheus-operator/template.go | 4 ++- 3 files changed, 29 insertions(+), 1 deletion(-) diff --git a/docs/configuration-reference/components/prometheus-operator.md b/docs/configuration-reference/components/prometheus-operator.md index 9ab46d0bd..36c252170 100644 --- a/docs/configuration-reference/components/prometheus-operator.md +++ b/docs/configuration-reference/components/prometheus-operator.md @@ -147,6 +147,7 @@ Example: | `prometheus.ingress.host` | Ingress URL host to expose Prometheus over the internet. **NOTE:** When running on Packet, a DNS entry pointing at the ingress controller needs to be created. | - | string | true | | `prometheus.ingress.class` | Ingress class to use for Prometheus ingress. | `contour` | string | false | | `prometheus.ingress.certmanager_cluster_issuer` | `ClusterIssuer` to be used by cert-manager while issuing TLS certificates. Supported values: `letsencrypt-production`, `letsencrypt-staging`. | `letsencrypt-production` | string | false | +| `prometheus.external_url` | The URL on which Prometheus will be accessible on. If this is not provided then the URL is taken from `prometheus.ingress.host` with `https` as a scheme. | - | string | false | | `alertmanager_retention` | Time duration Alertmanager shall retain data for. Must match the regular expression `[0-9]+(ms\|s\|m\|h)` (milliseconds, seconds, minutes and hours). | `120h` | string | false | | `alertmanager_external_url` | The external URL the Alertmanager instances will be available under. This is necessary to generate correct URLs. This is necessary if Alertmanager is not served from root of a DNS name. | "" | string | false | | `alertmanager_config` | Provide YAML file path to configure Alertmanager. See [https://prometheus.io/docs/alerting/configuration/#configuration-file](https://prometheus.io/docs/alerting/configuration/#configuration-file). | `{"global":{"resolve_timeout":"5m"},"route":{"group_by":["job"],"group_wait":"30s","group_interval":"5m","repeat_interval":"12h","receiver":"null","routes":[{"match":{"alertname":"Watchdog"},"receiver":"null"}]},"receivers":[{"name":"null"}]}` | string | false | diff --git a/pkg/components/prometheus-operator/component.go b/pkg/components/prometheus-operator/component.go index 1fb5731cb..cd71635d0 100644 --- a/pkg/components/prometheus-operator/component.go +++ b/pkg/components/prometheus-operator/component.go @@ -16,6 +16,7 @@ package prometheus import ( "fmt" + "net/url" "github.com/hashicorp/hcl/v2" "github.com/hashicorp/hcl/v2/gohcl" @@ -63,6 +64,7 @@ type Prometheus struct { WatchLabeledPrometheusRules bool `hcl:"watch_labeled_prometheus_rules,optional"` Ingress *types.Ingress `hcl:"ingress,block"` ExternalLabels map[string]string `hcl:"external_labels,optional"` + ExternalURL string `hcl:"external_url,optional"` } type component struct { @@ -162,6 +164,29 @@ func (c *component) LoadConfig(configBody *hcl.Body, evalContext *hcl.EvalContex c.Prometheus.Ingress.SetDefaults() } + // If user has provided `ingress` block in prometheus and `externalURL` both then they should be + // same. + if c.Prometheus != nil && c.Prometheus.ExternalURL != "" && c.Prometheus.Ingress != nil { + exu, err := url.Parse(c.Prometheus.ExternalURL) + if err != nil { + return hcl.Diagnostics{ + { + Severity: hcl.DiagError, + Summary: fmt.Sprintf("'external_url' is invalid, got error: %v", err), + }, + } + } + + if exu.Host != c.Prometheus.Ingress.Host { + return hcl.Diagnostics{ + { + Severity: hcl.DiagError, + Summary: "'external_url' and 'prometheus.ingress.host' does not match", + }, + } + } + } + return nil } diff --git a/pkg/components/prometheus-operator/template.go b/pkg/components/prometheus-operator/template.go index 183f21e2b..f3ef78352 100644 --- a/pkg/components/prometheus-operator/template.go +++ b/pkg/components/prometheus-operator/template.go @@ -118,7 +118,9 @@ prometheus: secretName: {{ .Prometheus.Ingress.Host }}-tls {{ end }} prometheusSpec: - {{ if .Prometheus.Ingress }} + {{ if .Prometheus.ExternalURL }} + externalUrl: {{ .Prometheus.ExternalURL }} + {{ else if .Prometheus.Ingress }} externalUrl: https://{{.Prometheus.Ingress.Host}} {{ end }} {{ if .Prometheus.NodeSelector }}