@@ -78,10 +78,10 @@ var _ = SIGDescribe("NetworkPolicy [LinuxOnly]", func() {
78
78
cleanupServerPodAndService (f , podServer , service )
79
79
})
80
80
81
- ginkgo .It ("should support a 'default-deny' policy [Feature:NetworkPolicy]" , func () {
81
+ ginkgo .It ("should support a 'default-deny-ingress ' policy [Feature:NetworkPolicy]" , func () {
82
82
policy := & networkingv1.NetworkPolicy {
83
83
ObjectMeta : metav1.ObjectMeta {
84
- Name : "deny-all " ,
84
+ Name : "deny-ingress " ,
85
85
},
86
86
Spec : networkingv1.NetworkPolicySpec {
87
87
PodSelector : metav1.LabelSelector {},
@@ -98,6 +98,28 @@ var _ = SIGDescribe("NetworkPolicy [LinuxOnly]", func() {
98
98
testCannotConnect (f , f .Namespace , "client-cannot-connect" , service , 80 )
99
99
})
100
100
101
+ ginkgo .It ("should support a 'default-deny-all' policy [Feature:NetworkPolicy]" , func () {
102
+ policy := & networkingv1.NetworkPolicy {
103
+ ObjectMeta : metav1.ObjectMeta {
104
+ Name : "default-deny-all" ,
105
+ },
106
+ Spec : networkingv1.NetworkPolicySpec {
107
+ PodSelector : metav1.LabelSelector {},
108
+ PolicyTypes : []networkingv1.PolicyType {networkingv1 .PolicyTypeEgress , networkingv1 .PolicyTypeIngress },
109
+ Ingress : []networkingv1.NetworkPolicyIngressRule {},
110
+ Egress : []networkingv1.NetworkPolicyEgressRule {},
111
+ },
112
+ }
113
+
114
+ policy , err := f .ClientSet .NetworkingV1 ().NetworkPolicies (f .Namespace .Name ).Create (context .TODO (), policy , metav1.CreateOptions {})
115
+ framework .ExpectNoError (err )
116
+ defer cleanupNetworkPolicy (f , policy )
117
+
118
+ // Create a pod with name 'client-cannot-connect', which will attempt to communicate with the server,
119
+ // but should not be able to now that isolation is on.
120
+ testCannotConnect (f , f .Namespace , "client-cannot-connect" , service , 80 )
121
+ })
122
+
101
123
ginkgo .It ("should enforce policy to allow traffic from pods within server namespace based on PodSelector [Feature:NetworkPolicy]" , func () {
102
124
nsA := f .Namespace
103
125
nsBName := f .BaseName + "-b"
@@ -906,6 +928,94 @@ var _ = SIGDescribe("NetworkPolicy [LinuxOnly]", func() {
906
928
testCannotConnect (f , f .Namespace , "client-a" , service , allowedPort )
907
929
})
908
930
931
+ ginkgo .It ("should work with Ingress,Egress specified together [Feature:NetworkPolicy]" , func () {
932
+ const allowedPort = 80
933
+ const notAllowedPort = 81
934
+ protocolUDP := v1 .ProtocolUDP
935
+
936
+ nsBName := f .BaseName + "-b"
937
+ nsB , err := f .CreateNamespace (nsBName , map [string ]string {
938
+ "ns-name" : nsBName ,
939
+ })
940
+ framework .ExpectNoError (err , "Error occurred while creating namespace-b." )
941
+
942
+ podB , serviceB := createServerPodAndService (f , nsB , "pod-b" , []int {allowedPort , notAllowedPort })
943
+ defer cleanupServerPodAndService (f , podB , serviceB )
944
+
945
+ // Wait for Server with Service in NS-B to be ready
946
+ framework .Logf ("Waiting for servers to be ready." )
947
+ err = e2epod .WaitTimeoutForPodReadyInNamespace (f .ClientSet , podB .Name , nsB .Name , framework .PodStartTimeout )
948
+ framework .ExpectNoError (err , "Error occurred while waiting for pod status in namespace: Ready." )
949
+
950
+ ginkgo .By ("Create a network policy for the server which denies both Ingress and Egress traffic." )
951
+ policy := & networkingv1.NetworkPolicy {
952
+ ObjectMeta : metav1.ObjectMeta {
953
+ Name : "ingress-egress-rule" ,
954
+ },
955
+ Spec : networkingv1.NetworkPolicySpec {
956
+ PolicyTypes : []networkingv1.PolicyType {networkingv1 .PolicyTypeIngress , networkingv1 .PolicyTypeEgress },
957
+ Ingress : []networkingv1.NetworkPolicyIngressRule {{
958
+ From : []networkingv1.NetworkPolicyPeer {{
959
+ NamespaceSelector : & metav1.LabelSelector {
960
+ MatchLabels : map [string ]string {
961
+ "ns-name" : nsBName ,
962
+ },
963
+ },
964
+ }},
965
+ Ports : []networkingv1.NetworkPolicyPort {{
966
+ Port : & intstr.IntOrString {IntVal : allowedPort },
967
+ }},
968
+ }},
969
+ Egress : []networkingv1.NetworkPolicyEgressRule {
970
+ {
971
+ Ports : []networkingv1.NetworkPolicyPort {
972
+ // Allow DNS look-ups
973
+ {
974
+ Protocol : & protocolUDP ,
975
+ Port : & intstr.IntOrString {Type : intstr .Int , IntVal : 53 },
976
+ },
977
+ },
978
+ },
979
+ {
980
+ To : []networkingv1.NetworkPolicyPeer {
981
+ {
982
+ NamespaceSelector : & metav1.LabelSelector {
983
+ MatchLabels : map [string ]string {
984
+ "ns-name" : nsBName ,
985
+ },
986
+ },
987
+ },
988
+ },
989
+ Ports : []networkingv1.NetworkPolicyPort {{
990
+ Port : & intstr.IntOrString {IntVal : allowedPort },
991
+ }},
992
+ },
993
+ },
994
+ },
995
+ }
996
+
997
+ policy , err = f .ClientSet .NetworkingV1 ().NetworkPolicies (f .Namespace .Name ).Create (context .TODO (), policy , metav1.CreateOptions {})
998
+ framework .ExpectNoError (err , "Error creating Network Policy %v: %v" , policy .ObjectMeta .Name , err )
999
+ defer cleanupNetworkPolicy (f , policy )
1000
+
1001
+ ginkgo .By ("client-a should be able to communicate with server port 80 in namespace-b" , func () {
1002
+ testCanConnect (f , f .Namespace , "client-a" , serviceB , allowedPort )
1003
+ })
1004
+
1005
+ ginkgo .By ("client-b should be able to communicate with server port 80 in namespace-a" , func () {
1006
+ testCanConnect (f , nsB , "client-b" , service , allowedPort )
1007
+ })
1008
+
1009
+ ginkgo .By ("client-a should not be able to communicate with server port 81 in namespace-b" , func () {
1010
+ testCannotConnect (f , f .Namespace , "client-a" , serviceB , notAllowedPort )
1011
+ })
1012
+
1013
+ ginkgo .By ("client-b should not be able to communicate with server port 81 in namespace-a" , func () {
1014
+ testCannotConnect (f , nsB , "client-b" , service , notAllowedPort )
1015
+ })
1016
+
1017
+ })
1018
+
909
1019
ginkgo .It ("should enforce egress policy allowing traffic to a server in a different namespace based on PodSelector and NamespaceSelector [Feature:NetworkPolicy]" , func () {
910
1020
var nsBserviceA , nsBserviceB * v1.Service
911
1021
var nsBpodServerA , nsBpodServerB * v1.Pod
0 commit comments