Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
K
k3s
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Registry
Registry
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Jacklull
k3s
Commits
07ead7d8
Commit
07ead7d8
authored
Dec 18, 2017
by
Dan Winship
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Don't create no-op iptables rules for services with no endpoints
parent
6c91c420
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
27 additions
and
30 deletions
+27
-30
proxier.go
pkg/proxy/iptables/proxier.go
+27
-30
No files found.
pkg/proxy/iptables/proxier.go
View file @
07ead7d8
...
@@ -824,7 +824,6 @@ func (proxier *Proxier) syncProxyRules() {
...
@@ -824,7 +824,6 @@ func (proxier *Proxier) syncProxyRules() {
args
:=
make
([]
string
,
64
)
args
:=
make
([]
string
,
64
)
// Build rules for each service.
// Build rules for each service.
var
svcNameString
string
for
svcName
,
svc
:=
range
proxier
.
serviceMap
{
for
svcName
,
svc
:=
range
proxier
.
serviceMap
{
svcInfo
,
ok
:=
svc
.
(
*
serviceInfo
)
svcInfo
,
ok
:=
svc
.
(
*
serviceInfo
)
if
!
ok
{
if
!
ok
{
...
@@ -833,16 +832,19 @@ func (proxier *Proxier) syncProxyRules() {
...
@@ -833,16 +832,19 @@ func (proxier *Proxier) syncProxyRules() {
}
}
isIPv6
:=
utilproxy
.
IsIPv6
(
svcInfo
.
clusterIP
)
isIPv6
:=
utilproxy
.
IsIPv6
(
svcInfo
.
clusterIP
)
protocol
:=
strings
.
ToLower
(
string
(
svcInfo
.
protocol
))
protocol
:=
strings
.
ToLower
(
string
(
svcInfo
.
protocol
))
svcNameString
=
svcInfo
.
serviceNameString
svcNameString
:=
svcInfo
.
serviceNameString
hasEndpoints
:=
len
(
proxier
.
endpointsMap
[
svcName
])
>
0
// Create the per-service chain, retaining counters if possible.
svcChain
:=
svcInfo
.
servicePortChainName
svcChain
:=
svcInfo
.
servicePortChainName
if
hasEndpoints
{
// Create the per-service chain, retaining counters if possible.
if
chain
,
ok
:=
existingNATChains
[
svcChain
];
ok
{
if
chain
,
ok
:=
existingNATChains
[
svcChain
];
ok
{
writeLine
(
proxier
.
natChains
,
chain
)
writeLine
(
proxier
.
natChains
,
chain
)
}
else
{
}
else
{
writeLine
(
proxier
.
natChains
,
utiliptables
.
MakeChainLine
(
svcChain
))
writeLine
(
proxier
.
natChains
,
utiliptables
.
MakeChainLine
(
svcChain
))
}
}
activeNATChains
[
svcChain
]
=
true
activeNATChains
[
svcChain
]
=
true
}
svcXlbChain
:=
svcInfo
.
serviceLBChainName
svcXlbChain
:=
svcInfo
.
serviceLBChainName
if
svcInfo
.
onlyNodeLocalEndpoints
{
if
svcInfo
.
onlyNodeLocalEndpoints
{
...
@@ -854,12 +856,10 @@ func (proxier *Proxier) syncProxyRules() {
...
@@ -854,12 +856,10 @@ func (proxier *Proxier) syncProxyRules() {
writeLine
(
proxier
.
natChains
,
utiliptables
.
MakeChainLine
(
svcXlbChain
))
writeLine
(
proxier
.
natChains
,
utiliptables
.
MakeChainLine
(
svcXlbChain
))
}
}
activeNATChains
[
svcXlbChain
]
=
true
activeNATChains
[
svcXlbChain
]
=
true
}
else
if
activeNATChains
[
svcXlbChain
]
{
// Cleanup the previously created XLB chain for this service
delete
(
activeNATChains
,
svcXlbChain
)
}
}
// Capture the clusterIP.
// Capture the clusterIP.
if
hasEndpoints
{
args
=
append
(
args
[
:
0
],
args
=
append
(
args
[
:
0
],
"-A"
,
string
(
kubeServicesChain
),
"-A"
,
string
(
kubeServicesChain
),
"-m"
,
"comment"
,
"--comment"
,
fmt
.
Sprintf
(
`"%s cluster IP"`
,
svcNameString
),
"-m"
,
"comment"
,
"--comment"
,
fmt
.
Sprintf
(
`"%s cluster IP"`
,
svcNameString
),
...
@@ -878,6 +878,16 @@ func (proxier *Proxier) syncProxyRules() {
...
@@ -878,6 +878,16 @@ func (proxier *Proxier) syncProxyRules() {
writeLine
(
proxier
.
natRules
,
append
(
args
,
"! -s"
,
proxier
.
clusterCIDR
,
"-j"
,
string
(
KubeMarkMasqChain
))
...
)
writeLine
(
proxier
.
natRules
,
append
(
args
,
"! -s"
,
proxier
.
clusterCIDR
,
"-j"
,
string
(
KubeMarkMasqChain
))
...
)
}
}
writeLine
(
proxier
.
natRules
,
append
(
args
,
"-j"
,
string
(
svcChain
))
...
)
writeLine
(
proxier
.
natRules
,
append
(
args
,
"-j"
,
string
(
svcChain
))
...
)
}
else
{
writeLine
(
proxier
.
filterRules
,
"-A"
,
string
(
kubeServicesChain
),
"-m"
,
"comment"
,
"--comment"
,
fmt
.
Sprintf
(
`"%s has no endpoints"`
,
svcNameString
),
"-m"
,
protocol
,
"-p"
,
protocol
,
"-d"
,
utilproxy
.
ToCIDR
(
svcInfo
.
clusterIP
),
"--dport"
,
strconv
.
Itoa
(
svcInfo
.
port
),
"-j"
,
"REJECT"
,
)
}
// Capture externalIPs.
// Capture externalIPs.
for
_
,
externalIP
:=
range
svcInfo
.
externalIPs
{
for
_
,
externalIP
:=
range
svcInfo
.
externalIPs
{
...
@@ -913,7 +923,9 @@ func (proxier *Proxier) syncProxyRules() {
...
@@ -913,7 +923,9 @@ func (proxier *Proxier) syncProxyRules() {
}
}
replacementPortsMap
[
lp
]
=
socket
replacementPortsMap
[
lp
]
=
socket
}
}
}
// We're holding the port, so it's OK to install iptables rules.
}
if
hasEndpoints
{
args
=
append
(
args
[
:
0
],
args
=
append
(
args
[
:
0
],
"-A"
,
string
(
kubeServicesChain
),
"-A"
,
string
(
kubeServicesChain
),
"-m"
,
"comment"
,
"--comment"
,
fmt
.
Sprintf
(
`"%s external IP"`
,
svcNameString
),
"-m"
,
"comment"
,
"--comment"
,
fmt
.
Sprintf
(
`"%s external IP"`
,
svcNameString
),
...
@@ -936,10 +948,7 @@ func (proxier *Proxier) syncProxyRules() {
...
@@ -936,10 +948,7 @@ func (proxier *Proxier) syncProxyRules() {
// Allow traffic bound for external IPs that happen to be recognized as local IPs to stay local.
// Allow traffic bound for external IPs that happen to be recognized as local IPs to stay local.
// This covers cases like GCE load-balancers which get added to the local routing table.
// This covers cases like GCE load-balancers which get added to the local routing table.
writeLine
(
proxier
.
natRules
,
append
(
dstLocalOnlyArgs
,
"-j"
,
string
(
svcChain
))
...
)
writeLine
(
proxier
.
natRules
,
append
(
dstLocalOnlyArgs
,
"-j"
,
string
(
svcChain
))
...
)
}
else
{
// If the service has no endpoints then reject packets coming via externalIP
// Install ICMP Reject rule in filter table for destination=externalIP and dport=svcport
if
len
(
proxier
.
endpointsMap
[
svcName
])
==
0
{
writeLine
(
proxier
.
filterRules
,
writeLine
(
proxier
.
filterRules
,
"-A"
,
string
(
kubeServicesChain
),
"-A"
,
string
(
kubeServicesChain
),
"-m"
,
"comment"
,
"--comment"
,
fmt
.
Sprintf
(
`"%s has no endpoints"`
,
svcNameString
),
"-m"
,
"comment"
,
"--comment"
,
fmt
.
Sprintf
(
`"%s has no endpoints"`
,
svcNameString
),
...
@@ -952,6 +961,7 @@ func (proxier *Proxier) syncProxyRules() {
...
@@ -952,6 +961,7 @@ func (proxier *Proxier) syncProxyRules() {
}
}
// Capture load-balancer ingress.
// Capture load-balancer ingress.
if
hasEndpoints
{
fwChain
:=
svcInfo
.
serviceFirewallChainName
fwChain
:=
svcInfo
.
serviceFirewallChainName
for
_
,
ingress
:=
range
svcInfo
.
loadBalancerStatus
.
Ingress
{
for
_
,
ingress
:=
range
svcInfo
.
loadBalancerStatus
.
Ingress
{
if
ingress
.
IP
!=
""
{
if
ingress
.
IP
!=
""
{
...
@@ -1017,6 +1027,8 @@ func (proxier *Proxier) syncProxyRules() {
...
@@ -1017,6 +1027,8 @@ func (proxier *Proxier) syncProxyRules() {
writeLine
(
proxier
.
natRules
,
append
(
args
,
"-j"
,
string
(
KubeMarkDropChain
))
...
)
writeLine
(
proxier
.
natRules
,
append
(
args
,
"-j"
,
string
(
KubeMarkDropChain
))
...
)
}
}
}
}
}
// FIXME: do we need REJECT rules for load-balancer ingress if !hasEndpoints?
// Capture nodeports. If we had more than 2 rules it might be
// Capture nodeports. If we had more than 2 rules it might be
// worthwhile to make a new per-service chain for nodeport rules, but
// worthwhile to make a new per-service chain for nodeport rules, but
...
@@ -1050,8 +1062,9 @@ func (proxier *Proxier) syncProxyRules() {
...
@@ -1050,8 +1062,9 @@ func (proxier *Proxier) syncProxyRules() {
}
}
}
}
replacementPortsMap
[
lp
]
=
socket
replacementPortsMap
[
lp
]
=
socket
}
// We're holding the port, so it's OK to install iptables rules.
}
if
hasEndpoints
{
args
=
append
(
args
[
:
0
],
args
=
append
(
args
[
:
0
],
"-A"
,
string
(
kubeNodePortsChain
),
"-A"
,
string
(
kubeNodePortsChain
),
"-m"
,
"comment"
,
"--comment"
,
svcNameString
,
"-m"
,
"comment"
,
"--comment"
,
svcNameString
,
...
@@ -1075,12 +1088,7 @@ func (proxier *Proxier) syncProxyRules() {
...
@@ -1075,12 +1088,7 @@ func (proxier *Proxier) syncProxyRules() {
writeLine
(
proxier
.
natRules
,
append
(
args
,
"-s"
,
loopback
,
"-j"
,
string
(
KubeMarkMasqChain
))
...
)
writeLine
(
proxier
.
natRules
,
append
(
args
,
"-s"
,
loopback
,
"-j"
,
string
(
KubeMarkMasqChain
))
...
)
writeLine
(
proxier
.
natRules
,
append
(
args
,
"-j"
,
string
(
svcXlbChain
))
...
)
writeLine
(
proxier
.
natRules
,
append
(
args
,
"-j"
,
string
(
svcXlbChain
))
...
)
}
}
}
else
{
// If the service has no endpoints then reject packets. The filter
// table doesn't currently have the same per-service structure that
// the nat table does, so we just stick this into the kube-services
// chain.
if
len
(
proxier
.
endpointsMap
[
svcName
])
==
0
{
writeLine
(
proxier
.
filterRules
,
writeLine
(
proxier
.
filterRules
,
"-A"
,
string
(
kubeServicesChain
),
"-A"
,
string
(
kubeServicesChain
),
"-m"
,
"comment"
,
"--comment"
,
fmt
.
Sprintf
(
`"%s has no endpoints"`
,
svcNameString
),
"-m"
,
"comment"
,
"--comment"
,
fmt
.
Sprintf
(
`"%s has no endpoints"`
,
svcNameString
),
...
@@ -1092,21 +1100,10 @@ func (proxier *Proxier) syncProxyRules() {
...
@@ -1092,21 +1100,10 @@ func (proxier *Proxier) syncProxyRules() {
}
}
}
}
// If the service has no endpoints then reject packets.
if
!
hasEndpoints
{
if
len
(
proxier
.
endpointsMap
[
svcName
])
==
0
{
writeLine
(
proxier
.
filterRules
,
"-A"
,
string
(
kubeServicesChain
),
"-m"
,
"comment"
,
"--comment"
,
fmt
.
Sprintf
(
`"%s has no endpoints"`
,
svcNameString
),
"-m"
,
protocol
,
"-p"
,
protocol
,
"-d"
,
utilproxy
.
ToCIDR
(
svcInfo
.
clusterIP
),
"--dport"
,
strconv
.
Itoa
(
svcInfo
.
port
),
"-j"
,
"REJECT"
,
)
continue
continue
}
}
// From here on, we assume there are active endpoints.
// Generate the per-endpoint chains. We do this in multiple passes so we
// Generate the per-endpoint chains. We do this in multiple passes so we
// can group rules together.
// can group rules together.
// These two slices parallel each other - keep in sync
// These two slices parallel each other - keep in sync
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment