@@ -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,51 @@ 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
+ nsA := f .Namespace
103
+ nsBName := f .BaseName + "-b"
104
+ nsB , err := f .CreateNamespace (nsBName , map [string ]string {
105
+ "ns-name" : nsBName ,
106
+ })
107
+ framework .ExpectNoError (err , "Error occurred while creating namespace-b." )
108
+
109
+ ginkgo .By ("Creating a simple server in another namespace that serves on port 80 and 81." )
110
+ podB , serviceB := createServerPodAndService (f , nsB , "pod-b" , []int {80 , 81 })
111
+
112
+ ginkgo .By ("Waiting for pod ready" , func () {
113
+ err := e2epod .WaitTimeoutForPodReadyInNamespace (f .ClientSet , podB .Name , nsB .Name , framework .PodStartTimeout )
114
+ framework .ExpectNoError (err )
115
+ })
116
+
117
+ ginkgo .By ("Creating client-a, which should be able to contact the server in another namespace." , func () {
118
+ testCanConnect (f , nsA , "client-a" , serviceB , 80 )
119
+ })
120
+
121
+ policy := & networkingv1.NetworkPolicy {
122
+ ObjectMeta : metav1.ObjectMeta {
123
+ Name : "default-deny-all" ,
124
+ },
125
+ Spec : networkingv1.NetworkPolicySpec {
126
+ PodSelector : metav1.LabelSelector {},
127
+ PolicyTypes : []networkingv1.PolicyType {networkingv1 .PolicyTypeEgress , networkingv1 .PolicyTypeIngress },
128
+ Ingress : []networkingv1.NetworkPolicyIngressRule {},
129
+ Egress : []networkingv1.NetworkPolicyEgressRule {},
130
+ },
131
+ }
132
+
133
+ policy , err = f .ClientSet .NetworkingV1 ().NetworkPolicies (f .Namespace .Name ).Create (context .TODO (), policy , metav1.CreateOptions {})
134
+ framework .ExpectNoError (err )
135
+ defer cleanupNetworkPolicy (f , policy )
136
+
137
+ ginkgo .By ("Creating client-to-a, which should not be able to contact the server in the same namespace, Ingress check." , func () {
138
+ testCannotConnect (f , nsA , "client-to-a" , service , 80 )
139
+ })
140
+
141
+ ginkgo .By ("Creating client-to-b, which should not be able to contact the server in another namespace, Egress check." , func () {
142
+ testCannotConnect (f , nsA , "client-to-b" , serviceB , 80 )
143
+ })
144
+ })
145
+
101
146
ginkgo .It ("should enforce policy to allow traffic from pods within server namespace based on PodSelector [Feature:NetworkPolicy]" , func () {
102
147
nsA := f .Namespace
103
148
nsBName := f .BaseName + "-b"
@@ -906,6 +951,94 @@ var _ = SIGDescribe("NetworkPolicy [LinuxOnly]", func() {
906
951
testCannotConnect (f , f .Namespace , "client-a" , service , allowedPort )
907
952
})
908
953
954
+ ginkgo .It ("should work with Ingress,Egress specified together [Feature:NetworkPolicy]" , func () {
955
+ const allowedPort = 80
956
+ const notAllowedPort = 81
957
+ protocolUDP := v1 .ProtocolUDP
958
+
959
+ nsBName := f .BaseName + "-b"
960
+ nsB , err := f .CreateNamespace (nsBName , map [string ]string {
961
+ "ns-name" : nsBName ,
962
+ })
963
+ framework .ExpectNoError (err , "Error occurred while creating namespace-b." )
964
+
965
+ podB , serviceB := createServerPodAndService (f , nsB , "pod-b" , []int {allowedPort , notAllowedPort })
966
+ defer cleanupServerPodAndService (f , podB , serviceB )
967
+
968
+ // Wait for Server with Service in NS-B to be ready
969
+ framework .Logf ("Waiting for servers to be ready." )
970
+ err = e2epod .WaitTimeoutForPodReadyInNamespace (f .ClientSet , podB .Name , nsB .Name , framework .PodStartTimeout )
971
+ framework .ExpectNoError (err , "Error occurred while waiting for pod status in namespace: Ready." )
972
+
973
+ ginkgo .By ("Create a network policy for the server which denies both Ingress and Egress traffic." )
974
+ policy := & networkingv1.NetworkPolicy {
975
+ ObjectMeta : metav1.ObjectMeta {
976
+ Name : "ingress-egress-rule" ,
977
+ },
978
+ Spec : networkingv1.NetworkPolicySpec {
979
+ PolicyTypes : []networkingv1.PolicyType {networkingv1 .PolicyTypeIngress , networkingv1 .PolicyTypeEgress },
980
+ Ingress : []networkingv1.NetworkPolicyIngressRule {{
981
+ From : []networkingv1.NetworkPolicyPeer {{
982
+ NamespaceSelector : & metav1.LabelSelector {
983
+ MatchLabels : map [string ]string {
984
+ "ns-name" : nsBName ,
985
+ },
986
+ },
987
+ }},
988
+ Ports : []networkingv1.NetworkPolicyPort {{
989
+ Port : & intstr.IntOrString {IntVal : allowedPort },
990
+ }},
991
+ }},
992
+ Egress : []networkingv1.NetworkPolicyEgressRule {
993
+ {
994
+ Ports : []networkingv1.NetworkPolicyPort {
995
+ // Allow DNS look-ups
996
+ {
997
+ Protocol : & protocolUDP ,
998
+ Port : & intstr.IntOrString {Type : intstr .Int , IntVal : 53 },
999
+ },
1000
+ },
1001
+ },
1002
+ {
1003
+ To : []networkingv1.NetworkPolicyPeer {
1004
+ {
1005
+ NamespaceSelector : & metav1.LabelSelector {
1006
+ MatchLabels : map [string ]string {
1007
+ "ns-name" : nsBName ,
1008
+ },
1009
+ },
1010
+ },
1011
+ },
1012
+ Ports : []networkingv1.NetworkPolicyPort {{
1013
+ Port : & intstr.IntOrString {IntVal : allowedPort },
1014
+ }},
1015
+ },
1016
+ },
1017
+ },
1018
+ }
1019
+
1020
+ policy , err = f .ClientSet .NetworkingV1 ().NetworkPolicies (f .Namespace .Name ).Create (context .TODO (), policy , metav1.CreateOptions {})
1021
+ framework .ExpectNoError (err , "Error creating Network Policy %v: %v" , policy .ObjectMeta .Name , err )
1022
+ defer cleanupNetworkPolicy (f , policy )
1023
+
1024
+ ginkgo .By ("client-a should be able to communicate with server port 80 in namespace-b" , func () {
1025
+ testCanConnect (f , f .Namespace , "client-a" , serviceB , allowedPort )
1026
+ })
1027
+
1028
+ ginkgo .By ("client-b should be able to communicate with server port 80 in namespace-a" , func () {
1029
+ testCanConnect (f , nsB , "client-b" , service , allowedPort )
1030
+ })
1031
+
1032
+ ginkgo .By ("client-a should not be able to communicate with server port 81 in namespace-b" , func () {
1033
+ testCannotConnect (f , f .Namespace , "client-a" , serviceB , notAllowedPort )
1034
+ })
1035
+
1036
+ ginkgo .By ("client-b should not be able to communicate with server port 81 in namespace-a" , func () {
1037
+ testCannotConnect (f , nsB , "client-b" , service , notAllowedPort )
1038
+ })
1039
+
1040
+ })
1041
+
909
1042
ginkgo .It ("should enforce egress policy allowing traffic to a server in a different namespace based on PodSelector and NamespaceSelector [Feature:NetworkPolicy]" , func () {
910
1043
var nsBserviceA , nsBserviceB * v1.Service
911
1044
var nsBpodServerA , nsBpodServerB * v1.Pod
0 commit comments