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 e2e network policy tests. #35660

Merged
merged 1 commit into from
May 2, 2017

Conversation

ozdanborne
Copy link
Contributor

@ozdanborne ozdanborne commented Oct 27, 2016

What this PR does / why we need it:
Add set of e2e tests for Network Policy. This has succesfully run againast a deployment using Calico as the network policy provider.

Specifically, adds a new e2e test file (/test/e2e/network_policy.go) which tests TCP connectivity between pods with isolation.

See #25638 for PR that added NetworkPolicy resource.

This PR is a replacement for #27447 .


This change is Reviewable

@k8s-ci-robot
Copy link
Contributor

Can a kubernetes member verify that this patch is reasonable to test? If so, please reply with "@k8s-bot ok to test" on its own line. Until that is done, I will not automatically test new commits in this PR, but the usual testing commands will still work. Regular contributors should join the org to skip this step.

If you have questions or suggestions related to this bot's behavior, please file an issue against the kubernetes/test-infra repository.

@googlebot
Copy link

Thanks for your pull request. It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

📝 Please visit https://cla.developers.google.com/ to sign.

Once you've signed, please reply here (e.g. I signed it!) and we'll verify. Thanks.


  • If you've already signed a CLA, it's possible we don't have your GitHub username or you're using a different email address. Check your existing CLA data and verify that your email is set on your git commits.
  • If you signed the CLA as a corporation, please let us know the company's name.

@k8s-github-robot k8s-github-robot added size/XL Denotes a PR that changes 500-999 lines, ignoring generated files. release-note-label-needed labels Oct 27, 2016
@spxtr spxtr assigned caseydavenport and unassigned spxtr Oct 27, 2016
Copy link
Member

@caseydavenport caseydavenport left a comment

Choose a reason for hiding this comment

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

@djosborne Thanks for doing this!

A few nitty comments from me, but overall this looks like a nice set of basic e2es for NetworkPolicy.

a simple netcat server,

We test the following:
- Namespace ingress isolation set to DefaultDeny
Copy link
Member

Choose a reason for hiding this comment

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

extra leading space on the first bullet.

- Policy rules to allow mono-directional TCP connectivity
- Policy rules to allow bi-directional TCP connectivity

TODO:
Copy link
Member

Choose a reason for hiding this comment

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

Not a big fan of leaving TODOs in the code, but not too fussed.

}
}()

framework.Logf("Waiting for client-b to run.")
Copy link
Member

Choose a reason for hiding this comment

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

This log doesn't correspond with the equivalent one for client-a above?

framework.Logf("Waiting for client-a to to come up.")

setNamespaceIsolation(f, ns, "DefaultDeny")

By("Creating a simple server.")
podServer, service := createServerPod(f, ns, serverName, []int{listeningPort1})
Copy link
Member

Choose a reason for hiding this comment

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

Naming nit: podServer is actually a serverPod

By("Creating a network policy for the server which allows traffic from the pod 'client-a'.")
policy, err := f.Client.NetworkPolicies(ns.Name).Create(&extensions.NetworkPolicy{
ObjectMeta: api.ObjectMeta{
Name: "test-policy",
Copy link
Member

Choose a reason for hiding this comment

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

A more descriptive name here would be nice - something like Name: "allow-client-a" maybe?

}()

// Create a pod with name 'client-b', which will attempt to comunicate with the server,
// but should not be able to do to the label-based policy.
Copy link
Member

Choose a reason for hiding this comment

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

extra space at beginning of comment.

s/do to/due to

}
}()

// Create a pod with name 'client-a', which should be able to communicate with server.
Copy link
Member

Choose a reason for hiding this comment

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

comment doesn't match the test.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I don't see how that's true? This is the PodSelector test, which allows connections from pods with label "pod-name": "client-a"

Copy link
Member

Choose a reason for hiding this comment

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

D'oh, I somehow put a "not" in the comment when reading it. Sorry...

Copy link
Contributor

Choose a reason for hiding this comment

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

No, Casey's original comment was correct; line 347 says:

// Create a pod with name 'client-a', which should be able to communicate with server.

and 348 says:

 By("Creating client-a in ns-a which should *not* be able to contact the server.")

(or maybe the comment got moved around after new commits were pushed...)

for i, port := range ports {
// Create the Container
containers = append(containers, api.Container{
Name: fmt.Sprintf("%s-container-%d", podName, i),
Copy link
Member

Choose a reason for hiding this comment

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

Instead of naming this after i, could you name it after the port it is listening on?

},
Ports: []api.ContainerPort{{ContainerPort: int32(port)}},
})
// Create the Service.
Copy link
Member

Choose a reason for hiding this comment

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

comment doesn't match the code is describes.

// Create a server pod with a listening container for each port in ports[].
// Will also assign a pod label with key: "pod-name" and label set to the given podname for later use by the network
// policy.
func createServerPod(f *framework.Framework, namespace *api.Namespace, podName string, ports []int) (*api.Pod, *api.Service) {
Copy link
Member

Choose a reason for hiding this comment

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

naming nit: createServerPodAndService

@caseydavenport
Copy link
Member

@djosborne You need to sign the linuxfoundation CLA.

@ozdanborne
Copy link
Contributor Author

I signed it!

@ozdanborne
Copy link
Contributor Author

Worth noting, I used the etcd container as it was the only one that had latest netcat installed, and doesn't suffer from the dreaded search domain issue with alpine.

Might be worth adding a busybox alternative / fix for e2e tests to use so I don't have to hijack etcd.

@caseydavenport
Copy link
Member

@kubernetes/sig-network

@caseydavenport
Copy link
Member

@k8s-bot ok to test

@caseydavenport
Copy link
Member

caseydavenport commented Oct 28, 2016

@djosborne Looks like you've got some build failures.

W1028 13:10:07.906] test/e2e/network_policy.go:86: f.Client undefined (type *framework.Framework has no field or method Client)
W1028 13:10:07.906] test/e2e/network_policy.go:415: f.Client undefined (type *framework.Framework has no field or method Client)
W1028 13:10:07.906] test/e2e/network_policy.go:432: f.Client undefined (type *framework.Framework has no field or method Client)
W1028 13:10:07.907] test/e2e/network_policy.go:453: f.Client undefined (type *framework.Framework has no field or method Client)
W1028 13:10:07.907] test/e2e/network_policy.go:500: f.Client undefined (type *framework.Framework has no field or method Client)

@danwinship
Copy link
Contributor

Worth noting, I used the etcd container as it was the only one that had latest netcat installed, and doesn't suffer from the dreaded search domain issue with alpine.

Might be worth adding a busybox alternative / fix for e2e tests to use so I don't have to hijack etcd.

We also have LaunchWebserverPod() / CheckConnectivityToHost() in test/e2e/framework/util.go for this... (Well, it looks like they're currently unused, but this is what they were intended for; they originally came from the OpenShift network isolation tests.)

err = framework.WaitForPodSuccessInNamespace(f.Client, podClientA.Name, ns.Name)
Expect(err).NotTo(HaveOccurred(), "checking Client-A could communicate with server.")

By("Creating a client-b that should not succesfully connect to the closed port.")
Copy link
Contributor

Choose a reason for hiding this comment

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

"closed" sounds like it means "has had close() called on it". Something like "blocked" or "isolated" would be better. (And "unblocked"/"unisolated" rather than "opened" before)


// Create Server with Service
By("Creating a simple server.")
serverPod, service := createServerPodAndService(f, ns, serverName, []int{listeningPort1, listeningPort2})
Copy link
Contributor

Choose a reason for hiding this comment

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

This is the only test case that passes multiple ports to createServerPodAndService, but nothing tests that createServerPodAndService actually works correctly in that case. (If there is a bug in createServerPodAndService such that it only opens the first port, this test would still pass.) And then if that happened, then the test would always pass, even if the NetworkPolicy implementation was broken, because it doesn't distinguish client-b failing because of isolation vs client-b failing because of connection refused.

Simple fix: flip the policy around so port2 is allowed and port1 is denied, and test that instead (and assume that createServerPodAndService isn't likely to be buggy in a way that would let this pass erroneously)
Better fix: test that both listeningPort1 and listeningPort2 work before applying the NetworkPolicy

}
}()

// Create a pod with name 'client-a', which should be able to communicate with server.
Copy link
Contributor

Choose a reason for hiding this comment

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

No, Casey's original comment was correct; line 347 says:

// Create a pod with name 'client-a', which should be able to communicate with server.

and 348 says:

 By("Creating client-a in ns-a which should *not* be able to contact the server.")

(or maybe the comment got moved around after new commits were pushed...)


// Create a pod with name 'client-b', which will attempt to comunicate with the server,
// but should not be able to do to the label-based policy.
By("Creating client-b in ns-b which should be able to contact the server.")
Copy link
Contributor

Choose a reason for hiding this comment

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

and this comment is backwards too

@danwinship
Copy link
Contributor

danwinship commented Oct 31, 2016

Additional test possibilities:

  • "allow all" (eg, last example in https://github.com/kubernetes/kubernetes/blob/master/docs/proposals/network-policy.md)
  • multiple NetworkPolicies (eg, adding a second NetworkPolicy to a namespace allows additional connections, as opposed to being ignored, or replacing the first NetworkPolicy)
  • adding a NetworkPolicy object to a namespace without the isolation annotation has no effect (ie, does not implicitly cause a DefaultDeny)?

@ozdanborne
Copy link
Contributor Author

Sorry for the delay, thanks for the review guys. Will revisit and clean up next week.

@ozdanborne ozdanborne force-pushed the e2e-network-policy branch 2 times, most recently from 4fd1006 to 338c9b8 Compare November 10, 2016 01:14
@k8s-github-robot k8s-github-robot added size/L Denotes a PR that changes 100-499 lines, ignoring generated files. do-not-merge DEPRECATED. Indicates that a PR should not merge. Label can only be manually applied/removed. and removed size/XL Denotes a PR that changes 500-999 lines, ignoring generated files. labels Nov 10, 2016
@ozdanborne ozdanborne force-pushed the e2e-network-policy branch 2 times, most recently from 6992f2c to fe48e86 Compare November 11, 2016 16:28
@apelisse apelisse removed the do-not-merge DEPRECATED. Indicates that a PR should not merge. Label can only be manually applied/removed. label Nov 11, 2016
@ozdanborne
Copy link
Contributor Author

hey @deads2k I've finally done it - all green. Ready to merge?

cc: @caseydavenport

@deads2k deads2k removed their assignment Mar 15, 2017
@deads2k
Copy link
Contributor

deads2k commented Mar 15, 2017

hey @deads2k I've finally done it - all green. Ready to merge?

cc: @caseydavenport

My name got caught during the initial approver storm. I have approved, so when @caseydavenport lgtm's, you'll enter the queue. We're about to fork for 1.6, so I'm inclined to leave this in 1.7.

@caseydavenport
Copy link
Member

/approve

@caseydavenport
Copy link
Member

@thockin can we get a lgtm label on this guy so the merge bot will let it through?

@thockin
Copy link
Member

thockin commented Apr 5, 2017

/lgtm

@k8s-ci-robot k8s-ci-robot added the lgtm "Looks good to me", indicates that a PR is ready to be merged. label Apr 5, 2017
@grodrigues3 grodrigues3 added the needs-ok-to-test Indicates a PR that requires an org member to verify it is safe to test. label Apr 5, 2017
@caseydavenport
Copy link
Member

@k8s-bot ok to test

@k8s-github-robot k8s-github-robot removed the lgtm "Looks good to me", indicates that a PR is ready to be merged. label Apr 10, 2017
@caseydavenport
Copy link
Member

@djosborne looks like the rebase broke things:

I0410 16:49:18.147] k8s.io/kubernetes/test/e2e/network_policy.go:22: can't find import: "k8s.io/kubernetes/pkg/apis/meta/v1"

@deads2k
Copy link
Contributor

deads2k commented May 1, 2017

can't find import: "k8s.io/kubernetes/pkg/apis/meta/v1"

metav1 k8s.io/apimachinery/pkg/apis/meta/v1

@ozdanborne ozdanborne force-pushed the e2e-network-policy branch 2 times, most recently from bd6d843 to 0a49451 Compare May 1, 2017 20:41
@ozdanborne ozdanborne force-pushed the e2e-network-policy branch from 0a49451 to e3762c6 Compare May 1, 2017 21:02
@k8s-ci-robot
Copy link
Contributor

k8s-ci-robot commented May 1, 2017

@djosborne: The following test(s) failed:

Test name Commit Details Rerun command
Jenkins GKE smoke e2e b78057cf8a83a0363f4945659b2ce1140711c206 link @k8s-bot cvm gke e2e test this
Jenkins GCI GKE smoke e2e b78057cf8a83a0363f4945659b2ce1140711c206 link @k8s-bot gci gke e2e test this
Jenkins GCE e2e 1d05a44701889beaf6b52952212246afce45244f link @k8s-bot cvm gce e2e test this
Jenkins GCI GCE e2e 1d05a44701889beaf6b52952212246afce45244f link @k8s-bot gci gce e2e test this
Jenkins non-CRI GCE e2e 1d05a44701889beaf6b52952212246afce45244f link @k8s-bot non-cri e2e test this

Full PR test history. Your PR dashboard. Please help us cut down on flakes by linking to an open issue when you hit one in your PR.

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository. I understand the commands that are listed here.

@caseydavenport
Copy link
Member

/lgtm

@k8s-ci-robot k8s-ci-robot added the lgtm "Looks good to me", indicates that a PR is ready to be merged. label May 1, 2017
@k8s-github-robot
Copy link

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: caseydavenport, deads2k, djosborne, thockin

Needs approval from an approver in each of these OWNERS Files:

You can indicate your approval by writing /approve in a comment
You can cancel your approval by writing /approve cancel in a comment

@k8s-github-robot
Copy link

Automatic merge from submit-queue

@k8s-github-robot k8s-github-robot merged commit 11c5d12 into kubernetes:master May 2, 2017
@ozdanborne ozdanborne deleted the e2e-network-policy branch May 2, 2017 00:46
@fejta
Copy link
Contributor

fejta commented May 2, 2017

FYI @caseydavenport @djosborne @deads2k this merge broke the submit queue! Looks like a bad rebase of test_owners.csv

kubernetes/test-infra#2630

@dcbw dcbw added the sig/network Categorizes an issue or PR as relevant to SIG Network. label May 23, 2017
openshift-merge-robot added a commit to openshift/origin that referenced this pull request Aug 10, 2017
Automatic merge from submit-queue

NetworkPolicy tests

This pulls in the NetworkPolicy tests from kubernetes/kubernetes#35660, and then makes a few changes to get it to work with our extended networking test, and to get it to actually pass given the current state of our NetworkPolicy support; hopefully the last commit will be able to be reverted at some point.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
approved Indicates a PR has been approved by an approver from all required OWNERS files. cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. lgtm "Looks good to me", indicates that a PR is ready to be merged. needs-ok-to-test Indicates a PR that requires an org member to verify it is safe to test. release-note-none Denotes a PR that doesn't merit a release note. sig/network Categorizes an issue or PR as relevant to SIG Network. size/XXL Denotes a PR that changes 1000+ lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.