Skip to content

[prometheus-pushgateway] add support for basicAuth in ServiceMonitor #5489

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

Merged
merged 13 commits into from
Apr 6, 2025
4 changes: 3 additions & 1 deletion charts/prometheus-pushgateway/.helmignore
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,6 @@
*.tmproj

# OWNERS file for Kubernetes
OWNERS
OWNERS

ci/
2 changes: 1 addition & 1 deletion charts/prometheus-pushgateway/Chart.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ apiVersion: v2
appVersion: "v1.11.0"
description: A Helm chart for prometheus pushgateway
name: prometheus-pushgateway
version: 3.0.0
version: 3.1.0
home: https://github.com/prometheus/pushgateway
sources:
- https://github.com/prometheus/pushgateway
Expand Down
4 changes: 4 additions & 0 deletions charts/prometheus-pushgateway/ci/basic-auth-values.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---
webConfiguration:
basicAuthUsers:
job: A7ERGdgwLHnY
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
---
webConfiguration:
basicAuthUsers:
job: A7ERGdgwLHnY

serviceMonitor:
enabled: true
namespace: null
basicAuth:
password:
name: smsecret
key: password
username:
name: smsecret
key: username
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
webConfiguration:
basicAuthUsers:
job: A7ERGdgwLHnY

serviceMonitor:
enabled: true
namespace: null
11 changes: 11 additions & 0 deletions charts/prometheus-pushgateway/templates/_helpers.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,17 @@ Define Authorization
{{- $user }}:{{ $password }}
{{- end }}

{{/*
Define basicAuth
*/}}
{{- define "prometheus-pushgateway.basicAuth" -}}
{{- $users := keys .Values.webConfiguration.basicAuthUsers }}
{{- $user := first $users }}
{{- $password := index .Values.webConfiguration.basicAuthUsers $user -}}
user: {{ $user | b64enc | quote }}
password: {{ $password | b64enc | quote }}
{{- end }}

{{/*
Returns pod spec
*/}}
Expand Down
13 changes: 13 additions & 0 deletions charts/prometheus-pushgateway/templates/secret.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,16 @@ metadata:
data:
web-config.yaml: {{ include "prometheus-pushgateway.webConfiguration" . | b64enc}}
{{- end }}
---
{{- if and .Values.webConfiguration .Values.serviceMonitor.enabled (empty .Values.serviceMonitor.basicAuth) }}
apiVersion: v1
kind: Secret
metadata:
name: {{ include "prometheus-pushgateway.fullname" . }}-basic-auth
namespace: {{ default (include "prometheus-pushgateway.namespace" .) .Values.serviceMonitor.namespace }}
labels:
{{- include "prometheus-pushgateway.defaultLabels" . | nindent 4 }}
data:
{{- include "prometheus-pushgateway.basicAuth" . | nindent 2 }}
type: Opaque
{{- end }}
17 changes: 12 additions & 5 deletions charts/prometheus-pushgateway/templates/servicemonitor.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,7 @@ metadata:
{{- toYaml .Values.serviceMonitor.additionalLabels | nindent 4 }}
{{- end }}
name: {{ include "prometheus-pushgateway.fullname" . }}
{{- if .Values.serviceMonitor.namespace }}
namespace: {{ .Values.serviceMonitor.namespace }}
{{- else }}
namespace: {{ template "prometheus-pushgateway.namespace" . }}
{{- end }}
namespace: {{ default (include "prometheus-pushgateway.namespace" .) .Values.serviceMonitor.namespace }}
spec:
endpoints:
- port: {{ .Values.service.portName }}
Expand All @@ -22,6 +18,17 @@ spec:
{{- with .Values.serviceMonitor.scheme }}
scheme: {{ . }}
{{- end }}
{{- if and .Values.webConfiguration (empty .Values.serviceMonitor.basicAuth) }}
basicAuth:
password:
name: {{ include "prometheus-pushgateway.fullname" . }}-basic-auth
key: password
username:
name: {{ include "prometheus-pushgateway.fullname" . }}-basic-auth
key: user
{{- else if not (empty .Values.serviceMonitor.basicAuth) }}
basicAuth: {{ toYaml .Values.serviceMonitor.basicAuth | nindent 6 }}
{{- end }}
{{- with .Values.serviceMonitor.bearerTokenFile }}
bearerTokenFile: {{ . }}
{{- end }}
Expand Down
16 changes: 12 additions & 4 deletions charts/prometheus-pushgateway/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,8 @@ resources: {}

# -- Sets web configuration
# To enable basic authentication, provide basicAuthUsers as a map
# If serviceMonitor.enabled is set, a secret with these credentials will be created
# and configured in serviceMonitor. serviceMonitor.basicAuth overrides this secret.
webConfiguration: {}
# basicAuthUsers:
# username: password
Expand Down Expand Up @@ -252,18 +254,24 @@ serviceMonitor:
telemetryPath: "/metrics"

# Fallback to the prometheus default unless specified
# interval: 10s
interval: ""

## scheme: HTTP scheme to use for scraping. Can be used with `tlsConfig` for example if using istio mTLS.
# scheme: ""
scheme: ""

## Basic authentication
basicAuth: {}

## Bearer token file
bearerTokenFile: ""

## tlsConfig: TLS configuration to use when scraping the endpoint. For example if using istio mTLS.
## Of type: https://github.com/coreos/prometheus-operator/blob/master/Documentation/api.md#tlsconfig
# tlsConfig: {}
tlsConfig: {}

# bearerTokenFile:
# Fallback to the prometheus default unless specified
# scrapeTimeout: 30s
scrapeTimeout: ""

## Used to pass Labels that are used by the Prometheus installed in your cluster to select Service Monitors to work with
## ref: https://github.com/coreos/prometheus-operator/blob/master/Documentation/api.md#prometheusspec
Expand Down