Skip to content

Fix Services-vs-NetworkPolicy - #14466

Merged
openshift-bot merged 3 commits into
openshift:masterfrom
danwinship:networkpolicy-services
Jun 18, 2017
Merged

openshift-bot merged 3 commits into
openshift:masterfrom
danwinship:networkpolicy-services

Conversation

@danwinship

Copy link
Copy Markdown
Contributor

This fixes the "you can't use service IPs to connect to a pod if that connection is only allowed by a NetworkPolicy with a podSelector" bug, by making it so that we don't masquerade pod->service connections when using the networkpolicy plugin, so that we can do the policy checks after the service IP rewrite rather than before like we do now (and like we do in the multitenant plugin).


To recap: suppose pod 1 (10.128.0.2) wants to connect to service 172.30.99.99:1234, which is implemented by pod 2 (10.128.0.3) on port 80. If we just used kubernetes' default masquerading rules, we'd get:

  • pod1 sends a SYN 10.128.0.2:49152 -> 172.30.99.99:1234
  • ovs outputs it to tun0 because 172.30.99.99 is not in the cluster network
  • iptables DNATs the packet to 10.128.0.3:80
  • the packet is then routed back to tun0 because of the new dest IP
  • ovs outputs it to pod2
  • pod2 then responds with an ACK 10.128.0.3:80 -> 10.128.0.2:49152
  • ovs outputs it to pod1
  • pod1 rejects the packet because it doesn't have a pending SYN to 10.128.0.3:80

So we add another iptables rule, causing the packet to get masqueraded:

  • pod1 sends a SYN 10.128.0.2:49152 -> 172.30.99.99:1234
  • ovs outputs it to tun0 because 172.30.99.99 is not in the cluster network
  • iptables masquerades the packet, making it 10.128.0.1:9999 -> 10.128.0.3:80
  • the packet is then routed back to tun0 because of the new dest IP
  • ovs outputs it to pod2
  • pod2 then responds with an ACK 10.128.0.3:80 -> 10.128.0.1:9999
  • ovs outputs it to tun0 (because it's addressed to the node)
  • iptables unmasquerades the packet, making it 172.30.99.99:1234 -> 10.128.0.2:49152
  • the packet is routed back to tun0 because of the new dest IP
  • ovs outputs it to pod1
  • pod1 accepts the packet and continues the TCP handshake

The original plan was to change this to:

  • pod1 sends a SYN 10.128.0.2:49152 -> 172.30.99.99:1234
  • ovs outputs it to tun0 because 172.30.99.99 is not in the cluster network
  • iptables DNATs the packet to 10.128.0.3:80
  • the packet is then routed back to tun0 because of the new dest IP
  • ovs outputs it to pod2
  • pod2 then responds with an ACK 10.128.0.3:80 -> 10.128.0.2:49152
  • ovs bounces the packet to tun0 because it knows, via conntrack state, that the packet needs to be un-DNATted
  • iptables un-DNATs the packet, making it 172.30.99.99:1234 -> 10.128.0.2:49152
  • the packet is routed back to tun0 because of the new dest IP
  • ovs outputs it to pod1
  • pod1 accepts the packet and continues the TCP handshake

But we were never able to get that to work right. Eventually we settled on:

  • pod1 sends a SYN 10.128.0.2:49152 -> 172.30.99.99:1234
  • ovs outputs it to tun0 because 172.30.99.99 is not in the cluster network
  • iptables DNATs the packet to 10.128.0.3:80
  • the packet is then routed back to tun0 because of the new dest IP
  • ovs outputs it to pod2
  • pod2 then responds with an ACK 10.128.0.3:80 -> 10.128.0.2:49152
  • ovs un-DNATs the packet itself, via actions=ct(nat), making it 172.30.99.99:1234 -> 10.128.0.2:49152
  • ovs outputs it to pod1
  • pod1 accepts the packet and continues the TCP handshake

After discussion in the scrum yesterday, this only changes the masquerading behavior when using the networkpolicy plugin; we'll change it for subnet/multitenant in 3.7 after the code has gotten a little more testing.

Also, there is one piece missing here: if you use this with the userspace proxier, then all pod->service connections will be allowed, even if they shouldn't be; this is because we no longer check the policy before passing the connection to the proxier, and when it comes back, if you're using the userspace proxier, the source IP will be the node's IP, and in that case we'll accept it unconditionally (because you have to do that, in order for health checks to work, etc). So we need to do one of:

  1. Require the iptables proxier when using the networkpolicy plugin
  2. Tweak things so that you get the old broken "always fail" behavior rather than the new broken "always succeed" behavior.

It seems like neither of those solutions works for the case of the hybrid proxier on routers though... right?

otx.AddFlow("table=81, priority=100, ip, ct_state=+trk+est, actions=output:NXM_NX_REG2[]")
otx.AddFlow("table=81, priority=0, actions=drop")
otx.AddFlow("table=21, priority=200, ip, nw_dst=%s, actions=ct(commit,table=30)", np.node.networkInfo.ClusterNetwork.String())
otx.AddFlow("table=80, priority=200, ip, ct_state=+rpl, actions=output:NXM_NX_REG2[]")

@danwinship danwinship Jun 3, 2017 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

So this basically reverts #13877; we added the ct_zone because the table=21 rule was causing problems with pod-to-external connections, but actually we only need to call commit there on pod-to-pod connections anyway (so they get tracked so we can recognize replies to them as replies). So by only calling commit in a case where iptables will never see the packet anyway, we can avoid needing to fiddle with a zone. The table=80 rule didn't need to be calling commit anyway, it just needed to call ct() on the packet so we can then read its ct_state, but the new rules added in ovscontroller.go end up calling ct() on all packets now, so we don't need to do it again here.

otx.AddFlow("table=30, priority=300, ip, nw_dst=%s, actions=output:2", localSubnetGateway)
otx.AddFlow("table=30, priority=100, ip, nw_dst=%s, actions=goto_table:60", serviceNetworkCIDR)
if oc.useConnTrack {
otx.AddFlow("table=30, priority=300, ip, nw_dst=%s, ct_state=+rpl, actions=ct(nat),goto_table:70", localSubnetCIDR)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

This is kind of a hack; we call ct(nat) on all pod-reply-to-pod packets; if they're DNATted this will undo that and if they're not it's a no-op. In earlier versions of the patch we were setting ct_state on the outgoing packets and then only un-DNATting the replies with ct_state set. But then we have to worry about our use of ct_state conflicting with other uses, etc. So this avoids that

@danwinship

Copy link
Copy Markdown
Contributor Author

@openshift/networking PTAL
@knobunc note the question about the hybrid proxier

@knobunc

knobunc commented Jun 5, 2017

Copy link
Copy Markdown
Contributor

Regarding the Q, I vote we require the iptables proxier. Regarding the router, do we need to do anything if idling is not supported (i.e. if EnableUnidling = false in the config?).

We have to document that this means the userspace proxy and idling are not supported with NetworkPolicy (at the moment) and then I think we need to implement the service rules in ovs so that we can support the userspace proxy before we move out of tech preview.

@danwinship
danwinship force-pushed the networkpolicy-services branch from 3c4b5a2 to 2a081e7 Compare June 6, 2017 15:00
@danwinship danwinship changed the title Fix Services-vs-NetworkPolicy [DO NOT MERGE] Fix Services-vs-NetworkPolicy Jun 6, 2017
@danwinship

Copy link
Copy Markdown
Contributor Author

Regarding the Q, I vote we require the iptables proxier.

Done

Regarding the router, do we need to do anything if idling is not supported (i.e. if EnableUnidling = false in the config?).

I assume not?

We have to document that this means the userspace proxy and idling are not supported with NetworkPolicy (at the moment) and then I think we need to implement the service rules in ovs so that we can support the userspace proxy before we move out of tech preview.

Filed https://trello.com/c/LO5B6TKY about supporting userspace proxy

@danwinship danwinship mentioned this pull request Jun 6, 2017

@knobunc knobunc left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

LGTM. Thanks Dan.

@knobunc

knobunc commented Jun 9, 2017

Copy link
Copy Markdown
Contributor

[test]

@knobunc

knobunc commented Jun 9, 2017

Copy link
Copy Markdown
Contributor

Test failure:

[WARNING] `go test` had the following output to stderr:
pkg/sdn/plugin/ovscontroller_test.go:18: not enough arguments in call to NewOVSController
pkg/sdn/plugin/ovscontroller_test.go:19: too many arguments in call to oc.SetupOVS

@danwinship
danwinship force-pushed the networkpolicy-services branch 2 times, most recently from 3212b17 to a888264 Compare June 10, 2017 14:40
@openshift-bot openshift-bot added the needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. label Jun 10, 2017
@danwinship
danwinship force-pushed the networkpolicy-services branch from a888264 to 88b46f1 Compare June 12, 2017 11:48
@openshift-bot openshift-bot removed the needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. label Jun 12, 2017
@knobunc
knobunc requested a review from dcbw June 12, 2017 14:29
@knobunc

knobunc commented Jun 13, 2017

Copy link
Copy Markdown
Contributor

@danwinship
danwinship force-pushed the networkpolicy-services branch from 88b46f1 to a15ce2f Compare June 14, 2017 13:12
@openshift openshift deleted a comment from openshift-bot Jun 14, 2017
@openshift openshift deleted a comment from openshift-bot Jun 14, 2017
@danwinship

Copy link
Copy Markdown
Contributor Author

@smarterclayton

Copy link
Copy Markdown
Contributor

[test]

@smarterclayton

Copy link
Copy Markdown
Contributor
• Failure [419.997 seconds]
[networking] services
/data/src/github.com/openshift/origin/_output/local/go/src/github.com/openshift/origin/test/extended/networking/services.go:65
  basic functionality
  /data/src/github.com/openshift/origin/_output/local/go/src/github.com/openshift/origin/test/extended/networking/services.go:21
    should allow connections to another pod on a different node via a service IP [It]
    /data/src/github.com/openshift/origin/_output/local/go/src/github.com/openshift/origin/test/extended/networking/services.go:20

    Expected error:
        <*errors.errorString | 0xc4203e91f0>: {
            s: "timed out waiting for the condition",
        }
        timed out waiting for the condition
    not to have occurred

    /data/src/github.com/openshift/origin/_output/local/go/src/github.com/openshift/origin/vendor/k8s.io/kubernetes/test/e2e/framework/util.go:4963

isn't related to this change?

@danwinship

Copy link
Copy Markdown
Contributor Author

@smarterclayton The majority of this PR only affects the networkpolicy plugin, which doesn't get tested by jenkins yet. (Merging this PR is one of the prereqs for enabling the jenkins networkpolicy tests.)

It's possible that some of the code reorg could have broken the multitenant plugin and thus the tests, but given that the conformance-gce and conformance-install-update failures also look spurious, and that the networking-minimal test also failed even harder during its cleanup:

[INFO] Saving container logs
/data/src/github.com/openshift/origin/test/extended/networking.sh: line 253: 14735 Killed

I'm assuming that this is just "something went horribly wrong with jenkins"

@smarterclayton

Copy link
Copy Markdown
Contributor

[test]

@danwinship

Copy link
Copy Markdown
Contributor Author

OK. Once again, jenkins had multiple failures (everything but "integration" this time!), including random compile failures, random connectivity failures, and random ansible failures. And in particular, networking-minimal got killed somehow again:

STEP: Building a namespace api object
Jun 15 11:11:12.165: INFO: Unexpected error while creating namespace: Post https://172.17.0.2:8443/api/v1/namespaces: dial tcp 172.17.0.2:8443: getsockopt: connection refused
Jun 15 11:11:14.826: INFO: Unexpected error while creating namespace: Post https://172.17.0.2:8443/api/v1/namespaces: dial tcp 172.17.0.2:8443: getsockopt: connection refused
/data/src/github.com/openshift/origin/test/extended/networking.sh: line 253: 17504 Killed                  extended.test --test.v '--ginkgo.skip=\[Feature:Federation\]|should preserve source pod IP for traffic thru service cluster IP|Networking IPerf|should provide unchanging, static URL paths for kubernetes api services|should provide Internet connection for containers|should be able to up and down services|should work after restarting kube-proxy|should work after restarting apiserver|should be able to change the type and ports of a service|Networking.*should check kube-proxy urls|OVS|multicast' '--ginkgo.focus=etworking|Services should be able to create a functioning NodePort service|EmptyDir volumes should support \(root,0644,tmpfs\)'
     17505 Done                    | tee /data/src/github.com/openshift/origin/_output/scripts/networking-minimal/logs/multitenant/test.log
Connection to 172.18.1.191 closed by remote host.

Working theory: the openvswitch 2.7.0 package which we were installing to get OVS NAT support has some horrible memory leak in it that eventually breaks everything? (Possibly even causing problems with other VMs on the same machine? I don't know the details of how the different test suites are run...)

I've repushed without "58834dc Grab a newer OVS binary for the dind image". (It's acceptable for this to merge without that for now; it just means we won't be able to merge #12991 [it won't pass tests].) If that makes things work I'll try replacing the 2.7.0 package with a 2.6.x one...

@smarterclayton

Copy link
Copy Markdown
Contributor

Still failing?

Comment thread pkg/sdn/plugin/node.go
node.watchServices()
if !node.useConnTrack {
go kwait.Forever(node.watchServices, 0)
}

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Doh. A bad rebase after the shared informers PR ended up turning the node process into a fork bomb when not using networkpolicy.

Require OVS 2.6 when using the NetworkPolicy plugin, and tweak the OVS
and iptables rules to avoid masquerading pod-to-service connections,
so that we can then correctly match NetworkPolicies against those
connections.
@danwinship
danwinship force-pushed the networkpolicy-services branch from 08aec76 to 22afa39 Compare June 16, 2017 14:34
@danwinship
danwinship force-pushed the networkpolicy-services branch from 22afa39 to f9b8548 Compare June 16, 2017 17:38
@smarterclayton

smarterclayton commented Jun 16, 2017 via email

Copy link
Copy Markdown
Contributor

@openshift-bot

Copy link
Copy Markdown
Contributor

Evaluated for origin test up to f9b8548

@openshift-bot

Copy link
Copy Markdown
Contributor

continuous-integration/openshift-jenkins/test SUCCESS (https://ci.openshift.redhat.com/jenkins/job/test_pull_request_origin/2339/) (Base Commit: ee67917) (PR Branch Commit: f9b8548)

@openshift openshift deleted a comment from openshift-bot Jun 18, 2017
@openshift openshift deleted a comment from openshift-bot Jun 18, 2017
@openshift openshift deleted a comment from knobunc Jun 18, 2017
@openshift openshift deleted a comment from openshift-bot Jun 18, 2017
@danwinship

Copy link
Copy Markdown
Contributor Author

[merge][severity:blocker]

@openshift-bot

Copy link
Copy Markdown
Contributor

Evaluated for origin merge up to f9b8548

@openshift-bot

openshift-bot commented Jun 18, 2017 •

Copy link
Copy Markdown
Contributor

continuous-integration/openshift-jenkins/merge SUCCESS (https://ci.openshift.redhat.com/jenkins/job/merge_pull_request_origin/1039/) (Base Commit: 18f6c93) (PR Branch Commit: f9b8548) (Extended Tests: blocker) (Image: devenv-rhel7_6374)

@openshift-bot
openshift-bot merged commit fa750b9 into openshift:master Jun 18, 2017
@danwinship
danwinship deleted the networkpolicy-services branch June 18, 2017 20:16
@knobunc

knobunc commented Jun 23, 2017

Copy link
Copy Markdown
Contributor

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants