forked from kubernetes/kube-state-metrics
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdiscovery_test.go
434 lines (397 loc) · 12 KB
/
discovery_test.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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
/*
Copyright 2023 The Kubernetes Authors All rights reserved.
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 e2e
import (
"context"
"net"
"os"
"os/exec"
"strings"
"testing"
"time"
"k8s.io/apimachinery/pkg/util/wait"
"k8s.io/klog/v2"
"k8s.io/kube-state-metrics/v2/internal"
"k8s.io/kube-state-metrics/v2/internal/discovery"
"k8s.io/kube-state-metrics/v2/pkg/options"
)
// PopulateTimeout is the timeout on populating the cache for the first time.
const PopulateTimeout = 10 * time.Second
type resourceManager struct {
crConfigFile *os.File
initCrdFile *os.File
initCrFile *os.File
newCrdFile *os.File
newCrFile *os.File
}
func (rm *resourceManager) createConfigAndResourceFiles(t *testing.T) {
crConfigFile, err := os.CreateTemp("", "cr-config.yaml")
if err != nil {
t.Fatal(err)
}
rm.crConfigFile = crConfigFile
initCrdFile, err := os.CreateTemp("", "crd.yaml")
if err != nil {
t.Fatal(err)
}
rm.initCrdFile = initCrdFile
initCrFile, err := os.CreateTemp("", "cr.yaml")
if err != nil {
t.Fatal(err)
}
rm.initCrFile = initCrFile
newCrdFile, err := os.CreateTemp("", "new-crd.yaml")
if err != nil {
t.Fatal(err)
}
rm.newCrdFile = newCrdFile
newCrFile, err := os.CreateTemp("", "new-cr.yaml")
if err != nil {
t.Fatal(err)
}
rm.newCrFile = newCrFile
klog.InfoS("testdata", "crConfigFile", crConfigFile.Name(), "initCrdFile", initCrdFile.Name(), "initCrFile", initCrFile.Name(), "newCrdFile", newCrdFile.Name(), "newCrFile", newCrFile.Name())
}
func (rm *resourceManager) removeResourceFiles(t *testing.T) {
err := os.Remove(rm.crConfigFile.Name())
if err != nil {
t.Fatalf("failed to remove CR config: %v", err)
}
err = os.Remove(rm.initCrdFile.Name())
if err != nil {
t.Fatalf("failed to remove initial CRD manifest: %v", err)
}
err = os.Remove(rm.initCrFile.Name())
if err != nil {
t.Fatalf("failed to remove initial CR manifest: %v", err)
}
err = os.Remove(rm.newCrdFile.Name())
if err != nil {
t.Fatalf("failed to remove new CRD manifest: %v", err)
}
err = os.Remove(rm.newCrFile.Name())
if err != nil {
t.Fatalf("failed to remove new CR manifest: %v", err)
}
klog.InfoS("deleted artefacts", "crConfigFile", rm.crConfigFile.Name(), "initCrdFile", rm.initCrdFile.Name(), "initCrFile", rm.initCrFile.Name(), "newCrdFile", rm.newCrdFile.Name(), "newCrFile", rm.newCrFile.Name())
}
func (rm *resourceManager) writeConfigFile(t *testing.T) {
crConfig := getCRConfig()
configFile := rm.crConfigFile.Name()
err := os.WriteFile(configFile, []byte(crConfig), 0600 /* rw------- */)
if err != nil {
t.Fatalf("cannot write to config file: %v", err)
}
klog.InfoS("populated cr config file", "crConfigFile", configFile)
}
func (rm *resourceManager) writeResourceFiles(t *testing.T) {
initCr := getCR()
initCrd := getCRD()
newCr := getNewCR()
newCrd := getNewCRD()
err := os.WriteFile(rm.initCrdFile.Name(), []byte(initCrd), 0600 /* rw------- */)
if err != nil {
t.Fatalf("cannot write to initial crd file: %v", err)
}
err = os.WriteFile(rm.initCrFile.Name(), []byte(initCr), 0600 /* rw------- */)
if err != nil {
t.Fatalf("cannot write to initial cr file: %v", err)
}
err = os.WriteFile(rm.newCrdFile.Name(), []byte(newCrd), 0600 /* rw------- */)
if err != nil {
t.Fatalf("cannot write to new crd file: %v", err)
}
err = os.WriteFile(rm.newCrFile.Name(), []byte(newCr), 0600 /* rw------- */)
if err != nil {
t.Fatalf("cannot write to new cr file: %v", err)
}
klog.InfoS("created initial and new CR and CRD manifests")
}
func TestVariableVKsDiscoveryAndResolution(t *testing.T) {
rm := &resourceManager{}
// Create testdata.
rm.createConfigAndResourceFiles(t)
// Initialise options.
opts := options.NewOptions()
cmd := options.InitCommand
opts.AddFlags(cmd)
klog.InfoS("options", "options", opts)
// Delete artefacts.
defer rm.removeResourceFiles(t)
// Populate options, and parse them.
opts.CustomResourceConfigFile = rm.crConfigFile.Name()
opts.Kubeconfig = os.Getenv("HOME") + "/.kube/config"
if err := opts.Parse(); err != nil {
t.Fatalf("failed to parse options: %v", err)
}
klog.InfoS("parsed options", "options", opts)
// Write to the config file.
rm.writeConfigFile(t)
// Make the process asynchronous.
go internal.RunKubeStateMetricsWrapper(opts)
klog.InfoS("started KSM")
// Wait for port 8080 to come up.
err := wait.PollUntilContextTimeout(context.TODO(), 1*time.Second, 20*time.Second, true, func(_ context.Context) (bool, error) {
conn, err := net.Dial("tcp", "localhost:8080")
if err != nil {
return false, nil
}
err = conn.Close()
if err != nil {
return false, err
}
return true, nil
})
if err != nil {
t.Fatalf("failed while waiting for port 8080 to come up: %v", err)
}
klog.InfoS("port 8080 up")
// Create CRD and CR files.
rm.writeResourceFiles(t)
// Apply initial CRD and CR to the cluster.
err = exec.Command("kubectl", "apply", "-f", rm.initCrdFile.Name()).Run() //nolint:gosec
if err != nil {
t.Fatalf("failed to apply initial crd: %v", err)
}
err = exec.Command("kubectl", "apply", "-f", rm.initCrFile.Name()).Run() //nolint:gosec
if err != nil {
t.Fatalf("failed to apply initial cr: %v", err)
}
klog.InfoS("applied initial CR and CRD manifests")
// Wait for the metric to be available.
ch := make(chan bool, 1)
klog.InfoS("waiting for first metrics to become available")
testMetric := `kube_customresource_test_metric{customresource_group="contoso.com",customresource_kind="MyPlatform",customresource_version="v1alpha1",name="test-dotnet-app"}`
err = wait.PollUntilContextTimeout(context.TODO(), discovery.Interval, PopulateTimeout, true, func(_ context.Context) (bool, error) {
out, err := exec.Command("curl", "localhost:8080/metrics").Output()
if err != nil {
return false, err
}
if string(out) == "" {
return false, nil
}
// Note: we use count to make sure that only one metrics handler is running
if strings.Count(string(out), testMetric) == 1 {
// klog.InfoS("metrics available", "metric", string(out))
// Signal the process to exit, since we know the metrics are being generated as expected.
ch <- true
return true, nil
}
return false, nil
})
if err != nil {
t.Fatalf("failed while waiting for initial metrics to be available: %v", err)
}
// Wait for process to exit.
select {
case <-ch:
t.Log("initial metrics are available")
case <-time.After(PopulateTimeout * 2):
t.Fatal("timed out waiting for test to pass, check the logs for more info")
}
// Apply new CRD and CR to the cluster.
err = exec.Command("kubectl", "apply", "-f", rm.newCrdFile.Name()).Run() //nolint:gosec
if err != nil {
t.Fatalf("failed to apply new crd: %v", err)
}
err = exec.Command("kubectl", "apply", "-f", rm.newCrFile.Name()).Run() //nolint:gosec
if err != nil {
t.Fatalf("failed to apply new cr: %v", err)
}
err = exec.Command("kubectl", "delete", "myplatform", "test-dotnet-app").Run() //nolint:gosec
if err != nil {
t.Fatalf("failed to delete myplatform resource: %v", err)
}
klog.InfoS("applied new CR and CRD manifests")
// Wait for the the new metric to be available
ch = make(chan bool, 1)
klog.InfoS("waiting for new metrics to become available")
testUpdateCRDMetric := `kube_customresource_test_update_crd_metric{customresource_group="contoso.com",customresource_kind="Update",customresource_version="v1",name="test-dotnet-app-update"}`
err = wait.PollUntilContextTimeout(context.TODO(), discovery.Interval, PopulateTimeout, true, func(_ context.Context) (bool, error) {
out, err := exec.Command("curl", "localhost:8080/metrics").Output()
if err != nil {
return false, err
}
if string(out) == "" {
return false, nil
}
// Note: we use count to make sure that only one metrics handler is running, and we also want to validate that the
// new metric is available and the old one was removed, otherwise, the response could come from the
// previous handler before its context was cancelled, or maybe because it failed to be cancelled.
if strings.Contains(string(out), testUpdateCRDMetric) && !strings.Contains(string(out), testMetric) {
klog.InfoS("metrics available", "metric", string(out))
// Signal the process to exit, since we know the metrics are being generated as expected.
ch <- true
return true, nil
}
klog.InfoS("metrics available", "metric", string(out))
return false, nil
})
if err != nil {
t.Fatalf("failed while waiting for new metrics to be available: %v", err)
}
// Wait for process to exit.
select {
case <-ch:
t.Log("test passed successfully")
case <-time.After(PopulateTimeout * 2):
t.Fatal("timed out waiting for test to pass, check the logs for more info")
}
}
func getCR() string {
return `
apiVersion: contoso.com/v1alpha1
kind: MyPlatform
metadata:
name: test-dotnet-app
spec:
appId: testdotnetapp
language: csharp
os: linux
instanceSize: small
environmentType: dev
replicas: 3
`
}
func getCRD() string {
return `
apiVersion: apiextensions.k8s.io/v1
kind: CustomResourceDefinition
metadata:
name: myplatforms.contoso.com
spec:
group: contoso.com
names:
plural: myplatforms
singular: myplatform
kind: MyPlatform
shortNames:
- myp
scope: Namespaced
versions:
- name: v1alpha1
served: true
storage: true
schema:
openAPIV3Schema:
type: object
properties:
spec:
type: object
properties:
appId:
type: string
language:
type: string
enum:
- csharp
- python
- go
os:
type: string
enum:
- windows
- linux
instanceSize:
type: string
enum:
- small
- medium
- large
environmentType:
type: string
enum:
- dev
- test
- prod
replicas:
type: integer
minimum: 1
required: ["appId", "language", "environmentType"]
required: ["spec"]
`
}
func getCRConfig() string {
return `
kind: CustomResourceStateMetrics
spec:
resources:
- groupVersionKind:
group: "contoso.com"
version: "v1alpha1"
kind: "MyPlatform"
metrics:
- name: "test_metric"
help: "foo baz"
each:
type: Info
info:
path: [metadata]
labelsFromPath:
name: [name]
- groupVersionKind:
group: "contoso.com"
version: "v1"
kind: "Update"
metrics:
- name: "test_update_crd_metric"
help: "foo baz"
each:
type: Info
info:
path: [metadata]
labelsFromPath:
name: [name]
`
}
func getNewCR() string {
return `
apiVersion: contoso.com/v1
kind: Update
metadata:
name: test-dotnet-app-update
spec:
new: just-added
`
}
func getNewCRD() string {
return `
apiVersion: apiextensions.k8s.io/v1
kind: CustomResourceDefinition
metadata:
name: updates.contoso.com
spec:
group: contoso.com
names:
plural: updates
singular: update
kind: Update
shortNames:
- updt
scope: Namespaced
versions:
- name: v1
served: true
storage: true
schema:
openAPIV3Schema:
type: object
properties:
spec:
type: object
properties:
new:
type: string
required: ["spec"]
`
}