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
7c108f59
Commit
7c108f59
authored
Sep 14, 2017
by
Erik Stidham
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Initial changes for adding forward rules
parent
3c8fb4b9
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
83 additions
and
7 deletions
+83
-7
proxier.go
pkg/proxy/iptables/proxier.go
+82
-7
iptables.go
pkg/util/iptables/iptables.go
+1
-0
No files found.
pkg/proxy/iptables/proxier.go
View file @
7c108f59
...
...
@@ -79,6 +79,9 @@ const (
// the mark-for-drop chain
KubeMarkDropChain
utiliptables
.
Chain
=
"KUBE-MARK-DROP"
// the kubernetes forward chain
kubeForwardChain
utiliptables
.
Chain
=
"KUBE-FORWARD"
)
// IPTablesVersioner can query the current iptables version.
...
...
@@ -543,6 +546,18 @@ func CleanupLeftovers(ipt utiliptables.Interface) (encounteredError bool) {
}
}
// Unlink the forwarding chain.
args
=
[]
string
{
"-m"
,
"comment"
,
"--comment"
,
"kubernetes forwarding rules"
,
"-j"
,
string
(
kubeForwardChain
),
}
if
err
:=
ipt
.
DeleteRule
(
utiliptables
.
TableFilter
,
utiliptables
.
ChainForward
,
args
...
);
err
!=
nil
{
if
!
utiliptables
.
IsNotFoundError
(
err
)
{
glog
.
Errorf
(
"Error removing pure-iptables proxy rule: %v"
,
err
)
encounteredError
=
true
}
}
// Flush and remove all of our chains.
iptablesData
:=
bytes
.
NewBuffer
(
nil
)
if
err
:=
ipt
.
SaveInto
(
utiliptables
.
TableNAT
,
iptablesData
);
err
!=
nil
{
...
...
@@ -578,14 +593,28 @@ func CleanupLeftovers(ipt utiliptables.Interface) (encounteredError bool) {
encounteredError
=
true
}
}
{
filterBuf
:=
bytes
.
NewBuffer
(
nil
)
writeLine
(
filterBuf
,
"*filter"
)
writeLine
(
filterBuf
,
fmt
.
Sprintf
(
":%s - [0:0]"
,
kubeServicesChain
))
writeLine
(
filterBuf
,
fmt
.
Sprintf
(
"-X %s"
,
kubeServicesChain
))
writeLine
(
filterBuf
,
"COMMIT"
)
// Flush and remove all of our chains.
iptablesData
=
bytes
.
NewBuffer
(
nil
)
if
err
:=
ipt
.
SaveInto
(
utiliptables
.
TableFilter
,
iptablesData
);
err
!=
nil
{
glog
.
Errorf
(
"Failed to execute iptables-save for %s: %v"
,
utiliptables
.
TableFilter
,
err
)
encounteredError
=
true
}
else
{
existingFilterChains
:=
utiliptables
.
GetChainLines
(
utiliptables
.
TableFilter
,
iptablesData
.
Bytes
())
filterChains
:=
bytes
.
NewBuffer
(
nil
)
filterRules
:=
bytes
.
NewBuffer
(
nil
)
writeLine
(
filterChains
,
"*filter"
)
for
_
,
chain
:=
range
[]
utiliptables
.
Chain
{
kubeServicesChain
,
kubeForwardChain
}
{
if
_
,
found
:=
existingFilterChains
[
chain
];
found
{
chainString
:=
string
(
chain
)
writeLine
(
filterChains
,
existingFilterChains
[
chain
])
writeLine
(
filterRules
,
"-X"
,
chainString
)
}
}
writeLine
(
filterRules
,
"COMMIT"
)
filterLines
:=
append
(
filterChains
.
Bytes
(),
filterRules
.
Bytes
()
...
)
// Write it.
if
err
:=
ipt
.
Restore
(
utiliptables
.
TableFilter
,
filter
Buf
.
Bytes
()
,
utiliptables
.
NoFlushTables
,
utiliptables
.
RestoreCounters
);
err
!=
nil
{
if
err
:=
ipt
.
Restore
(
utiliptables
.
TableFilter
,
filter
Lines
,
utiliptables
.
NoFlushTables
,
utiliptables
.
RestoreCounters
);
err
!=
nil
{
glog
.
Errorf
(
"Failed to execute iptables-restore for %s: %v"
,
utiliptables
.
TableFilter
,
err
)
encounteredError
=
true
}
...
...
@@ -1026,6 +1055,21 @@ func (proxier *Proxier) syncProxyRules() {
}
}
// Create and link the kube forward chain.
{
if
_
,
err
:=
proxier
.
iptables
.
EnsureChain
(
utiliptables
.
TableFilter
,
kubeForwardChain
);
err
!=
nil
{
glog
.
Errorf
(
"Failed to ensure that %s chain %s exists: %v"
,
utiliptables
.
TableFilter
,
kubeForwardChain
,
err
)
return
}
comment
:=
"kubernetes forward rules"
args
:=
[]
string
{
"-m"
,
"comment"
,
"--comment"
,
comment
,
"-j"
,
string
(
kubeForwardChain
)}
if
_
,
err
:=
proxier
.
iptables
.
EnsureRule
(
utiliptables
.
Prepend
,
utiliptables
.
TableFilter
,
utiliptables
.
ChainForward
,
args
...
);
err
!=
nil
{
glog
.
Errorf
(
"Failed to ensure that %s chain %s jumps to %s: %v"
,
utiliptables
.
TableFilter
,
utiliptables
.
ChainForward
,
kubeForwardChain
,
err
)
return
}
}
//
// Below this point we will not return until we try to write the iptables rules.
//
...
...
@@ -1068,6 +1112,11 @@ func (proxier *Proxier) syncProxyRules() {
}
else
{
writeLine
(
proxier
.
filterChains
,
utiliptables
.
MakeChainLine
(
kubeServicesChain
))
}
if
chain
,
ok
:=
existingFilterChains
[
kubeForwardChain
];
ok
{
writeLine
(
proxier
.
filterChains
,
chain
)
}
else
{
writeLine
(
proxier
.
filterChains
,
utiliptables
.
MakeChainLine
(
kubeForwardChain
))
}
if
chain
,
ok
:=
existingNATChains
[
kubeServicesChain
];
ok
{
writeLine
(
proxier
.
natChains
,
chain
)
}
else
{
...
...
@@ -1555,6 +1604,32 @@ func (proxier *Proxier) syncProxyRules() {
"-m"
,
"addrtype"
,
"--dst-type"
,
"LOCAL"
,
"-j"
,
string
(
kubeNodePortsChain
))
if
len
(
proxier
.
clusterCIDR
)
!=
0
{
glog
.
Error
(
"Should be adding the rules now"
)
writeLine
(
proxier
.
filterRules
,
"-A"
,
string
(
kubeForwardChain
),
"-m"
,
"comment"
,
"--comment"
,
`"kubernetes forwarding rules"`
,
"-m"
,
"mark"
,
"--mark"
,
proxier
.
masqueradeMark
,
"-j"
,
"ACCEPT"
,
)
writeLine
(
proxier
.
filterRules
,
"-A"
,
string
(
kubeForwardChain
),
"-s"
,
proxier
.
clusterCIDR
,
"-m"
,
"comment"
,
"--comment"
,
`"kubernetes forwarding conntrack pod source rule"`
,
"-m"
,
"conntrack"
,
"--ctstate"
,
"RELATED,ESTABLISHED"
,
"-j"
,
"ACCEPT"
,
)
writeLine
(
proxier
.
filterRules
,
"-A"
,
string
(
kubeForwardChain
),
"-m"
,
"comment"
,
"--comment"
,
`"kubernetes forwarding conntrack pod destination rule"`
,
"-d"
,
proxier
.
clusterCIDR
,
"-m"
,
"conntrack"
,
"--ctstate"
,
"RELATED,ESTABLISHED"
,
"-j"
,
"ACCEPT"
,
)
}
// Write the end-of-table markers.
writeLine
(
proxier
.
filterRules
,
"COMMIT"
)
writeLine
(
proxier
.
natRules
,
"COMMIT"
)
...
...
pkg/util/iptables/iptables.go
View file @
7c108f59
...
...
@@ -91,6 +91,7 @@ const (
ChainPrerouting
Chain
=
"PREROUTING"
ChainOutput
Chain
=
"OUTPUT"
ChainInput
Chain
=
"INPUT"
ChainForward
Chain
=
"FORWARD"
)
const
(
...
...
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