-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
If an Ingress/Egress Ports field does not specify a `Port` value, but it has a valid `Protocol`, then the rule should match every possible port number. Add unit and end2end test to cover the feature Signed-off-by: Andrea Panattoni <[email protected]>
- Loading branch information
Showing
6 changed files
with
297 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
#!/usr/bin/env bats | ||
|
||
# Note: | ||
# These test cases, simple, will create simple (one policy for ingress) and test the | ||
# traffic policying by ncat (nc) command. In addition, these cases also verifies that | ||
# simple iptables generation check by iptables-save and pod-iptable in multi-networkpolicy pod. | ||
|
||
setup() { | ||
cd $BATS_TEST_DIRNAME | ||
load "common" | ||
pod_a_net1=$(get_net1_ip "test-protocol-only-ports" "pod-a") | ||
pod_b_net1=$(get_net1_ip "test-protocol-only-ports" "pod-b") | ||
} | ||
|
||
@test "setup environments" { | ||
# create test manifests | ||
kubectl create -f protocol-only-ports.yml | ||
|
||
# verify all pods are available | ||
run kubectl -n test-protocol-only-ports wait --for=condition=ready -l app=test-protocol-only-ports pod --timeout=${kubewait_timeout} | ||
[ "$status" -eq "0" ] | ||
|
||
sleep 3 | ||
} | ||
|
||
@test "test-protocol-only-ports check pod-a -> pod-b TCP" { | ||
# nc should succeed from client-a to server by policy | ||
run kubectl -n test-protocol-only-ports exec pod-a -- sh -c "echo x | nc -w 1 ${pod_b_net1} 5555" | ||
[ "$status" -eq "0" ] | ||
} | ||
|
||
@test "test-protocol-only-ports check pod-a -> pod-b UDP" { | ||
# nc should succeed from client-a to server by policy | ||
run kubectl -n test-protocol-only-ports exec pod-a -- sh -c "echo x | nc --udp -w 1 ${pod_b_net1} 6666" | ||
[ "$status" -eq "1" ] | ||
} | ||
|
||
@test "test-protocol-only-ports check pod-b -> pod-a TCP" { | ||
# nc should succeed from client-a to server by policy | ||
run kubectl -n test-protocol-only-ports exec pod-b -- sh -c "echo x | nc -w 1 ${pod_a_net1} 5555" | ||
[ "$status" -eq "1" ] | ||
} | ||
|
||
@test "test-protocol-only-ports check pod-b -> pod-a UDP" { | ||
# nc should succeed from client-a to server by policy | ||
run kubectl -n test-protocol-only-ports exec pod-b -- sh -c "echo x | nc --udp -w 1 ${pod_a_net1} 6666" | ||
[ "$status" -eq "0" ] | ||
} | ||
|
||
@test "cleanup environments" { | ||
# remove test manifests | ||
kubectl delete -f protocol-only-ports.yml | ||
run kubectl -n test-protocol-only-ports wait --for=delete -l app=test-protocol-only-ports pod --timeout=${kubewait_timeout} | ||
[ "$status" -eq "0" ] | ||
} | ||
#2.2.6.18 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
--- | ||
apiVersion: "k8s.cni.cncf.io/v1" | ||
kind: NetworkAttachmentDefinition | ||
metadata: | ||
namespace: default | ||
name: macvlan1-simple | ||
spec: | ||
config: '{ | ||
"cniVersion": "0.3.1", | ||
"name": "macvlan1-simple", | ||
"plugins": [ | ||
{ | ||
"type": "macvlan", | ||
"mode": "bridge", | ||
"ipam":{ | ||
"type":"host-local", | ||
"subnet":"2.2.6.0/24", | ||
"rangeStart":"2.2.6.8", | ||
"rangeEnd":"2.2.6.67" | ||
} | ||
}] | ||
}' | ||
--- | ||
# namespace for MultiNetworkPolicy | ||
apiVersion: v1 | ||
kind: Namespace | ||
metadata: | ||
name: test-protocol-only-ports | ||
--- | ||
# Pods | ||
apiVersion: v1 | ||
kind: Pod | ||
metadata: | ||
name: pod-a | ||
namespace: test-protocol-only-ports | ||
annotations: | ||
k8s.v1.cni.cncf.io/networks: default/macvlan1-simple | ||
labels: | ||
app: test-protocol-only-ports | ||
name: pod-a | ||
spec: | ||
containers: | ||
- name: netcat-tcp | ||
image: ghcr.io/k8snetworkplumbingwg/multi-networkpolicy-iptables:e2e-test | ||
command: ["nc", "-klp", "5555"] | ||
securityContext: | ||
privileged: true | ||
- name: netcat-udp | ||
image: ghcr.io/k8snetworkplumbingwg/multi-networkpolicy-iptables:e2e-test | ||
command: ["nc", "-vv", "--udp", "--keep-open", "--sh-exec", "/bin/cat >&2", "--listen", "6666"] | ||
securityContext: | ||
privileged: true | ||
--- | ||
apiVersion: v1 | ||
kind: Pod | ||
metadata: | ||
name: pod-b | ||
namespace: test-protocol-only-ports | ||
annotations: | ||
k8s.v1.cni.cncf.io/networks: default/macvlan1-simple | ||
labels: | ||
app: test-protocol-only-ports | ||
name: pod-b | ||
spec: | ||
containers: | ||
- name: netcat-tcp | ||
image: ghcr.io/k8snetworkplumbingwg/multi-networkpolicy-iptables:e2e-test | ||
command: ["nc", "-klp", "5555"] | ||
securityContext: | ||
privileged: true | ||
- name: netcat-udp | ||
image: ghcr.io/k8snetworkplumbingwg/multi-networkpolicy-iptables:e2e-test | ||
command: ["nc", "-vv", "--udp", "--keep-open", "--sh-exec", "/bin/cat >&2", "--listen", "6666"] | ||
securityContext: | ||
privileged: true | ||
--- | ||
# MultiNetworkPolicies | ||
apiVersion: k8s.cni.cncf.io/v1beta1 | ||
kind: MultiNetworkPolicy | ||
metadata: | ||
name: test-multinetwork-policy-simple-1 | ||
namespace: test-protocol-only-ports | ||
annotations: | ||
k8s.v1.cni.cncf.io/policy-for: default/macvlan1-simple | ||
spec: | ||
podSelector: | ||
matchLabels: | ||
name: pod-a | ||
policyTypes: | ||
- Egress | ||
- Ingress | ||
egress: | ||
- ports: | ||
- protocol: TCP | ||
ingress: | ||
- ports: | ||
- protocol: UDP |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters