-
Notifications
You must be signed in to change notification settings - Fork 142
/
nodescrape.go
235 lines (213 loc) · 8.31 KB
/
nodescrape.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
package vmagent
import (
"context"
"fmt"
"sort"
"strings"
victoriametricsv1beta1 "github.com/VictoriaMetrics/operator/api/v1beta1"
"gopkg.in/yaml.v2"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
func generateNodeScrapeConfig(
ctx context.Context,
crAgent *victoriametricsv1beta1.VMAgent,
cr *victoriametricsv1beta1.VMNodeScrape,
i int,
apiserverConfig *victoriametricsv1beta1.APIServerConfig,
ssCache *scrapesSecretsCache,
ignoreHonorLabels bool,
overrideHonorTimestamps bool,
enforcedNamespaceLabel string,
) yaml.MapSlice {
nodeSpec := cr.Spec
hl := honorLabels(nodeSpec.HonorLabels, ignoreHonorLabels)
cfg := yaml.MapSlice{
{
Key: "job_name",
Value: fmt.Sprintf("nodeScrape/%s/%s/%d", cr.Namespace, cr.Name, i),
},
{
Key: "honor_labels",
Value: hl,
},
}
cfg = honorTimestamps(cfg, nodeSpec.HonorTimestamps, overrideHonorTimestamps)
cfg = append(cfg, generateK8SSDConfig(nil, apiserverConfig, ssCache, kubernetesSDRoleNode, nil))
var scrapeInterval string
if nodeSpec.ScrapeInterval != "" {
scrapeInterval = nodeSpec.ScrapeInterval
} else if nodeSpec.Interval != "" {
scrapeInterval = nodeSpec.Interval
}
scrapeInterval = limitScrapeInterval(ctx, scrapeInterval, crAgent.Spec.MinScrapeInterval, crAgent.Spec.MaxScrapeInterval)
if scrapeInterval != "" {
cfg = append(cfg, yaml.MapItem{Key: "scrape_interval", Value: scrapeInterval})
}
if nodeSpec.ScrapeTimeout != "" {
cfg = append(cfg, yaml.MapItem{Key: "scrape_timeout", Value: nodeSpec.ScrapeTimeout})
}
if nodeSpec.Path != "" {
cfg = append(cfg, yaml.MapItem{Key: "metrics_path", Value: nodeSpec.Path})
}
if nodeSpec.ProxyURL != nil {
cfg = append(cfg, yaml.MapItem{Key: "proxy_url", Value: nodeSpec.ProxyURL})
}
if cr.Spec.SampleLimit > 0 {
cfg = append(cfg, yaml.MapItem{Key: "sample_limit", Value: cr.Spec.SampleLimit})
}
if cr.Spec.SeriesLimit > 0 {
cfg = append(cfg, yaml.MapItem{Key: "series_limit", Value: cr.Spec.SeriesLimit})
}
if nodeSpec.Params != nil {
cfg = append(cfg, yaml.MapItem{Key: "params", Value: nodeSpec.Params})
}
if cr.Spec.FollowRedirects != nil {
cfg = append(cfg, yaml.MapItem{Key: "follow_redirects", Value: cr.Spec.FollowRedirects})
}
if nodeSpec.Scheme != "" {
cfg = append(cfg, yaml.MapItem{Key: "scheme", Value: nodeSpec.Scheme})
}
cfg = addTLStoYaml(cfg, cr.Namespace, nodeSpec.TLSConfig, false)
if nodeSpec.BearerTokenFile != "" {
cfg = append(cfg, yaml.MapItem{Key: "bearer_token_file", Value: nodeSpec.BearerTokenFile})
}
if nodeSpec.BearerTokenSecret != nil && nodeSpec.BearerTokenSecret.Name != "" {
if s, ok := ssCache.bearerTokens[cr.AsMapKey()]; ok {
cfg = append(cfg, yaml.MapItem{Key: "bearer_token", Value: s})
}
}
if cr.Spec.BasicAuth != nil {
var bac yaml.MapSlice
if s, ok := ssCache.baSecrets[cr.AsMapKey()]; ok {
bac = append(bac,
yaml.MapItem{Key: "username", Value: s.Username},
yaml.MapItem{Key: "password", Value: s.Password},
)
}
if len(cr.Spec.BasicAuth.PasswordFile) > 0 {
bac = append(bac, yaml.MapItem{Key: "password_file", Value: cr.Spec.BasicAuth.PasswordFile})
}
if len(bac) > 0 {
cfg = append(cfg, yaml.MapItem{Key: "basic_auth", Value: bac})
}
}
var (
relabelings []yaml.MapSlice
labelKeys []string
)
// Filter targets by pods selected by the scrape.
// Exact label matches.
for k := range cr.Spec.Selector.MatchLabels {
labelKeys = append(labelKeys, k)
}
sort.Strings(labelKeys)
for _, k := range labelKeys {
relabelings = append(relabelings, yaml.MapSlice{
{Key: "action", Value: "keep"},
{Key: "source_labels", Value: []string{"__meta_kubernetes_node_label_" + sanitizeLabelName(k)}},
{Key: "regex", Value: cr.Spec.Selector.MatchLabels[k]},
})
}
// Set based label matching. We have to map the valid relations
// `In`, `NotIn`, `Exists`, and `DoesNotExist`, into relabeling rules.
for _, exp := range cr.Spec.Selector.MatchExpressions {
switch exp.Operator {
case metav1.LabelSelectorOpIn:
relabelings = append(relabelings, yaml.MapSlice{
{Key: "action", Value: "keep"},
{Key: "source_labels", Value: []string{"__meta_kubernetes_node_label_" + sanitizeLabelName(exp.Key)}},
{Key: "regex", Value: strings.Join(exp.Values, "|")},
})
case metav1.LabelSelectorOpNotIn:
relabelings = append(relabelings, yaml.MapSlice{
{Key: "action", Value: "drop"},
{Key: "source_labels", Value: []string{"__meta_kubernetes_node_label_" + sanitizeLabelName(exp.Key)}},
{Key: "regex", Value: strings.Join(exp.Values, "|")},
})
case metav1.LabelSelectorOpExists:
relabelings = append(relabelings, yaml.MapSlice{
{Key: "action", Value: "keep"},
{Key: "source_labels", Value: []string{"__meta_kubernetes_node_labelpresent_" + sanitizeLabelName(exp.Key)}},
{Key: "regex", Value: "true"},
})
case metav1.LabelSelectorOpDoesNotExist:
relabelings = append(relabelings, yaml.MapSlice{
{Key: "action", Value: "drop"},
{Key: "source_labels", Value: []string{"__meta_kubernetes_node_labelpresent_" + sanitizeLabelName(exp.Key)}},
{Key: "regex", Value: "true"},
})
}
}
// Add __address__ as internalIP and pod and service labels into proper labels.
relabelings = append(relabelings, []yaml.MapSlice{
{
{Key: "source_labels", Value: []string{"__meta_kubernetes_node_name"}},
{Key: "target_label", Value: "node"},
},
}...)
// Relabel targetLabels from Node onto target.
for _, l := range cr.Spec.TargetLabels {
relabelings = append(relabelings, yaml.MapSlice{
{Key: "source_labels", Value: []string{"__meta_kubernetes_node_label_" + sanitizeLabelName(l)}},
{Key: "target_label", Value: sanitizeLabelName(l)},
{Key: "regex", Value: "(.+)"},
{Key: "replacement", Value: "${1}"},
})
}
// By default, generate a safe job name from the NodeScrape. We also keep
// this around if a jobLabel is set in case the targets don't actually have a
// value for it. A single pod may potentially have multiple metrics
// endpoints, therefore the endpoints labels is filled with the ports name or
// as a fallback the port number.
relabelings = append(relabelings, yaml.MapSlice{
{Key: "target_label", Value: "job"},
{Key: "replacement", Value: fmt.Sprintf("%s/%s", cr.GetNamespace(), cr.GetName())},
})
if cr.Spec.JobLabel != "" {
relabelings = append(relabelings, yaml.MapSlice{
{Key: "source_labels", Value: []string{"__meta_kubernetes_node_label_" + sanitizeLabelName(cr.Spec.JobLabel)}},
{Key: "target_label", Value: "job"},
{Key: "regex", Value: "(.+)"},
{Key: "replacement", Value: "${1}"},
})
}
if nodeSpec.Port != "" {
relabelings = append(relabelings, yaml.MapSlice{
{Key: "source_labels", Value: []string{"__address__"}},
{Key: "target_label", Value: "__address__"},
{Key: "regex", Value: "^(.*):(.*)"},
{Key: "replacement", Value: fmt.Sprintf("${1}:%s", nodeSpec.Port)},
})
}
for _, c := range nodeSpec.RelabelConfigs {
relabelings = append(relabelings, generateRelabelConfig(c))
}
for _, trc := range crAgent.Spec.NodeScrapeRelabelTemplate {
relabelings = append(relabelings, generateRelabelConfig(trc))
}
// Because of security risks, whenever enforcedNamespaceLabel is set, we want to append it to the
// relabel_configs as the last relabeling, to ensure it overrides any other relabelings.
relabelings = enforceNamespaceLabel(relabelings, cr.Namespace, enforcedNamespaceLabel)
cfg = append(cfg, yaml.MapItem{Key: "relabel_configs", Value: relabelings})
if cr.Spec.SampleLimit > 0 {
cfg = append(cfg, yaml.MapItem{Key: "sample_limit", Value: cr.Spec.SampleLimit})
}
if cr.Spec.SeriesLimit > 0 {
cfg = append(cfg, yaml.MapItem{Key: "series_limit", Value: cr.Spec.SeriesLimit})
}
if nodeSpec.MetricRelabelConfigs != nil {
var metricRelabelings []yaml.MapSlice
for _, c := range nodeSpec.MetricRelabelConfigs {
if c.TargetLabel != "" && enforcedNamespaceLabel != "" && c.TargetLabel == enforcedNamespaceLabel {
continue
}
relabeling := generateRelabelConfig(c)
metricRelabelings = append(metricRelabelings, relabeling)
}
cfg = append(cfg, yaml.MapItem{Key: "metric_relabel_configs", Value: metricRelabelings})
}
cfg = append(cfg, buildVMScrapeParams(cr.Namespace, cr.AsProxyKey(), cr.Spec.VMScrapeParams, ssCache)...)
cfg = addOAuth2Config(cfg, cr.AsMapKey(), nodeSpec.OAuth2, ssCache.oauth2Secrets)
cfg = addAuthorizationConfig(cfg, cr.AsMapKey(), nodeSpec.Authorization, ssCache.authorizationSecrets)
return cfg
}