-
Notifications
You must be signed in to change notification settings - Fork 25
/
flowvisibility_test.go
1290 lines (1212 loc) · 63.1 KB
/
flowvisibility_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
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright 2022 Antrea 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 e2e
import (
"bytes"
"encoding/base64"
"encoding/json"
"errors"
"fmt"
"io"
"net"
"net/http"
"os/exec"
"strconv"
"strings"
"syscall"
"testing"
"time"
log "github.com/sirupsen/logrus"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/tidwall/gjson"
ipfixregistry "github.com/vmware/go-ipfix/pkg/registry"
corev1 "k8s.io/api/core/v1"
networkingv1 "k8s.io/api/networking/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/wait"
"antrea.io/antrea/pkg/agent/openflow"
secv1alpha1 "antrea.io/antrea/pkg/apis/crd/v1alpha1"
"antrea.io/antrea/test/e2e/utils"
)
/* Sample record in ClickHouse table:
timeInserted: 1637706985
flowStartSeconds: 1637706961
flowEndSeconds: 1637706973
flowEndSecondsFromSourceNode: 1637706973
flowEndSecondsFromDestinationNode: 1637706973
flowEndReason: 3
sourceIP: 10.10.0.79
destinationIP: 10.10.0.80
sourceTransportPort: 44752
destinationTransportPort: 5201
protocolIdentifier: 6
packetTotalCount: 823188
octetTotalCount: 30472817041
packetDeltaCount: 241333
octetDeltaCount: 8982624938
reversePacketTotalCount: 471111
reverseOctetTotalCount: 24500996
reversePacketDeltaCount: 136211
reverseOctetDeltaCount: 7083284
sourcePodName: perftest-a
sourcePodNamespace: antrea-test
sourceNodeName: k8s-node-control-plane
destinationPodName: perftest-b
destinationPodNamespace: antrea-test
destinationNodeName: k8s-node-control-plane
destinationClusterIP: 0.0.0.0
destinationServicePort: 0
destinationServicePortName:
ingressNetworkPolicyName: test-flow-aggregator-networkpolicy-ingress-allow
ingressNetworkPolicyNamespace: antrea-test
ingressNetworkPolicyRuleName:
ingressNetworkPolicyRuleAction: 1
ingressNetworkPolicyType: 1
egressNetworkPolicyName: test-flow-aggregator-networkpolicy-egress-allow
egressNetworkPolicyNamespace: antrea-test
egressNetworkPolicyRuleName:
egressNetworkPolicyRuleAction: 1
egressNetworkPolicyType: 1
tcpState: TIME_WAIT
flowType: 1
sourcePodLabels: {"antrea-e2e":"perftest-a","app":"perftool"}
destinationPodLabels: {"antrea-e2e":"perftest-b","app":"perftool"}
throughput: 15902813472
reverseThroughput: 12381344
throughputFromSourceNode: 15902813472
throughputFromDestinationNode: 15902813472
reverseThroughputFromSourceNode: 12381344
reverseThroughputFromDestinationNode: 12381344
clusterUUID: 7e2e1de2-c85f-476e-ab1a-fce1bf83ee2c
trusted: 0
*/
const (
iperfTimeSec = 12
// Set target bandwidth(bits/sec) of iPerf traffic to a relatively small value
// (default unlimited for TCP), to reduce the variances caused by network performance
// during 12s, and make the throughput test more stable.
iperfBandwidth = "10m"
antreaEgressTableInitFlowCount = 3
antreaIngressTableInitFlowCount = 6
ingressTableInitFlowCount = 1
egressTableInitFlowCount = 1
monitorThreshold = 0.1
monitorDeletePercentage = 0.5
monitorIperfRounds = 80
grafanaCred = "admin:admin"
grafanaLocalPort = "5000"
grafanaSvcPort = "3000"
grafanaAddr = "http://127.0.0.1:5000"
grafanaQueryTimeout = 10 * time.Second
grafanaDefaultIntervalMS = "60000"
)
var (
// Single iperf run results in two connections with separate ports (control connection and actual data connection).
// As 2s is the export active timeout of flow exporter and iperf traffic runs for 12s, we expect totally 12 records
// exporting to the flow aggregator at time 2s, 4s, 6s, 8s, 10s, and 12s after iperf traffic begins.
// Since flow aggregator will aggregate records based on 5-tuple connection key and active timeout is 3.5 seconds,
// we expect 3 records at time 5.5s, 9s, and 12.5s after iperf traffic begins.
expectedNumDataRecords = 3
grafanaPorts = fmt.Sprintf("%s:%s", grafanaLocalPort, grafanaSvcPort)
portForwardCmd = exec.Command("kubectl", "port-forward", "service/grafana", "-n", "flow-visibility", grafanaPorts)
)
type testFlow struct {
srcIP string
dstIP string
srcPodName string
dstPodName string
}
func TestFlowVisibility(t *testing.T) {
config := FlowVisibiltiySetUpConfig{
withSparkOperator: false,
withGrafana: true,
withClickHouseLocalPv: false,
withFlowAggregator: true,
}
data, v4Enabled, v6Enabled, err := setupTestForFlowVisibility(t, config)
if err != nil {
t.Errorf("Error when setting up test: %v", err)
failOnError(err, t, data)
}
// port-forward Grafana Service port to local port
if err := portForwardCmd.Start(); err != nil {
t.Errorf("Error when port forwarding Grafana Service: %v", err)
failOnError(err, t, data)
}
defer portForwardCmd.Process.Kill()
defer flowVisibilityCleanup(t, data, config)
podAIPs, podBIPs, podCIPs, podDIPs, podEIPs, err := createPerftestPods(data)
if err != nil {
failOnError(fmt.Errorf("error when creating perftest Pods: %v", err), t, data)
}
if v4Enabled {
t.Run("IPv4", func(t *testing.T) {
testHelper(t, data, podAIPs, podBIPs, podCIPs, podDIPs, podEIPs, false)
})
}
if v6Enabled {
t.Run("IPv6", func(t *testing.T) {
testHelper(t, data, podAIPs, podBIPs, podCIPs, podDIPs, podEIPs, true)
})
}
}
func testHelper(t *testing.T, data *TestData, podAIPs, podBIPs, podCIPs, podDIPs, podEIPs *PodIPs, isIPv6 bool) {
svcB, svcC, err := createPerftestServices(data, isIPv6)
if err != nil {
failOnError(fmt.Errorf("error when creating perftest Services: %v", err), t, data)
}
// FlowAggregator takes up to 20s to set up the initial connection with
// ClickHouse DB. Most of the wait happens when looking up for ClickHouse
// on kube-dns. During the wait, FlowAggregator Pod is also likely to crash
// and restart once due to ping timeout. It usually does not introduce too
// much issue in real-life use cases, but will fail the e2e tests, as we
// start the iPerf traffic right away and we check whether ClickHouse
// receive every record. Here we add a workaround to wait 30s before
// starting sending iPerf traffic to avoid missing the first a few records.
// We should consider support a method to check the connection setup status
// later on, e.g. a new Antctl command.
time.Sleep(30 * time.Second)
// IntraNodeFlows tests the case, where Pods are deployed on same Node
// and their flow information is exported as IPFIX flow records.
// K8s network policies are being tested here.
t.Run("IntraNodeFlows", func(t *testing.T) {
np1, np2 := deployK8sNetworkPolicies(t, data, "perftest-a", "perftest-b")
defer func() {
if np1 != nil {
if err = data.deleteNetworkpolicy(np1); err != nil {
t.Errorf("Error when deleting network policy: %v", err)
}
}
if np2 != nil {
if err = data.deleteNetworkpolicy(np2); err != nil {
t.Errorf("Error when deleting network policy: %v", err)
}
}
}()
if !isIPv6 {
checkRecordsForFlows(t, data, podAIPs.ipv4.String(), podBIPs.ipv4.String(), isIPv6, true, false, true, false)
} else {
checkRecordsForFlows(t, data, podAIPs.ipv6.String(), podBIPs.ipv6.String(), isIPv6, true, false, true, false)
}
})
// IntraNodeDenyConnIngressANP tests the case, where Pods are deployed on same Node with an Antrea ingress deny policy rule
// applied to destination Pod (one reject rule, one drop rule) and their flow information is exported as IPFIX flow records.
// perftest-a -> perftest-b (Ingress reject), perftest-a -> perftest-d (Ingress drop)
t.Run("IntraNodeDenyConnIngressANP", func(t *testing.T) {
anp1, anp2 := deployDenyAntreaNetworkPolicies(t, data, "perftest-a", "perftest-b", "perftest-d", controlPlaneNodeName(), controlPlaneNodeName(), true)
defer func() {
if anp1 != nil {
if err = data.deleteAntreaNetworkpolicy(anp1); err != nil {
t.Errorf("Error when deleting Antrea Network Policy: %v", err)
}
}
if anp2 != nil {
if err = data.deleteAntreaNetworkpolicy(anp2); err != nil {
t.Errorf("Error when deleting Antrea Network Policy: %v", err)
}
}
}()
testFlow1 := testFlow{
srcPodName: "perftest-a",
dstPodName: "perftest-b",
}
testFlow2 := testFlow{
srcPodName: "perftest-a",
dstPodName: "perftest-d",
}
if !isIPv6 {
testFlow1.srcIP, testFlow1.dstIP, testFlow2.srcIP, testFlow2.dstIP = podAIPs.ipv4.String(), podBIPs.ipv4.String(), podAIPs.ipv4.String(), podDIPs.ipv4.String()
checkRecordsForDenyFlows(t, data, testFlow1, testFlow2, isIPv6, true, true)
} else {
testFlow1.srcIP, testFlow1.dstIP, testFlow2.srcIP, testFlow2.dstIP = podAIPs.ipv6.String(), podBIPs.ipv6.String(), podAIPs.ipv6.String(), podDIPs.ipv6.String()
checkRecordsForDenyFlows(t, data, testFlow1, testFlow2, isIPv6, true, true)
}
})
// IntraNodeDenyConnEgressANP tests the case, where Pods are deployed on same Node with an Antrea egress deny policy rule
// applied to source Pods (one reject rule, one drop rule) and their flow information is exported as IPFIX flow records.
// perftest-a (Egress reject) -> perftest-b , perftest-a (Egress drop) -> perftest-d
t.Run("IntraNodeDenyConnEgressANP", func(t *testing.T) {
anp1, anp2 := deployDenyAntreaNetworkPolicies(t, data, "perftest-a", "perftest-b", "perftest-d", controlPlaneNodeName(), controlPlaneNodeName(), false)
defer func() {
if anp1 != nil {
if err = data.deleteAntreaNetworkpolicy(anp1); err != nil {
t.Errorf("Error when deleting Antrea Network Policy: %v", err)
}
}
if anp2 != nil {
if err = data.deleteAntreaNetworkpolicy(anp2); err != nil {
t.Errorf("Error when deleting Antrea Network Policy: %v", err)
}
}
}()
testFlow1 := testFlow{
srcPodName: "perftest-a",
dstPodName: "perftest-b",
}
testFlow2 := testFlow{
srcPodName: "perftest-a",
dstPodName: "perftest-d",
}
if !isIPv6 {
testFlow1.srcIP, testFlow1.dstIP, testFlow2.srcIP, testFlow2.dstIP = podAIPs.ipv4.String(), podBIPs.ipv4.String(), podAIPs.ipv4.String(), podDIPs.ipv4.String()
checkRecordsForDenyFlows(t, data, testFlow1, testFlow2, isIPv6, true, true)
} else {
testFlow1.srcIP, testFlow1.dstIP, testFlow2.srcIP, testFlow2.dstIP = podAIPs.ipv6.String(), podBIPs.ipv6.String(), podAIPs.ipv6.String(), podDIPs.ipv6.String()
checkRecordsForDenyFlows(t, data, testFlow1, testFlow2, isIPv6, true, true)
}
})
// IntraNodeDenyConnNP tests the case, where Pods are deployed on same Node with an ingress and an egress deny policy rule
// applied to one destination Pod, one source Pod, respectively and their flow information is exported as IPFIX flow records.
// perftest-a -> perftest-b (Ingress deny), perftest-d (Egress deny) -> perftest-a
t.Run("IntraNodeDenyConnNP", func(t *testing.T) {
np1, np2 := deployDenyNetworkPolicies(t, data, "perftest-b", "perftest-d", controlPlaneNodeName(), controlPlaneNodeName())
defer func() {
if np1 != nil {
if err = data.deleteNetworkpolicy(np1); err != nil {
t.Errorf("Error when deleting Network Policy: %v", err)
}
}
if np2 != nil {
if err = data.deleteNetworkpolicy(np2); err != nil {
t.Errorf("Error when deleting Network Policy: %v", err)
}
}
}()
testFlow1 := testFlow{
srcPodName: "perftest-a",
dstPodName: "perftest-b",
}
testFlow2 := testFlow{
srcPodName: "perftest-d",
dstPodName: "perftest-a",
}
if !isIPv6 {
testFlow1.srcIP, testFlow1.dstIP, testFlow2.srcIP, testFlow2.dstIP = podAIPs.ipv4.String(), podBIPs.ipv4.String(), podDIPs.ipv4.String(), podAIPs.ipv4.String()
checkRecordsForDenyFlows(t, data, testFlow1, testFlow2, isIPv6, true, false)
} else {
testFlow1.srcIP, testFlow1.dstIP, testFlow2.srcIP, testFlow2.dstIP = podAIPs.ipv6.String(), podBIPs.ipv6.String(), podDIPs.ipv6.String(), podAIPs.ipv6.String()
checkRecordsForDenyFlows(t, data, testFlow1, testFlow2, isIPv6, true, false)
}
})
// InterNodeFlows tests the case, where Pods are deployed on different Nodes
// and their flow information is exported as IPFIX flow records.
// Antrea network policies are being tested here.
t.Run("InterNodeFlows", func(t *testing.T) {
anp1, anp2 := deployAntreaNetworkPolicies(t, data, "perftest-a", "perftest-c", controlPlaneNodeName(), workerNodeName(1))
defer func() {
if anp1 != nil {
data.DeleteANP(testNamespace, anp1.Name)
}
if anp2 != nil {
data.DeleteANP(testNamespace, anp2.Name)
}
}()
if !isIPv6 {
checkRecordsForFlows(t, data, podAIPs.ipv4.String(), podCIPs.ipv4.String(), isIPv6, false, false, false, true)
} else {
checkRecordsForFlows(t, data, podAIPs.ipv6.String(), podCIPs.ipv6.String(), isIPv6, false, false, false, true)
}
})
// InterNodeDenyConnIngressANP tests the case, where Pods are deployed on different Nodes with an Antrea ingress deny policy rule
// applied to destination Pod (one reject rule, one drop rule) and their flow information is exported as IPFIX flow records.
// perftest-a -> perftest-c (Ingress reject), perftest-a -> perftest-e (Ingress drop)
t.Run("InterNodeDenyConnIngressANP", func(t *testing.T) {
anp1, anp2 := deployDenyAntreaNetworkPolicies(t, data, "perftest-a", "perftest-c", "perftest-e", controlPlaneNodeName(), workerNodeName(1), true)
defer func() {
if anp1 != nil {
if err = data.deleteAntreaNetworkpolicy(anp1); err != nil {
t.Errorf("Error when deleting Antrea Network Policy: %v", err)
}
}
if anp2 != nil {
if err = data.deleteAntreaNetworkpolicy(anp2); err != nil {
t.Errorf("Error when deleting Antrea Network Policy: %v", err)
}
}
}()
testFlow1 := testFlow{
srcPodName: "perftest-a",
dstPodName: "perftest-c",
}
testFlow2 := testFlow{
srcPodName: "perftest-a",
dstPodName: "perftest-e",
}
if !isIPv6 {
testFlow1.srcIP, testFlow1.dstIP, testFlow2.srcIP, testFlow2.dstIP = podAIPs.ipv4.String(), podCIPs.ipv4.String(), podAIPs.ipv4.String(), podEIPs.ipv4.String()
checkRecordsForDenyFlows(t, data, testFlow1, testFlow2, isIPv6, false, true)
} else {
testFlow1.srcIP, testFlow1.dstIP, testFlow2.srcIP, testFlow2.dstIP = podAIPs.ipv6.String(), podCIPs.ipv6.String(), podAIPs.ipv6.String(), podEIPs.ipv6.String()
checkRecordsForDenyFlows(t, data, testFlow1, testFlow2, isIPv6, false, true)
}
})
// InterNodeDenyConnEgressANP tests the case, where Pods are deployed on different Nodes with an Antrea egress deny policy rule
// applied to source Pod (one reject rule, one drop rule) and their flow information is exported as IPFIX flow records.
// perftest-a (Egress reject) -> perftest-c, perftest-a (Egress drop)-> perftest-e
t.Run("InterNodeDenyConnEgressANP", func(t *testing.T) {
anp1, anp2 := deployDenyAntreaNetworkPolicies(t, data, "perftest-a", "perftest-c", "perftest-e", controlPlaneNodeName(), workerNodeName(1), false)
defer func() {
if anp1 != nil {
if err = data.deleteAntreaNetworkpolicy(anp1); err != nil {
t.Errorf("Error when deleting Antrea Network Policy: %v", err)
}
}
if anp2 != nil {
if err = data.deleteAntreaNetworkpolicy(anp2); err != nil {
t.Errorf("Error when deleting Antrea Network Policy: %v", err)
}
}
}()
testFlow1 := testFlow{
srcPodName: "perftest-a",
dstPodName: "perftest-c",
}
testFlow2 := testFlow{
srcPodName: "perftest-a",
dstPodName: "perftest-e",
}
if !isIPv6 {
testFlow1.srcIP, testFlow1.dstIP, testFlow2.srcIP, testFlow2.dstIP = podAIPs.ipv4.String(), podCIPs.ipv4.String(), podAIPs.ipv4.String(), podEIPs.ipv4.String()
checkRecordsForDenyFlows(t, data, testFlow1, testFlow2, isIPv6, false, true)
} else {
testFlow1.srcIP, testFlow1.dstIP, testFlow2.srcIP, testFlow2.dstIP = podAIPs.ipv6.String(), podCIPs.ipv6.String(), podAIPs.ipv6.String(), podEIPs.ipv6.String()
checkRecordsForDenyFlows(t, data, testFlow1, testFlow2, isIPv6, false, true)
}
})
// InterNodeDenyConnNP tests the case, where Pods are deployed on different Nodes with an ingress and an egress deny policy rule
// applied to one destination Pod, one source Pod, respectively and their flow information is exported as IPFIX flow records.
// perftest-a -> perftest-c (Ingress deny), perftest-b (Egress deny) -> perftest-e
t.Run("InterNodeDenyConnNP", func(t *testing.T) {
np1, np2 := deployDenyNetworkPolicies(t, data, "perftest-c", "perftest-b", workerNodeName(1), controlPlaneNodeName())
defer func() {
if np1 != nil {
if err = data.deleteNetworkpolicy(np1); err != nil {
t.Errorf("Error when deleting Network Policy: %v", err)
}
}
if np2 != nil {
if err = data.deleteNetworkpolicy(np2); err != nil {
t.Errorf("Error when deleting Network Policy: %v", err)
}
}
}()
testFlow1 := testFlow{
srcPodName: "perftest-a",
dstPodName: "perftest-c",
}
testFlow2 := testFlow{
srcPodName: "perftest-b",
dstPodName: "perftest-e",
}
if !isIPv6 {
testFlow1.srcIP, testFlow1.dstIP, testFlow2.srcIP, testFlow2.dstIP = podAIPs.ipv4.String(), podCIPs.ipv4.String(), podBIPs.ipv4.String(), podEIPs.ipv4.String()
checkRecordsForDenyFlows(t, data, testFlow1, testFlow2, isIPv6, false, false)
} else {
testFlow1.srcIP, testFlow1.dstIP, testFlow2.srcIP, testFlow2.dstIP = podAIPs.ipv6.String(), podCIPs.ipv6.String(), podBIPs.ipv6.String(), podEIPs.ipv6.String()
checkRecordsForDenyFlows(t, data, testFlow1, testFlow2, isIPv6, false, false)
}
})
// ToExternalFlows tests the export of IPFIX flow records when a source Pod
// sends traffic to an external IP
t.Run("ToExternalFlows", func(t *testing.T) {
// Creating an agnhost server as a host network Pod
serverPodPort := int32(80)
_, serverIPs, cleanupFunc := createAndWaitForPod(t, data, func(name string, ns string, nodeName string, hostNetwork bool) error {
return data.createServerPod(name, testNamespace, "", serverPodPort, false, true)
}, "test-server-", "", testNamespace, false)
defer cleanupFunc()
clientName, clientIPs, cleanupFunc := createAndWaitForPod(t, data, data.createBusyboxPodOnNode, "test-client-", nodeName(0), testNamespace, false)
defer cleanupFunc()
if !isIPv6 {
toExternalServerIP = serverIPs.ipv4.String()
} else {
toExternalServerIP = serverIPs.ipv6.String()
}
toExternalClientName = clientName
if !isIPv6 {
if clientIPs.ipv4 != nil && serverIPs.ipv4 != nil {
checkRecordsForToExternalFlows(t, data, nodeName(0), clientName, clientIPs.ipv4.String(), serverIPs.ipv4.String(), serverPodPort, isIPv6)
}
} else {
if clientIPs.ipv6 != nil && serverIPs.ipv6 != nil {
checkRecordsForToExternalFlows(t, data, nodeName(0), clientName, clientIPs.ipv6.String(), serverIPs.ipv6.String(), serverPodPort, isIPv6)
}
}
})
// LocalServiceAccess tests the case, where Pod and Service are deployed on the same Node and their flow information is exported as IPFIX flow records.
t.Run("LocalServiceAccess", func(t *testing.T) {
// In dual stack cluster, Service IP can be assigned as different IP family from specified.
// In that case, source IP and destination IP will align with IP family of Service IP.
// For IPv4-only and IPv6-only cluster, IP family of Service IP will be same as Pod IPs.
isServiceIPv6 := net.ParseIP(svcB.Spec.ClusterIP).To4() == nil
if isServiceIPv6 {
checkRecordsForFlows(t, data, podAIPs.ipv6.String(), svcB.Spec.ClusterIP, isServiceIPv6, true, true, false, false)
} else {
checkRecordsForFlows(t, data, podAIPs.ipv4.String(), svcB.Spec.ClusterIP, isServiceIPv6, true, true, false, false)
}
})
// RemoteServiceAccess tests the case, where Pod and Service are deployed on different Nodes and their flow information is exported as IPFIX flow records.
t.Run("RemoteServiceAccess", func(t *testing.T) {
// In dual stack cluster, Service IP can be assigned as different IP family from specified.
// In that case, source IP and destination IP will align with IP family of Service IP.
// For IPv4-only and IPv6-only cluster, IP family of Service IP will be same as Pod IPs.
isServiceIPv6 := net.ParseIP(svcC.Spec.ClusterIP).To4() == nil
if isServiceIPv6 {
checkRecordsForFlows(t, data, podAIPs.ipv6.String(), svcC.Spec.ClusterIP, isServiceIPv6, false, true, false, false)
} else {
checkRecordsForFlows(t, data, podAIPs.ipv4.String(), svcC.Spec.ClusterIP, isServiceIPv6, false, true, false, false)
}
})
// Grafana tests the queries in self-defined dashboards can be successfully
// executed, and the returned data contains expected results.
t.Run("Grafana", func(t *testing.T) {
for _, tc := range grafanaTestCases {
checkGrafanaQueryResults(t, data, tc.dashboardName, tc.dashboardUid, &tc.queryList)
}
})
// ClickHouseMonitor tests ensure ClickHouse monitor inspects the ClickHouse
// Pod storage usage and deletes the data when the stoage usage grows above
// the threshold.
t.Run("ClickHouseMonitor", func(t *testing.T) {
var flow testFlow
if !isIPv6 {
flow = testFlow{
srcIP: podAIPs.ipv4.String(),
dstIP: podBIPs.ipv4.String(),
srcPodName: "perftest-a",
dstPodName: "perftest-b",
}
} else {
flow = testFlow{
srcIP: podAIPs.ipv6.String(),
dstIP: podBIPs.ipv6.String(),
srcPodName: "perftest-a",
dstPodName: "perftest-b",
}
}
checkClickHouseMonitor(t, data, isIPv6, flow)
})
// SupportBundle tests collection of log bundle via Theia CLI is successful
// and contains log files from common components in the test setup.
t.Run("SupportBundle", func(t *testing.T) {
testSupportBundleCollection(t, data)
})
}
func checkGrafanaQueryResults(t *testing.T, data *TestData, dashboardName, dashboardUid string, queryList *[]query) {
queries, err := getQueriesByDashboard("/api/dashboards/uid/", dashboardUid, http.MethodGet, queryList)
if err != nil {
t.Errorf("Error when getting queries from dashboard: %s\nuid:%s, err: %v", dashboardName, dashboardUid, err)
failOnError(err, t, data)
}
err = checkQueryResult(t, "/api/ds/query", http.MethodPost, queries, queryList)
if err != nil {
t.Errorf("Error when checking query results from dashboard: %s\nuid:%s, err: %v", dashboardName, dashboardUid, err)
failOnError(err, t, data)
}
}
func createNewHttpRequest(httpMethod, url string, body io.Reader) (*http.Request, error) {
encodedCreds := base64.StdEncoding.EncodeToString([]byte(grafanaCred))
req, err := http.NewRequest(httpMethod, url, body)
if err != nil {
return nil, err
}
req.Header.Add("Authorization", "Basic "+encodedCreds)
req.Header.Add("Accept", "application/json")
req.Header.Add("Content-Type", "application/json")
return req, nil
}
func getQueriesByDashboard(apiEndpoint, dashboardUid, httpMethod string, queryList *[]query) ([]string, error) {
queries := []string{}
url := grafanaAddr + apiEndpoint + dashboardUid
req, err := createNewHttpRequest(httpMethod, url, nil)
if err != nil {
return queries, fmt.Errorf("error when creating http request: %v", err)
}
client := &http.Client{}
var resp *http.Response
var respErr error
err = wait.PollImmediate(500*time.Millisecond, grafanaQueryTimeout, func() (bool, error) {
resp, respErr = client.Do(req)
// If connection refused, keep trying
if errors.Is(respErr, syscall.ECONNREFUSED) {
return false, respErr
}
return true, nil
})
if err != nil {
return queries, fmt.Errorf("unable to access Grafana Service, err: %v", err)
}
if respErr != nil {
return queries, fmt.Errorf("error on response, err: %v", respErr)
}
defer resp.Body.Close()
dashboard, err := io.ReadAll(resp.Body)
if err != nil {
return queries, fmt.Errorf("error while reading the response bytes: %v", err)
}
for _, q := range *queryList {
query := gjson.GetBytes(dashboard, fmt.Sprintf("dashboard.panels*.%d.targets", q.queryId))
queryWithTimeRange := fmt.Sprintf(`{"queries":%s,"from":"now-15m","to":"now"}`, query.Raw)
queryWithTimeRange = strings.ReplaceAll(queryWithTimeRange, "$__interval_ms", grafanaDefaultIntervalMS)
queries = append(queries, queryWithTimeRange)
}
return queries, nil
}
func checkQueryResult(t *testing.T, apiEndpoint, httpMethod string, queries []string, queryList *[]query) error {
url := grafanaAddr + apiEndpoint
for i, q := range *queryList {
req, err := createNewHttpRequest(httpMethod, url, bytes.NewBuffer([]byte(queries[i])))
if err != nil {
return fmt.Errorf("error when creating http request: %v", err)
}
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
return fmt.Errorf("error on response, err: %v", err)
}
defer resp.Body.Close()
result, err := io.ReadAll(resp.Body)
if err != nil {
return fmt.Errorf("error while reading the response bytes: %v", err)
}
data := gjson.GetBytes(result, "results.A.frames*.0").Raw
assert := assert.New(t)
for _, str := range q.expectResult {
assert.Containsf(data, str, "Panel name: %s", q.panelName)
}
}
return nil
}
func checkClickHouseMonitor(t *testing.T, data *TestData, isIPv6 bool, flow testFlow) {
checkClickHouseMonitorLogs(t, data, false, 0)
var cmdStr string
// iperf3 has a limit on maximum parallel streams at 128
if !isIPv6 {
cmdStr = fmt.Sprintf("iperf3 -u -c %s -P 128 -n 1", flow.dstIP)
} else {
cmdStr = fmt.Sprintf("iperf3 -u -6 -c %s -P 128 -n 1", flow.dstIP)
}
log.Infof("Generating flow records to exceed monitor threshold...")
for i := 0; i < monitorIperfRounds; i++ {
stdout, stderr, err := data.RunCommandFromPod(testNamespace, flow.srcPodName, "perftool", []string{"bash", "-c", cmdStr})
require.NoErrorf(t, err, "Error when running iPerf3 client: %v,\nstdout:%s\nstderr:%s", err, stdout, stderr)
}
log.Infof("Waiting for the flows to be exported...")
time.Sleep(30 * time.Second)
// Get the number of records in database before the monitor deletes the records
stdout, stderr, err := data.RunCommandFromPod(flowVisibilityNamespace, clickHousePodName, "clickhouse", []string{"bash", "-c", "clickhouse client -q \"SELECT COUNT() FROM default.flows\""})
require.NoErrorf(t, err, "Error when querying ClickHouse server: %v,\nstdout:%s\nstderr:%s", err, stdout, stderr)
numRecord, err := strconv.ParseInt(strings.TrimSuffix(stdout, "\n"), 10, 64)
require.NoErrorf(t, err, "Failed when parsing the number of records %v", err)
log.Infof("Waiting for the monitor to detect and clean up the ClickHouse storage")
time.Sleep(2 * time.Minute)
checkClickHouseMonitorLogs(t, data, true, numRecord)
}
func checkClickHouseMonitorLogs(t *testing.T, data *TestData, deleted bool, numRecord int64) {
logString, err := data.GetPodLogs(flowVisibilityNamespace, clickHousePodName,
&corev1.PodLogOptions{
Container: "clickhouse-monitor",
},
)
require.NoErrorf(t, err, "Error when getting ClickHouse monitor logs: %v", err)
// ClickHouse monitor log sample
// "Memory usage" total=2062296555 used=42475 percentage=2.0595970980516912e-05
logs := strings.Split(logString, "Memory usage")
memoryUsageLog := strings.Split(logs[len(logs)-1], "\n")[0]
assert.Contains(t, memoryUsageLog, "total=")
assert.Contains(t, memoryUsageLog, "used=")
assert.Contains(t, memoryUsageLog, "percentage=")
percentage, err := strconv.ParseFloat(strings.Split(memoryUsageLog, "percentage=")[1], 64)
require.NoErrorf(t, err, "Failed when parsing the memory usage percentage %v", err)
if !deleted {
assert.LessOrEqual(t, percentage, monitorThreshold)
} else {
assert.Greater(t, percentage, monitorThreshold)
// Monitor deletes records from table flows and related MVs
assert.Contains(t, logString, "ALTER TABLE default.flows_local DELETE WHERE timeInserted < toDateTime", "Monitor should delete records from Table flows")
assert.Contains(t, logString, "ALTER TABLE default.pod_view_table_local DELETE WHERE timeInserted < toDateTime", "Monitor should delete records from View flows_pod_view")
assert.Contains(t, logString, "ALTER TABLE default.node_view_table_local DELETE WHERE timeInserted < toDateTime", "Monitor should delete records from View flows_node_view")
assert.Contains(t, logString, "ALTER TABLE default.policy_view_table_local DELETE WHERE timeInserted < toDateTime", "Monitor should delete records from View flows_policy_view")
assert.Contains(t, logString, "Skip rounds after a successful deletion", "Monitor should skip rounds after a successful deletion")
require.Contains(t, logString, "[send query] SELECT timeInserted FROM default.flows_local LIMIT 1 OFFSET (", "Monitor should log the deletion SQL command")
deletedRecordLog := strings.Split(logString, "[send query] SELECT timeInserted FROM default.flows_local LIMIT 1 OFFSET (")[1]
deletedRecordLog = strings.Split(deletedRecordLog, ")\n")[0]
numDeletedRecord, err := strconv.ParseInt(deletedRecordLog, 10, 64)
require.NoErrorf(t, err, "Failed when parsing the number of deleted records %v", err)
assert.InDeltaf(t, numDeletedRecord, float64(numRecord)*monitorDeletePercentage, float64(numDeletedRecord)*0.15, "Difference between expected and actual number of deleted Records should be lower than 15%")
}
}
func checkRecordsForFlows(t *testing.T, data *TestData, srcIP string, dstIP string, isIPv6 bool, isIntraNode bool, checkService bool, checkK8sNetworkPolicy bool, checkAntreaNetworkPolicy bool) {
var cmdStr string
if !isIPv6 {
cmdStr = fmt.Sprintf("iperf3 -c %s -t %d -b %s", dstIP, iperfTimeSec, iperfBandwidth)
} else {
cmdStr = fmt.Sprintf("iperf3 -6 -c %s -t %d -b %s", dstIP, iperfTimeSec, iperfBandwidth)
}
stdout, _, err := data.RunCommandFromPod(testNamespace, "perftest-a", "perftool", []string{"bash", "-c", cmdStr})
require.NoErrorf(t, err, "Error when running iperf3 client: %v", err)
bwSlice, srcPort, _ := getBandwidthAndPorts(stdout)
require.Equal(t, 2, len(bwSlice), "bandwidth value and / or bandwidth unit are not available")
// bandwidth from iperf output
bandwidthInFloat, err := strconv.ParseFloat(bwSlice[0], 64)
require.NoErrorf(t, err, "Error when converting iperf bandwidth %s to float64 type", bwSlice[0])
var bandwidthInMbps float64
if strings.Contains(bwSlice[1], "Mbits") {
bandwidthInMbps = bandwidthInFloat
} else {
failOnError(fmt.Errorf("unit of the traffic bandwidth reported by iperf should be Mbits."), t, data)
}
checkRecordsForFlowsClickHouse(t, data, srcIP, dstIP, srcPort, isIntraNode, checkService, checkK8sNetworkPolicy, checkAntreaNetworkPolicy, bandwidthInMbps)
}
func checkRecordsForFlowsClickHouse(t *testing.T, data *TestData, srcIP, dstIP, srcPort string, isIntraNode, checkService, checkK8sNetworkPolicy, checkAntreaNetworkPolicy bool, bandwidthInMbps float64) {
// Check the source port along with source and destination IPs as there
// are flow records for control flows during the iperf with same IPs
// and destination port.
clickHouseRecords := getClickHouseOutput(t, data, srcIP, dstIP, srcPort, checkService, true)
for _, record := range clickHouseRecords {
// Check if record has both Pod name of source and destination Pod.
if isIntraNode {
checkPodAndNodeDataClickHouse(t, record, "perftest-a", controlPlaneNodeName(), "perftest-b", controlPlaneNodeName())
checkFlowTypeClickHouse(t, record, ipfixregistry.FlowTypeIntraNode)
} else {
checkPodAndNodeDataClickHouse(t, record, "perftest-a", controlPlaneNodeName(), "perftest-c", workerNodeName(1))
checkFlowTypeClickHouse(t, record, ipfixregistry.FlowTypeInterNode)
}
assert := assert.New(t)
if checkService {
if isIntraNode {
assert.Contains(record.DestinationServicePortName, "antrea-test/perftest-b", "Record with ServiceIP does not have Service name")
} else {
assert.Contains(record.DestinationServicePortName, "antrea-test/perftest-c", "Record with ServiceIP does not have Service name")
}
}
if checkK8sNetworkPolicy {
// Check if records have both ingress and egress network policies.
assert.Equal(record.IngressNetworkPolicyName, ingressAllowNetworkPolicyName, "Record does not have the correct NetworkPolicy name with the ingress rule")
assert.Equal(record.IngressNetworkPolicyNamespace, testNamespace, "Record does not have the correct NetworkPolicy Namespace with the ingress rule")
assert.Equal(record.IngressNetworkPolicyType, ipfixregistry.PolicyTypeK8sNetworkPolicy, "Record does not have the correct NetworkPolicy Type with the ingress rule")
assert.Equal(record.EgressNetworkPolicyName, egressAllowNetworkPolicyName, "Record does not have the correct NetworkPolicy name with the egress rule")
assert.Equal(record.EgressNetworkPolicyNamespace, testNamespace, "Record does not have the correct NetworkPolicy Namespace with the egress rule")
assert.Equal(record.EgressNetworkPolicyType, ipfixregistry.PolicyTypeK8sNetworkPolicy, "Record does not have the correct NetworkPolicy Type with the egress rule")
}
if checkAntreaNetworkPolicy {
// Check if records have both ingress and egress network policies.
assert.Equal(record.IngressNetworkPolicyName, ingressAntreaNetworkPolicyName, "Record does not have the correct NetworkPolicy name with the ingress rule")
assert.Equal(record.IngressNetworkPolicyNamespace, testNamespace, "Record does not have the correct NetworkPolicy Namespace with the ingress rule")
assert.Equal(record.IngressNetworkPolicyType, ipfixregistry.PolicyTypeAntreaNetworkPolicy, "Record does not have the correct NetworkPolicy Type with the ingress rule")
assert.Equal(record.IngressNetworkPolicyRuleName, testIngressRuleName, "Record does not have the correct NetworkPolicy RuleName with the ingress rule")
assert.Equal(record.IngressNetworkPolicyRuleAction, ipfixregistry.NetworkPolicyRuleActionAllow, "Record does not have the correct NetworkPolicy RuleAction with the ingress rule")
assert.Equal(record.EgressNetworkPolicyName, egressAntreaNetworkPolicyName, "Record does not have the correct NetworkPolicy name with the egress rule")
assert.Equal(record.EgressNetworkPolicyNamespace, testNamespace, "Record does not have the correct NetworkPolicy Namespace with the egress rule")
assert.Equal(record.EgressNetworkPolicyType, ipfixregistry.PolicyTypeAntreaNetworkPolicy, "Record does not have the correct NetworkPolicy Type with the egress rule")
assert.Equal(record.EgressNetworkPolicyRuleName, testEgressRuleName, "Record does not have the correct NetworkPolicy RuleName with the egress rule")
assert.Equal(record.EgressNetworkPolicyRuleAction, ipfixregistry.NetworkPolicyRuleActionAllow, "Record does not have the correct NetworkPolicy RuleAction with the egress rule")
}
// Skip the bandwidth check for the iperf control flow records which have 0 throughput.
if record.Throughput > 0 {
flowStartTime := record.FlowStartSeconds.Unix()
exportTime := record.FlowEndSeconds.Unix()
var recBandwidth float64
// flowEndReason == 3 means the end of flow detected
if exportTime >= flowStartTime+iperfTimeSec || record.FlowEndReason == 3 {
octetTotalCount := record.OctetTotalCount
recBandwidth = float64(octetTotalCount) * 8 / float64(exportTime-flowStartTime) / 1000000
} else {
// Check bandwidth with the field "throughput" except for the last record,
// as their throughput may be significantly lower than the average Iperf throughput.
throughput := record.Throughput
recBandwidth = float64(throughput) / 1000000
}
t.Logf("Throughput check on record with flowEndSeconds-flowStartSeconds: %v, Iperf throughput: %.2f Mbits/s, ClickHouse record throughput: %.2f Mbits/s", exportTime-flowStartTime, bandwidthInMbps, recBandwidth)
assert.InDeltaf(recBandwidth, bandwidthInMbps, bandwidthInMbps*0.15, "Difference between Iperf bandwidth and ClickHouse record bandwidth should be lower than 15%%, record: %v", record)
}
}
// Checking only data records as data records cannot be decoded without template record.
assert.GreaterOrEqualf(t, len(clickHouseRecords), expectedNumDataRecords, "ClickHouse should receive expected number of flow records. Considered records: %s", clickHouseRecords)
}
func checkRecordsForToExternalFlows(t *testing.T, data *TestData, srcNodeName string, srcPodName string, srcIP string, dstIP string, dstPort int32, isIPv6 bool) {
var cmd string
if !isIPv6 {
cmd = fmt.Sprintf("wget -O- %s:%d", dstIP, dstPort)
} else {
cmd = fmt.Sprintf("wget -O- [%s]:%d", dstIP, dstPort)
}
stdout, stderr, err := data.RunCommandFromPod(testNamespace, srcPodName, busyboxContainerName, strings.Fields(cmd))
require.NoErrorf(t, err, "Error when running wget command, stdout: %s, stderr: %s", stdout, stderr)
clickHouseRecords := getClickHouseOutput(t, data, srcIP, dstIP, "", false, false)
for _, record := range clickHouseRecords {
checkPodAndNodeDataClickHouse(t, record, srcPodName, srcNodeName, "", "")
checkFlowTypeClickHouse(t, record, ipfixregistry.FlowTypeToExternal)
// Since the OVS userspace conntrack implementation doesn't maintain
// packet or byte counter statistics, skip the check for Kind clusters
if testOptions.providerName != "kind" {
assert.Greater(t, record.OctetDeltaCount, uint64(0), "octetDeltaCount should be non-zero")
}
}
}
func checkRecordsForDenyFlows(t *testing.T, data *TestData, testFlow1, testFlow2 testFlow, isIPv6, isIntraNode, isANP bool) {
var cmdStr1, cmdStr2 string
if !isIPv6 {
cmdStr1 = fmt.Sprintf("iperf3 -c %s -n 1", testFlow1.dstIP)
cmdStr2 = fmt.Sprintf("iperf3 -c %s -n 1", testFlow2.dstIP)
} else {
cmdStr1 = fmt.Sprintf("iperf3 -6 -c %s -n 1", testFlow1.dstIP)
cmdStr2 = fmt.Sprintf("iperf3 -6 -c %s -n 1", testFlow2.dstIP)
}
_, _, err := data.RunCommandFromPod(testNamespace, testFlow1.srcPodName, "", []string{"timeout", "2", "bash", "-c", cmdStr1})
assert.Error(t, err)
_, _, err = data.RunCommandFromPod(testNamespace, testFlow2.srcPodName, "", []string{"timeout", "2", "bash", "-c", cmdStr2})
assert.Error(t, err)
checkRecordsForDenyFlowsClickHouse(t, data, testFlow1, testFlow2, isIPv6, isIntraNode, isANP)
}
func checkRecordsForDenyFlowsClickHouse(t *testing.T, data *TestData, testFlow1, testFlow2 testFlow, isIPv6, isIntraNode, isANP bool) {
clickHouseRecords1 := getClickHouseOutput(t, data, testFlow1.srcIP, testFlow1.dstIP, "", false, false)
clickHouseRecords2 := getClickHouseOutput(t, data, testFlow2.srcIP, testFlow2.dstIP, "", false, false)
recordSlices := append(clickHouseRecords1, clickHouseRecords2...)
// Iterate over recordSlices and build some results to test with expected results
for _, record := range recordSlices {
var srcPodName, dstPodName string
if record.SourceIP == testFlow1.srcIP && record.DestinationIP == testFlow1.dstIP {
srcPodName = testFlow1.srcPodName
dstPodName = testFlow1.dstPodName
} else if record.SourceIP == testFlow2.srcIP && record.DestinationIP == testFlow2.dstIP {
srcPodName = testFlow2.srcPodName
dstPodName = testFlow2.dstPodName
}
if isIntraNode {
checkPodAndNodeDataClickHouse(t, record, srcPodName, controlPlaneNodeName(), dstPodName, controlPlaneNodeName())
checkFlowTypeClickHouse(t, record, ipfixregistry.FlowTypeIntraNode)
} else {
checkPodAndNodeDataClickHouse(t, record, srcPodName, controlPlaneNodeName(), dstPodName, workerNodeName(1))
checkFlowTypeClickHouse(t, record, ipfixregistry.FlowTypeInterNode)
}
assert := assert.New(t)
if !isANP { // K8s Network Policies
if (record.IngressNetworkPolicyRuleAction == ipfixregistry.NetworkPolicyRuleActionDrop) && (record.IngressNetworkPolicyName != ingressDropANPName) {
assert.Equal(record.DestinationIP, testFlow1.dstIP)
} else if (record.EgressNetworkPolicyRuleAction == ipfixregistry.NetworkPolicyRuleActionDrop) && (record.EgressNetworkPolicyName != egressDropANPName) {
assert.Equal(record.DestinationIP, testFlow2.dstIP)
}
} else { // Antrea Network Policies
if record.IngressNetworkPolicyRuleAction == ipfixregistry.NetworkPolicyRuleActionReject {
assert.Equal(record.IngressNetworkPolicyName, ingressRejectANPName, "Record does not have Antrea NetworkPolicy name with ingress reject rule")
assert.Equal(record.IngressNetworkPolicyNamespace, testNamespace, "Record does not have correct ingressNetworkPolicyNamespace")
assert.Equal(record.IngressNetworkPolicyType, ipfixregistry.PolicyTypeAntreaNetworkPolicy, "Record does not have the correct NetworkPolicy Type with the ingress reject rule")
assert.Equal(record.IngressNetworkPolicyRuleName, testIngressRuleName, "Record does not have the correct NetworkPolicy RuleName with the ingress reject rule")
} else if record.IngressNetworkPolicyRuleAction == ipfixregistry.NetworkPolicyRuleActionDrop {
assert.Equal(record.IngressNetworkPolicyName, ingressDropANPName, "Record does not have Antrea NetworkPolicy name with ingress drop rule")
assert.Equal(record.IngressNetworkPolicyNamespace, testNamespace, "Record does not have correct ingressNetworkPolicyNamespace")
assert.Equal(record.IngressNetworkPolicyType, ipfixregistry.PolicyTypeAntreaNetworkPolicy, "Record does not have the correct NetworkPolicy Type with the ingress drop rule")
assert.Equal(record.IngressNetworkPolicyRuleName, testIngressRuleName, "Record does not have the correct NetworkPolicy RuleName with the ingress drop rule")
} else if record.EgressNetworkPolicyRuleAction == ipfixregistry.NetworkPolicyRuleActionReject {
assert.Equal(record.EgressNetworkPolicyName, egressRejectANPName, "Record does not have Antrea NetworkPolicy name with egress reject rule")
assert.Equal(record.EgressNetworkPolicyNamespace, testNamespace, "Record does not have correct egressNetworkPolicyNamespace")
assert.Equal(record.EgressNetworkPolicyType, ipfixregistry.PolicyTypeAntreaNetworkPolicy, "Record does not have the correct NetworkPolicy Type with the egress reject rule")
assert.Equal(record.EgressNetworkPolicyRuleName, testEgressRuleName, "Record does not have the correct NetworkPolicy RuleName with the egress reject rule")
} else if record.EgressNetworkPolicyRuleAction == ipfixregistry.NetworkPolicyRuleActionDrop {
assert.Equal(record.EgressNetworkPolicyName, egressDropANPName, "Record does not have Antrea NetworkPolicy name with egress drop rule")
assert.Equal(record.EgressNetworkPolicyNamespace, testNamespace, "Record does not have correct egressNetworkPolicyNamespace")
assert.Equal(record.EgressNetworkPolicyType, ipfixregistry.PolicyTypeAntreaNetworkPolicy, "Record does not have the correct NetworkPolicy Type with the egress drop rule")
assert.Equal(record.EgressNetworkPolicyRuleName, testEgressRuleName, "Record does not have the correct NetworkPolicy RuleName with the egress drop rule")
}
}
}
}
func checkPodAndNodeDataClickHouse(t *testing.T, record *ClickHouseFullRow, srcPod, srcNode, dstPod, dstNode string) {
assert := assert.New(t)
assert.Equal(record.SourcePodName, srcPod, "Record with srcIP does not have Pod name: %s", srcPod)
assert.Equal(record.SourcePodNamespace, testNamespace, "Record does not have correct sourcePodNamespace: %s", testNamespace)
assert.Equal(record.SourceNodeName, srcNode, "Record does not have correct sourceNodeName: %s", srcNode)
// For Pod-To-External flow type, we send traffic to an external address,
// so we skip the verification of destination Pod info.
// Also, source Pod labels are different for Pod-To-External flow test.
if dstPod != "" {
assert.Equal(record.DestinationPodName, dstPod, "Record with dstIP does not have Pod name: %s", dstPod)
assert.Equal(record.DestinationPodNamespace, testNamespace, "Record does not have correct destinationPodNamespace: %s", testNamespace)
assert.Equal(record.DestinationNodeName, dstNode, "Record does not have correct destinationNodeName: %s", dstNode)
assert.Equal(record.SourcePodLabels, fmt.Sprintf("{\"antrea-e2e\":\"%s\",\"app\":\"perftool\"}", srcPod), "Record does not have correct label for source Pod")
assert.Equal(record.DestinationPodLabels, fmt.Sprintf("{\"antrea-e2e\":\"%s\",\"app\":\"perftool\"}", dstPod), "Record does not have correct label for destination Pod")
} else {
assert.Equal(record.SourcePodLabels, fmt.Sprintf("{\"antrea-e2e\":\"%s\",\"app\":\"busybox\"}", srcPod), "Record does not have correct label for source Pod")
}
}
func checkFlowTypeClickHouse(t *testing.T, record *ClickHouseFullRow, flowType uint8) {
assert.Equal(t, record.FlowType, flowType, "Record does not have correct flowType")
}
// getClickHouseOutput queries clickhouse with built-in client and checks if we have
// received all the expected records for a given flow with source IP, destination IP
// and source port. We send source port to ignore the control flows during the iperf test.
// Polling timeout is coded assuming IPFIX output has been checked first.
func getClickHouseOutput(t *testing.T, data *TestData, srcIP, dstIP, srcPort string, isDstService, checkAllRecords bool) []*ClickHouseFullRow {
var flowRecords []*ClickHouseFullRow
var queryOutput string
query := fmt.Sprintf("SELECT * FROM flows WHERE (sourceIP = '%s') AND (destinationIP = '%s')", srcIP, dstIP)
if isDstService {
query = fmt.Sprintf("SELECT * FROM flows WHERE (sourceIP = '%s') AND (destinationClusterIP = '%s')", srcIP, dstIP)
}
if len(srcPort) > 0 {
query = fmt.Sprintf("%s AND (sourceTransportPort = %s)", query, srcPort)
}
cmd := []string{
"clickhouse-client",
"--date_time_output_format=iso",
"--format=JSONEachRow",
fmt.Sprintf("--query=%s", query),
}
// Waiting additional 4x commit interval to be adequate for 3 commit attempts.
timeout := (exporterActiveFlowExportTimeout + aggregatorActiveFlowRecordTimeout*2 + aggregatorClickHouseCommitInterval*4) * 2
err := wait.PollImmediate(500*time.Millisecond, timeout, func() (bool, error) {
queryOutput, _, err := data.RunCommandFromPod(flowVisibilityNamespace, clickHousePodName, "clickhouse", cmd)
if err != nil {
return false, err
}
rows := strings.Split(queryOutput, "\n")
flowRecords = make([]*ClickHouseFullRow, 0, len(rows))
for _, row := range rows {
row = strings.TrimSpace(row)
if len(row) == 0 {
continue
}
flowRecord := ClickHouseFullRow{}
err = json.Unmarshal([]byte(row), &flowRecord)
if err != nil {
return false, err
}
flowRecords = append(flowRecords, &flowRecord)
}
if checkAllRecords {
for _, record := range flowRecords {
flowStartTime := record.FlowStartSeconds.Unix()
exportTime := record.FlowEndSeconds.Unix()
// flowEndReason == 3 means the end of flow detected
if exportTime >= flowStartTime+iperfTimeSec || record.FlowEndReason == 3 {
return true, nil
}
}
return false, nil
}
return len(flowRecords) > 0, nil
})
require.NoErrorf(t, err, "ClickHouse did not receive the expected records in query output: %v; query: %s", queryOutput, query)
return flowRecords
}
func deployK8sNetworkPolicies(t *testing.T, data *TestData, srcPod, dstPod string) (np1 *networkingv1.NetworkPolicy, np2 *networkingv1.NetworkPolicy) {
// Add K8s NetworkPolicy between two iperf Pods.
var err error
np1, err = data.createNetworkPolicy(ingressAllowNetworkPolicyName, &networkingv1.NetworkPolicySpec{
PodSelector: metav1.LabelSelector{},
PolicyTypes: []networkingv1.PolicyType{networkingv1.PolicyTypeIngress},
Ingress: []networkingv1.NetworkPolicyIngressRule{{
From: []networkingv1.NetworkPolicyPeer{{
PodSelector: &metav1.LabelSelector{
MatchLabels: map[string]string{
"antrea-e2e": srcPod,
},
}},
},
}},
})
if err != nil {
t.Errorf("Error when creating Network Policy: %v", err)
}
np2, err = data.createNetworkPolicy(egressAllowNetworkPolicyName, &networkingv1.NetworkPolicySpec{
PodSelector: metav1.LabelSelector{},
PolicyTypes: []networkingv1.PolicyType{networkingv1.PolicyTypeEgress},
Egress: []networkingv1.NetworkPolicyEgressRule{{
To: []networkingv1.NetworkPolicyPeer{{
PodSelector: &metav1.LabelSelector{
MatchLabels: map[string]string{
"antrea-e2e": dstPod,
},
}},
},
}},
})
if err != nil {
t.Errorf("Error when creating Network Policy: %v", err)
}
// Wait for network policies to be realized.