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
daae0e6c
Commit
daae0e6c
authored
Feb 06, 2018
by
Rohit Ramkumar
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Cleanup of ipvs utils
parent
ffda1e22
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
103 additions
and
103 deletions
+103
-103
ipvs_linux.go
pkg/util/ipvs/ipvs_linux.go
+64
-64
ipvs_linux_test.go
pkg/util/ipvs/ipvs_linux_test.go
+39
-39
No files found.
pkg/util/ipvs/ipvs_linux.go
View file @
daae0e6c
...
@@ -25,83 +25,86 @@ import (
...
@@ -25,83 +25,86 @@ import (
"strings"
"strings"
"syscall"
"syscall"
"github.com/docker/libnetwork/ipvs"
libipvs
"github.com/docker/libnetwork/ipvs"
"github.com/golang/glog"
"github.com/golang/glog"
utilexec
"k8s.io/utils/exec"
utilexec
"k8s.io/utils/exec"
)
)
// runner implements Interface.
// runner implements
ipvs.
Interface.
type
runner
struct
{
type
runner
struct
{
exec
utilexec
.
Interface
exec
utilexec
.
Interface
ipvsHandle
*
ipvs
.
Handle
ipvsHandle
*
lib
ipvs
.
Handle
}
}
// Protocol is the IPVS service protocol type
type
Protocol
uint16
// New returns a new Interface which will call ipvs APIs.
// New returns a new Interface which will call ipvs APIs.
func
New
(
exec
utilexec
.
Interface
)
Interface
{
func
New
(
exec
utilexec
.
Interface
)
Interface
{
ihandle
,
err
:=
ipvs
.
New
(
""
)
handle
,
err
:=
lib
ipvs
.
New
(
""
)
if
err
!=
nil
{
if
err
!=
nil
{
glog
.
Errorf
(
"IPVS interface can't be initialized, error: %v"
,
err
)
glog
.
Errorf
(
"IPVS interface can't be initialized, error: %v"
,
err
)
return
nil
return
nil
}
}
return
&
runner
{
return
&
runner
{
exec
:
exec
,
exec
:
exec
,
ipvsHandle
:
i
handle
,
ipvsHandle
:
handle
,
}
}
}
}
// AddVirtualServer is part of Interface.
// AddVirtualServer is part of
ipvs.
Interface.
func
(
runner
*
runner
)
AddVirtualServer
(
vs
*
VirtualServer
)
error
{
func
(
runner
*
runner
)
AddVirtualServer
(
vs
*
VirtualServer
)
error
{
eSvc
,
err
:=
toBackend
Service
(
vs
)
svc
,
err
:=
toIPVS
Service
(
vs
)
if
err
!=
nil
{
if
err
!=
nil
{
return
err
return
err
}
}
return
runner
.
ipvsHandle
.
NewService
(
eS
vc
)
return
runner
.
ipvsHandle
.
NewService
(
s
vc
)
}
}
// UpdateVirtualServer is part of Interface.
// UpdateVirtualServer is part of
ipvs.
Interface.
func
(
runner
*
runner
)
UpdateVirtualServer
(
vs
*
VirtualServer
)
error
{
func
(
runner
*
runner
)
UpdateVirtualServer
(
vs
*
VirtualServer
)
error
{
bSvc
,
err
:=
toBackend
Service
(
vs
)
svc
,
err
:=
toIPVS
Service
(
vs
)
if
err
!=
nil
{
if
err
!=
nil
{
return
err
return
err
}
}
return
runner
.
ipvsHandle
.
UpdateService
(
bS
vc
)
return
runner
.
ipvsHandle
.
UpdateService
(
s
vc
)
}
}
// DeleteVirtualServer is part of Interface.
// DeleteVirtualServer is part of
ipvs.
Interface.
func
(
runner
*
runner
)
DeleteVirtualServer
(
vs
*
VirtualServer
)
error
{
func
(
runner
*
runner
)
DeleteVirtualServer
(
vs
*
VirtualServer
)
error
{
bSvc
,
err
:=
toBackend
Service
(
vs
)
svc
,
err
:=
toIPVS
Service
(
vs
)
if
err
!=
nil
{
if
err
!=
nil
{
return
err
return
err
}
}
return
runner
.
ipvsHandle
.
DelService
(
bS
vc
)
return
runner
.
ipvsHandle
.
DelService
(
s
vc
)
}
}
// GetVirtualServer is part of Interface.
// GetVirtualServer is part of
ipvs.
Interface.
func
(
runner
*
runner
)
GetVirtualServer
(
vs
*
VirtualServer
)
(
*
VirtualServer
,
error
)
{
func
(
runner
*
runner
)
GetVirtualServer
(
vs
*
VirtualServer
)
(
*
VirtualServer
,
error
)
{
bSvc
,
err
:=
toBackend
Service
(
vs
)
svc
,
err
:=
toIPVS
Service
(
vs
)
if
err
!=
nil
{
if
err
!=
nil
{
return
nil
,
err
return
nil
,
err
}
}
ipvsS
ervice
,
err
:=
runner
.
ipvsHandle
.
GetService
(
bS
vc
)
ipvsS
vc
,
err
:=
runner
.
ipvsHandle
.
GetService
(
s
vc
)
if
err
!=
nil
{
if
err
!=
nil
{
return
nil
,
err
return
nil
,
err
}
}
v
irtualServer
,
err
:=
toVirtualServer
(
ipvsService
)
v
Serv
,
err
:=
toVirtualServer
(
ipvsSvc
)
if
err
!=
nil
{
if
err
!=
nil
{
return
nil
,
err
return
nil
,
err
}
}
return
v
irtualServer
,
nil
return
v
Serv
,
nil
}
}
// GetVirtualServers is part of Interface.
// GetVirtualServers is part of
ipvs.
Interface.
func
(
runner
*
runner
)
GetVirtualServers
()
([]
*
VirtualServer
,
error
)
{
func
(
runner
*
runner
)
GetVirtualServers
()
([]
*
VirtualServer
,
error
)
{
ipvsS
ervice
s
,
err
:=
runner
.
ipvsHandle
.
GetServices
()
ipvsS
vc
s
,
err
:=
runner
.
ipvsHandle
.
GetServices
()
if
err
!=
nil
{
if
err
!=
nil
{
return
nil
,
err
return
nil
,
err
}
}
vss
:=
make
([]
*
VirtualServer
,
0
)
vss
:=
make
([]
*
VirtualServer
,
0
)
for
_
,
ipvsS
ervice
:=
range
ipvsService
s
{
for
_
,
ipvsS
vc
:=
range
ipvsSvc
s
{
vs
,
err
:=
toVirtualServer
(
ipvsS
ervice
)
vs
,
err
:=
toVirtualServer
(
ipvsS
vc
)
if
err
!=
nil
{
if
err
!=
nil
{
return
nil
,
err
return
nil
,
err
}
}
...
@@ -110,61 +113,61 @@ func (runner *runner) GetVirtualServers() ([]*VirtualServer, error) {
...
@@ -110,61 +113,61 @@ func (runner *runner) GetVirtualServers() ([]*VirtualServer, error) {
return
vss
,
nil
return
vss
,
nil
}
}
// Flush is part of
Interface.
Currently we delete IPVS services one by one
// Flush is part of
ipvs.Interface.
Currently we delete IPVS services one by one
func
(
runner
*
runner
)
Flush
()
error
{
func
(
runner
*
runner
)
Flush
()
error
{
return
runner
.
ipvsHandle
.
Flush
()
return
runner
.
ipvsHandle
.
Flush
()
}
}
// AddRealServer is part of Interface.
// AddRealServer is part of
ipvs.
Interface.
func
(
runner
*
runner
)
AddRealServer
(
vs
*
VirtualServer
,
rs
*
RealServer
)
error
{
func
(
runner
*
runner
)
AddRealServer
(
vs
*
VirtualServer
,
rs
*
RealServer
)
error
{
bSvc
,
err
:=
toBackend
Service
(
vs
)
svc
,
err
:=
toIPVS
Service
(
vs
)
if
err
!=
nil
{
if
err
!=
nil
{
return
err
return
err
}
}
bDst
,
err
:=
toBackend
Destination
(
rs
)
dst
,
err
:=
toIPVS
Destination
(
rs
)
if
err
!=
nil
{
if
err
!=
nil
{
return
err
return
err
}
}
return
runner
.
ipvsHandle
.
NewDestination
(
bSvc
,
bD
st
)
return
runner
.
ipvsHandle
.
NewDestination
(
svc
,
d
st
)
}
}
// DeleteRealServer is part of Interface.
// DeleteRealServer is part of
ipvs.
Interface.
func
(
runner
*
runner
)
DeleteRealServer
(
vs
*
VirtualServer
,
rs
*
RealServer
)
error
{
func
(
runner
*
runner
)
DeleteRealServer
(
vs
*
VirtualServer
,
rs
*
RealServer
)
error
{
bSvc
,
err
:=
toBackend
Service
(
vs
)
svc
,
err
:=
toIPVS
Service
(
vs
)
if
err
!=
nil
{
if
err
!=
nil
{
return
err
return
err
}
}
bDst
,
err
:=
toBackend
Destination
(
rs
)
dst
,
err
:=
toIPVS
Destination
(
rs
)
if
err
!=
nil
{
if
err
!=
nil
{
return
err
return
err
}
}
return
runner
.
ipvsHandle
.
DelDestination
(
bSvc
,
bD
st
)
return
runner
.
ipvsHandle
.
DelDestination
(
svc
,
d
st
)
}
}
// GetRealServers is part of Interface.
// GetRealServers is part of
ipvs.
Interface.
func
(
runner
*
runner
)
GetRealServers
(
vs
*
VirtualServer
)
([]
*
RealServer
,
error
)
{
func
(
runner
*
runner
)
GetRealServers
(
vs
*
VirtualServer
)
([]
*
RealServer
,
error
)
{
bSvc
,
err
:=
toBackend
Service
(
vs
)
svc
,
err
:=
toIPVS
Service
(
vs
)
if
err
!=
nil
{
if
err
!=
nil
{
return
nil
,
err
return
nil
,
err
}
}
bDestinations
,
err
:=
runner
.
ipvsHandle
.
GetDestinations
(
bS
vc
)
dsts
,
err
:=
runner
.
ipvsHandle
.
GetDestinations
(
s
vc
)
if
err
!=
nil
{
if
err
!=
nil
{
return
nil
,
err
return
nil
,
err
}
}
r
ealServer
s
:=
make
([]
*
RealServer
,
0
)
r
s
s
:=
make
([]
*
RealServer
,
0
)
for
_
,
d
est
:=
range
bDestination
s
{
for
_
,
d
st
:=
range
dst
s
{
dst
,
err
:=
toRealServer
(
d
e
st
)
dst
,
err
:=
toRealServer
(
dst
)
// TODO: aggregate errors?
// TODO: aggregate errors?
if
err
!=
nil
{
if
err
!=
nil
{
return
nil
,
err
return
nil
,
err
}
}
r
ealServers
=
append
(
realServer
s
,
dst
)
r
ss
=
append
(
rs
s
,
dst
)
}
}
return
r
ealServer
s
,
nil
return
r
s
s
,
nil
}
}
// toVirtualServer converts an IPVS
service representation to the equivalent virtual s
erver structure.
// toVirtualServer converts an IPVS
Service to the equivalent VirtualS
erver structure.
func
toVirtualServer
(
svc
*
ipvs
.
Service
)
(
*
VirtualServer
,
error
)
{
func
toVirtualServer
(
svc
*
lib
ipvs
.
Service
)
(
*
VirtualServer
,
error
)
{
if
svc
==
nil
{
if
svc
==
nil
{
return
nil
,
errors
.
New
(
"ipvs svc should not be empty"
)
return
nil
,
errors
.
New
(
"ipvs svc should not be empty"
)
}
}
...
@@ -172,7 +175,7 @@ func toVirtualServer(svc *ipvs.Service) (*VirtualServer, error) {
...
@@ -172,7 +175,7 @@ func toVirtualServer(svc *ipvs.Service) (*VirtualServer, error) {
Address
:
svc
.
Address
,
Address
:
svc
.
Address
,
Port
:
svc
.
Port
,
Port
:
svc
.
Port
,
Scheduler
:
svc
.
SchedName
,
Scheduler
:
svc
.
SchedName
,
Protocol
:
protocol
NumbeToString
(
ProtoType
(
svc
.
Protocol
)),
Protocol
:
protocol
ToString
(
Protocol
(
svc
.
Protocol
)),
Timeout
:
svc
.
Timeout
,
Timeout
:
svc
.
Timeout
,
}
}
...
@@ -194,8 +197,8 @@ func toVirtualServer(svc *ipvs.Service) (*VirtualServer, error) {
...
@@ -194,8 +197,8 @@ func toVirtualServer(svc *ipvs.Service) (*VirtualServer, error) {
return
vs
,
nil
return
vs
,
nil
}
}
// toRealServer converts an IPVS
destination representation to the equivalent real s
erver structure.
// toRealServer converts an IPVS
Destination to the equivalent RealS
erver structure.
func
toRealServer
(
dst
*
ipvs
.
Destination
)
(
*
RealServer
,
error
)
{
func
toRealServer
(
dst
*
lib
ipvs
.
Destination
)
(
*
RealServer
,
error
)
{
if
dst
==
nil
{
if
dst
==
nil
{
return
nil
,
errors
.
New
(
"ipvs destination should not be empty"
)
return
nil
,
errors
.
New
(
"ipvs destination should not be empty"
)
}
}
...
@@ -206,14 +209,14 @@ func toRealServer(dst *ipvs.Destination) (*RealServer, error) {
...
@@ -206,14 +209,14 @@ func toRealServer(dst *ipvs.Destination) (*RealServer, error) {
},
nil
},
nil
}
}
// to
BackendService converts an IPVS real server representation to the equivalent "backend" s
ervice structure.
// to
IPVSService converts a VirtualServer to the equivalent IPVS S
ervice structure.
func
to
BackendService
(
vs
*
VirtualServer
)
(
*
ipvs
.
Service
,
error
)
{
func
to
IPVSService
(
vs
*
VirtualServer
)
(
*
lib
ipvs
.
Service
,
error
)
{
if
vs
==
nil
{
if
vs
==
nil
{
return
nil
,
errors
.
New
(
"virtual server should not be empty"
)
return
nil
,
errors
.
New
(
"virtual server should not be empty"
)
}
}
bakSvc
:=
&
ipvs
.
Service
{
ipvsSvc
:=
&
lib
ipvs
.
Service
{
Address
:
vs
.
Address
,
Address
:
vs
.
Address
,
Protocol
:
stringToProtocol
Number
(
vs
.
Protocol
),
Protocol
:
stringToProtocol
(
vs
.
Protocol
),
Port
:
vs
.
Port
,
Port
:
vs
.
Port
,
SchedName
:
vs
.
Scheduler
,
SchedName
:
vs
.
Scheduler
,
Flags
:
uint32
(
vs
.
Flags
),
Flags
:
uint32
(
vs
.
Flags
),
...
@@ -221,29 +224,29 @@ func toBackendService(vs *VirtualServer) (*ipvs.Service, error) {
...
@@ -221,29 +224,29 @@ func toBackendService(vs *VirtualServer) (*ipvs.Service, error) {
}
}
if
ip4
:=
vs
.
Address
.
To4
();
ip4
!=
nil
{
if
ip4
:=
vs
.
Address
.
To4
();
ip4
!=
nil
{
bak
Svc
.
AddressFamily
=
syscall
.
AF_INET
ipvs
Svc
.
AddressFamily
=
syscall
.
AF_INET
bak
Svc
.
Netmask
=
0xffffffff
ipvs
Svc
.
Netmask
=
0xffffffff
}
else
{
}
else
{
bak
Svc
.
AddressFamily
=
syscall
.
AF_INET6
ipvs
Svc
.
AddressFamily
=
syscall
.
AF_INET6
bak
Svc
.
Netmask
=
128
ipvs
Svc
.
Netmask
=
128
}
}
return
bak
Svc
,
nil
return
ipvs
Svc
,
nil
}
}
// to
BackendDestination converts an IPVS real server representation to the equivalent "backend" d
estination structure.
// to
IPVSDestination converts a RealServer to the equivalent IPVS D
estination structure.
func
to
BackendDestination
(
rs
*
RealServer
)
(
*
ipvs
.
Destination
,
error
)
{
func
to
IPVSDestination
(
rs
*
RealServer
)
(
*
lib
ipvs
.
Destination
,
error
)
{
if
rs
==
nil
{
if
rs
==
nil
{
return
nil
,
errors
.
New
(
"real server should not be empty"
)
return
nil
,
errors
.
New
(
"real server should not be empty"
)
}
}
return
&
ipvs
.
Destination
{
return
&
lib
ipvs
.
Destination
{
Address
:
rs
.
Address
,
Address
:
rs
.
Address
,
Port
:
rs
.
Port
,
Port
:
rs
.
Port
,
Weight
:
rs
.
Weight
,
Weight
:
rs
.
Weight
,
},
nil
},
nil
}
}
// stringToProtocol
Number returns the protocol valu
e for the given name
// stringToProtocol
Type returns the protocol typ
e for the given name
func
stringToProtocol
Number
(
protocol
string
)
uint16
{
func
stringToProtocol
(
protocol
string
)
uint16
{
switch
strings
.
ToLower
(
protocol
)
{
switch
strings
.
ToLower
(
protocol
)
{
case
"tcp"
:
case
"tcp"
:
return
uint16
(
syscall
.
IPPROTO_TCP
)
return
uint16
(
syscall
.
IPPROTO_TCP
)
...
@@ -253,8 +256,8 @@ func stringToProtocolNumber(protocol string) uint16 {
...
@@ -253,8 +256,8 @@ func stringToProtocolNumber(protocol string) uint16 {
return
uint16
(
0
)
return
uint16
(
0
)
}
}
// protocol
NumbeToString returns the name for the given protocol value
.
// protocol
TypeToString returns the name for the given protocol
.
func
protocol
NumbeToString
(
proto
ProtoType
)
string
{
func
protocol
ToString
(
proto
Protocol
)
string
{
switch
proto
{
switch
proto
{
case
syscall
.
IPPROTO_TCP
:
case
syscall
.
IPPROTO_TCP
:
return
"TCP"
return
"TCP"
...
@@ -263,6 +266,3 @@ func protocolNumbeToString(proto ProtoType) string {
...
@@ -263,6 +266,3 @@ func protocolNumbeToString(proto ProtoType) string {
}
}
return
""
return
""
}
}
// ProtoType is IPVS service protocol type
type
ProtoType
uint16
pkg/util/ipvs/ipvs_linux_test.go
View file @
daae0e6c
...
@@ -25,18 +25,18 @@ import (
...
@@ -25,18 +25,18 @@ import (
"syscall"
"syscall"
"testing"
"testing"
"github.com/docker/libnetwork/ipvs"
libipvs
"github.com/docker/libnetwork/ipvs"
)
)
func
Test_toVirtualServer
(
t
*
testing
.
T
)
{
func
Test_toVirtualServer
(
t
*
testing
.
T
)
{
Tests
:=
[]
struct
{
Tests
:=
[]
struct
{
ipvsService
ipvs
.
Service
ipvsService
lib
ipvs
.
Service
virtualServer
VirtualServer
virtualServer
VirtualServer
expectError
bool
expectError
bool
reason
string
reason
string
}{
}{
{
{
ipvs
.
Service
{
lib
ipvs
.
Service
{
Flags
:
0x0
,
Flags
:
0x0
,
},
},
VirtualServer
{},
VirtualServer
{},
...
@@ -44,7 +44,7 @@ func Test_toVirtualServer(t *testing.T) {
...
@@ -44,7 +44,7 @@ func Test_toVirtualServer(t *testing.T) {
fmt
.
Sprintf
(
"IPVS Service Flags should be >= %d, got 0x0"
,
FlagHashed
),
fmt
.
Sprintf
(
"IPVS Service Flags should be >= %d, got 0x0"
,
FlagHashed
),
},
},
{
{
ipvs
.
Service
{
lib
ipvs
.
Service
{
Flags
:
0x1
,
Flags
:
0x1
,
},
},
VirtualServer
{},
VirtualServer
{},
...
@@ -52,7 +52,7 @@ func Test_toVirtualServer(t *testing.T) {
...
@@ -52,7 +52,7 @@ func Test_toVirtualServer(t *testing.T) {
fmt
.
Sprintf
(
"IPVS Service Flags should be >= %d, got 0x1"
,
FlagHashed
),
fmt
.
Sprintf
(
"IPVS Service Flags should be >= %d, got 0x1"
,
FlagHashed
),
},
},
{
{
ipvs
.
Service
{
lib
ipvs
.
Service
{
Protocol
:
syscall
.
IPPROTO_TCP
,
Protocol
:
syscall
.
IPPROTO_TCP
,
Port
:
80
,
Port
:
80
,
FWMark
:
0
,
FWMark
:
0
,
...
@@ -76,7 +76,7 @@ func Test_toVirtualServer(t *testing.T) {
...
@@ -76,7 +76,7 @@ func Test_toVirtualServer(t *testing.T) {
""
,
""
,
},
},
{
{
ipvs
.
Service
{
lib
ipvs
.
Service
{
Protocol
:
syscall
.
IPPROTO_UDP
,
Protocol
:
syscall
.
IPPROTO_UDP
,
Port
:
33434
,
Port
:
33434
,
FWMark
:
0
,
FWMark
:
0
,
...
@@ -100,7 +100,7 @@ func Test_toVirtualServer(t *testing.T) {
...
@@ -100,7 +100,7 @@ func Test_toVirtualServer(t *testing.T) {
""
,
""
,
},
},
{
{
ipvs
.
Service
{
lib
ipvs
.
Service
{
Protocol
:
0
,
Protocol
:
0
,
Port
:
0
,
Port
:
0
,
FWMark
:
0
,
FWMark
:
0
,
...
@@ -124,7 +124,7 @@ func Test_toVirtualServer(t *testing.T) {
...
@@ -124,7 +124,7 @@ func Test_toVirtualServer(t *testing.T) {
""
,
""
,
},
},
{
{
ipvs
.
Service
{
lib
ipvs
.
Service
{
Protocol
:
0
,
Protocol
:
0
,
Port
:
0
,
Port
:
0
,
FWMark
:
0
,
FWMark
:
0
,
...
@@ -165,13 +165,13 @@ func Test_toVirtualServer(t *testing.T) {
...
@@ -165,13 +165,13 @@ func Test_toVirtualServer(t *testing.T) {
}
}
}
}
func
Test_to
Backend
Service
(
t
*
testing
.
T
)
{
func
Test_to
IPVS
Service
(
t
*
testing
.
T
)
{
Tests
:=
[]
struct
{
Tests
:=
[]
struct
{
ipvsService
ipvs
.
Service
ipvsService
lib
ipvs
.
Service
virtualServer
VirtualServer
virtualServer
VirtualServer
}{
}{
{
{
ipvs
.
Service
{
lib
ipvs
.
Service
{
Protocol
:
syscall
.
IPPROTO_TCP
,
Protocol
:
syscall
.
IPPROTO_TCP
,
Port
:
80
,
Port
:
80
,
FWMark
:
0
,
FWMark
:
0
,
...
@@ -193,7 +193,7 @@ func Test_toBackendService(t *testing.T) {
...
@@ -193,7 +193,7 @@ func Test_toBackendService(t *testing.T) {
},
},
},
},
{
{
ipvs
.
Service
{
lib
ipvs
.
Service
{
Protocol
:
syscall
.
IPPROTO_UDP
,
Protocol
:
syscall
.
IPPROTO_UDP
,
Port
:
33434
,
Port
:
33434
,
FWMark
:
0
,
FWMark
:
0
,
...
@@ -215,7 +215,7 @@ func Test_toBackendService(t *testing.T) {
...
@@ -215,7 +215,7 @@ func Test_toBackendService(t *testing.T) {
},
},
},
},
{
{
ipvs
.
Service
{
lib
ipvs
.
Service
{
Protocol
:
0
,
Protocol
:
0
,
Port
:
0
,
Port
:
0
,
FWMark
:
0
,
FWMark
:
0
,
...
@@ -237,7 +237,7 @@ func Test_toBackendService(t *testing.T) {
...
@@ -237,7 +237,7 @@ func Test_toBackendService(t *testing.T) {
},
},
},
},
{
{
ipvs
.
Service
{
lib
ipvs
.
Service
{
Protocol
:
0
,
Protocol
:
0
,
Port
:
0
,
Port
:
0
,
FWMark
:
0
,
FWMark
:
0
,
...
@@ -261,7 +261,7 @@ func Test_toBackendService(t *testing.T) {
...
@@ -261,7 +261,7 @@ func Test_toBackendService(t *testing.T) {
}
}
for
i
:=
range
Tests
{
for
i
:=
range
Tests
{
got
,
err
:=
to
Backend
Service
(
&
Tests
[
i
]
.
virtualServer
)
got
,
err
:=
to
IPVS
Service
(
&
Tests
[
i
]
.
virtualServer
)
if
err
!=
nil
{
if
err
!=
nil
{
t
.
Errorf
(
"case: %d, unexpected error: %v"
,
i
,
err
)
t
.
Errorf
(
"case: %d, unexpected error: %v"
,
i
,
err
)
}
}
...
@@ -271,13 +271,13 @@ func Test_toBackendService(t *testing.T) {
...
@@ -271,13 +271,13 @@ func Test_toBackendService(t *testing.T) {
}
}
}
}
func
Test_to
FrontendDestination
(
t
*
testing
.
T
)
{
func
Test_to
RealServer
(
t
*
testing
.
T
)
{
Tests
:=
[]
struct
{
Tests
:=
[]
struct
{
ipvsDestination
ipvs
.
Destination
ipvsDestination
lib
ipvs
.
Destination
realServer
RealServer
realServer
RealServer
}{
}{
{
{
ipvs
.
Destination
{
lib
ipvs
.
Destination
{
Port
:
54321
,
Port
:
54321
,
ConnectionFlags
:
0
,
ConnectionFlags
:
0
,
Weight
:
1
,
Weight
:
1
,
...
@@ -290,7 +290,7 @@ func Test_toFrontendDestination(t *testing.T) {
...
@@ -290,7 +290,7 @@ func Test_toFrontendDestination(t *testing.T) {
},
},
},
},
{
{
ipvs
.
Destination
{
lib
ipvs
.
Destination
{
Port
:
53
,
Port
:
53
,
ConnectionFlags
:
0
,
ConnectionFlags
:
0
,
Weight
:
1
,
Weight
:
1
,
...
@@ -314,10 +314,10 @@ func Test_toFrontendDestination(t *testing.T) {
...
@@ -314,10 +314,10 @@ func Test_toFrontendDestination(t *testing.T) {
}
}
}
}
func
Test_to
Backend
Destination
(
t
*
testing
.
T
)
{
func
Test_to
IPVS
Destination
(
t
*
testing
.
T
)
{
Tests
:=
[]
struct
{
Tests
:=
[]
struct
{
realServer
RealServer
realServer
RealServer
ipvsDestination
ipvs
.
Destination
ipvsDestination
lib
ipvs
.
Destination
}{
}{
{
{
RealServer
{
RealServer
{
...
@@ -325,7 +325,7 @@ func Test_toBackendDestination(t *testing.T) {
...
@@ -325,7 +325,7 @@ func Test_toBackendDestination(t *testing.T) {
Port
:
54321
,
Port
:
54321
,
Weight
:
1
,
Weight
:
1
,
},
},
ipvs
.
Destination
{
lib
ipvs
.
Destination
{
Port
:
54321
,
Port
:
54321
,
ConnectionFlags
:
0
,
ConnectionFlags
:
0
,
Weight
:
1
,
Weight
:
1
,
...
@@ -338,7 +338,7 @@ func Test_toBackendDestination(t *testing.T) {
...
@@ -338,7 +338,7 @@ func Test_toBackendDestination(t *testing.T) {
Port
:
53
,
Port
:
53
,
Weight
:
1
,
Weight
:
1
,
},
},
ipvs
.
Destination
{
lib
ipvs
.
Destination
{
Port
:
53
,
Port
:
53
,
ConnectionFlags
:
0
,
ConnectionFlags
:
0
,
Weight
:
1
,
Weight
:
1
,
...
@@ -347,44 +347,44 @@ func Test_toBackendDestination(t *testing.T) {
...
@@ -347,44 +347,44 @@ func Test_toBackendDestination(t *testing.T) {
},
},
}
}
for
i
:=
range
Tests
{
for
i
:=
range
Tests
{
got
,
err
:=
to
Backend
Destination
(
&
Tests
[
i
]
.
realServer
)
got
,
err
:=
to
IPVS
Destination
(
&
Tests
[
i
]
.
realServer
)
if
err
!=
nil
{
if
err
!=
nil
{
t
.
Errorf
(
"case %d unexpected error: %d"
,
i
,
err
)
t
.
Errorf
(
"case %d unexpected error: %d"
,
i
,
err
)
}
}
if
!
reflect
.
DeepEqual
(
*
got
,
Tests
[
i
]
.
ipvsDestination
)
{
if
!
reflect
.
DeepEqual
(
*
got
,
Tests
[
i
]
.
ipvsDestination
)
{
t
.
Errorf
(
"case %d
F
ailed to translate Destination - got %#v, want %#v"
,
i
,
*
got
,
Tests
[
i
]
.
ipvsDestination
)
t
.
Errorf
(
"case %d
f
ailed to translate Destination - got %#v, want %#v"
,
i
,
*
got
,
Tests
[
i
]
.
ipvsDestination
)
}
}
}
}
}
}
func
Test_stringToProtocol
Number
(
t
*
testing
.
T
)
{
func
Test_stringToProtocol
(
t
*
testing
.
T
)
{
tests
:=
[]
string
{
tests
:=
[]
string
{
"TCP"
,
"UDP"
,
"ICMP"
,
"TCP"
,
"UDP"
,
"ICMP"
,
}
}
expected
s
:=
[]
uint16
{
expected
:=
[]
uint16
{
uint16
(
syscall
.
IPPROTO_TCP
),
uint16
(
syscall
.
IPPROTO_UDP
),
uint16
(
0
),
uint16
(
syscall
.
IPPROTO_TCP
),
uint16
(
syscall
.
IPPROTO_UDP
),
uint16
(
0
),
}
}
for
i
:=
range
tests
{
for
i
:=
range
tests
{
got
:=
stringToProtocol
Number
(
tests
[
i
])
got
:=
stringToProtocol
(
tests
[
i
])
if
got
!=
expected
s
[
i
]
{
if
got
!=
expected
[
i
]
{
t
.
Errorf
(
"stringToProtocol
Number
() failed - got %#v, want %#v"
,
t
.
Errorf
(
"stringToProtocol() failed - got %#v, want %#v"
,
got
,
expected
s
[
i
])
got
,
expected
[
i
])
}
}
}
}
}
}
func
Test_protocol
Number
ToString
(
t
*
testing
.
T
)
{
func
Test_protocolToString
(
t
*
testing
.
T
)
{
tests
:=
[]
Proto
Type
{
tests
:=
[]
Proto
col
{
syscall
.
IPPROTO_TCP
,
syscall
.
IPPROTO_UDP
,
Proto
Type
(
0
),
syscall
.
IPPROTO_TCP
,
syscall
.
IPPROTO_UDP
,
Proto
col
(
0
),
}
}
expected
s
:=
[]
string
{
expected
:=
[]
string
{
"TCP"
,
"UDP"
,
""
,
"TCP"
,
"UDP"
,
""
,
}
}
for
i
:=
range
tests
{
for
i
:=
range
tests
{
got
:=
protocol
Numbe
ToString
(
tests
[
i
])
got
:=
protocolToString
(
tests
[
i
])
if
got
!=
expected
s
[
i
]
{
if
got
!=
expected
[
i
]
{
t
.
Errorf
(
"protocol
Numbe
ToString() failed - got %#v, want %#v"
,
t
.
Errorf
(
"protocolToString() failed - got %#v, want %#v"
,
got
,
expected
s
[
i
])
got
,
expected
[
i
])
}
}
}
}
}
}
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