Commit Graph

217 Commits

Author SHA1 Message Date
Kubernetes Prow Robot
dc4e91a875
Merge pull request #109844 from danwinship/iptables-tests-new
improve parsing in iptables unit tests
2022-06-10 14:27:44 -07:00
Dan Winship
24e1e3d9ee proxy/iptables: port packet-flow tests to use new parsing stuff 2022-05-09 11:29:08 -04:00
Dan Winship
10a72a9e03 pkg/util/iptables/testing: Add IPTables dump-parsing helpers 2022-05-09 11:29:06 -04:00
Dan Winship
f0f47ae590 proxy/iptables: tweak sortIPTablesRules some more
Sort the ":CHAINNAME" lines in the same order as the "-A CHAINNAME"
lines (meaning, KUBE-NODEPORTS and KUBE-SERVICES come first).

(This will simplify IPTablesDump because it won't need to keep track
of the declaration order and the rule order separately.)
2022-05-09 11:19:28 -04:00
Dan Winship
b0d9c063a8 unexport mistakenly-exported constants 2022-05-06 07:33:29 -04:00
Kubernetes Prow Robot
2b3508e0f1
Merge pull request #109826 from danwinship/multi-load-balancer
fix kube-proxy bug with multiple LB IPs and source ranges
2022-05-06 03:09:15 -07:00
Dan Winship
813aca47af proxy/iptables: fix firewall rules with multiple LB IPs
The various loops in the LoadBalancer rule section were mis-nested
such that if a service had multiple LoadBalancer IPs, we would write
out the firewall rules multiple times (and the allowFromNode rule for
the second and later IPs would end up being written after the "else
DROP" rule from the first IP).
2022-05-05 10:58:09 -04:00
Dan Winship
df589b46a1 proxy/iptables: test multiple LoadBalancer IPs on one service 2022-05-05 10:58:09 -04:00
Dan Winship
709b4f696d proxy/iptables: test LoadBalancerSourceRanges vs node IP
The LoadBalancer rules change if the node IP is in one of the
LoadBalancerSourceRange subnets, so make sure to set nodeIP on the
fake proxier so we can test this, and add a second source range to
TestLoadBalancer containing the node IP. (This changes the result of
one flow test that previously expected that node-to-LB would be
dropped.)
2022-05-05 10:58:07 -04:00
Kubernetes Prow Robot
50e1f70027
Merge pull request #109782 from danwinship/no-local-endpoints-metric
Don't increment "no local endpoints" metric when there are no remote endpoints
2022-05-05 05:02:20 -07:00
Kubernetes Prow Robot
794d3ab686
Merge pull request #109740 from AllenZMC/fix_test_iptables
Optimize test cases for iptables
2022-05-05 01:42:51 -07:00
Dan Winship
84ad54f0e5 Don't increment "no local endpoints" metric when there are no remote endpoints
A service having no _local_ endpoints when it does have remote
endpoints is different from a service having no endpoints at all.
2022-05-04 12:38:17 -04:00
AllenZMC
a5fb9bbd35 Optimize test cases for iptables 2022-05-01 19:33:06 +08:00
Dan Winship
0ecf11a23b proxy/iptables: add a general test of internal vs external traffic
Add TestInternalExternalMasquerade, which tests whether various
packets are considered internal or external for purposes of traffic
policy, and whether they get masqueraded, with and without
--masquerade-all, with and without a working LocalTrafficDetector.
(This extends and replaces the old TestMasqueradeAll.)
2022-04-11 13:23:08 -04:00
Dan Winship
f4261283ac proxy/iptables: add packet-flow-based tests of iptables rules
Add a new framework for testing out how particular packets would be
handled by a given set of iptables rules. (eg, "assert that a packet
from 10.180.0.2 to 172.30.0.41:80 gets NATted to 10.180.0.1:80 without
being masqueraded"). Add tests using this to all of the existing unit
tests.

This makes it easier to tell whether a given code change has any
effect on behavior, without having to carefully examine the diffs to
the generated iptables rules.
2022-04-11 13:23:08 -04:00
Dan Winship
cd55f35306 proxy/iptables: tweak sortIPTablesRules a bit
It's confusing to have the "this must be the last rule" get sorted to
be the first rule in KUBE-SERVICES...
2022-04-07 08:38:14 -04:00
Dan Winship
d9ca665551 proxy/iptables: fix up some test case comments 2022-04-07 08:38:14 -04:00
Dan Winship
261f4f9e2d proxy/iptables: remove redundant test
We originally had one HealthCheckNodePort test that used
assertIPTablesRulesEqual() and one that didn't, but later I went
through and made all the tests use assertIPTablesRulesEqual() and
didn't notice that this resulted in there now being two
nearly-identical HealthCheckNodePort tests.
2022-04-07 08:36:34 -04:00
Max Renaud
198367a486 Added test where both policies are set 2022-03-31 18:54:28 +00:00
Max Renaud
f0dfac5d07 Add sync_proxy_rules_no_local_endpoints_total metric 2022-03-31 18:54:23 +00:00
Tim Hockin
40e21e310f Elide the -FW- chain when possible
This makes it epsilon harder to reason about but saves one chain
declaration and one rule per service-port usually.
2022-03-30 09:55:34 -07:00
Tim Hockin
c4271c9a6f Rename tests to avoid underscores 2022-03-30 09:55:34 -07:00
Tim Hockin
9ed6b73495 kube-proxy: comment endpoint in SEP jumps 2022-03-30 09:55:34 -07:00
Tim Hockin
f1553f58c5 kube-proxy: Remove now unneeded rule
Now that NodePorts jump to EXT, we don't need a specific rule for
loopback source detection.
2022-03-30 09:54:40 -07:00
Tim Hockin
db932a0ab1 kube-proxy: Rework LB VIP capture logic
* Comments
* If there are multiple VIPs, don't declare the fwChain multiple times.
* Don't emit the last -j DROP if there's no source ranges
2022-03-30 09:54:40 -07:00
Tim Hockin
07b2585927 kube-proxy: Rename XLB -> EXT
This changes the "XLB" chain into the "EXT" chain - the "external
destinations" chain.
2022-03-30 09:54:38 -07:00
Tim Hockin
482f3bc4bf kube-proxy: all external jumps to XLB chain
This makes the "destination" policy model clearer.  All external
destination captures now jump to the "XLB chain, which is the main place
that masquerade is done (removing it from most other places).

This is simpler to trace - XLB *always* exists (as long as you have an
external exposure) and never gets bypassed.
2022-03-30 09:52:18 -07:00
Tim Hockin
dd0fc6b354 kube-proxy: print line number for test failures 2022-03-29 18:48:27 -07:00
Tim Hockin
ef959f00af kube-proxy: clean up tests
No functional changes, much whitespace.

Make assertIPTablesRulesEqual() *not* sort the `expected` value - make
the test cases all be pre-sorted.  This will make followup commits
cleaner.

Make the test output cleaner when this fails.

Use dedent everywhere for easier reading.
2022-03-29 18:48:27 -07:00
Andrew Sy Kim
718a655e42 pkg/proxy/iptables: add and fix existing unit tests based on changes to ProxyTermintingEndpoints
Signed-off-by: Andrew Sy Kim <andrewsy@google.com>
2022-03-29 11:37:15 -04:00
Dan Winship
548cf9d5de proxy/iptables: fix internal-vs-external traffic policy handling
Fix internal and external traffic policy to be handled separately (so
that, in particular, services with Local internal traffic policy and
Cluster external traffic policy do not behave as though they had Local
external traffic policy as well.

Additionally, traffic to an `internalTrafficPolicy: Local` service on
a node with no endpoints is now dropped rather than being rejected
(which, as in the external case, may prevent traffic from being lost
when endpoints are in flux).
2022-03-26 11:06:34 -04:00
Dan Winship
2e780ecd99 proxy/iptables: Split KUBE-SVL-XXX chain out of KUBE-XLB-XXX
Now the XLB chain _only_ implements the "short-circuit local
connections to the SVC chain" rule, and the actual endpoint selection
happens in the SVL chain.

Though not quite implemented yet, this will eventually also mean that
"SVC" = "Service, Cluster traffic policy" as opposed to "SVL" =
"Service, Local traffic policy"
2022-03-26 11:06:34 -04:00
Dan Winship
dd4d88398c proxy/iptables: fix up endpoint chain name computation
Rather than lazily computing and then caching the endpoint chain name
because we don't have the right information at construct time, just
pass the right information at construct time and compute the chain
name then.
2022-03-18 16:10:33 -04:00
Khaled (Kal) Henidak
407dcf5164 iptables: remove port opener 2022-03-03 20:04:08 +00:00
Dan Winship
d830ef6112 proxy/iptables: add HealthCheckNodePorts to unit tests that need them
To avoid spurious errors in the test output:

  E0114 08:43:27.453974 3718376 service.go:221] "Service has no healthcheck nodeport" service="ns1/svc1"
2022-02-21 09:16:23 -05:00
Dan Winship
d74df127e9 proxy/iptables: Fix up IPs and ports in unit tests
All of the tests used a localDetector that considered the pod IP range
to be 10.0.0.0/24, but lots of the tests used pod IPs in 10.180.0.0/16
or 10.0.1.0/24, meaning the generated iptables rules were somewhat
inconsistent. Fix this by expanding the localDetector's pod IP range
to 10.0.0.0/8. (Changing the pod IPs to all be in 10.0.0.0/24 instead
would be a much larger change since it would result in the SEP chain
names changing.)

Meanwhile, the different tests were also horribly inconsistent about
what values they used for other IPs, and some of them even used the
same IPs (or ports) for different things in the same test case. Fix
these all up and create a consistent set of IP assignments:

// Pod IPs:             10.0.0.0/8
// Service ClusterIPs:  172.30.0.0/16
// Node IPs:            192.168.0.0/24
// Local Node IP:       192.168.0.2
// Service ExternalIPs: 192.168.99.0/24
// LoadBalancer IPs:    1.2.3.4, 5.6.7.8, 9.10.11.12
// Non-cluster IPs:     203.0.113.0/24
// LB Source Range:     203.0.113.0/25
2022-02-21 09:16:22 -05:00
Dan Winship
37ada4b04f proxy/iptables: Don't create unused chains, and enable the unit test for that 2022-02-21 09:16:22 -05:00
Dan Winship
ef4324eaf5 proxy/iptables: refactor unit test code / fix error reporting
Only run assertIPTablesRuleJumps() on the expected output, not on the
actual output, since if there's a problem with the actual output, we'd
rather see it as the diff from the expected output.
2022-02-21 09:16:22 -05:00
Dan Winship
4af471f8be proxy/iptables: move GetChainLines unit tests to the right package
GetChainLines is a utiliptables method, so it should be part of the
unit tests there.
2022-02-21 09:16:22 -05:00
Quan Tian
6ce612ef65 kube-proxy: fix duplicate port opening
When nodePortAddresses is not specified for kube-proxy, it tried to open
the node port for a NodePort service twice, triggered by IPv4ZeroCIDR
and IPv6ZeroCIDR separately. The first attempt would succeed and the
second one would always generate an error log like below:

"listen tcp4 :30522: bind: address already in use"

This patch fixes it by ensuring nodeAddresses of a proxier only contain
the addresses for its IP family.
2022-01-08 02:35:35 +08:00
Quan Tian
95a706ba7c Remove redundant forwarding rule in filter table 2021-11-11 10:27:53 +08:00
Dan Winship
a4e6d2f6fa proxy/iptables: add a unit test for the comment elision code 2021-11-10 09:08:02 -05:00
Dan Winship
8ef1255cdd proxy/iptables: Abstract out code for writing service-chain-to-endpoint-chain rules
The same code appeared twice, once for the SVC chain and once for the
XLB chain, with the only difference being that the XLB version had
more verbose comments.
2021-11-09 20:59:33 -05:00
Dan Winship
9cd0552ddd proxy/iptables: Remove unnecessary /32 and /128 in iptables rules
If you pass just an IP address to "-s" or "-d", the iptables command
will fill in the correct mask automatically.

Originally, the proxier was just hardcoding "/32" for all of these,
which was unnecessary but simple. But when IPv6 support was added, the
code was made more complicated to deal with the fact that the "/32"
needed to be "/128" in the IPv6 case, so it would parse the IPs to
figure out which family they were, which in turn involved adding some
checks in case the parsing fails (even though that "can't happen" and
the old code didn't check for invalid IPs, even though that would
break the iptables-restore if there had been any).

Anyway, all of that is unnecessary because we can just pass the IP
strings to iptables directly rather than parsing and unparsing them
first.

(The diff to proxier_test.go is just deleting "/32" everywhere.)
2021-11-09 09:32:50 -05:00
Kubernetes Prow Robot
0940dd6fc4
Merge pull request #106163 from aojea/conntrack_readiness
kube-proxy consider endpoint readiness to delete UDP stale conntrack entries
2021-11-08 13:11:44 -08:00
Tim Hockin
f662170ff7 kube-proxy: make iptables buffer-writing cleaner 2021-11-05 12:28:19 -07:00
Antonio Ojea
909925b492 kube-proxy: fix stale detection logic
The logic to detect stale endpoints was not assuming the endpoint
readiness.

We can have stale entries on UDP services for 2 reasons:
- an endpoint was receiving traffic and is removed or replaced
- a service was receiving traffic but not forwarding it, and starts
to forward it.

Add an e2e test to cover the regression
2021-11-05 20:14:56 +01:00
Dan Winship
229ae58520 proxy/iptables: fix all-vs-ready endpoints a bit
Filter the allEndpoints list into readyEndpoints sooner, and set
"hasEndpoints" based (mostly) on readyEndpoints, not allEndpoints (so
that, eg, we correctly generate REJECT rules for services with no
_functioning_ endpoints, even if they have unusable terminating
endpoints).

Also, write out the endpoint chains at the top of the loop when we
iterate the endpoints for the first time, rather than copying some of
the data to another set of variables and then writing them out later.
And don't write out endpoint chains that won't be used

Also, generate affinity rules only for readyEndpoints rather than
allEndpoints, so affinity gets broken correctly when an endpoint
becomes unready.
2021-11-04 16:32:08 -04:00
Dan Winship
6ab3dc6875 proxy/iptables: Add more stuff to the unit test
The external traffic policy terminating endpoints test was testing
LoadBalancer functionality against a NodePort service with no
nodePorts (or loadBalancer IPs). It managed to test what it wanted to
test, but it's kind of dubious (and we probably _shouldn't_ have been
generating the rules it was looking for since there was no way to
actually reach the XLB chains). So fix that.

Also make the terminating endpoints test use session affinity, to add
more testing for that. Also, remove the multiple copies of the same
identical Service that is used for all of the test cases in that test.

Also add a "Cluster traffic policy and no source ranges" test to
TestOverallIPTablesRulesWithMultipleServices since we weren't really
testing either of those.

Also add a test of --masquerade-all.
2021-11-04 16:32:08 -04:00
Dan Winship
22a951c096 proxy/iptables: Fix TestOnlyLocalNodePortsNoClusterCIDR
The test got broken to not actually use "no cluster CIDR" when
LocalDetector was implemented (and the old version of the unit test
didn't check enough to actually notice this).
2021-11-04 16:32:08 -04:00