Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add test for ingress/egress combination #90095

Merged
merged 3 commits into from Apr 25, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
137 changes: 135 additions & 2 deletions test/e2e/network/network_policy.go
Expand Up @@ -78,10 +78,10 @@ var _ = SIGDescribe("NetworkPolicy [LinuxOnly]", func() {
cleanupServerPodAndService(f, podServer, service)
})

ginkgo.It("should support a 'default-deny' policy [Feature:NetworkPolicy]", func() {
ginkgo.It("should support a 'default-deny-ingress' policy [Feature:NetworkPolicy]", func() {
policy := &networkingv1.NetworkPolicy{
ObjectMeta: metav1.ObjectMeta{
Name: "deny-all",
Name: "deny-ingress",
},
Spec: networkingv1.NetworkPolicySpec{
PodSelector: metav1.LabelSelector{},
Expand All @@ -98,6 +98,51 @@ var _ = SIGDescribe("NetworkPolicy [LinuxOnly]", func() {
testCannotConnect(f, f.Namespace, "client-cannot-connect", service, 80)
})

ginkgo.It("should support a 'default-deny-all' policy [Feature:NetworkPolicy]", func() {
nsA := f.Namespace
nsBName := f.BaseName + "-b"
nsB, err := f.CreateNamespace(nsBName, map[string]string{
agadelshin marked this conversation as resolved.
Show resolved Hide resolved
"ns-name": nsBName,
})
framework.ExpectNoError(err, "Error occurred while creating namespace-b.")

ginkgo.By("Creating a simple server in another namespace that serves on port 80 and 81.")
podB, serviceB := createServerPodAndService(f, nsB, "pod-b", []int{80, 81})

ginkgo.By("Waiting for pod ready", func() {
err := e2epod.WaitTimeoutForPodReadyInNamespace(f.ClientSet, podB.Name, nsB.Name, framework.PodStartTimeout)
framework.ExpectNoError(err)
})

ginkgo.By("Creating client-a, which should be able to contact the server in another namespace.", func() {
testCanConnect(f, nsA, "client-a", serviceB, 80)
})

policy := &networkingv1.NetworkPolicy{
ObjectMeta: metav1.ObjectMeta{
Name: "default-deny-all",
},
Spec: networkingv1.NetworkPolicySpec{
PodSelector: metav1.LabelSelector{},
PolicyTypes: []networkingv1.PolicyType{networkingv1.PolicyTypeEgress, networkingv1.PolicyTypeIngress},
Ingress: []networkingv1.NetworkPolicyIngressRule{},
Egress: []networkingv1.NetworkPolicyEgressRule{},
agadelshin marked this conversation as resolved.
Show resolved Hide resolved
},
}

policy, err = f.ClientSet.NetworkingV1().NetworkPolicies(f.Namespace.Name).Create(context.TODO(), policy, metav1.CreateOptions{})
framework.ExpectNoError(err)
defer cleanupNetworkPolicy(f, policy)

ginkgo.By("Creating client-to-a, which should not be able to contact the server in the same namespace, Ingress check.", func() {
testCannotConnect(f, nsA, "client-to-a", service, 80)
})

ginkgo.By("Creating client-to-b, which should not be able to contact the server in another namespace, Egress check.", func() {
testCannotConnect(f, nsA, "client-to-b", serviceB, 80)
})
})

ginkgo.It("should enforce policy to allow traffic from pods within server namespace based on PodSelector [Feature:NetworkPolicy]", func() {
nsA := f.Namespace
nsBName := f.BaseName + "-b"
Expand Down Expand Up @@ -906,6 +951,94 @@ var _ = SIGDescribe("NetworkPolicy [LinuxOnly]", func() {
testCannotConnect(f, f.Namespace, "client-a", service, allowedPort)
})

ginkgo.It("should work with Ingress,Egress specified together [Feature:NetworkPolicy]", func() {
const allowedPort = 80
const notAllowedPort = 81
protocolUDP := v1.ProtocolUDP

nsBName := f.BaseName + "-b"
agadelshin marked this conversation as resolved.
Show resolved Hide resolved
nsB, err := f.CreateNamespace(nsBName, map[string]string{
agadelshin marked this conversation as resolved.
Show resolved Hide resolved
"ns-name": nsBName,
})
framework.ExpectNoError(err, "Error occurred while creating namespace-b.")

podB, serviceB := createServerPodAndService(f, nsB, "pod-b", []int{allowedPort, notAllowedPort})
defer cleanupServerPodAndService(f, podB, serviceB)

// Wait for Server with Service in NS-B to be ready
framework.Logf("Waiting for servers to be ready.")
err = e2epod.WaitTimeoutForPodReadyInNamespace(f.ClientSet, podB.Name, nsB.Name, framework.PodStartTimeout)
framework.ExpectNoError(err, "Error occurred while waiting for pod status in namespace: Ready.")

ginkgo.By("Create a network policy for the server which denies both Ingress and Egress traffic.")
policy := &networkingv1.NetworkPolicy{
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it possible to add helper func to create/generate the needed network policy? It isn't too easy to parse this and having a big chunk of struct specification makes codes less readable :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I know.

There is an issue here #88375 (comment) and @jayunit100 works on KEP I believe.

ObjectMeta: metav1.ObjectMeta{
Name: "ingress-egress-rule",
},
Spec: networkingv1.NetworkPolicySpec{
PolicyTypes: []networkingv1.PolicyType{networkingv1.PolicyTypeIngress, networkingv1.PolicyTypeEgress},
Ingress: []networkingv1.NetworkPolicyIngressRule{{
From: []networkingv1.NetworkPolicyPeer{{
NamespaceSelector: &metav1.LabelSelector{
MatchLabels: map[string]string{
"ns-name": nsBName,
},
},
}},
Ports: []networkingv1.NetworkPolicyPort{{
Port: &intstr.IntOrString{IntVal: allowedPort},
}},
}},
Egress: []networkingv1.NetworkPolicyEgressRule{
{
Ports: []networkingv1.NetworkPolicyPort{
// Allow DNS look-ups
{
Protocol: &protocolUDP,
Port: &intstr.IntOrString{Type: intstr.Int, IntVal: 53},
},
},
},
{
To: []networkingv1.NetworkPolicyPeer{
{
NamespaceSelector: &metav1.LabelSelector{
MatchLabels: map[string]string{
"ns-name": nsBName,
},
},
},
},
Ports: []networkingv1.NetworkPolicyPort{{
Port: &intstr.IntOrString{IntVal: allowedPort},
}},
},
},
},
}

policy, err = f.ClientSet.NetworkingV1().NetworkPolicies(f.Namespace.Name).Create(context.TODO(), policy, metav1.CreateOptions{})
framework.ExpectNoError(err, "Error creating Network Policy %v: %v", policy.ObjectMeta.Name, err)
defer cleanupNetworkPolicy(f, policy)

ginkgo.By("client-a should be able to communicate with server port 80 in namespace-b", func() {
testCanConnect(f, f.Namespace, "client-a", serviceB, allowedPort)
})

ginkgo.By("client-b should be able to communicate with server port 80 in namespace-a", func() {
testCanConnect(f, nsB, "client-b", service, allowedPort)
})

ginkgo.By("client-a should not be able to communicate with server port 81 in namespace-b", func() {
testCannotConnect(f, f.Namespace, "client-a", serviceB, notAllowedPort)
})

ginkgo.By("client-b should not be able to communicate with server port 81 in namespace-a", func() {
testCannotConnect(f, nsB, "client-b", service, notAllowedPort)
})

})

ginkgo.It("should enforce egress policy allowing traffic to a server in a different namespace based on PodSelector and NamespaceSelector [Feature:NetworkPolicy]", func() {
var nsBserviceA, nsBserviceB *v1.Service
var nsBpodServerA, nsBpodServerB *v1.Pod
Expand Down