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
ca032528
Commit
ca032528
authored
Feb 16, 2018
by
Zihong Zheng
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
[kube-proxy] Add more IP version related utils
parent
dfbec1a6
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
214 additions
and
0 deletions
+214
-0
utils.go
pkg/proxy/util/utils.go
+17
-0
net.go
pkg/util/net/net.go
+29
-0
net_test.go
pkg/util/net/net_test.go
+168
-0
No files found.
pkg/proxy/util/utils.go
View file @
ca032528
...
@@ -20,8 +20,10 @@ import (
...
@@ -20,8 +20,10 @@ import (
"fmt"
"fmt"
"net"
"net"
"k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/types"
"k8s.io/apimachinery/pkg/types"
"k8s.io/apimachinery/pkg/util/sets"
"k8s.io/apimachinery/pkg/util/sets"
"k8s.io/client-go/tools/record"
api
"k8s.io/kubernetes/pkg/apis/core"
api
"k8s.io/kubernetes/pkg/apis/core"
"k8s.io/kubernetes/pkg/apis/core/helper"
"k8s.io/kubernetes/pkg/apis/core/helper"
utilnet
"k8s.io/kubernetes/pkg/util/net"
utilnet
"k8s.io/kubernetes/pkg/util/net"
...
@@ -129,3 +131,18 @@ func GetNodeAddresses(cidrs []string, nw NetworkInterfacer) (sets.String, error)
...
@@ -129,3 +131,18 @@ func GetNodeAddresses(cidrs []string, nw NetworkInterfacer) (sets.String, error)
}
}
return
uniqueAddressList
,
nil
return
uniqueAddressList
,
nil
}
}
// LogAndEmitIncorrectIPVersionEvent logs and emits incorrect IP version event.
func
LogAndEmitIncorrectIPVersionEvent
(
recorder
record
.
EventRecorder
,
fieldName
,
fieldValue
,
svcNamespace
,
svcName
string
,
svcUID
types
.
UID
)
{
errMsg
:=
fmt
.
Sprintf
(
"%s in %s has incorrect IP version"
,
fieldValue
,
fieldName
)
glog
.
Errorf
(
"%s (service %s/%s)."
,
errMsg
,
svcNamespace
,
svcName
)
if
recorder
!=
nil
{
recorder
.
Eventf
(
&
v1
.
ObjectReference
{
Kind
:
"Service"
,
Name
:
svcName
,
Namespace
:
svcNamespace
,
UID
:
svcUID
,
},
v1
.
EventTypeWarning
,
"KubeProxyIncorrectIPVersion"
,
errMsg
)
}
}
pkg/util/net/net.go
View file @
ca032528
...
@@ -30,3 +30,32 @@ func IsIPv6String(ip string) bool {
...
@@ -30,3 +30,32 @@ func IsIPv6String(ip string) bool {
netIP
:=
net
.
ParseIP
(
ip
)
netIP
:=
net
.
ParseIP
(
ip
)
return
IsIPv6
(
netIP
)
return
IsIPv6
(
netIP
)
}
}
// IsIPv6CIDR returns if cidr is IPv6.
// This assumes cidr is a valid CIDR.
func
IsIPv6CIDR
(
cidr
string
)
bool
{
ip
,
_
,
_
:=
net
.
ParseCIDR
(
cidr
)
return
IsIPv6
(
ip
)
}
// FilterIncorrectIPVersion filters out the incorrect IP version case from a slice of IP strings.
func
FilterIncorrectIPVersion
(
ipStrings
[]
string
,
isIPv6Mode
bool
)
([]
string
,
[]
string
)
{
return
filterWithCondition
(
ipStrings
,
isIPv6Mode
,
IsIPv6String
)
}
// FilterIncorrectCIDRVersion filters out the incorrect IP version case from a slice of CIDR strings.
func
FilterIncorrectCIDRVersion
(
ipStrings
[]
string
,
isIPv6Mode
bool
)
([]
string
,
[]
string
)
{
return
filterWithCondition
(
ipStrings
,
isIPv6Mode
,
IsIPv6CIDR
)
}
func
filterWithCondition
(
strs
[]
string
,
expectedCondition
bool
,
conditionFunc
func
(
string
)
bool
)
([]
string
,
[]
string
)
{
var
corrects
,
incorrects
[]
string
for
_
,
str
:=
range
strs
{
if
conditionFunc
(
str
)
!=
expectedCondition
{
incorrects
=
append
(
incorrects
,
str
)
}
else
{
corrects
=
append
(
corrects
,
str
)
}
}
return
corrects
,
incorrects
}
pkg/util/net/net_test.go
View file @
ca032528
...
@@ -18,6 +18,7 @@ package net
...
@@ -18,6 +18,7 @@ package net
import
(
import
(
"net"
"net"
"reflect"
"testing"
"testing"
)
)
...
@@ -116,3 +117,170 @@ func TestIsIPv6(t *testing.T) {
...
@@ -116,3 +117,170 @@ func TestIsIPv6(t *testing.T) {
}
}
}
}
}
}
func
TestIsIPv6CIDR
(
t
*
testing
.
T
)
{
testCases
:=
[]
struct
{
desc
string
cidr
string
expectResult
bool
}{
{
desc
:
"ipv4 CIDR 1"
,
cidr
:
"10.0.0.0/8"
,
expectResult
:
false
,
},
{
desc
:
"ipv4 CIDR 2"
,
cidr
:
"192.168.0.0/16"
,
expectResult
:
false
,
},
{
desc
:
"ipv6 CIDR 1"
,
cidr
:
"::/1"
,
expectResult
:
true
,
},
{
desc
:
"ipv6 CIDR 2"
,
cidr
:
"2000::/10"
,
expectResult
:
true
,
},
{
desc
:
"ipv6 CIDR 3"
,
cidr
:
"2001:db8::/32"
,
expectResult
:
true
,
},
}
for
_
,
tc
:=
range
testCases
{
res
:=
IsIPv6CIDR
(
tc
.
cidr
)
if
res
!=
tc
.
expectResult
{
t
.
Errorf
(
"%v: want IsIPv6CIDR=%v, got %v"
,
tc
.
desc
,
tc
.
expectResult
,
res
)
}
}
}
func
TestFilterIncorrectIPVersion
(
t
*
testing
.
T
)
{
testCases
:=
[]
struct
{
desc
string
isIPv6
bool
ipStrings
[]
string
expectCorrects
[]
string
expectIncorrects
[]
string
}{
{
desc
:
"all ipv4 strings in ipv4 mode"
,
isIPv6
:
false
,
ipStrings
:
[]
string
{
"10.0.0.1"
,
"192.168.0.1"
,
"127.0.0.1"
},
expectCorrects
:
[]
string
{
"10.0.0.1"
,
"192.168.0.1"
,
"127.0.0.1"
},
expectIncorrects
:
nil
,
},
{
desc
:
"all ipv6 strings in ipv4 mode"
,
isIPv6
:
false
,
ipStrings
:
[]
string
{
"::1"
,
"fd00::600d:f00d"
,
"2001:db8::5"
},
expectCorrects
:
nil
,
expectIncorrects
:
[]
string
{
"::1"
,
"fd00::600d:f00d"
,
"2001:db8::5"
},
},
{
desc
:
"mixed versions in ipv4 mode"
,
isIPv6
:
false
,
ipStrings
:
[]
string
{
"10.0.0.1"
,
"192.168.0.1"
,
"127.0.0.1"
,
"::1"
,
"fd00::600d:f00d"
,
"2001:db8::5"
},
expectCorrects
:
[]
string
{
"10.0.0.1"
,
"192.168.0.1"
,
"127.0.0.1"
},
expectIncorrects
:
[]
string
{
"::1"
,
"fd00::600d:f00d"
,
"2001:db8::5"
},
},
{
desc
:
"all ipv4 strings in ipv6 mode"
,
isIPv6
:
true
,
ipStrings
:
[]
string
{
"10.0.0.1"
,
"192.168.0.1"
,
"127.0.0.1"
},
expectCorrects
:
nil
,
expectIncorrects
:
[]
string
{
"10.0.0.1"
,
"192.168.0.1"
,
"127.0.0.1"
},
},
{
desc
:
"all ipv6 strings in ipv6 mode"
,
isIPv6
:
true
,
ipStrings
:
[]
string
{
"::1"
,
"fd00::600d:f00d"
,
"2001:db8::5"
},
expectCorrects
:
[]
string
{
"::1"
,
"fd00::600d:f00d"
,
"2001:db8::5"
},
expectIncorrects
:
nil
,
},
{
desc
:
"mixed versions in ipv6 mode"
,
isIPv6
:
true
,
ipStrings
:
[]
string
{
"10.0.0.1"
,
"192.168.0.1"
,
"127.0.0.1"
,
"::1"
,
"fd00::600d:f00d"
,
"2001:db8::5"
},
expectCorrects
:
[]
string
{
"::1"
,
"fd00::600d:f00d"
,
"2001:db8::5"
},
expectIncorrects
:
[]
string
{
"10.0.0.1"
,
"192.168.0.1"
,
"127.0.0.1"
},
},
}
for
_
,
tc
:=
range
testCases
{
corrects
,
incorrects
:=
FilterIncorrectIPVersion
(
tc
.
ipStrings
,
tc
.
isIPv6
)
if
!
reflect
.
DeepEqual
(
tc
.
expectCorrects
,
corrects
)
{
t
.
Errorf
(
"%v: want corrects=%v, got %v"
,
tc
.
desc
,
tc
.
expectCorrects
,
corrects
)
}
if
!
reflect
.
DeepEqual
(
tc
.
expectIncorrects
,
incorrects
)
{
t
.
Errorf
(
"%v: want incorrects=%v, got %v"
,
tc
.
desc
,
tc
.
expectIncorrects
,
incorrects
)
}
}
}
func
TestFilterIncorrectCIDRVersion
(
t
*
testing
.
T
)
{
testCases
:=
[]
struct
{
desc
string
isIPv6
bool
cidrStrings
[]
string
expectCorrects
[]
string
expectIncorrects
[]
string
}{
{
desc
:
"all ipv4 strings in ipv4 mode"
,
isIPv6
:
false
,
cidrStrings
:
[]
string
{
"0.0.0.0/1"
,
"1.0.0.0/1"
},
expectCorrects
:
[]
string
{
"0.0.0.0/1"
,
"1.0.0.0/1"
},
expectIncorrects
:
nil
,
},
{
desc
:
"all ipv6 strings in ipv4 mode"
,
isIPv6
:
false
,
cidrStrings
:
[]
string
{
"2001:db8::/32"
,
"2001:0db8:0123:4567::/64"
},
expectCorrects
:
nil
,
expectIncorrects
:
[]
string
{
"2001:db8::/32"
,
"2001:0db8:0123:4567::/64"
},
},
{
desc
:
"mixed versions in ipv4 mode"
,
isIPv6
:
false
,
cidrStrings
:
[]
string
{
"0.0.0.0/1"
,
"1.0.0.0/1"
,
"2001:db8::/32"
,
"2001:0db8:0123:4567::/64"
},
expectCorrects
:
[]
string
{
"0.0.0.0/1"
,
"1.0.0.0/1"
},
expectIncorrects
:
[]
string
{
"2001:db8::/32"
,
"2001:0db8:0123:4567::/64"
},
},
{
desc
:
"all ipv4 strings in ipv6 mode"
,
isIPv6
:
true
,
cidrStrings
:
[]
string
{
"0.0.0.0/1"
,
"1.0.0.0/1"
},
expectCorrects
:
nil
,
expectIncorrects
:
[]
string
{
"0.0.0.0/1"
,
"1.0.0.0/1"
},
},
{
desc
:
"all ipv6 strings in ipv6 mode"
,
isIPv6
:
true
,
cidrStrings
:
[]
string
{
"2001:db8::/32"
,
"2001:0db8:0123:4567::/64"
},
expectCorrects
:
[]
string
{
"2001:db8::/32"
,
"2001:0db8:0123:4567::/64"
},
expectIncorrects
:
nil
,
},
{
desc
:
"mixed versions in ipv6 mode"
,
isIPv6
:
true
,
cidrStrings
:
[]
string
{
"0.0.0.0/1"
,
"1.0.0.0/1"
,
"2001:db8::/32"
,
"2001:0db8:0123:4567::/64"
},
expectCorrects
:
[]
string
{
"2001:db8::/32"
,
"2001:0db8:0123:4567::/64"
},
expectIncorrects
:
[]
string
{
"0.0.0.0/1"
,
"1.0.0.0/1"
},
},
}
for
_
,
tc
:=
range
testCases
{
corrects
,
incorrects
:=
FilterIncorrectCIDRVersion
(
tc
.
cidrStrings
,
tc
.
isIPv6
)
if
!
reflect
.
DeepEqual
(
tc
.
expectCorrects
,
corrects
)
{
t
.
Errorf
(
"%v: want corrects=%v, got %v"
,
tc
.
desc
,
tc
.
expectCorrects
,
corrects
)
}
if
!
reflect
.
DeepEqual
(
tc
.
expectIncorrects
,
incorrects
)
{
t
.
Errorf
(
"%v: want incorrects=%v, got %v"
,
tc
.
desc
,
tc
.
expectIncorrects
,
incorrects
)
}
}
}
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