Skip to content

Commit 8804294

Browse files
Merge pull request #2580 from marioferh/proxy_url_alertmanager_
MON-4043: Configuring external Alertmangers with proxy_url
2 parents 5df10b7 + 674d576 commit 8804294

File tree

8 files changed

+252
-20
lines changed

8 files changed

+252
-20
lines changed

assets/prometheus-k8s/prometheus.yaml

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,14 @@ spec:
154154
volumeMounts:
155155
- mountPath: /etc/tls/grpc
156156
name: secret-grpc-tls
157-
- name: prometheus
157+
- env:
158+
- name: HTTP_PROXY
159+
value: ""
160+
- name: HTTPS_PROXY
161+
value: ""
162+
- name: NO_PROXY
163+
value: ""
164+
name: prometheus
158165
terminationMessagePolicy: FallbackToLogsOnError
159166
volumeMounts:
160167
- mountPath: /etc/pki/ca-trust/extracted/pem/

assets/prometheus-user-workload/prometheus.yaml

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,14 @@ spec:
168168
volumeMounts:
169169
- mountPath: /etc/tls/grpc
170170
name: secret-grpc-tls
171-
- name: prometheus
171+
- env:
172+
- name: HTTP_PROXY
173+
value: ""
174+
- name: HTTPS_PROXY
175+
value: ""
176+
- name: NO_PROXY
177+
value: ""
178+
name: prometheus
172179
terminationMessagePolicy: FallbackToLogsOnError
173180
volumeMounts:
174181
- mountPath: /etc/pki/ca-trust/extracted/pem/

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ require (
2525
github.com/prometheus/common v0.60.0
2626
github.com/prometheus/prometheus v0.54.1
2727
github.com/stretchr/testify v1.9.0
28+
golang.org/x/net v0.30.0
2829
golang.org/x/sync v0.8.0
2930
golang.org/x/text v0.19.0
3031
gopkg.in/yaml.v2 v2.4.0
@@ -130,7 +131,6 @@ require (
130131
go.uber.org/zap v1.26.0 // indirect
131132
golang.org/x/crypto v0.28.0 // indirect
132133
golang.org/x/exp v0.0.0-20241004190924-225e2abe05e6 // indirect
133-
golang.org/x/net v0.30.0 // indirect
134134
golang.org/x/oauth2 v0.23.0 // indirect
135135
golang.org/x/sys v0.26.0 // indirect
136136
golang.org/x/term v0.25.0 // indirect

jsonnet/components/prometheus-user-workload.libsonnet

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -563,6 +563,16 @@ function(params)
563563
// See e.g pkg/manifests/manifests.go where the startup probe is added
564564
{
565565
name: 'prometheus',
566+
env: [{
567+
name: 'HTTP_PROXY',
568+
value: '',
569+
}, {
570+
name: 'HTTPS_PROXY',
571+
value: '',
572+
}, {
573+
name: 'NO_PROXY',
574+
value: '',
575+
}],
566576
volumeMounts+: [
567577
{
568578
name: $.trustedCaBundle.metadata.name,

jsonnet/components/prometheus.libsonnet

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -562,6 +562,16 @@ function(params)
562562
},
563563
{
564564
name: 'prometheus',
565+
env: [{
566+
name: 'HTTP_PROXY',
567+
value: '',
568+
}, {
569+
name: 'HTTPS_PROXY',
570+
value: '',
571+
}, {
572+
name: 'NO_PROXY',
573+
value: '',
574+
}],
565575
volumeMounts+: [
566576
{
567577
name: $.trustedCaBundle.metadata.name,

pkg/manifests/amcfg.go

Lines changed: 47 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@ package manifests
22

33
import (
44
"fmt"
5+
"net/url"
56

7+
"golang.org/x/net/http/httpproxy"
68
v1 "k8s.io/api/core/v1"
79
)
810

@@ -29,13 +31,14 @@ func (a PrometheusAdditionalAlertmanagerConfigs) MarshalYAML() (interface{}, err
2931

3032
// amConfigPrometheus is our internal representation of the Prometheus alerting configuration.
3133
type amConfigPrometheus struct {
32-
Scheme string `yaml:"scheme,omitempty"`
33-
PathPrefix string `yaml:"path_prefix,omitempty"`
34-
Timeout *string `yaml:"timeout,omitempty"`
35-
APIVersion string `yaml:"api_version,omitempty"`
36-
Authorization amConfigAuthorization `yaml:"authorization,omitempty"`
37-
TLSConfig amConfigTLS `yaml:"tls_config,omitempty"`
38-
StaticConfigs []amConfigStaticConfigs `yaml:"static_configs,omitempty"`
34+
Scheme string `yaml:"scheme,omitempty"`
35+
PathPrefix string `yaml:"path_prefix,omitempty"`
36+
Timeout *string `yaml:"timeout,omitempty"`
37+
APIVersion string `yaml:"api_version,omitempty"`
38+
Authorization amConfigAuthorization `yaml:"authorization,omitempty"`
39+
TLSConfig amConfigTLS `yaml:"tls_config,omitempty"`
40+
StaticConfigs []amConfigStaticConfigs `yaml:"static_configs,omitempty"`
41+
ProxyFromEnvironment bool `yaml:"proxy_from_environment,omitempty"`
3942
}
4043

4144
type amConfigAuthorization struct {
@@ -64,10 +67,11 @@ type prometheusAdditionalAlertmanagerConfig AdditionalAlertmanagerConfig
6467
// compatible with the Prometheus configuration.
6568
func (a prometheusAdditionalAlertmanagerConfig) MarshalYAML() (interface{}, error) {
6669
cfg := amConfigPrometheus{
67-
Scheme: a.Scheme,
68-
PathPrefix: a.PathPrefix,
69-
Timeout: a.Timeout,
70-
APIVersion: a.APIVersion,
70+
Scheme: a.Scheme,
71+
PathPrefix: a.PathPrefix,
72+
Timeout: a.Timeout,
73+
APIVersion: a.APIVersion,
74+
ProxyFromEnvironment: true,
7175
TLSConfig: amConfigTLS{
7276
CA: "",
7377
Cert: "",
@@ -126,14 +130,15 @@ type thanosAlertmanagerConfiguration struct {
126130
APIVersion string `yaml:"api_version,omitempty"`
127131
HTTPConfig amHTTPConfig `yaml:"http_config,omitempty"`
128132
StaticConfigs []string `yaml:"static_configs,omitempty"`
133+
ProxyURL string `yaml:"proxy_url,omitempty"`
129134
}
130135

131136
type amHTTPConfig struct {
132137
BearerTokenFile string `yaml:"bearer_token_file,omitempty"`
133138
TLSConfig amConfigTLS `yaml:"tls_config,omitempty"`
134139
}
135140

136-
func ConvertToThanosAlertmanagerConfiguration(ta []AdditionalAlertmanagerConfig) ([]thanosAlertmanagerConfiguration, error) {
141+
func (f *Factory) ConvertToThanosAlertmanagerConfiguration(ta []AdditionalAlertmanagerConfig) ([]thanosAlertmanagerConfiguration, error) {
137142
result := make([]thanosAlertmanagerConfiguration, len(ta))
138143

139144
for i, a := range ta {
@@ -180,6 +185,36 @@ func ConvertToThanosAlertmanagerConfiguration(ta []AdditionalAlertmanagerConfig)
180185

181186
cfg.StaticConfigs = a.StaticConfigs
182187

188+
httpConfig := httpproxy.Config{
189+
HTTPProxy: f.proxy.HTTPProxy(),
190+
HTTPSProxy: f.proxy.HTTPSProxy(),
191+
NoProxy: f.proxy.NoProxy(),
192+
}
193+
194+
proxyFunc := httpConfig.ProxyFunc()
195+
196+
for _, host := range cfg.StaticConfigs {
197+
if host == "" {
198+
continue
199+
}
200+
201+
u := &url.URL{
202+
Scheme: cfg.Scheme,
203+
Host: host,
204+
}
205+
206+
proxyURL, err := proxyFunc(u)
207+
if err != nil {
208+
return nil, err
209+
}
210+
211+
// Assumes that all hosts share the same proxy policy
212+
if proxyURL != nil {
213+
cfg.ProxyURL = proxyURL.String()
214+
break
215+
}
216+
}
217+
183218
result[i] = cfg
184219
}
185220

pkg/manifests/manifests.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1177,7 +1177,7 @@ func (f *Factory) ThanosRulerAlertmanagerConfigSecret() (*v1.Secret, error) {
11771177
alertingConfiguration.Alertmanagers = []thanosAlertmanagerConfiguration{}
11781178
}
11791179

1180-
additionalConfigs, err := ConvertToThanosAlertmanagerConfiguration(f.config.GetThanosRulerAlertmanagerConfigs())
1180+
additionalConfigs, err := f.ConvertToThanosAlertmanagerConfiguration(f.config.GetThanosRulerAlertmanagerConfigs())
11811181
if err != nil {
11821182
return nil, err
11831183
}
@@ -1460,6 +1460,9 @@ func (f *Factory) PrometheusK8s(grpcTLS *v1.Secret, telemetrySecret *v1.Secret)
14601460

14611461
for i, container := range p.Spec.Containers {
14621462
switch container.Name {
1463+
case "prometheus":
1464+
// Inject the proxy env vars into the Prometheus container for configuring external Alertmanagers
1465+
f.injectProxyVariables(&p.Spec.Containers[i])
14631466
case "kube-rbac-proxy", "kube-rbac-proxy-web", "kube-rbac-proxy-thanos":
14641467
p.Spec.Containers[i].Image = f.config.Images.KubeRbacProxy
14651468
p.Spec.Containers[i].Args = f.setTLSSecurityConfiguration(container.Args, KubeRbacProxyTLSCipherSuitesFlag, KubeRbacProxyMinTLSVersionFlag)
@@ -1794,7 +1797,8 @@ func (f *Factory) PrometheusUserWorkload(grpcTLS *v1.Secret) (*monv1.Prometheus,
17941797
PeriodSeconds: 15,
17951798
FailureThreshold: 240,
17961799
}
1797-
1800+
// Inject the proxy env vars into the Prometheus container for configuring external Alertmanagers
1801+
f.injectProxyVariables(&p.Spec.Containers[i])
17981802
case "kube-rbac-proxy-metrics", "kube-rbac-proxy-federate", "kube-rbac-proxy-thanos":
17991803
p.Spec.Containers[i].Image = f.config.Images.KubeRbacProxy
18001804
p.Spec.Containers[i].Args = f.setTLSSecurityConfiguration(container.Args, KubeRbacProxyTLSCipherSuitesFlag, KubeRbacProxyMinTLSVersionFlag)

0 commit comments

Comments
 (0)