-
Notifications
You must be signed in to change notification settings - Fork 518
/
probes.go
329 lines (292 loc) · 10.6 KB
/
probes.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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
/*
Copyright 2019 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package probes
import (
"embed"
"fmt"
"path"
"strings"
"time"
"k8s.io/apimachinery/pkg/util/wait"
"k8s.io/klog/v2"
"k8s.io/perf-tests/clusterloader2/pkg/errors"
"k8s.io/perf-tests/clusterloader2/pkg/framework"
"k8s.io/perf-tests/clusterloader2/pkg/framework/client"
"k8s.io/perf-tests/clusterloader2/pkg/measurement"
measurementutil "k8s.io/perf-tests/clusterloader2/pkg/measurement/util"
"k8s.io/perf-tests/clusterloader2/pkg/prometheus"
prom "k8s.io/perf-tests/clusterloader2/pkg/prometheus/clients"
"k8s.io/perf-tests/clusterloader2/pkg/util"
)
const (
probesNamespace = "probes"
manifestsPathPrefix = "manifests/"
checkProbesReadyInterval = 15 * time.Second
defaultCheckProbesReadyTimeout = 15 * time.Minute
defaultPingSleepDuration = time.Second
)
var (
networkLatencyConfig = proberConfig{
Name: "InClusterNetworkLatency",
MetricVersion: "v1",
Query: "quantile_over_time(0.99, probes:in_cluster_network_latency:histogram_quantile[%v])",
Manifests: "*.yaml",
ProbeLabelValues: []string{"ping-client", "ping-server"},
ProberType: "latency",
}
dnsLookupConfig = proberConfig{
Name: "DnsLookupLatency",
MetricVersion: "v1",
Query: "quantile_over_time(0.99, probes:dns_lookup_latency:histogram_quantile[%v])",
Manifests: "dnsLookup/*yaml",
ProbeLabelValues: []string{"dns"},
ProberType: "latency",
}
metricsServerLatencyConfig = proberConfig{
Name: "InClusterAPIServerRequestLatency",
MetricVersion: "v1",
Query: "quantile_over_time(0.99, probes:in_cluster_apiserver_request_latency:histogram_quantile[%v])",
Manifests: "metricsServer/*.yaml",
ProbeLabelValues: []string{"metrics-server-prober"},
ProberType: "latency",
}
dnsPropagationConfig = proberConfig{
Name: "DnsPropagation",
MetricVersion: "v1",
Query: "probes_dns_propagation_seconds",
Manifests: "dnsPropagation/*.yaml",
ProbeLabelValues: []string{"dns-propagation-prober"},
ProberType: "propagation",
}
//go:embed manifests
manifestsFS embed.FS
)
func init() {
create := func() measurement.Measurement { return createProber(networkLatencyConfig) }
if err := measurement.Register(networkLatencyConfig.Name, create); err != nil {
klog.Errorf("cannot register %s: %v", networkLatencyConfig.Name, err)
}
create = func() measurement.Measurement { return createProber(dnsLookupConfig) }
if err := measurement.Register(dnsLookupConfig.Name, create); err != nil {
klog.Errorf("cannot register %s: %v", dnsLookupConfig.Name, err)
}
create = func() measurement.Measurement { return createProber(metricsServerLatencyConfig) }
if err := measurement.Register(metricsServerLatencyConfig.Name, create); err != nil {
klog.Errorf("cannot register %s: %v", metricsServerLatencyConfig.Name, err)
}
create = func() measurement.Measurement { return createProber(dnsPropagationConfig) }
if err := measurement.Register(dnsPropagationConfig.Name, create); err != nil {
klog.Errorf("cannot register %s: %v", dnsPropagationConfig.Name, err)
}
}
type proberConfig struct {
Name string
MetricVersion string
Query string
Manifests string
ProbeLabelValues []string
ProberType string
}
func createProber(config proberConfig) measurement.Measurement {
return &probesMeasurement{
config: config,
}
}
type probesMeasurement struct {
config proberConfig
framework *framework.Framework
replicasPerProbe int
templateMapping map[string]interface{}
startTime time.Time
}
// Execute supports two actions:
// - start - starts probes and sets up monitoring
// - gather - Gathers and prints metrics.
func (p *probesMeasurement) Execute(config *measurement.Config) ([]measurement.Summary, error) {
if !config.CloudProvider.Features().SupportProbe {
klog.V(1).Infof("%s: Probes cannot work in %s, skipping the measurement!", p, config.CloudProvider.Name())
return nil, nil
}
if config.PrometheusFramework == nil {
klog.Warningf("%s: Prometheus is disabled, skipping the measurement!", p)
return nil, nil
}
action, err := util.GetString(config.Params, "action")
if err != nil {
return nil, err
}
switch action {
case "start":
return nil, p.start(config)
case "gather":
summary, err := p.gather(config.Params)
if err != nil && !errors.IsMetricViolationError(err) {
return nil, err
}
return []measurement.Summary{summary}, err
default:
return nil, fmt.Errorf("unknown action %v", action)
}
}
// Dispose cleans up after the measurement.
func (p *probesMeasurement) Dispose() {
if p.framework == nil {
klog.V(1).Infof("Probe %s wasn't started, skipping the Dispose() step", p)
return
}
klog.V(2).Infof("Stopping %s probe...", p)
k8sClient := p.framework.GetClientSets().GetClient()
if err := client.DeleteNamespace(k8sClient, probesNamespace); err != nil {
klog.Errorf("error while deleting %s namespace: %v", probesNamespace, err)
}
if err := client.WaitForDeleteNamespace(k8sClient, probesNamespace, client.DefaultNamespaceDeletionTimeout); err != nil {
klog.Errorf("error while waiting for %s namespace to be deleted: %v", probesNamespace, err)
}
}
// String returns string representation of this measurement.
func (p *probesMeasurement) String() string {
return p.config.Name
}
func (p *probesMeasurement) initialize(config *measurement.Config) error {
replicasPerProbe, err := util.GetInt(config.Params, "replicasPerProbe")
if err != nil {
return err
}
pingSleepDuration, err := util.GetDuration(config.Params, "pingSleepDuration")
if err != nil {
pingSleepDuration = defaultPingSleepDuration
}
p.framework = config.ClusterFramework
p.replicasPerProbe = replicasPerProbe
p.templateMapping = map[string]interface{}{
"Replicas": replicasPerProbe,
"PingSleepDuration": pingSleepDuration,
}
if p.config.Name == "DnsPropagation" {
dnsPropagationTemplateMapping, err := InitializeTemplateMappingForDNSPropagationProbe(config)
if err != nil {
return err
}
for k, v := range dnsPropagationTemplateMapping {
p.templateMapping[k] = v
}
}
return nil
}
func (p *probesMeasurement) start(config *measurement.Config) error {
klog.V(2).Infof("Starting %s probe...", p)
if !p.startTime.IsZero() {
return fmt.Errorf("measurement %s cannot be started twice", p)
}
if err := p.initialize(config); err != nil {
return err
}
k8sClient := p.framework.GetClientSets().GetClient()
if err := client.CreateNamespace(k8sClient, probesNamespace); err != nil {
return err
}
if err := p.createProbesObjects(); err != nil {
return err
}
if err := p.waitForProbesReady(config); err != nil {
return err
}
p.startTime = time.Now()
return nil
}
func (p *probesMeasurement) gather(params map[string]interface{}) (measurement.Summary, error) {
klog.V(2).Info("Gathering metrics from probes...")
if p.startTime.IsZero() {
return nil, fmt.Errorf("measurement %s has not been started", p)
}
threshold, err := util.GetDurationOrDefault(params, "threshold", 0)
if err != nil {
return nil, err
}
measurementEnd := time.Now()
query := prepareQuery(p.config.Query, p.startTime, measurementEnd)
pc := prom.NewInClusterPrometheusClient(p.framework.GetClientSets().GetClient())
executor := measurementutil.NewQueryExecutor(pc)
samples, err := executor.Query(query, measurementEnd)
if err != nil {
return nil, err
}
var latency *measurementutil.LatencyMetric
if p.config.ProberType == "propagation" {
latency, err = NewPropagationMetricPrometheus(samples)
} else {
latency, err = measurementutil.NewLatencyMetricPrometheus(samples)
}
if err != nil {
return nil, err
}
var violation error
prefix, suffix := "", ""
if threshold > 0 {
suffix = fmt.Sprintf(", expected perc99 <= %v", threshold)
if err := latency.VerifyThreshold(threshold); err != nil {
violation = errors.NewMetricViolationError(p.String(), err.Error())
prefix = " WARNING"
}
}
klog.V(2).Infof("%s:%s got %v%s", p, prefix, latency, suffix)
summary, err := p.createSummary(*latency)
if err != nil {
return nil, err
}
return summary, violation
}
func (p *probesMeasurement) createProbesObjects() error {
return p.framework.ApplyTemplatedManifests(manifestsFS, path.Join(manifestsPathPrefix, p.config.Manifests), p.templateMapping)
}
func (p *probesMeasurement) waitForProbesReady(config *measurement.Config) error {
klog.V(2).Infof("Waiting for Probe %s to become ready...", p)
checkProbesReadyTimeout, err := util.GetDurationOrDefault(config.Params, "checkProbesReadyTimeout", defaultCheckProbesReadyTimeout)
if err != nil {
return err
}
return wait.Poll(checkProbesReadyInterval, checkProbesReadyTimeout, p.checkProbesReady)
}
func (p *probesMeasurement) checkProbesReady() (bool, error) {
// TODO(mm4tt): Using prometheus targets to check whether probes are up is a bit hacky.
// Consider rewriting this to something more intuitive.
selector := func(t prometheus.Target) bool {
for _, value := range p.config.ProbeLabelValues {
// NOTE(oxddr): Prometheus does some translation of labels. Labels here are not the same as labels defined on a monitored pod.
if t.Labels["job"] == value && t.Labels["namespace"] == probesNamespace {
return true
}
}
return false
}
expectedTargets := p.replicasPerProbe * len(p.config.ProbeLabelValues)
return prometheus.CheckAllTargetsReady(
p.framework.GetClientSets().GetClient(), selector, expectedTargets)
}
func (p *probesMeasurement) createSummary(latency measurementutil.LatencyMetric) (measurement.Summary, error) {
content, err := util.PrettyPrintJSON(&measurementutil.PerfData{
Version: p.config.MetricVersion,
DataItems: []measurementutil.DataItem{latency.ToPerfData(p.String())},
})
if err != nil {
return nil, err
}
return measurement.CreateSummary(p.String(), "json", content), nil
}
func prepareQuery(queryTemplate string, startTime, endTime time.Time) string {
measurementDuration := endTime.Sub(startTime)
if !strings.Contains(queryTemplate, "%v") {
return queryTemplate
}
return fmt.Sprintf(queryTemplate, measurementutil.ToPrometheusTime(measurementDuration))
}