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
5d69faa6
Commit
5d69faa6
authored
Nov 10, 2014
by
Haney Maxwell
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Allow proxy to run on systems with iptables <1.4.11
parent
21a6e964
Expand all
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
113 additions
and
3 deletions
+113
-3
iptables.go
pkg/util/iptables/iptables.go
+113
-3
iptables_test.go
pkg/util/iptables/iptables_test.go
+0
-0
No files found.
pkg/util/iptables/iptables.go
View file @
5d69faa6
...
@@ -18,8 +18,12 @@ package iptables
...
@@ -18,8 +18,12 @@ package iptables
import
(
import
(
"fmt"
"fmt"
"regexp"
"strconv"
"strings"
"sync"
"sync"
"github.com/GoogleCloudPlatform/kubernetes/pkg/util"
utilexec
"github.com/GoogleCloudPlatform/kubernetes/pkg/util/exec"
utilexec
"github.com/GoogleCloudPlatform/kubernetes/pkg/util/exec"
"github.com/golang/glog"
"github.com/golang/glog"
)
)
...
@@ -100,7 +104,7 @@ func (runner *runner) EnsureRule(table Table, chain Chain, args ...string) (bool
...
@@ -100,7 +104,7 @@ func (runner *runner) EnsureRule(table Table, chain Chain, args ...string) (bool
runner
.
mu
.
Lock
()
runner
.
mu
.
Lock
()
defer
runner
.
mu
.
Unlock
()
defer
runner
.
mu
.
Unlock
()
exists
,
err
:=
runner
.
checkRule
(
fullArgs
)
exists
,
err
:=
runner
.
checkRule
(
table
,
chain
,
args
...
)
if
err
!=
nil
{
if
err
!=
nil
{
return
false
,
err
return
false
,
err
}
}
...
@@ -121,7 +125,7 @@ func (runner *runner) DeleteRule(table Table, chain Chain, args ...string) error
...
@@ -121,7 +125,7 @@ func (runner *runner) DeleteRule(table Table, chain Chain, args ...string) error
runner
.
mu
.
Lock
()
runner
.
mu
.
Lock
()
defer
runner
.
mu
.
Unlock
()
defer
runner
.
mu
.
Unlock
()
exists
,
err
:=
runner
.
checkRule
(
fullArgs
)
exists
,
err
:=
runner
.
checkRule
(
table
,
chain
,
args
...
)
if
err
!=
nil
{
if
err
!=
nil
{
return
err
return
err
}
}
...
@@ -146,7 +150,47 @@ func (runner *runner) run(op operation, args []string) ([]byte, error) {
...
@@ -146,7 +150,47 @@ func (runner *runner) run(op operation, args []string) ([]byte, error) {
// Returns (bool, nil) if it was able to check the existence of the rule, or
// Returns (bool, nil) if it was able to check the existence of the rule, or
// (<undefined>, error) if the process of checking failed.
// (<undefined>, error) if the process of checking failed.
func
(
runner
*
runner
)
checkRule
(
args
[]
string
)
(
bool
,
error
)
{
func
(
runner
*
runner
)
checkRule
(
table
Table
,
chain
Chain
,
args
...
string
)
(
bool
,
error
)
{
checkPresent
,
err
:=
getIptablesHasCheckCommand
(
runner
.
exec
)
if
err
!=
nil
{
glog
.
Warning
(
"Error checking iptables version, assuming version at least 1.4.11: %v"
,
err
)
checkPresent
=
true
}
if
checkPresent
{
return
runner
.
checkRuleUsingCheck
(
makeFullArgs
(
table
,
chain
,
args
...
))
}
else
{
return
runner
.
checkRuleWithoutCheck
(
table
,
chain
,
args
...
)
}
}
// Executes the rule check without using the "-C" flag, instead parsing iptables-save.
// Present for compatibility with <1.4.11 versions of iptables.
func
(
runner
*
runner
)
checkRuleWithoutCheck
(
table
Table
,
chain
Chain
,
args
...
string
)
(
bool
,
error
)
{
out
,
err
:=
runner
.
exec
.
Command
(
"iptables-save"
,
"-t"
,
string
(
table
))
.
CombinedOutput
()
if
err
!=
nil
{
return
false
,
fmt
.
Errorf
(
"error checking rule: %s"
,
err
)
}
argset
:=
util
.
NewStringSet
(
args
...
)
for
_
,
line
:=
range
strings
.
Split
(
string
(
out
),
"
\n
"
)
{
var
fields
=
strings
.
Fields
(
line
)
// Check that this is a rule for the correct chain, and that it has
// the correct number of argument (+2 for "-A <chain name>")
if
strings
.
HasPrefix
(
line
,
fmt
.
Sprintf
(
"-A %s"
,
string
(
chain
)))
&&
len
(
fields
)
==
len
(
args
)
+
2
{
// TODO: This misses reorderings e.g. "-x foo ! -y bar" will match "! -x foo -y bar"
if
util
.
NewStringSet
(
fields
...
)
.
IsSuperset
(
argset
)
{
return
true
,
nil
}
}
}
return
false
,
nil
}
// Executes the rule check using the "-C" flag
func
(
runner
*
runner
)
checkRuleUsingCheck
(
args
[]
string
)
(
bool
,
error
)
{
out
,
err
:=
runner
.
run
(
opCheckRule
,
args
)
out
,
err
:=
runner
.
run
(
opCheckRule
,
args
)
if
err
==
nil
{
if
err
==
nil
{
return
true
,
nil
return
true
,
nil
...
@@ -174,3 +218,69 @@ const (
...
@@ -174,3 +218,69 @@ const (
func
makeFullArgs
(
table
Table
,
chain
Chain
,
args
...
string
)
[]
string
{
func
makeFullArgs
(
table
Table
,
chain
Chain
,
args
...
string
)
[]
string
{
return
append
([]
string
{
string
(
chain
),
"-t"
,
string
(
table
)},
args
...
)
return
append
([]
string
{
string
(
chain
),
"-t"
,
string
(
table
)},
args
...
)
}
}
// Checks if iptables has the "-C" flag
func
getIptablesHasCheckCommand
(
exec
utilexec
.
Interface
)
(
bool
,
error
)
{
vstring
,
err
:=
getIptablesVersionString
(
exec
)
if
err
!=
nil
{
return
false
,
err
}
v1
,
v2
,
v3
,
err
:=
extractIptablesVersion
(
vstring
)
if
err
!=
nil
{
return
false
,
err
}
return
iptablesHasCheckCommand
(
v1
,
v2
,
v3
),
nil
}
// getIptablesVersion returns the first three components of the iptables version.
// e.g. "iptables v1.3.66" would return (1, 3, 66, nil)
func
extractIptablesVersion
(
str
string
)
(
int
,
int
,
int
,
error
)
{
versionMatcher
:=
regexp
.
MustCompile
(
"v([0-9]+)
\\
.([0-9]+)
\\
.([0-9]+)"
)
result
:=
versionMatcher
.
FindStringSubmatch
(
str
)
if
result
==
nil
{
return
0
,
0
,
0
,
fmt
.
Errorf
(
"No iptables version found in string: %s"
,
str
)
}
v1
,
err
:=
strconv
.
Atoi
(
result
[
1
])
if
err
!=
nil
{
return
0
,
0
,
0
,
err
}
v2
,
err
:=
strconv
.
Atoi
(
result
[
2
])
if
err
!=
nil
{
return
0
,
0
,
0
,
err
}
v3
,
err
:=
strconv
.
Atoi
(
result
[
3
])
if
err
!=
nil
{
return
0
,
0
,
0
,
err
}
return
v1
,
v2
,
v3
,
nil
}
// Runs "iptables --version" to get the version string
func
getIptablesVersionString
(
exec
utilexec
.
Interface
)
(
string
,
error
)
{
bytes
,
err
:=
exec
.
Command
(
"iptables"
,
"--version"
)
.
CombinedOutput
()
if
err
!=
nil
{
return
""
,
err
}
return
string
(
bytes
),
nil
}
// Checks if an iptables version is after 1.4.11, when --check was added
func
iptablesHasCheckCommand
(
v1
int
,
v2
int
,
v3
int
)
bool
{
if
v1
>
1
{
return
true
}
if
v1
==
1
&&
v2
>
4
{
return
true
}
if
v1
==
1
&&
v2
==
4
&&
v3
>=
11
{
return
true
}
return
false
}
pkg/util/iptables/iptables_test.go
View file @
5d69faa6
This diff is collapsed.
Click to expand it.
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