-
Notifications
You must be signed in to change notification settings - Fork 114
/
Copy pathcollector_test.go
953 lines (853 loc) · 30 KB
/
collector_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
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
package telemetry_test
import (
"context"
"errors"
"reflect"
"runtime"
tel "github.com/nginx/telemetry-exporter/pkg/telemetry"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
appsv1 "k8s.io/api/apps/v1"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/types"
"sigs.k8s.io/controller-runtime/pkg/client"
gatewayv1 "sigs.k8s.io/gateway-api/apis/v1"
ngfAPI "github.com/nginx/nginx-gateway-fabric/apis/v1alpha1"
"github.com/nginx/nginx-gateway-fabric/internal/framework/kinds"
"github.com/nginx/nginx-gateway-fabric/internal/framework/kubernetes/kubernetesfakes"
"github.com/nginx/nginx-gateway-fabric/internal/mode/static/config"
"github.com/nginx/nginx-gateway-fabric/internal/mode/static/state/dataplane"
"github.com/nginx/nginx-gateway-fabric/internal/mode/static/state/graph"
"github.com/nginx/nginx-gateway-fabric/internal/mode/static/state/resolver"
"github.com/nginx/nginx-gateway-fabric/internal/mode/static/telemetry"
"github.com/nginx/nginx-gateway-fabric/internal/mode/static/telemetry/telemetryfakes"
)
type listCallsFunc = func(
context.Context,
client.ObjectList,
...client.ListOption,
) error
func createListCallsFunc(objects ...client.ObjectList) listCallsFunc {
return func(_ context.Context, object client.ObjectList, option ...client.ListOption) error {
Expect(option).To(BeEmpty())
for _, obj := range objects {
if reflect.TypeOf(obj) == reflect.TypeOf(object) {
reflect.ValueOf(object).Elem().Set(reflect.ValueOf(obj).Elem())
return nil
}
}
return nil
}
}
type getCallsFunc = func(
context.Context,
types.NamespacedName,
client.Object,
...client.GetOption,
) error
func createGetCallsFunc(objects ...client.Object) getCallsFunc {
return func(_ context.Context, _ types.NamespacedName, object client.Object, option ...client.GetOption) error {
Expect(option).To(BeEmpty())
for _, obj := range objects {
if reflect.TypeOf(obj) == reflect.TypeOf(object) {
reflect.ValueOf(object).Elem().Set(reflect.ValueOf(obj).Elem())
return nil
}
}
return nil
}
}
var _ = Describe("Collector", Ordered, func() {
var (
k8sClientReader *kubernetesfakes.FakeReader
fakeGraphGetter *telemetryfakes.FakeGraphGetter
fakeConfigurationGetter *telemetryfakes.FakeConfigurationGetter
dataCollector telemetry.DataCollector
version string
expData telemetry.Data
podNSName types.NamespacedName
ngfPod *v1.Pod
ngfReplicaSet *appsv1.ReplicaSet
kubeNamespace *v1.Namespace
baseGetCalls getCallsFunc
baseListCalls listCallsFunc
flags config.Flags
nodeList *v1.NodeList
)
BeforeAll(func() {
version = "1.1"
ngfPod = &v1.Pod{
ObjectMeta: metav1.ObjectMeta{
Name: "pod1",
OwnerReferences: []metav1.OwnerReference{
{
Kind: "ReplicaSet",
Name: "replicaset1",
},
},
},
}
replicas := int32(1)
ngfReplicaSet = &appsv1.ReplicaSet{
Spec: appsv1.ReplicaSetSpec{
Replicas: &replicas,
},
ObjectMeta: metav1.ObjectMeta{
Name: "replica",
OwnerReferences: []metav1.OwnerReference{
{
Kind: "Deployment",
Name: "Deployment1",
UID: "test-uid-replicaSet",
},
},
},
}
podNSName = types.NamespacedName{
Namespace: "nginx-gateway",
Name: "ngf-pod",
}
kubeNamespace = &v1.Namespace{
ObjectMeta: metav1.ObjectMeta{
Name: metav1.NamespaceSystem,
UID: "test-uid",
},
}
flags = config.Flags{
Names: []string{"boolFlag", "intFlag", "stringFlag"},
Values: []string{"false", "default", "user-defined"},
}
nodeList = &v1.NodeList{
Items: []v1.Node{
{
ObjectMeta: metav1.ObjectMeta{
Name: "node1",
},
Spec: v1.NodeSpec{
ProviderID: "k3s://ip-172-16-0-210",
},
Status: v1.NodeStatus{
NodeInfo: v1.NodeSystemInfo{
KubeletVersion: "v1.28.6+k3s2",
},
},
},
},
}
})
BeforeEach(func() {
expData = telemetry.Data{
Data: tel.Data{
ProjectName: "NGF",
ProjectVersion: version,
ProjectArchitecture: runtime.GOARCH,
ClusterID: string(kubeNamespace.GetUID()),
ClusterVersion: "1.28.6",
ClusterPlatform: "k3s",
InstallationID: string(ngfReplicaSet.ObjectMeta.OwnerReferences[0].UID),
ClusterNodeCount: 1,
},
NGFResourceCounts: telemetry.NGFResourceCounts{},
NGFReplicaCount: 1,
ImageSource: "local",
FlagNames: flags.Names,
FlagValues: flags.Values,
SnippetsFiltersDirectives: []string{},
SnippetsFiltersDirectivesCount: []int64{},
}
k8sClientReader = &kubernetesfakes.FakeReader{}
fakeGraphGetter = &telemetryfakes.FakeGraphGetter{}
fakeConfigurationGetter = &telemetryfakes.FakeConfigurationGetter{}
fakeGraphGetter.GetLatestGraphReturns(&graph.Graph{})
fakeConfigurationGetter.GetLatestConfigurationReturns(&dataplane.Configuration{})
dataCollector = telemetry.NewDataCollectorImpl(telemetry.DataCollectorConfig{
K8sClientReader: k8sClientReader,
GraphGetter: fakeGraphGetter,
ConfigurationGetter: fakeConfigurationGetter,
Version: version,
PodNSName: podNSName,
ImageSource: "local",
Flags: flags,
})
baseGetCalls = createGetCallsFunc(ngfPod, ngfReplicaSet, kubeNamespace)
k8sClientReader.GetCalls(baseGetCalls)
baseListCalls = createListCallsFunc(nodeList)
k8sClientReader.ListCalls(baseListCalls)
})
mergeGetCallsWithBase := func(f getCallsFunc) getCallsFunc {
return func(
ctx context.Context,
nsName types.NamespacedName,
object client.Object,
option ...client.GetOption,
) error {
err := baseGetCalls(ctx, nsName, object, option...)
Expect(err).ToNot(HaveOccurred())
return f(ctx, nsName, object, option...)
}
}
mergeListCallsWithBase := func(f listCallsFunc) listCallsFunc {
return func(
ctx context.Context,
object client.ObjectList,
option ...client.ListOption,
) error {
err := baseListCalls(ctx, object, option...)
Expect(err).ToNot(HaveOccurred())
return f(ctx, object, option...)
}
}
Describe("Normal case", func() {
When("collecting telemetry data", func() {
It("collects all fields", func(ctx SpecContext) {
nodes := &v1.NodeList{
Items: []v1.Node{
{
ObjectMeta: metav1.ObjectMeta{
Name: "node1",
},
Spec: v1.NodeSpec{
ProviderID: "kind://docker/kind/kind-control-plane",
},
Status: v1.NodeStatus{
NodeInfo: v1.NodeSystemInfo{
KubeletVersion: "v1.29.2",
},
},
},
{
ObjectMeta: metav1.ObjectMeta{
Name: "node2",
},
},
{
ObjectMeta: metav1.ObjectMeta{
Name: "node3",
},
},
},
}
k8sClientReader.ListCalls(createListCallsFunc(nodes))
secret1 := &v1.Secret{ObjectMeta: metav1.ObjectMeta{Name: "secret1"}}
secret2 := &v1.Secret{ObjectMeta: metav1.ObjectMeta{Name: "secret2"}}
nilsecret := &v1.Secret{ObjectMeta: metav1.ObjectMeta{Name: "nilsecret"}}
svc1 := &v1.Service{ObjectMeta: metav1.ObjectMeta{Name: "svc1"}}
svc2 := &v1.Service{ObjectMeta: metav1.ObjectMeta{Name: "svc2"}}
nilsvc := &v1.Service{ObjectMeta: metav1.ObjectMeta{Name: "nilsvc"}}
graph := &graph.Graph{
GatewayClass: &graph.GatewayClass{},
Gateway: &graph.Gateway{},
IgnoredGatewayClasses: map[types.NamespacedName]*gatewayv1.GatewayClass{
{Name: "ignoredGC1"}: {},
{Name: "ignoredGC2"}: {},
},
IgnoredGateways: map[types.NamespacedName]*gatewayv1.Gateway{
{Name: "ignoredGw1"}: {},
{Name: "ignoredGw2"}: {},
},
Routes: map[graph.RouteKey]*graph.L7Route{
{NamespacedName: types.NamespacedName{Namespace: "test", Name: "hr-1"}}: {RouteType: graph.RouteTypeHTTP},
{NamespacedName: types.NamespacedName{Namespace: "test", Name: "hr-2"}}: {RouteType: graph.RouteTypeHTTP},
{NamespacedName: types.NamespacedName{Namespace: "test", Name: "hr-3"}}: {RouteType: graph.RouteTypeHTTP},
{NamespacedName: types.NamespacedName{Namespace: "test", Name: "gr-1"}}: {RouteType: graph.RouteTypeGRPC},
{NamespacedName: types.NamespacedName{Namespace: "test", Name: "gr-2"}}: {RouteType: graph.RouteTypeGRPC},
},
L4Routes: map[graph.L4RouteKey]*graph.L4Route{
{NamespacedName: types.NamespacedName{Namespace: "test", Name: "tr-1"}}: {},
{NamespacedName: types.NamespacedName{Namespace: "test", Name: "tr-2"}}: {},
{NamespacedName: types.NamespacedName{Namespace: "test", Name: "tr-3"}}: {},
},
ReferencedSecrets: map[types.NamespacedName]*graph.Secret{
client.ObjectKeyFromObject(secret1): {
Source: secret1,
},
client.ObjectKeyFromObject(secret2): {
Source: secret2,
},
client.ObjectKeyFromObject(nilsecret): nil,
},
ReferencedServices: map[types.NamespacedName]*graph.ReferencedService{
client.ObjectKeyFromObject(svc1): {},
client.ObjectKeyFromObject(svc2): {},
client.ObjectKeyFromObject(nilsvc): {},
},
BackendTLSPolicies: map[types.NamespacedName]*graph.BackendTLSPolicy{
{Namespace: "test", Name: "backendTLSPolicy-1"}: {},
{Namespace: "test", Name: "backendTLSPolicy-2"}: {},
{Namespace: "test", Name: "backendTLSPolicy-3"}: {},
},
NGFPolicies: map[graph.PolicyKey]*graph.Policy{
{
NsName: types.NamespacedName{Namespace: "test", Name: "ClientSettingsPolicy-1"},
GVK: schema.GroupVersionKind{Kind: kinds.ClientSettingsPolicy},
}: {TargetRefs: []graph.PolicyTargetRef{{Kind: kinds.Gateway}}},
{
NsName: types.NamespacedName{Namespace: "test", Name: "ClientSettingsPolicy-2"},
GVK: schema.GroupVersionKind{Kind: kinds.ClientSettingsPolicy},
}: {TargetRefs: []graph.PolicyTargetRef{{Kind: kinds.HTTPRoute}}},
{
NsName: types.NamespacedName{Namespace: "test", Name: "ClientSettingsPolicy-3"},
GVK: schema.GroupVersionKind{Kind: kinds.ClientSettingsPolicy},
}: {TargetRefs: []graph.PolicyTargetRef{{Kind: kinds.GRPCRoute}}},
{
NsName: types.NamespacedName{Namespace: "test", Name: "ObservabilityPolicy-1"},
GVK: schema.GroupVersionKind{Kind: kinds.ObservabilityPolicy},
}: {},
{
NsName: types.NamespacedName{Namespace: "test", Name: "UpstreamSettingsPolicy-1"},
GVK: schema.GroupVersionKind{Kind: kinds.UpstreamSettingsPolicy},
}: {},
},
ReferencedNginxProxies: map[types.NamespacedName]*graph.NginxProxy{
{Namespace: "test", Name: "NginxProxy-1"}: {},
{Namespace: "test", Name: "NginxProxy-2"}: {},
}, SnippetsFilters: map[types.NamespacedName]*graph.SnippetsFilter{
{Namespace: "test", Name: "sf-1"}: {
Snippets: map[ngfAPI.NginxContext]string{
ngfAPI.NginxContextMain: "worker_priority 0;",
ngfAPI.NginxContextHTTP: "aio on;",
ngfAPI.NginxContextHTTPServer: "auth_delay 10s;",
ngfAPI.NginxContextHTTPServerLocation: "keepalive_time 10s;",
},
},
{Namespace: "test", Name: "sf-2"}: {
Snippets: map[ngfAPI.NginxContext]string{
// String representation of multi-line yaml value using > character
ngfAPI.NginxContextMain: "worker_priority 1; worker_rlimit_nofile 50;\n",
// String representation of NGINX values on same line
ngfAPI.NginxContextHTTP: "aio off; client_body_timeout 70s;",
// String representation of multi-line yaml using no special character besides a new line
ngfAPI.NginxContextHTTPServer: "auth_delay 100s; ignore_invalid_headers off;",
// String representation of multi-line yaml value using | character
ngfAPI.NginxContextHTTPServerLocation: "keepalive_time 100s;\nallow 10.0.0.0/8;\n",
},
},
{Namespace: "test", Name: "sf-3"}: {
Snippets: map[ngfAPI.NginxContext]string{
// Tests lexicographical ordering when count and context is the same
ngfAPI.NginxContextMain: "worker_rlimit_core 1m;",
ngfAPI.NginxContextHTTPServer: "auth_delay 10s;",
},
},
},
}
config := &dataplane.Configuration{
Upstreams: []dataplane.Upstream{
{
Name: "upstream1",
ErrorMsg: "",
Endpoints: []resolver.Endpoint{
{
Address: "endpoint1",
Port: 80,
}, {
Address: "endpoint2",
Port: 80,
}, {
Address: "endpoint3",
Port: 80,
},
},
},
{
Name: "upstream2",
ErrorMsg: "",
Endpoints: []resolver.Endpoint{
{
Address: "endpoint1",
Port: 80,
},
},
},
},
}
fakeGraphGetter.GetLatestGraphReturns(graph)
fakeConfigurationGetter.GetLatestConfigurationReturns(config)
expData.ClusterNodeCount = 3
expData.NGFResourceCounts = telemetry.NGFResourceCounts{
GatewayCount: 3,
GatewayClassCount: 3,
HTTPRouteCount: 3,
TLSRouteCount: 3,
SecretCount: 3,
ServiceCount: 3,
EndpointCount: 4,
GRPCRouteCount: 2,
BackendTLSPolicyCount: 3,
GatewayAttachedClientSettingsPolicyCount: 1,
RouteAttachedClientSettingsPolicyCount: 2,
ObservabilityPolicyCount: 1,
NginxProxyCount: 2,
SnippetsFilterCount: 3,
UpstreamSettingsPolicyCount: 1,
}
expData.ClusterVersion = "1.29.2"
expData.ClusterPlatform = "kind"
expData.SnippetsFiltersDirectives = []string{
"auth_delay-server",
"aio-http",
"keepalive_time-location",
"worker_priority-main",
"client_body_timeout-http",
"allow-location",
"worker_rlimit_core-main",
"worker_rlimit_nofile-main",
"ignore_invalid_headers-server",
}
expData.SnippetsFiltersDirectivesCount = []int64{
3,
2,
2,
2,
1,
1,
1,
1,
1,
}
data, err := dataCollector.Collect(ctx)
Expect(err).ToNot(HaveOccurred())
Expect(data).To(Equal(expData))
})
})
})
Describe("cluster information collector", func() {
When("collecting cluster platform", func() {
When("it encounters an error while collecting data", func() {
It("should error if the kubernetes client errored when getting the NamespaceList", func(ctx SpecContext) {
expectedError := errors.New("failed to get NamespaceList")
k8sClientReader.ListCalls(mergeListCallsWithBase(
func(_ context.Context, object client.ObjectList, _ ...client.ListOption) error {
switch object.(type) {
case *v1.NamespaceList:
return expectedError
default:
return nil
}
}))
_, err := dataCollector.Collect(ctx)
Expect(err).To(MatchError(expectedError))
})
})
})
When("collecting clusterID data", func() {
When("it encounters an error while collecting data", func() {
It("should error if the kubernetes client errored when getting the namespace", func(ctx SpecContext) {
expectedError := errors.New("there was an error getting clusterID")
k8sClientReader.GetCalls(mergeGetCallsWithBase(
func(_ context.Context, _ types.NamespacedName, object client.Object, _ ...client.GetOption) error {
switch object.(type) {
case *v1.Namespace:
return expectedError
default:
return nil
}
}))
_, err := dataCollector.Collect(ctx)
Expect(err).To(MatchError(expectedError))
})
})
})
When("collecting cluster version data", func() {
When("the kubelet version is missing", func() {
It("should be report 'unknown'", func(ctx SpecContext) {
nodes := &v1.NodeList{
Items: []v1.Node{
{
ObjectMeta: metav1.ObjectMeta{
Name: "node1",
},
Spec: v1.NodeSpec{
ProviderID: "k3s://ip-172-16-0-210",
},
},
},
}
k8sClientReader.ListCalls(createListCallsFunc(nodes))
expData.ClusterVersion = "unknown"
expData.ClusterPlatform = "k3s"
data, err := dataCollector.Collect(ctx)
Expect(err).ToNot(HaveOccurred())
Expect(expData).To(Equal(data))
})
})
})
})
Describe("node count collector", func() {
When("collecting node count data", func() {
It("collects correct data for one node", func(ctx SpecContext) {
k8sClientReader.ListCalls(createListCallsFunc(nodeList))
expData.Data.ClusterNodeCount = 1
data, err := dataCollector.Collect(ctx)
Expect(err).ToNot(HaveOccurred())
Expect(expData).To(Equal(data))
})
When("it encounters an error while collecting data", func() {
It("should error when there are no nodes", func(ctx SpecContext) {
expectedError := errors.New("failed to collect cluster information: NodeList length is zero")
k8sClientReader.ListCalls(createListCallsFunc(nil))
_, err := dataCollector.Collect(ctx)
Expect(err).To(MatchError(expectedError))
})
It("should error on kubernetes client api errors", func(ctx SpecContext) {
expectedError := errors.New("failed to get NodeList")
k8sClientReader.ListCalls(
func(_ context.Context, object client.ObjectList, _ ...client.ListOption) error {
switch object.(type) {
case *v1.NodeList:
return expectedError
default:
return nil
}
})
_, err := dataCollector.Collect(ctx)
Expect(err).To(MatchError(expectedError))
})
})
})
})
Describe("NGF resource count collector", func() {
var (
graph1 *graph.Graph
config1, invalidUpstreamsConfig *dataplane.Configuration
)
BeforeAll(func() {
secret := &v1.Secret{ObjectMeta: metav1.ObjectMeta{Name: "secret1"}}
svc := &v1.Service{ObjectMeta: metav1.ObjectMeta{Name: "svc1"}}
graph1 = &graph.Graph{
GatewayClass: &graph.GatewayClass{},
Gateway: &graph.Gateway{},
Routes: map[graph.RouteKey]*graph.L7Route{
{NamespacedName: types.NamespacedName{Namespace: "test", Name: "hr-1"}}: {RouteType: graph.RouteTypeHTTP},
},
L4Routes: map[graph.L4RouteKey]*graph.L4Route{
{NamespacedName: types.NamespacedName{Namespace: "test", Name: "tr-1"}}: {},
},
ReferencedSecrets: map[types.NamespacedName]*graph.Secret{
client.ObjectKeyFromObject(secret): {
Source: secret,
},
},
ReferencedServices: map[types.NamespacedName]*graph.ReferencedService{
client.ObjectKeyFromObject(svc): {},
},
NGFPolicies: map[graph.PolicyKey]*graph.Policy{
{
NsName: types.NamespacedName{Namespace: "test", Name: "ClientSettingsPolicy-1"},
GVK: schema.GroupVersionKind{Kind: kinds.ClientSettingsPolicy},
}: {TargetRefs: []graph.PolicyTargetRef{{Kind: kinds.Gateway}}},
{
NsName: types.NamespacedName{Namespace: "test", Name: "ClientSettingsPolicy-2"},
GVK: schema.GroupVersionKind{Kind: kinds.ClientSettingsPolicy},
}: {TargetRefs: []graph.PolicyTargetRef{{Kind: kinds.HTTPRoute}}},
{
NsName: types.NamespacedName{Namespace: "test", Name: "ClientSettingsPolicy-empty"},
GVK: schema.GroupVersionKind{Kind: kinds.ClientSettingsPolicy},
}: {},
{
NsName: types.NamespacedName{Namespace: "test", Name: "ObservabilityPolicy-1"},
GVK: schema.GroupVersionKind{Kind: kinds.ObservabilityPolicy},
}: {},
{
NsName: types.NamespacedName{Namespace: "test", Name: "UpstreamSettingsPolicy-1"},
GVK: schema.GroupVersionKind{Kind: kinds.UpstreamSettingsPolicy},
}: {},
},
ReferencedNginxProxies: map[types.NamespacedName]*graph.NginxProxy{
{Namespace: "test", Name: "NginxProxy-1"}: {},
{Namespace: "test", Name: "NginxProxy-2"}: {},
},
SnippetsFilters: map[types.NamespacedName]*graph.SnippetsFilter{
{Namespace: "test", Name: "sf-1"}: {},
},
}
config1 = &dataplane.Configuration{
Upstreams: []dataplane.Upstream{
{
Name: "upstream1",
ErrorMsg: "",
Endpoints: []resolver.Endpoint{
{
Address: "endpoint1",
Port: 80,
},
},
},
},
}
invalidUpstreamsConfig = &dataplane.Configuration{
Upstreams: []dataplane.Upstream{
{
Name: "invalidUpstream",
ErrorMsg: "there is an error here",
Endpoints: []resolver.Endpoint{
{
Address: "endpoint1",
Port: 80,
}, {
Address: "endpoint2",
Port: 80,
}, {
Address: "endpoint3",
Port: 80,
},
},
},
{
Name: "emptyUpstream",
ErrorMsg: "",
Endpoints: []resolver.Endpoint{},
},
},
}
})
When("collecting NGF resource counts", func() {
It("collects correct data for graph with no resources", func(ctx SpecContext) {
fakeGraphGetter.GetLatestGraphReturns(&graph.Graph{})
fakeConfigurationGetter.GetLatestConfigurationReturns(&dataplane.Configuration{})
expData.NGFResourceCounts = telemetry.NGFResourceCounts{}
data, err := dataCollector.Collect(ctx)
Expect(err).ToNot(HaveOccurred())
Expect(expData).To(Equal(data))
})
It("collects correct data for graph with one of each resource", func(ctx SpecContext) {
fakeGraphGetter.GetLatestGraphReturns(graph1)
fakeConfigurationGetter.GetLatestConfigurationReturns(config1)
expData.NGFResourceCounts = telemetry.NGFResourceCounts{
GatewayCount: 1,
GatewayClassCount: 1,
HTTPRouteCount: 1,
TLSRouteCount: 1,
SecretCount: 1,
ServiceCount: 1,
EndpointCount: 1,
GatewayAttachedClientSettingsPolicyCount: 1,
RouteAttachedClientSettingsPolicyCount: 1,
ObservabilityPolicyCount: 1,
NginxProxyCount: 2,
SnippetsFilterCount: 1,
UpstreamSettingsPolicyCount: 1,
}
data, err := dataCollector.Collect(ctx)
Expect(err).ToNot(HaveOccurred())
Expect(expData).To(Equal(data))
})
It("ignores invalid and empty upstreams", func(ctx SpecContext) {
fakeGraphGetter.GetLatestGraphReturns(&graph.Graph{})
fakeConfigurationGetter.GetLatestConfigurationReturns(invalidUpstreamsConfig)
expData.NGFResourceCounts = telemetry.NGFResourceCounts{}
data, err := dataCollector.Collect(ctx)
Expect(err).ToNot(HaveOccurred())
Expect(expData).To(Equal(data))
})
When("it encounters an error while collecting data", func() {
BeforeEach(func() {
fakeGraphGetter.GetLatestGraphReturns(&graph.Graph{})
fakeConfigurationGetter.GetLatestConfigurationReturns(&dataplane.Configuration{})
})
It("should error on nil latest graph", func(ctx SpecContext) {
expectedError := errors.New("failed to collect telemetry data: latest graph cannot be nil")
fakeGraphGetter.GetLatestGraphReturns(nil)
_, err := dataCollector.Collect(ctx)
Expect(err).To(MatchError(expectedError))
})
})
})
})
Describe("NGF replica count collector", func() {
When("collecting NGF replica count", func() {
When("it encounters an error while collecting data", func() {
It("should error if the kubernetes client errored when getting the Pod", func(ctx SpecContext) {
expectedErr := errors.New("there was an error getting the Pod")
k8sClientReader.GetCalls(mergeGetCallsWithBase(
func(_ context.Context, _ client.ObjectKey, object client.Object, _ ...client.GetOption) error {
switch object.(type) {
case *v1.Pod:
return expectedErr
default:
return nil
}
},
))
_, err := dataCollector.Collect(ctx)
Expect(err).To(MatchError(expectedErr))
})
It("should error if the Pod's owner reference is nil", func(ctx SpecContext) {
expectedErr := errors.New("expected one owner reference of the NGF Pod, got 0")
k8sClientReader.GetCalls(mergeGetCallsWithBase(createGetCallsFunc(
&v1.Pod{
ObjectMeta: metav1.ObjectMeta{
Name: "pod1",
OwnerReferences: nil,
},
},
)))
_, err := dataCollector.Collect(ctx)
Expect(err).To(MatchError(expectedErr))
})
It("should error if the Pod has multiple owner references", func(ctx SpecContext) {
expectedErr := errors.New("expected one owner reference of the NGF Pod, got 2")
k8sClientReader.GetCalls(mergeGetCallsWithBase(createGetCallsFunc(
&v1.Pod{
ObjectMeta: metav1.ObjectMeta{
Name: "pod1",
OwnerReferences: []metav1.OwnerReference{
{
Kind: "ReplicaSet",
Name: "replicaset1",
},
{
Kind: "ReplicaSet",
Name: "replicaset2",
},
},
},
},
)))
_, err := dataCollector.Collect(ctx)
Expect(err).To(MatchError(expectedErr))
})
It("should error if the Pod's owner reference is not a ReplicaSet", func(ctx SpecContext) {
expectedErr := errors.New("expected pod owner reference to be ReplicaSet, got Deployment")
k8sClientReader.GetCalls(mergeGetCallsWithBase(createGetCallsFunc(
&v1.Pod{
ObjectMeta: metav1.ObjectMeta{
Name: "pod1",
OwnerReferences: []metav1.OwnerReference{
{
Kind: "Deployment",
Name: "deployment1",
UID: "replica-uid",
},
},
},
},
)))
_, err := dataCollector.Collect(ctx)
Expect(err).To(MatchError(expectedErr))
})
It("should error if the replica set's replicas is nil", func(ctx SpecContext) {
expectedErr := errors.New("replica set replicas was nil")
k8sClientReader.GetCalls(mergeGetCallsWithBase(createGetCallsFunc(
&appsv1.ReplicaSet{
Spec: appsv1.ReplicaSetSpec{
Replicas: nil,
},
},
)))
_, err := dataCollector.Collect(ctx)
Expect(err).To(MatchError(expectedErr))
})
It("should error if the kubernetes client errored when getting the ReplicaSet", func(ctx SpecContext) {
expectedErr := errors.New("there was an error getting the ReplicaSet")
k8sClientReader.GetCalls(mergeGetCallsWithBase(
func(_ context.Context, _ client.ObjectKey, object client.Object, _ ...client.GetOption) error {
switch object.(type) {
case *appsv1.ReplicaSet:
return expectedErr
default:
return nil
}
}))
_, err := dataCollector.Collect(ctx)
Expect(err).To(MatchError(expectedErr))
})
})
})
})
Describe("DeploymentID collector", func() {
When("collecting deploymentID", func() {
When("it encounters an error while collecting data", func() {
It("should error if the replicaSet's owner reference is nil", func(ctx SpecContext) {
replicas := int32(1)
k8sClientReader.GetCalls(mergeGetCallsWithBase(createGetCallsFunc(
&appsv1.ReplicaSet{
Spec: appsv1.ReplicaSetSpec{
Replicas: &replicas,
},
},
)))
expectedErr := errors.New("expected one owner reference of the NGF ReplicaSet, got 0")
_, err := dataCollector.Collect(ctx)
Expect(err).To(MatchError(expectedErr))
})
It("should error if the replicaSet's owner reference kind is not deployment", func(ctx SpecContext) {
replicas := int32(1)
k8sClientReader.GetCalls(mergeGetCallsWithBase(createGetCallsFunc(
&appsv1.ReplicaSet{
Spec: appsv1.ReplicaSetSpec{
Replicas: &replicas,
},
ObjectMeta: metav1.ObjectMeta{
OwnerReferences: []metav1.OwnerReference{
{
Name: "replica",
Kind: "ReplicaSet",
},
},
},
},
)))
expectedErr := errors.New("expected replicaSet owner reference to be Deployment, got ReplicaSet")
_, err := dataCollector.Collect(ctx)
Expect(err).To(MatchError(expectedErr))
})
It("should error if the replicaSet's owner reference has empty UID", func(ctx SpecContext) {
replicas := int32(1)
k8sClientReader.GetCalls(mergeGetCallsWithBase(createGetCallsFunc(
&appsv1.ReplicaSet{
Spec: appsv1.ReplicaSetSpec{
Replicas: &replicas,
},
ObjectMeta: metav1.ObjectMeta{
OwnerReferences: []metav1.OwnerReference{
{
Name: "replica",
Kind: "Deployment",
},
},
},
},
)))
expectedErr := errors.New("expected replicaSet owner reference to have a UID")
_, err := dataCollector.Collect(ctx)
Expect(err).To(MatchError(expectedErr))
})
})
})
})
Describe("snippetsFilters collector", func() {
When("collecting snippetsFilters data", func() {
It("collects correct data for nil snippetsFilters", func(ctx SpecContext) {
fakeGraphGetter.GetLatestGraphReturns(&graph.Graph{
SnippetsFilters: map[types.NamespacedName]*graph.SnippetsFilter{
{Namespace: "test", Name: "sf-1"}: nil,
},
})
expData.SnippetsFilterCount = 1
data, err := dataCollector.Collect(ctx)
Expect(err).ToNot(HaveOccurred())
Expect(data).To(Equal(expData))
})
It("collects correct data when snippetsFilters context is not supported", func(ctx SpecContext) {
fakeGraphGetter.GetLatestGraphReturns(&graph.Graph{
SnippetsFilters: map[types.NamespacedName]*graph.SnippetsFilter{
{Namespace: "test", Name: "sf-1"}: {
Snippets: map[ngfAPI.NginxContext]string{
"unsupportedContext": "worker_priority 0;",
},
},
},
})
expData.SnippetsFilterCount = 1
expData.SnippetsFiltersDirectives = []string{"worker_priority-unknown"}
expData.SnippetsFiltersDirectivesCount = []int64{1}
data, err := dataCollector.Collect(ctx)
Expect(err).ToNot(HaveOccurred())
Expect(data).To(Equal(expData))
})
})
})
})