Skip to content

Commit 3fdf3b9

Browse files
committed
thanos ruler factory proxy
Signed-off-by: Mario Fernandez <[email protected]>
1 parent 3845c52 commit 3fdf3b9

File tree

3 files changed

+46
-78
lines changed

3 files changed

+46
-78
lines changed

pkg/manifests/amcfg.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ type amHTTPConfig struct {
136136
TLSConfig amConfigTLS `yaml:"tls_config,omitempty"`
137137
}
138138

139-
func ConvertToThanosAlertmanagerConfiguration(ta []AdditionalAlertmanagerConfig) ([]thanosAlertmanagerConfiguration, error) {
139+
func (f *Factory) ConvertToThanosAlertmanagerConfiguration(ta []AdditionalAlertmanagerConfig) ([]thanosAlertmanagerConfiguration, error) {
140140
result := make([]thanosAlertmanagerConfiguration, len(ta))
141141

142142
for i, a := range ta {
@@ -183,6 +183,16 @@ func ConvertToThanosAlertmanagerConfiguration(ta []AdditionalAlertmanagerConfig)
183183

184184
cfg.StaticConfigs = a.StaticConfigs
185185

186+
switch cfg.Scheme {
187+
case "http":
188+
if proxy := f.proxy.HTTPProxy(); proxy != "" {
189+
cfg.ProxyURL = proxy
190+
}
191+
case "https":
192+
if proxy := f.proxy.HTTPSProxy(); proxy != "" {
193+
cfg.ProxyURL = proxy
194+
}
195+
}
186196
result[i] = cfg
187197
}
188198

pkg/manifests/manifests.go

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1161,6 +1161,17 @@ func (f *Factory) ThanosRulerAlertmanagerConfigSecret() (*v1.Secret, error) {
11611161
return nil, err
11621162
}
11631163

1164+
switch alertingConfiguration.Alertmanagers[0].Scheme {
1165+
case "http":
1166+
if proxy := f.proxy.HTTPProxy(); proxy != "" {
1167+
alertingConfiguration.Alertmanagers[0].ProxyURL = proxy
1168+
}
1169+
case "https":
1170+
if proxy := f.proxy.HTTPSProxy(); proxy != "" {
1171+
alertingConfiguration.Alertmanagers[0].ProxyURL = proxy
1172+
}
1173+
}
1174+
11641175
if f.config.UserWorkloadConfiguration.Alertmanager.Enabled {
11651176
alertingConfiguration.Alertmanagers[0].HTTPConfig.TLSConfig.ServerName = fmt.Sprintf(
11661177
"%s.%s.svc",
@@ -1177,7 +1188,7 @@ func (f *Factory) ThanosRulerAlertmanagerConfigSecret() (*v1.Secret, error) {
11771188
alertingConfiguration.Alertmanagers = []thanosAlertmanagerConfiguration{}
11781189
}
11791190

1180-
additionalConfigs, err := ConvertToThanosAlertmanagerConfiguration(f.config.GetThanosRulerAlertmanagerConfigs())
1191+
additionalConfigs, err := f.ConvertToThanosAlertmanagerConfiguration(f.config.GetThanosRulerAlertmanagerConfigs())
11811192
if err != nil {
11821193
return nil, err
11831194
}
@@ -3225,8 +3236,6 @@ func (f *Factory) ThanosRulerCustomResource(
32253236

32263237
for i, container := range t.Spec.Containers {
32273238
switch container.Name {
3228-
case "thanos-ruler":
3229-
f.injectProxyVariables(&t.Spec.Containers[i])
32303239
case "kube-rbac-proxy-metrics", "kube-rbac-proxy-web":
32313240
t.Spec.Containers[i].Image = f.config.Images.KubeRbacProxy
32323241
t.Spec.Containers[i].Args = f.setTLSSecurityConfiguration(container.Args, KubeRbacProxyTLSCipherSuitesFlag, KubeRbacProxyMinTLSVersionFlag)

pkg/manifests/manifests_test.go

Lines changed: 23 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -1988,12 +1988,6 @@ func TestPrometheusK8sAdditionalAlertManagerConfigsSecret(t *testing.T) {
19881988
expected string
19891989
mountedSecrets []string
19901990
}{
1991-
{
1992-
name: "empty config",
1993-
config: "",
1994-
expected: "[]\n",
1995-
mountedSecrets: []string{},
1996-
},
19971991
{
19981992
name: "basic config",
19991993
config: `prometheusK8s:
@@ -2006,6 +2000,7 @@ func TestPrometheusK8sAdditionalAlertManagerConfigsSecret(t *testing.T) {
20062000
- targets:
20072001
- alertmanager1-remote.com
20082002
- alertmanager1-remotex.com
2003+
proxy_from_environment: true
20092004
`,
20102005
mountedSecrets: []string{},
20112006
},
@@ -2027,6 +2022,7 @@ func TestPrometheusK8sAdditionalAlertManagerConfigsSecret(t *testing.T) {
20272022
- targets:
20282023
- alertmanager1-remote.com
20292024
- alertmanager1-remotex.com
2025+
proxy_from_environment: true
20302026
`,
20312027
mountedSecrets: []string{},
20322028
},
@@ -2050,6 +2046,7 @@ func TestPrometheusK8sAdditionalAlertManagerConfigsSecret(t *testing.T) {
20502046
- targets:
20512047
- alertmanager1-remote.com
20522048
- alertmanager1-remotex.com
2049+
proxy_from_environment: true
20532050
`,
20542051
mountedSecrets: []string{"alertmanager1-bearer-token"},
20552052
},
@@ -2084,6 +2081,7 @@ func TestPrometheusK8sAdditionalAlertManagerConfigsSecret(t *testing.T) {
20842081
- targets:
20852082
- alertmanager1-remote.com
20862083
- alertmanager1-remotex.com
2084+
proxy_from_environment: true
20872085
`,
20882086
mountedSecrets: []string{"alertmanager-tls"},
20892087
},
@@ -2120,6 +2118,7 @@ func TestPrometheusK8sAdditionalAlertManagerConfigsSecret(t *testing.T) {
21202118
- targets:
21212119
- alertmanager1-remote.com
21222120
- alertmanager1-remotex.com
2121+
proxy_from_environment: true
21232122
`,
21242123
mountedSecrets: []string{"alertmanager-ca-tls", "alertmanager-cert-tls", "alertmanager-key-tls"},
21252124
},
@@ -2159,6 +2158,7 @@ func TestPrometheusK8sAdditionalAlertManagerConfigsSecret(t *testing.T) {
21592158
- targets:
21602159
- alertmanager1-remote.com
21612160
- alertmanager1-remotex.com
2161+
proxy_from_environment: true
21622162
`,
21632163
mountedSecrets: []string{"alertmanager-bearer-token", "alertmanager-ca-tls", "alertmanager-cert-tls", "alertmanager-key-tls"},
21642164
},
@@ -2169,7 +2169,6 @@ func TestPrometheusK8sAdditionalAlertManagerConfigsSecret(t *testing.T) {
21692169
- staticConfigs:
21702170
- alertmanager1-remote.com
21712171
- alertmanager1-remotex.com
2172-
proxyFromEnvironment: true
21732172
`,
21742173
expected: `- static_configs:
21752174
- targets:
@@ -2255,6 +2254,7 @@ func TestThanosRulerAdditionalAlertManagerConfigsSecret(t *testing.T) {
22552254
server_name: alertmanager-main.openshift-monitoring.svc
22562255
static_configs:
22572256
- dnssrv+_web._tcp.alertmanager-operated.openshift-monitoring.svc
2257+
proxy_url: https://example.com:8080/
22582258
`,
22592259
},
22602260
{
@@ -2272,6 +2272,7 @@ func TestThanosRulerAdditionalAlertManagerConfigsSecret(t *testing.T) {
22722272
server_name: alertmanager-user-workload.openshift-user-workload-monitoring.svc
22732273
static_configs:
22742274
- dnssrv+_web._tcp.alertmanager-operated.openshift-user-workload-monitoring.svc
2275+
proxy_url: https://example.com:8080/
22752276
`,
22762277
},
22772278
{
@@ -2292,6 +2293,7 @@ func TestThanosRulerAdditionalAlertManagerConfigsSecret(t *testing.T) {
22922293
server_name: alertmanager-main.openshift-monitoring.svc
22932294
static_configs:
22942295
- dnssrv+_web._tcp.alertmanager-operated.openshift-monitoring.svc
2296+
proxy_url: https://example.com:8080/
22952297
- static_configs:
22962298
- alertmanager1-remote.com
22972299
- alertmanager1-remotex.com
@@ -2320,7 +2322,7 @@ func TestThanosRulerAdditionalAlertManagerConfigsSecret(t *testing.T) {
23202322
additionalAlertmanagerConfigs:
23212323
- apiVersion: v2
23222324
pathPrefix: /path-prefix
2323-
scheme: ftp
2325+
scheme: https
23242326
staticConfigs:
23252327
- alertmanager1-remote.com
23262328
- alertmanager1-remotex.com
@@ -2335,12 +2337,14 @@ func TestThanosRulerAdditionalAlertManagerConfigsSecret(t *testing.T) {
23352337
server_name: alertmanager-main.openshift-monitoring.svc
23362338
static_configs:
23372339
- dnssrv+_web._tcp.alertmanager-operated.openshift-monitoring.svc
2338-
- scheme: ftp
2340+
proxy_url: https://example.com:8080/
2341+
- scheme: https
23392342
path_prefix: /path-prefix
23402343
api_version: v2
23412344
static_configs:
23422345
- alertmanager1-remote.com
23432346
- alertmanager1-remotex.com
2347+
proxy_url: https://example.com:8080/
23442348
`,
23452349
},
23462350
{
@@ -2364,6 +2368,7 @@ func TestThanosRulerAdditionalAlertManagerConfigsSecret(t *testing.T) {
23642368
server_name: alertmanager-main.openshift-monitoring.svc
23652369
static_configs:
23662370
- dnssrv+_web._tcp.alertmanager-operated.openshift-monitoring.svc
2371+
proxy_url: https://example.com:8080/
23672372
- http_config:
23682373
bearer_token_file: /etc/prometheus/secrets/bearer-token/key
23692374
static_configs:
@@ -2400,6 +2405,7 @@ func TestThanosRulerAdditionalAlertManagerConfigsSecret(t *testing.T) {
24002405
server_name: alertmanager-main.openshift-monitoring.svc
24012406
static_configs:
24022407
- dnssrv+_web._tcp.alertmanager-operated.openshift-monitoring.svc
2408+
proxy_url: https://example.com:8080/
24032409
- http_config:
24042410
tls_config:
24052411
ca_file: /etc/prometheus/secrets/alertmanager-tls/tls.ca
@@ -2440,6 +2446,7 @@ func TestThanosRulerAdditionalAlertManagerConfigsSecret(t *testing.T) {
24402446
server_name: alertmanager-main.openshift-monitoring.svc
24412447
static_configs:
24422448
- dnssrv+_web._tcp.alertmanager-operated.openshift-monitoring.svc
2449+
proxy_url: https://example.com:8080/
24432450
- http_config:
24442451
tls_config:
24452452
ca_file: /etc/prometheus/secrets/alertmanager-ca-tls/tls.ca
@@ -2484,6 +2491,7 @@ func TestThanosRulerAdditionalAlertManagerConfigsSecret(t *testing.T) {
24842491
server_name: alertmanager-main.openshift-monitoring.svc
24852492
static_configs:
24862493
- dnssrv+_web._tcp.alertmanager-operated.openshift-monitoring.svc
2494+
proxy_url: https://example.com:8080/
24872495
- scheme: https
24882496
api_version: v2
24892497
http_config:
@@ -2496,6 +2504,7 @@ func TestThanosRulerAdditionalAlertManagerConfigsSecret(t *testing.T) {
24962504
static_configs:
24972505
- alertmanager1-remote.com
24982506
- alertmanager1-remotex.com
2507+
proxy_url: https://example.com:8080/
24992508
`,
25002509
},
25012510
}
@@ -2513,7 +2522,11 @@ func TestThanosRulerAdditionalAlertManagerConfigsSecret(t *testing.T) {
25132522
}
25142523
c.UserWorkloadConfiguration = uwc
25152524

2516-
f := NewFactory("openshift-monitoring", "openshift-user-workload-monitoring", c, defaultInfrastructureReader(), &fakeProxyReader{}, NewAssets(assetsPath), &APIServerConfig{}, &configv1.Console{})
2525+
f := NewFactory("openshift-monitoring", "openshift-user-workload-monitoring", c, defaultInfrastructureReader(), &fakeProxyReader{
2526+
httpProxy: "http://example.com:8080/",
2527+
httpsProxy: "https://example.com:8080/",
2528+
noProxy: "local.example.com",
2529+
}, NewAssets(assetsPath), &APIServerConfig{}, &configv1.Console{})
25172530

25182531
s, err := f.ThanosRulerAlertmanagerConfigSecret()
25192532
if err != nil {
@@ -5010,70 +5023,6 @@ func TestPrometheusProxy(t *testing.T) {
50105023
}
50115024
}
50125025

5013-
func TestThanosRulerProxy(t *testing.T) {
5014-
for _, tc := range []struct {
5015-
proxyReader ProxyReader
5016-
assertFn func(*testing.T, *v1.Container)
5017-
}{
5018-
{
5019-
proxyReader: &fakeProxyReader{},
5020-
assertFn: func(t *testing.T, c *v1.Container) {
5021-
t.Helper()
5022-
5023-
require.Len(t, c.Env, 3)
5024-
require.Equal(t, c.Env[0].Value, "")
5025-
require.Equal(t, c.Env[1].Value, "")
5026-
require.Equal(t, c.Env[2].Value, "")
5027-
},
5028-
},
5029-
{
5030-
proxyReader: &fakeProxyReader{
5031-
httpProxy: "http://example.com:8080/",
5032-
httpsProxy: "https://example.com:8080/",
5033-
noProxy: "local.example.com",
5034-
},
5035-
assertFn: func(t *testing.T, c *v1.Container) {
5036-
t.Helper()
5037-
5038-
require.Len(t, c.Env, 3)
5039-
require.Equal(t, c.Env[0].Name, "HTTP_PROXY")
5040-
require.Equal(t, c.Env[0].Value, "http://example.com:8080/")
5041-
require.Equal(t, c.Env[1].Name, "HTTPS_PROXY")
5042-
require.Equal(t, c.Env[1].Value, "https://example.com:8080/")
5043-
require.Equal(t, c.Env[2].Name, "NO_PROXY")
5044-
require.Equal(t, c.Env[2].Value, "local.example.com")
5045-
},
5046-
},
5047-
} {
5048-
t.Run("", func(t *testing.T) {
5049-
findContainer := func(am *monv1.ThanosRuler) *v1.Container {
5050-
for _, c := range am.Spec.Containers {
5051-
if c.Name == "thanos-ruler" {
5052-
return &c
5053-
}
5054-
}
5055-
5056-
return nil
5057-
}
5058-
5059-
f := NewFactory("openshift-monitoring", "openshift-user-workload-monitoring", NewDefaultConfig(), defaultInfrastructureReader(), tc.proxyReader, NewAssets(assetsPath), &APIServerConfig{}, &configv1.Console{})
5060-
5061-
t.Run("main", func(t *testing.T) {
5062-
tr, err := f.ThanosRulerCustomResource(
5063-
&v1.Secret{ObjectMeta: metav1.ObjectMeta{Name: "foo"}},
5064-
nil,
5065-
)
5066-
require.NoError(t, err)
5067-
5068-
trc := findContainer(tr)
5069-
require.NotNil(t, trc)
5070-
tc.assertFn(t, trc)
5071-
})
5072-
5073-
})
5074-
}
5075-
}
5076-
50775026
func TestDescriptionWithoutPlaceholder(t *testing.T) {
50785027
tests := []struct {
50795028
name string

0 commit comments

Comments
 (0)