Skip to content

Commit 8c43575

Browse files
committed
add prometheus auth via environment variables
Signed-off-by: devpulse94-ui <[email protected]>
1 parent f15a08a commit 8c43575

File tree

5 files changed

+79
-4
lines changed

5 files changed

+79
-4
lines changed

vertical-pod-autoscaler/docs/faq.md

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,55 @@ Here you should see the flags that you set for the VPA recommender and you shoul
152152

153153
This means that the VPA recommender is now using Prometheus as the history provider.
154154

155+
156+
For authentication to Prometheus, you can provide credentials in following ways:
157+
158+
1) Set the flags `--username=<user>` and `--password=<password>` in the `VPA recommender deployment`. The `args` for the container should look something like this:
159+
160+
```yaml
161+
spec:
162+
containers:
163+
- args:
164+
- --v=4
165+
- --storage=prometheus
166+
- --prometheus-address=http://prometheus.default.svc.cluster.local:9090
167+
- --username=example-user
168+
- --password=example-password
169+
```
170+
171+
2) Set the environment variables `PROMETHEUS_USERNAME` and `PROMETHEUS_PASSWORD` in the `VPA recommender deployment`.
172+
173+
```yaml
174+
spec:
175+
containers:
176+
- args:
177+
- --storage=prometheus
178+
- --prometheus-address=http://prometheus.default.svc.cluster.local:9090
179+
env:
180+
- name: PROMETHEUS_USERNAME
181+
valueFrom:
182+
secretKeyRef:
183+
name: prometheus-auth
184+
key: example-user
185+
- name: PROMETHEUS_PASSWORD
186+
valueFrom:
187+
secretKeyRef:
188+
name: prometheus-auth
189+
key: example-password
190+
```
191+
192+
3) Set the flag `prometheus-bearer-token=<token>`, to use bearer token auth.
193+
194+
```yaml
195+
spec:
196+
containers:
197+
- args:
198+
- --v=4
199+
- --storage=prometheus
200+
- --prometheus-address=http://prometheus.default.svc.cluster.local:9090
201+
- --prometheus-bearer-token=<example-token>
202+
```
203+
155204
### I get recommendations for my single pod replicaset but they are not applied
156205

157206
By default, the [`--min-replicas`](https://github.com/kubernetes/autoscaler/tree/master/pkg/updater/main.go#L44) flag on the updater is set to 2. To change this, you can supply the arg in the [deploys/updater-deployment.yaml](https://github.com/kubernetes/autoscaler/tree/master/deploy/updater-deployment.yaml) file:

vertical-pod-autoscaler/docs/flags.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ This document is auto-generated from the flag definitions in the VPA recommender
9797
| `one-output` | severity | | If true, only write logs to their native level (vs also writing to each lower severity level; no effect when -logtostderr=true) |
9898
| `oom-bump-up-ratio` | float | 1.2 | The memory bump up ratio when OOM occurred, default is 1.2. |
9999
| `oom-min-bump-up-bytes` | float | 1.048576e+08 | The minimal increase of memory when OOM occurred in bytes, default is 100 * 1024 * 1024 |
100-
| `password` | string | | The password used in the prometheus server basic auth |
100+
| `password` | string | | The password used in the prometheus server basic auth. Can also be set via the PROMETHEUS_PASSWORD environment variable |
101101
| `pod-label-prefix` | string | "pod_label_" | Which prefix to look for pod labels in metrics |
102102
| `pod-name-label` | string | "kubernetes_pod_name" | Label name to look for pod names |
103103
| `pod-namespace-label` | string | "kubernetes_namespace" | Label name to look for pod namespaces |
@@ -127,7 +127,7 @@ This document is auto-generated from the flag definitions in the VPA recommender
127127
| `target-memory-percentile` | float | 0.9 | Memory usage percentile that will be used as a base for memory target recommendation. Doesn't affect memory lower bound nor memory upper bound. |
128128
| `update-worker-count` | int | 10 | Number of concurrent workers to update VPA recommendations and checkpoints. When increasing this setting, make sure the client-side rate limits ('kube-api-qps' and 'kube-api-burst') are either increased or turned off as well. Determines the minimum number of VPA checkpoints written per recommender loop. |
129129
| `use-external-metrics` | | | ALPHA. Use an external metrics provider instead of metrics_server. |
130-
| `username` | string | | The username used in the prometheus server basic auth |
130+
| `username` | string | | The username used in the prometheus server basic auth. Can also be set via the PROMETHEUS_USERNAME environment variable |
131131
| `v,` | | : 4 | , --v Level set the log level verbosity (default 4) |
132132
| `vmodule` | moduleSpec | | comma-separated list of pattern=N settings for file-filtered logging |
133133
| `vpa-object-namespace` | string | | Specifies the namespace to search for VPA objects. Leave empty to include all namespaces. If provided, the garbage collector will only clean this namespace. |

vertical-pod-autoscaler/pkg/recommender/input/history/history_provider.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"crypto/tls"
2222
"fmt"
2323
"net/http"
24+
"os"
2425
"sort"
2526
"strings"
2627
"time"
@@ -151,6 +152,17 @@ func NewPrometheusHistoryProvider(config PrometheusHistoryProviderConfig) (Histo
151152
Password: config.Authentication.Password,
152153
Base: prometheusTransport,
153154
}
155+
} else {
156+
// check if env vars for credentials are set
157+
prometheusUsername := os.Getenv("PROMETHEUS_USERNAME")
158+
prometheusPassword := os.Getenv("PROMETHEUS_PASSWORD")
159+
if prometheusUsername != "" && prometheusPassword != "" {
160+
prometheusTransport = &PrometheusBasicAuthTransport{
161+
Username: prometheusUsername,
162+
Password: prometheusPassword,
163+
Base: prometheusTransport,
164+
}
165+
}
154166
}
155167

156168
roundTripper := metrics_recommender.NewPrometheusRoundTripperCounter(

vertical-pod-autoscaler/pkg/recommender/input/history/history_provider_test.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -398,4 +398,18 @@ func TestPrometheusAuth(t *testing.T) {
398398
assert.NotContains(t, capturedRequest.Header.Get("Authorization"), "Basic")
399399
assert.Equal(t, capturedRequest.Header.Get("Authorization"), "Bearer token")
400400
})
401+
402+
t.Run("Basic auth with username/password configured as env", func(t *testing.T) {
403+
// clear auth config so only environment variables are used in this test
404+
cfg.Authentication = PrometheusCredentials{}
405+
t.Setenv("PROMETHEUS_USERNAME", "prom_user")
406+
t.Setenv("PROMETHEUS_PASSWORD", "prom_password")
407+
408+
prov, _ := NewPrometheusHistoryProvider(cfg)
409+
_, err := prov.GetClusterHistory()
410+
411+
assert.Nil(t, err)
412+
assert.Equal(t, capturedRequest.Header.Get("Authorization"), "Basic cHJvbV91c2VyOnByb21fcGFzc3dvcmQ=") // "prom_user:prom_password"
413+
414+
})
401415
}

vertical-pod-autoscaler/pkg/recommender/main.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,8 +84,8 @@ var (
8484
ctrNamespaceLabel = flag.String("container-namespace-label", "namespace", `Label name to look for container namespaces`)
8585
ctrPodNameLabel = flag.String("container-pod-name-label", "pod_name", `Label name to look for container pod names`)
8686
ctrNameLabel = flag.String("container-name-label", "name", `Label name to look for container names`)
87-
username = flag.String("username", "", "The username used in the prometheus server basic auth")
88-
password = flag.String("password", "", "The password used in the prometheus server basic auth")
87+
username = flag.String("username", "", "The username used in the prometheus server basic auth. Can also be set via the PROMETHEUS_USERNAME environment variable")
88+
password = flag.String("password", "", "The password used in the prometheus server basic auth. Can also be set via the PROMETHEUS_PASSWORD environment variable")
8989
prometheusBearerToken = flag.String("prometheus-bearer-token", "", "The bearer token used in the Prometheus server bearer token auth")
9090
prometheusBearerTokenFile = flag.String("prometheus-bearer-token-file", "", "Path to the bearer token file used for authentication by the Prometheus server")
9191
)

0 commit comments

Comments
 (0)