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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix handling of service comments in iptables #81563

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
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
12 changes: 6 additions & 6 deletions pkg/proxy/iptables/proxier.go
Original file line number Diff line number Diff line change
Expand Up @@ -727,14 +727,14 @@ func (proxier *Proxier) deleteEndpointConnections(connectionMap []proxy.ServiceE
const endpointChainsNumberThreshold = 1000

// Assumes proxier.mu is held.
func (proxier *Proxier) appendServiceCommentLocked(args []string, svcName string) {
func (proxier *Proxier) appendServiceCommentLocked(args []string, svcName string) []string {
// Not printing these comments, can reduce size of iptables (in case of large
// number of endpoints) even by 40%+. So if total number of endpoint chains
// is large enough, we simply drop those comments.
if proxier.endpointChainsNumber > endpointChainsNumberThreshold {
return
return args
}
args = append(args, "-m", "comment", "--comment", svcName)
return append(args, "-m", "comment", "--comment", svcName)
}

// This is where all of the iptables-save/restore calls happen.
Expand Down Expand Up @@ -1266,7 +1266,7 @@ func (proxier *Proxier) syncProxyRules() {
args = append(args[:0],
"-A", string(svcChain),
)
proxier.appendServiceCommentLocked(args, svcNameString)
args = proxier.appendServiceCommentLocked(args, svcNameString)
args = append(args,
"-m", "recent", "--name", string(endpointChain),
"--rcheck", "--seconds", strconv.Itoa(svcInfo.StickyMaxAgeSeconds()), "--reap",
Expand Down Expand Up @@ -1296,7 +1296,7 @@ func (proxier *Proxier) syncProxyRules() {

// Balancing rules in the per-service chain.
args = append(args[:0], "-A", string(svcChain))
proxier.appendServiceCommentLocked(args, svcNameString)
args = proxier.appendServiceCommentLocked(args, svcNameString)
if i < (n - 1) {
// Each rule is a probabilistic match.
args = append(args,
Expand All @@ -1310,7 +1310,7 @@ func (proxier *Proxier) syncProxyRules() {

// Rules in the per-endpoint chain.
args = append(args[:0], "-A", string(endpointChain))
proxier.appendServiceCommentLocked(args, svcNameString)
args = proxier.appendServiceCommentLocked(args, svcNameString)
// Handle traffic that loops back to the originator with SNAT.
writeLine(proxier.natRules, append(args,
"-s", utilproxy.ToCIDR(net.ParseIP(epIP)),
Expand Down
34 changes: 17 additions & 17 deletions pkg/proxy/iptables/proxier_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2350,23 +2350,23 @@ COMMIT
:KUBE-NODEPORTS - [0:0]
:KUBE-POSTROUTING - [0:0]
:KUBE-MARK-MASQ - [0:0]
:KUBE-SVC-AHZNAGK3SCETOS2T - [0:0]
:KUBE-SEP-PXD6POUVGD2I37UY - [0:0]
:KUBE-SEP-SOKZUIT7SCEVIP33 - [0:0]
:KUBE-SEP-WVE3FAB34S7NZGDJ - [0:0]
:KUBE-SVC-CW3AQCZPGQ5G56X3 - [0:0]
:KUBE-SEP-7O2UCMTPXKLP45P3 - [0:0]
:KUBE-SEP-YQ3A6XKPT42MSHMR - [0:0]
:KUBE-SEP-LJVHDBBAW75N3SGO - [0:0]
-A KUBE-POSTROUTING -m comment --comment "kubernetes service traffic requiring SNAT" -m mark --mark -j MASQUERADE
-A KUBE-MARK-MASQ -j MARK --set-xmark
-A KUBE-SERVICES -m comment --comment "ns1/svc1: cluster IP" -m tcp -p tcp -d 172.20.1.1/32 --dport 0 ! -s 10.0.0.0/24 -j KUBE-MARK-MASQ
-A KUBE-SERVICES -m comment --comment "ns1/svc1: cluster IP" -m tcp -p tcp -d 172.20.1.1/32 --dport 0 -j KUBE-SVC-AHZNAGK3SCETOS2T
-A KUBE-SVC-AHZNAGK3SCETOS2T -m statistic --mode random --probability 0.3333333333 -j KUBE-SEP-PXD6POUVGD2I37UY
-A KUBE-SEP-PXD6POUVGD2I37UY -s 10.0.1.1/32 -j KUBE-MARK-MASQ
-A KUBE-SEP-PXD6POUVGD2I37UY -m tcp -p tcp -j DNAT --to-destination 10.0.1.1:80
-A KUBE-SVC-AHZNAGK3SCETOS2T -m statistic --mode random --probability 0.5000000000 -j KUBE-SEP-SOKZUIT7SCEVIP33
-A KUBE-SEP-SOKZUIT7SCEVIP33 -s 10.0.1.2/32 -j KUBE-MARK-MASQ
-A KUBE-SEP-SOKZUIT7SCEVIP33 -m tcp -p tcp -j DNAT --to-destination 10.0.1.2:80
-A KUBE-SVC-AHZNAGK3SCETOS2T -j KUBE-SEP-WVE3FAB34S7NZGDJ
-A KUBE-SEP-WVE3FAB34S7NZGDJ -s 10.0.1.3/32 -j KUBE-MARK-MASQ
-A KUBE-SEP-WVE3FAB34S7NZGDJ -m tcp -p tcp -j DNAT --to-destination 10.0.1.3:80
-A KUBE-SERVICES -m comment --comment "ns1/svc1:http cluster IP" -m tcp -p tcp -d 172.20.1.1/32 --dport 0 ! -s 10.0.0.0/24 -j KUBE-MARK-MASQ
-A KUBE-SERVICES -m comment --comment "ns1/svc1:http cluster IP" -m tcp -p tcp -d 172.20.1.1/32 --dport 0 -j KUBE-SVC-CW3AQCZPGQ5G56X3
-A KUBE-SVC-CW3AQCZPGQ5G56X3 -m comment --comment ns1/svc1:http -m statistic --mode random --probability 0.3333333333 -j KUBE-SEP-7O2UCMTPXKLP45P3
-A KUBE-SEP-7O2UCMTPXKLP45P3 -m comment --comment ns1/svc1:http -s 10.0.1.1/32 -j KUBE-MARK-MASQ
-A KUBE-SEP-7O2UCMTPXKLP45P3 -m comment --comment ns1/svc1:http -m tcp -p tcp -j DNAT --to-destination 10.0.1.1:80
-A KUBE-SVC-CW3AQCZPGQ5G56X3 -m comment --comment ns1/svc1:http -m statistic --mode random --probability 0.5000000000 -j KUBE-SEP-YQ3A6XKPT42MSHMR
-A KUBE-SEP-YQ3A6XKPT42MSHMR -m comment --comment ns1/svc1:http -s 10.0.1.2/32 -j KUBE-MARK-MASQ
-A KUBE-SEP-YQ3A6XKPT42MSHMR -m comment --comment ns1/svc1:http -m tcp -p tcp -j DNAT --to-destination 10.0.1.2:80
-A KUBE-SVC-CW3AQCZPGQ5G56X3 -m comment --comment ns1/svc1:http -j KUBE-SEP-LJVHDBBAW75N3SGO
-A KUBE-SEP-LJVHDBBAW75N3SGO -m comment --comment ns1/svc1:http -s 10.0.1.3/32 -j KUBE-MARK-MASQ
-A KUBE-SEP-LJVHDBBAW75N3SGO -m comment --comment ns1/svc1:http -m tcp -p tcp -j DNAT --to-destination 10.0.1.3:80
-A KUBE-SERVICES -m comment --comment "kubernetes service nodeports; NOTE: this must be the last rule in this chain" -m addrtype --dst-type LOCAL -j KUBE-NODEPORTS
COMMIT
`
Expand All @@ -2385,7 +2385,7 @@ COMMIT
Spec: v1.ServiceSpec{
ClusterIP: "172.20.1.1",
Selector: map[string]string{"foo": "bar"},
Ports: []v1.ServicePort{{Name: "", TargetPort: intstr.FromInt(80), Protocol: v1.ProtocolTCP}},
Ports: []v1.ServicePort{{Name: "http", TargetPort: intstr.FromInt(80), Protocol: v1.ProtocolTCP}},
},
})

Expand All @@ -2397,7 +2397,7 @@ COMMIT
Labels: map[string]string{discovery.LabelServiceName: serviceName},
},
Ports: []discovery.EndpointPort{{
Name: utilpointer.StringPtr(""),
Name: utilpointer.StringPtr("http"),
Port: utilpointer.Int32Ptr(80),
Protocol: &tcpProtocol,
}},
Expand Down