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
5367a32e
Commit
5367a32e
authored
Oct 29, 2015
by
Abhishek Shah
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Read Iptables-save output in a more-memory-efficient way
parent
fcd99461
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
94 additions
and
10 deletions
+94
-10
proxier.go
pkg/proxy/iptables/proxier.go
+61
-10
proxier_test.go
pkg/proxy/iptables/proxier_test.go
+33
-0
No files found.
pkg/proxy/iptables/proxier.go
View file @
5367a32e
...
@@ -765,33 +765,84 @@ func makeChainLine(chain utiliptables.Chain) string {
...
@@ -765,33 +765,84 @@ func makeChainLine(chain utiliptables.Chain) string {
// getChainLines parses a table's iptables-save data to find chains in the table.
// getChainLines parses a table's iptables-save data to find chains in the table.
// It returns a map of iptables.Chain to string where the string is the chain line from the save (with counters etc).
// It returns a map of iptables.Chain to string where the string is the chain line from the save (with counters etc).
func
getChainLines
(
table
utiliptables
.
Table
,
save
[]
byte
)
map
[
utiliptables
.
Chain
]
string
{
func
getChainLines
(
table
utiliptables
.
Table
,
save
[]
byte
)
map
[
utiliptables
.
Chain
]
string
{
// get lines
lines
:=
strings
.
Split
(
string
(
save
),
"
\n
"
)
chainsMap
:=
make
(
map
[
utiliptables
.
Chain
]
string
)
chainsMap
:=
make
(
map
[
utiliptables
.
Chain
]
string
)
tablePrefix
:=
"*"
+
string
(
table
)
tablePrefix
:=
"*"
+
string
(
table
)
lineNum
:=
0
readIndex
:=
0
// find beginning of table
// find beginning of table
for
;
lineNum
<
len
(
lines
);
lineNum
++
{
for
readIndex
<
len
(
save
)
{
if
strings
.
HasPrefix
(
strings
.
TrimSpace
(
lines
[
lineNum
]),
tablePrefix
)
{
line
,
n
:=
readLine
(
readIndex
,
save
)
lineNum
++
readIndex
=
n
if
strings
.
HasPrefix
(
line
,
tablePrefix
)
{
break
break
}
}
}
}
// parse table lines
// parse table lines
for
;
lineNum
<
len
(
lines
);
lineNum
++
{
for
readIndex
<
len
(
save
)
{
line
:=
strings
.
TrimSpace
(
lines
[
lineNum
])
line
,
n
:=
readLine
(
readIndex
,
save
)
readIndex
=
n
if
len
(
line
)
==
0
{
continue
}
if
strings
.
HasPrefix
(
line
,
"COMMIT"
)
||
strings
.
HasPrefix
(
line
,
"*"
)
{
if
strings
.
HasPrefix
(
line
,
"COMMIT"
)
||
strings
.
HasPrefix
(
line
,
"*"
)
{
break
break
}
else
if
len
(
line
)
==
0
||
strings
.
HasPrefix
(
line
,
"#"
)
{
}
else
if
strings
.
HasPrefix
(
line
,
"#"
)
{
continue
continue
}
else
if
strings
.
HasPrefix
(
line
,
":"
)
&&
len
(
line
)
>
1
{
}
else
if
strings
.
HasPrefix
(
line
,
":"
)
&&
len
(
line
)
>
1
{
chain
:=
utiliptables
.
Chain
(
strings
.
SplitN
(
line
[
1
:
],
" "
,
2
)[
0
])
chain
:=
utiliptables
.
Chain
(
strings
.
SplitN
(
line
[
1
:
],
" "
,
2
)[
0
])
chainsMap
[
chain
]
=
line
s
[
lineNum
]
chainsMap
[
chain
]
=
line
}
}
}
}
return
chainsMap
return
chainsMap
}
}
func
readLine
(
readIndex
int
,
byteArray
[]
byte
)
(
string
,
int
)
{
currentReadIndex
:=
readIndex
// consume left spaces
for
currentReadIndex
<
len
(
byteArray
)
{
if
byteArray
[
currentReadIndex
]
==
' '
{
currentReadIndex
++
}
else
{
break
}
}
// leftTrimIndex stores the left index of the line after the line is left-trimmed
leftTrimIndex
:=
currentReadIndex
// rightTrimIndex stores the right index of the line after the line is right-trimmed
// it is set to -1 since the correct value has not yet been determined.
rightTrimIndex
:=
-
1
for
;
currentReadIndex
<
len
(
byteArray
);
currentReadIndex
++
{
if
byteArray
[
currentReadIndex
]
==
' '
{
// set rightTrimIndex
if
rightTrimIndex
==
-
1
{
rightTrimIndex
=
currentReadIndex
}
}
else
if
(
byteArray
[
currentReadIndex
]
==
'\n'
)
||
(
currentReadIndex
==
(
len
(
byteArray
)
-
1
))
{
// end of line or byte buffer is reached
if
currentReadIndex
<=
leftTrimIndex
{
return
""
,
currentReadIndex
+
1
}
// set the rightTrimIndex
if
rightTrimIndex
==
-
1
{
rightTrimIndex
=
currentReadIndex
if
currentReadIndex
==
(
len
(
byteArray
)
-
1
)
&&
(
byteArray
[
currentReadIndex
]
!=
'\n'
)
{
// ensure that the last character is part of the returned string,
// unless the last character is '\n'
rightTrimIndex
=
currentReadIndex
+
1
}
}
return
string
(
byteArray
[
leftTrimIndex
:
rightTrimIndex
]),
currentReadIndex
+
1
}
else
{
// unset rightTrimIndex
rightTrimIndex
=
-
1
}
}
return
""
,
currentReadIndex
}
func
isLocalIP
(
ip
string
)
(
bool
,
error
)
{
func
isLocalIP
(
ip
string
)
(
bool
,
error
)
{
addrs
,
err
:=
net
.
InterfaceAddrs
()
addrs
,
err
:=
net
.
InterfaceAddrs
()
if
err
!=
nil
{
if
err
!=
nil
{
...
...
pkg/proxy/iptables/proxier_test.go
View file @
5367a32e
...
@@ -35,6 +35,39 @@ func checkAllLines(t *testing.T, table utiliptables.Table, save []byte, expected
...
@@ -35,6 +35,39 @@ func checkAllLines(t *testing.T, table utiliptables.Table, save []byte, expected
}
}
}
}
func
TestReadLinesFromByteBuffer
(
t
*
testing
.
T
)
{
testFn
:=
func
(
byteArray
[]
byte
,
expected
[]
string
)
{
index
:=
0
readIndex
:=
0
for
;
readIndex
<
len
(
byteArray
);
index
++
{
line
,
n
:=
readLine
(
readIndex
,
byteArray
)
readIndex
=
n
if
expected
[
index
]
!=
line
{
t
.
Errorf
(
"expected:%q, actual:%q"
,
expected
[
index
],
line
)
}
}
// for
if
readIndex
<
len
(
byteArray
)
{
t
.
Errorf
(
"Byte buffer was only partially read. Buffer length is:%d, readIndex is:%d"
,
len
(
byteArray
),
readIndex
)
}
if
index
<
len
(
expected
)
{
t
.
Errorf
(
"All expected strings were not compared. expected arr length:%d, matched count:%d"
,
len
(
expected
),
index
-
1
)
}
}
byteArray1
:=
[]
byte
(
"
\n
Line 1
\n\n\n
L ine4
\n
Line 5
\n
\n
"
)
expected1
:=
[]
string
{
""
,
"Line 1"
,
""
,
""
,
"L ine4"
,
"Line 5"
,
""
}
testFn
(
byteArray1
,
expected1
)
byteArray1
=
[]
byte
(
""
)
expected1
=
[]
string
{}
testFn
(
byteArray1
,
expected1
)
byteArray1
=
[]
byte
(
"
\n\n
"
)
expected1
=
[]
string
{
""
,
""
}
testFn
(
byteArray1
,
expected1
)
}
func
TestgetChainLines
(
t
*
testing
.
T
)
{
func
TestgetChainLines
(
t
*
testing
.
T
)
{
iptables_save
:=
`# Generated by iptables-save v1.4.7 on Wed Oct 29 14:56:01 2014
iptables_save
:=
`# Generated by iptables-save v1.4.7 on Wed Oct 29 14:56:01 2014
*nat
*nat
...
...
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