Skip to content

Commit 06d7438

Browse files
authored
Netpol (#27)
* update e2e tests with netpol tests, and fix minor issues * fix lint * turn tuning parameters into constants * add changelog * fix first version name
1 parent b5c75a3 commit 06d7438

File tree

10 files changed

+162
-21
lines changed

10 files changed

+162
-21
lines changed

CHANGELOG.md

Lines changed: 30 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,34 @@
1-
## Version (0.0.0)
2-
### Releshed/Unreleased
3-
### Date
1+
# Changelog
42

5-
Changes:
6-
- List of changes
3+
All notable changes to this project will be documented in this file.
74

8-
Improvements:
9-
- List of improvements
5+
## Table of Contents
106

11-
Bug Fixes:
12-
- NA
7+
- [2.0.2](#202)
8+
- [2.0.1](#201)
9+
- [2.0.0](#200)
10+
- [0.1.0](#010)
1311

14-
---
12+
---
13+
14+
## `2.0.2`
15+
16+
- integrate e2e tests with network policies
17+
- fix a bug in udp testing
18+
19+
## `2.0.1`
20+
21+
- fix release naming
22+
23+
## `2.0.0`
24+
25+
- complete rewrite of the tool in Go, with unit and integration tests
26+
- leverages the ephemeral container support in Kubernetes > v1.25
27+
- test case(s) are written in YAML
28+
- support for Pods, StatefulSets, DaemonSets and Deployments which are directly referred through their names in the test suites
29+
- artifacts are available for download
30+
31+
## `0.1.0`
32+
33+
- initial release
34+
- no artifacts available

e2e/clusters/gke-dataplanev2/main.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ provider "google" {
1313

1414
resource "google_container_cluster" "e2etest" {
1515
name = var.cluster_name
16-
initial_node_count = 3
16+
initial_node_count = 4
1717
datapath_provider = var.use_dataplanev2 ? "ADVANCED_DATAPATH" : null
1818
ip_allocation_policy {}
1919
node_config {

e2e/clusters/gke-vpc/main.tf

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,18 @@ provider "google" {
1313

1414
resource "google_container_cluster" "e2etest" {
1515
name = var.cluster_name
16-
initial_node_count = 3
17-
datapath_provider = var.use_dataplanev2 ? "ADVANCED_DATAPATH" : null
16+
initial_node_count = 4
17+
addons_config {
18+
network_policy_config {
19+
disabled = false
20+
}
21+
}
22+
network_policy {
23+
enabled = true
24+
}
1825
ip_allocation_policy {}
1926
node_config {
20-
machine_type = "e2-medium"
27+
machine_type = "e2-standard-2"
2128
}
2229

2330
release_channel {

e2e/e2e_test.go

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,8 +177,33 @@ func createTestDestroy(t *testing.T, gc helpers.GenericCluster) {
177177
if err != nil {
178178
t.Fatal(err)
179179
}
180-
// run the tests
181180

181+
// run the tests without network policies
182+
runTests(ctx, t, svc, netAssertTestCases)
183+
184+
if gc.SkipNetPolTests() {
185+
return
186+
}
187+
188+
// create the network policies
189+
k8s.KubectlApply(t, options, "./manifests/networkpolicies.yaml")
190+
191+
// read the tests again for a fresh start
192+
netAssertTestCases, err = data.ReadTestsFromFile(testCasesFile)
193+
if err != nil {
194+
t.Fatal(err)
195+
}
196+
197+
// set the exit to 1 since this time the network policies will block the traffic
198+
for _, tc := range netAssertTestCases {
199+
tc.ExitCode = 1
200+
}
201+
202+
// run the tests with network policies
203+
runTests(ctx, t, svc, netAssertTestCases)
204+
}
205+
206+
func runTests(ctx context.Context, t *testing.T, svc *kubeops.Service, netAssertTestCases data.Tests) {
182207
lg := logger.NewHCLogger("INFO", "netassertv2-e2e", os.Stdout)
183208
testRunner := engine.New(svc, lg)
184209

e2e/helpers/common.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,5 @@ type GenericCluster interface {
1414
Create(t *testing.T)
1515
Destroy(t *testing.T)
1616
KubeConfigGet() string
17+
SkipNetPolTests() bool
1718
}

e2e/helpers/eks.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55
"os"
66
"testing"
7+
"time"
78

89
"github.com/controlplaneio/netassert/v2/internal/kubeops"
910
"github.com/controlplaneio/netassert/v2/internal/logger"
@@ -110,6 +111,9 @@ func (g *EKSCluster) installCalico(t *testing.T) {
110111
if _, err := terraform.InitAndApplyE(t, newTFOptions); err != nil {
111112
t.Fatalf("failed to run terraform init and apply: %s", err)
112113
}
114+
115+
svc.Log.Info("Sleeping 20 minutes so connectivity from the cluster to the Internet is restored")
116+
time.Sleep(20 * time.Minute)
113117
}
114118

115119
func (g *EKSCluster) Destroy(t *testing.T) {
@@ -121,3 +125,7 @@ func (g *EKSCluster) Destroy(t *testing.T) {
121125
func (g *EKSCluster) KubeConfigGet() string {
122126
return g.kubeConfigPath
123127
}
128+
129+
func (g *EKSCluster) SkipNetPolTests() bool {
130+
return g.networkMode != Calico
131+
}

e2e/helpers/gke.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,3 +60,7 @@ func (g *GKECluster) Destroy(t *testing.T) {
6060
func (g *GKECluster) KubeConfigGet() string {
6161
return g.kubeConfigPath
6262
}
63+
64+
func (g *GKECluster) SkipNetPolTests() bool {
65+
return false // network policies are supported by all gke cluster configurations
66+
}

e2e/manifests/networkpolicies.yaml

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
---
2+
kind: NetworkPolicy
3+
apiVersion: networking.k8s.io/v1
4+
metadata:
5+
namespace: fluentd
6+
name: fluentd-elasticsearch
7+
spec:
8+
podSelector:
9+
matchLabels:
10+
name: fluentd-elasticsearch
11+
ingress:
12+
---
13+
kind: NetworkPolicy
14+
apiVersion: networking.k8s.io/v1
15+
metadata:
16+
namespace: echoserver
17+
name: echoserver
18+
spec:
19+
podSelector:
20+
matchLabels:
21+
app: echoserver
22+
ingress:
23+
---
24+
kind: NetworkPolicy
25+
apiVersion: networking.k8s.io/v1
26+
metadata:
27+
namespace: busybox
28+
name: busybox
29+
spec:
30+
podSelector:
31+
matchLabels:
32+
app: busybox
33+
policyTypes:
34+
- Ingress
35+
- Egress
36+
ingress:
37+
egress:
38+
---
39+
kind: NetworkPolicy
40+
apiVersion: networking.k8s.io/v1
41+
metadata:
42+
namespace: pod1
43+
name: pod1
44+
spec:
45+
podSelector:
46+
matchLabels:
47+
name: pod1
48+
ingress:
49+
---
50+
kind: NetworkPolicy
51+
apiVersion: networking.k8s.io/v1
52+
metadata:
53+
namespace: pod2
54+
name: pod2
55+
spec:
56+
podSelector:
57+
matchLabels:
58+
name: pod2
59+
ingress:
60+
---
61+
kind: NetworkPolicy
62+
apiVersion: networking.k8s.io/v1
63+
metadata:
64+
namespace: web
65+
name: nginx
66+
spec:
67+
podSelector:
68+
matchLabels:
69+
app: nginx
70+
ingress:

e2e/manifests/pod1-pod2.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ kind: Pod
1515
metadata:
1616
name: pod2
1717
namespace: pod2
18+
labels:
19+
name: pod2
1820
spec:
1921
containers:
2022
- name: webserver
@@ -27,6 +29,8 @@ kind: Pod
2729
metadata:
2830
name: pod1
2931
namespace: pod1
32+
labels:
33+
name: pod1
3034
spec:
3135
containers:
3236
- name: busybox

internal/engine/run_udp.go

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,10 @@ import (
1212
)
1313

1414
const (
15-
defaultNetInt = `eth0` // default network interface
16-
defaultSnapLen = 1024 // default size of the packet snap length
15+
defaultNetInt = `eth0` // default network interface
16+
defaultSnapLen = 1024 // default size of the packet snap length
17+
ephemeralContainersExtraSeconds = 23 // fixed extra time given for the ephemeral containers to come online
18+
attemptsMultiplier = 3 // increase the attempts to ensure that we send three times the packets
1719
)
1820

1921
// RunUDPTest - runs a UDP test
@@ -114,7 +116,7 @@ func (e *Engine) RunUDPTest(
114116
string(te.Protocol),
115117
te.Attempts,
116118
networkInterface,
117-
te.TimeoutSeconds+5, // add 5 seconds for the Container to come online
119+
te.TimeoutSeconds,
118120
)
119121
if err != nil {
120122
return fmt.Errorf("failed to build sniffer ephemeral container for test %s: %w", te.Name, err)
@@ -128,7 +130,7 @@ func (e *Engine) RunUDPTest(
128130
strconv.Itoa(te.TargetPort),
129131
string(te.Protocol),
130132
msg,
131-
te.Attempts*3, // increase the attempts to ensure that we send three times the packets
133+
te.Attempts*attemptsMultiplier,
132134
)
133135
if err != nil {
134136
return fmt.Errorf("unable to build ephemeral scanner container for test %s: %w", te.Name, err)
@@ -152,7 +154,7 @@ func (e *Engine) RunUDPTest(
152154
exitCodeSnifferCtr, err := e.Service.GetExitStatusOfEphemeralContainer(
153155
ctx,
154156
snifferContainerName,
155-
time.Duration(te.TimeoutSeconds)*time.Second,
157+
time.Duration(te.TimeoutSeconds+ephemeralContainersExtraSeconds)*time.Second,
156158
dstPod.Name,
157159
dstPod.Namespace,
158160
)
@@ -174,7 +176,7 @@ func (e *Engine) RunUDPTest(
174176
// get the exit status of the scanner container
175177
exitCodeScanner, err := e.Service.GetExitStatusOfEphemeralContainer(
176178
ctx, scannerContainerName,
177-
time.Duration(te.TimeoutSeconds+10)*time.Second,
179+
time.Duration(te.TimeoutSeconds+ephemeralContainersExtraSeconds)*time.Second,
178180
srcPod.Name,
179181
srcPod.Namespace,
180182
)

0 commit comments

Comments
 (0)