Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
aos
GitHub Repository: aos/grafana-agent
Path: blob/main/pkg/operator/config/config_test.go
4096 views
1
package config
2
3
import (
4
"fmt"
5
"testing"
6
7
"github.com/grafana/agent/pkg/operator/assets"
8
"github.com/grafana/agent/pkg/util"
9
"github.com/grafana/agent/pkg/util/subset"
10
"github.com/stretchr/testify/assert"
11
"github.com/stretchr/testify/require"
12
v1 "k8s.io/api/core/v1"
13
meta_v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
14
"k8s.io/utils/pointer"
15
k8s_yaml "sigs.k8s.io/yaml"
16
17
gragent "github.com/grafana/agent/pkg/operator/apis/monitoring/v1alpha1"
18
)
19
20
func TestBuildConfigMetrics(t *testing.T) {
21
var store = make(assets.SecretStore)
22
23
store[assets.Key("/secrets/default/example-secret/key")] = "somesecret"
24
store[assets.Key("/configMaps/default/example-cm/key")] = "somecm"
25
store[assets.Key("/secrets/default/client-id/client_id")] = "my-client-id"
26
store[assets.Key("/secrets/default/client-secret/client_secret")] = "somesecret-client-secret"
27
28
tt := []struct {
29
input string
30
expect string
31
}{
32
{
33
input: util.Untab(`
34
metadata:
35
name: example
36
namespace: default
37
spec:
38
logLevel: debug
39
metrics:
40
scrapeInterval: 15s
41
scrapeTimeout: 10s
42
externalLabels:
43
cluster: prod
44
foo: bar
45
remoteWrite:
46
- name: rw-1
47
url: http://localhost:9090/api/v1/write
48
`),
49
expect: util.Untab(`
50
server:
51
log_level: debug
52
53
metrics:
54
wal_directory: /var/lib/grafana-agent/data
55
global:
56
scrape_interval: 15s
57
scrape_timeout: 10s
58
external_labels:
59
cluster: prod
60
foo: bar
61
__replica__: replica-$(STATEFULSET_ORDINAL_NUMBER)
62
remote_write:
63
- name: rw-1
64
url: http://localhost:9090/api/v1/write
65
`),
66
},
67
{
68
input: util.Untab(`
69
metadata:
70
name: example
71
namespace: default
72
spec:
73
logLevel: debug
74
metrics:
75
scrapeInterval: 15s
76
scrapeTimeout: 10s
77
externalLabels:
78
cluster: prod
79
foo: bar
80
remoteWrite:
81
- url: http://localhost:9090/api/v1/write
82
basicAuth:
83
username:
84
name: example-secret
85
key: key
86
password:
87
name: example-secret
88
key: pword
89
tlsConfig:
90
ca:
91
configMap:
92
name: example-cm
93
key: key
94
cert:
95
secret:
96
name: example-secret
97
key: key
98
keySecret:
99
name: example-secret
100
key: key
101
`),
102
expect: util.Untab(`
103
server:
104
log_level: debug
105
106
metrics:
107
wal_directory: /var/lib/grafana-agent/data
108
global:
109
scrape_interval: 15s
110
scrape_timeout: 10s
111
external_labels:
112
cluster: prod
113
foo: bar
114
__replica__: replica-$(STATEFULSET_ORDINAL_NUMBER)
115
remote_write:
116
- url: http://localhost:9090/api/v1/write
117
basic_auth:
118
username: somesecret
119
password_file: /var/lib/grafana-agent/secrets/_secrets_default_example_secret_pword
120
tls_config:
121
ca_file: /var/lib/grafana-agent/secrets/_configMaps_default_example_cm_key
122
cert_file: /var/lib/grafana-agent/secrets/_secrets_default_example_secret_key
123
key_file: /var/lib/grafana-agent/secrets/_secrets_default_example_secret_key
124
`),
125
},
126
{
127
input: util.Untab(`
128
metadata:
129
name: example
130
namespace: default
131
spec:
132
logLevel: debug
133
metrics:
134
scrapeInterval: 15s
135
scrapeTimeout: 10s
136
externalLabels:
137
cluster: prod
138
foo: bar
139
remoteWrite:
140
- url: http://localhost:9090/api/v1/write
141
oauth2:
142
clientId:
143
secret:
144
key: client_id
145
name: client-id
146
clientSecret:
147
key: client_secret
148
name: my-client-secret
149
tokenUrl: https://auth.example.com/realms/master/protocol/openid-connect/token
150
- url: http://localhost:9090/api/v1/write
151
oauth2:
152
clientId:
153
secret:
154
key: client_id
155
name: client-id
156
clientSecret:
157
key: client_secret
158
name: my-client-secret
159
# test optional parameters endpointParams and scopes
160
endpointParams:
161
params-key0: params-value
162
params-key1: params-value
163
scopes:
164
- value0
165
- value1
166
tokenUrl: https://auth.example.com/realms/master/protocol/openid-connect/token
167
`),
168
expect: util.Untab(`
169
server:
170
log_level: debug
171
172
metrics:
173
wal_directory: /var/lib/grafana-agent/data
174
global:
175
scrape_interval: 15s
176
scrape_timeout: 10s
177
external_labels:
178
cluster: prod
179
foo: bar
180
__replica__: replica-$(STATEFULSET_ORDINAL_NUMBER)
181
remote_write:
182
- url: http://localhost:9090/api/v1/write
183
oauth2:
184
client_id: my-client-id
185
client_secret_file: /var/lib/grafana-agent/secrets/_secrets_default_my_client_secret_client_secret
186
token_url: https://auth.example.com/realms/master/protocol/openid-connect/token
187
- url: http://localhost:9090/api/v1/write
188
oauth2:
189
client_id: my-client-id
190
client_secret_file: /var/lib/grafana-agent/secrets/_secrets_default_my_client_secret_client_secret
191
endpoint_params:
192
params-key0: params-value
193
params-key1: params-value
194
scopes:
195
- value0
196
- value1
197
token_url: https://auth.example.com/realms/master/protocol/openid-connect/token
198
`),
199
},
200
}
201
202
for i, tc := range tt {
203
t.Run(fmt.Sprintf("index_%d", i), func(t *testing.T) {
204
var spec gragent.GrafanaAgent
205
err := k8s_yaml.Unmarshal([]byte(tc.input), &spec)
206
require.NoError(t, err)
207
208
d := gragent.Deployment{Agent: &spec, Secrets: store}
209
result, err := BuildConfig(&d, MetricsType)
210
require.NoError(t, err)
211
212
if !assert.YAMLEq(t, tc.expect, result) {
213
fmt.Println(result)
214
}
215
})
216
}
217
}
218
219
func TestAdditionalScrapeConfigsMetrics(t *testing.T) {
220
var store = make(assets.SecretStore)
221
222
additionalSelector := &v1.SecretKeySelector{
223
LocalObjectReference: v1.LocalObjectReference{Name: "configs"},
224
Key: "configs",
225
}
226
227
input := gragent.Deployment{
228
Agent: &gragent.GrafanaAgent{
229
ObjectMeta: meta_v1.ObjectMeta{
230
Namespace: "operator",
231
Name: "agent",
232
},
233
Spec: gragent.GrafanaAgentSpec{
234
Image: pointer.String("grafana/agent:latest"),
235
ServiceAccountName: "agent",
236
Metrics: gragent.MetricsSubsystemSpec{
237
InstanceSelector: &meta_v1.LabelSelector{
238
MatchLabels: map[string]string{"agent": "agent"},
239
},
240
},
241
},
242
},
243
Metrics: []gragent.MetricsDeployment{{
244
Instance: &gragent.MetricsInstance{
245
ObjectMeta: meta_v1.ObjectMeta{
246
Namespace: "operator",
247
Name: "primary",
248
},
249
Spec: gragent.MetricsInstanceSpec{
250
RemoteWrite: []gragent.RemoteWriteSpec{{
251
URL: "http://cortex:80/api/prom/push",
252
}},
253
AdditionalScrapeConfigs: additionalSelector,
254
},
255
},
256
}},
257
258
Secrets: store,
259
}
260
261
store[assets.KeyForSecret("operator", additionalSelector)] = util.Untab(`
262
- job_name: job
263
kubernetes_sd_configs:
264
- role: node
265
- job_name: ec2
266
ec2_sd_configs:
267
- region: eu-west-1
268
port: 9100
269
`)
270
271
expect := util.Untab(`
272
server: {}
273
274
metrics:
275
wal_directory: /var/lib/grafana-agent/data
276
global:
277
external_labels:
278
__replica__: replica-$(STATEFULSET_ORDINAL_NUMBER)
279
cluster: operator/agent
280
configs:
281
- name: operator/primary
282
remote_write:
283
- url: http://cortex:80/api/prom/push
284
scrape_configs:
285
- job_name: job
286
kubernetes_sd_configs:
287
- role: node
288
- job_name: ec2
289
ec2_sd_configs:
290
- region: eu-west-1
291
port: 9100
292
`)
293
294
result, err := BuildConfig(&input, MetricsType)
295
require.NoError(t, err)
296
297
if !assert.YAMLEq(t, expect, result) {
298
fmt.Println(result)
299
}
300
}
301
302
func TestBuildConfigLogs(t *testing.T) {
303
var store = make(assets.SecretStore)
304
305
store[assets.Key("/secrets/default/example-secret/key")] = "somesecret"
306
store[assets.Key("/configMaps/default/example-cm/key")] = "somecm"
307
308
tt := []struct {
309
input string
310
expect string
311
}{
312
{
313
input: util.Untab(`
314
metadata:
315
name: example
316
namespace: default
317
spec:
318
logLevel: debug
319
`),
320
expect: util.Untab(`
321
server:
322
log_level: debug
323
logs:
324
positions_directory: /var/lib/grafana-agent/data
325
`),
326
},
327
}
328
329
for i, tc := range tt {
330
t.Run(fmt.Sprintf("index_%d", i), func(t *testing.T) {
331
var spec gragent.GrafanaAgent
332
err := k8s_yaml.Unmarshal([]byte(tc.input), &spec)
333
require.NoError(t, err)
334
335
d := gragent.Deployment{Agent: &spec, Secrets: store}
336
result, err := BuildConfig(&d, LogsType)
337
require.NoError(t, err)
338
339
if !assert.YAMLEq(t, tc.expect, result) {
340
fmt.Println(result)
341
}
342
})
343
}
344
}
345
346
func TestBuildConfigIntegrations(t *testing.T) {
347
in := util.Untab(`
348
Agent:
349
kind: GrafanaAgent
350
metadata:
351
name: test-agent
352
namespace: monitoring
353
Integrations:
354
- Instance:
355
kind: MetricsIntegration
356
metadata:
357
name: mysql-a
358
namespace: databases
359
spec:
360
name: mysqld_exporter
361
config:
362
data_source_names: root@(server-a:3306)/
363
- Instance:
364
kind: MetricsIntegration
365
metadata:
366
name: node
367
namespace: kube-system
368
spec:
369
name: node_exporter
370
type:
371
allNodes: true
372
unique: true
373
config:
374
rootfs_path: /host/root
375
sysfs_path: /host/sys
376
procfs_path: /host/proc
377
- Instance:
378
metadata:
379
name: mysql-b
380
namespace: databases
381
spec:
382
name: mysqld_exporter
383
config:
384
data_source_names: root@(server-b:3306)/
385
- Instance:
386
kind: MetricsIntegration
387
metadata:
388
name: redis-a
389
namespace: databases
390
spec:
391
name: redis_exporter
392
config:
393
redis_addr: redis-a:6379
394
`)
395
396
var h gragent.Deployment
397
err := k8s_yaml.UnmarshalStrict([]byte(in), &h)
398
require.NoError(t, err)
399
400
expect := util.Untab(`
401
server: {}
402
logs:
403
positions_directory: /var/lib/grafana-agent/data
404
metrics:
405
global:
406
external_labels:
407
cluster: monitoring/test-agent
408
wal_directory: /var/lib/grafana-agent/data
409
integrations:
410
metrics:
411
autoscrape:
412
enable: false
413
mysqld_exporter_configs:
414
- data_source_names: root@(server-a:3306)/
415
- data_source_names: root@(server-b:3306)/
416
node_exporter_configs:
417
- rootfs_path: /host/root
418
sysfs_path: /host/sys
419
procfs_path: /host/proc
420
redis_exporter_configs:
421
- redis_addr: redis-a:6379
422
`)
423
424
result, err := BuildConfig(&h, IntegrationsType)
425
require.NoError(t, err)
426
427
require.NoError(t, subset.YAMLAssert([]byte(expect), []byte(result)), "incomplete yaml\n%s", result)
428
}
429
430
// TestBuildConfigIntegrations_Instances ensures that metrics and logs
431
// instances are injected into the resulting config so integrations can use
432
// them for sending telemetry data.
433
func TestBuildConfigIntegrations_Instances(t *testing.T) {
434
in := util.Untab(`
435
Agent:
436
kind: GrafanaAgent
437
metadata:
438
name: test-agent
439
namespace: monitoring
440
Metrics:
441
- Instance:
442
kind: MetricsInstance
443
metadata:
444
name: operator-metrics
445
namespace: primary
446
spec:
447
remoteWrite:
448
- url: http://cortex:80/api/prom/push
449
Logs:
450
- Instance:
451
kind: LogsInstance
452
metadata:
453
name: operator-logs
454
namespace: primary
455
spec:
456
clients:
457
- url: http://loki:80/loki/api/v1/push
458
`)
459
460
var h gragent.Deployment
461
err := k8s_yaml.UnmarshalStrict([]byte(in), &h)
462
require.NoError(t, err)
463
464
expect := util.Untab(`
465
server: {}
466
metrics:
467
global:
468
external_labels:
469
cluster: monitoring/test-agent
470
wal_directory: /var/lib/grafana-agent/data
471
configs:
472
- name: primary/operator-metrics
473
remote_write:
474
- url: http://cortex:80/api/prom/push
475
logs:
476
positions_directory: /var/lib/grafana-agent/data
477
configs:
478
- name: primary/operator-logs
479
clients:
480
- url: http://loki:80/loki/api/v1/push
481
integrations:
482
metrics:
483
autoscrape:
484
enable: false
485
`)
486
487
result, err := BuildConfig(&h, IntegrationsType)
488
require.NoError(t, err)
489
490
require.NoError(t, subset.YAMLAssert([]byte(expect), []byte(result)), "incomplete yaml\n%s", result)
491
}
492
493