mirror of https://github.com/k3s-io/k3s
Merge pull request #59429 from rramkumar1/ipvs-cleanup
Automatic merge from submit-queue. If you want to cherry-pick this change to another branch, please follow the instructions <a href="https://github.com/kubernetes/community/blob/master/contributors/devel/cherry-picks.md">here</a>. Cleanup of ipvs utils **What this PR does / why we need it**: This PR is a small cleanup of pkg/util/ipvs. The changes are as follows: 1. Rename toBackendService -> toIPVSService and toBackendDestination -> toIPVSDestination. The use of the term 'backend' makes things really confusing and this renamig makes it more explicit about what we are doing. 2. Give the libnetwork/ipvs package the name libipvs. This makes it less confusing since the package these files are in is also ipvs. 3. Some variable naming cleanup to make things easier to read. /assign @m1093782566 **Release note**: ```release-note None ```pull/6/head
commit
07e849c986
|
@ -25,83 +25,86 @@ import (
|
|||
"strings"
|
||||
"syscall"
|
||||
|
||||
"github.com/docker/libnetwork/ipvs"
|
||||
libipvs "github.com/docker/libnetwork/ipvs"
|
||||
"github.com/golang/glog"
|
||||
utilexec "k8s.io/utils/exec"
|
||||
)
|
||||
|
||||
// runner implements Interface.
|
||||
// runner implements ipvs.Interface.
|
||||
type runner struct {
|
||||
exec utilexec.Interface
|
||||
ipvsHandle *ipvs.Handle
|
||||
ipvsHandle *libipvs.Handle
|
||||
}
|
||||
|
||||
// Protocol is the IPVS service protocol type
|
||||
type Protocol uint16
|
||||
|
||||
// New returns a new Interface which will call ipvs APIs.
|
||||
func New(exec utilexec.Interface) Interface {
|
||||
ihandle, err := ipvs.New("")
|
||||
handle, err := libipvs.New("")
|
||||
if err != nil {
|
||||
glog.Errorf("IPVS interface can't be initialized, error: %v", err)
|
||||
return nil
|
||||
}
|
||||
return &runner{
|
||||
exec: exec,
|
||||
ipvsHandle: ihandle,
|
||||
ipvsHandle: handle,
|
||||
}
|
||||
}
|
||||
|
||||
// AddVirtualServer is part of Interface.
|
||||
// AddVirtualServer is part of ipvs.Interface.
|
||||
func (runner *runner) AddVirtualServer(vs *VirtualServer) error {
|
||||
eSvc, err := toBackendService(vs)
|
||||
svc, err := toIPVSService(vs)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return runner.ipvsHandle.NewService(eSvc)
|
||||
return runner.ipvsHandle.NewService(svc)
|
||||
}
|
||||
|
||||
// UpdateVirtualServer is part of Interface.
|
||||
// UpdateVirtualServer is part of ipvs.Interface.
|
||||
func (runner *runner) UpdateVirtualServer(vs *VirtualServer) error {
|
||||
bSvc, err := toBackendService(vs)
|
||||
svc, err := toIPVSService(vs)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return runner.ipvsHandle.UpdateService(bSvc)
|
||||
return runner.ipvsHandle.UpdateService(svc)
|
||||
}
|
||||
|
||||
// DeleteVirtualServer is part of Interface.
|
||||
// DeleteVirtualServer is part of ipvs.Interface.
|
||||
func (runner *runner) DeleteVirtualServer(vs *VirtualServer) error {
|
||||
bSvc, err := toBackendService(vs)
|
||||
svc, err := toIPVSService(vs)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return runner.ipvsHandle.DelService(bSvc)
|
||||
return runner.ipvsHandle.DelService(svc)
|
||||
}
|
||||
|
||||
// GetVirtualServer is part of Interface.
|
||||
// GetVirtualServer is part of ipvs.Interface.
|
||||
func (runner *runner) GetVirtualServer(vs *VirtualServer) (*VirtualServer, error) {
|
||||
bSvc, err := toBackendService(vs)
|
||||
svc, err := toIPVSService(vs)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
ipvsService, err := runner.ipvsHandle.GetService(bSvc)
|
||||
ipvsSvc, err := runner.ipvsHandle.GetService(svc)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
virtualServer, err := toVirtualServer(ipvsService)
|
||||
vServ, err := toVirtualServer(ipvsSvc)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return virtualServer, nil
|
||||
return vServ, nil
|
||||
}
|
||||
|
||||
// GetVirtualServers is part of Interface.
|
||||
// GetVirtualServers is part of ipvs.Interface.
|
||||
func (runner *runner) GetVirtualServers() ([]*VirtualServer, error) {
|
||||
ipvsServices, err := runner.ipvsHandle.GetServices()
|
||||
ipvsSvcs, err := runner.ipvsHandle.GetServices()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
vss := make([]*VirtualServer, 0)
|
||||
for _, ipvsService := range ipvsServices {
|
||||
vs, err := toVirtualServer(ipvsService)
|
||||
for _, ipvsSvc := range ipvsSvcs {
|
||||
vs, err := toVirtualServer(ipvsSvc)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
@ -110,61 +113,61 @@ func (runner *runner) GetVirtualServers() ([]*VirtualServer, error) {
|
|||
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 {
|
||||
return runner.ipvsHandle.Flush()
|
||||
}
|
||||
|
||||
// AddRealServer is part of Interface.
|
||||
// AddRealServer is part of ipvs.Interface.
|
||||
func (runner *runner) AddRealServer(vs *VirtualServer, rs *RealServer) error {
|
||||
bSvc, err := toBackendService(vs)
|
||||
svc, err := toIPVSService(vs)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
bDst, err := toBackendDestination(rs)
|
||||
dst, err := toIPVSDestination(rs)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return runner.ipvsHandle.NewDestination(bSvc, bDst)
|
||||
return runner.ipvsHandle.NewDestination(svc, dst)
|
||||
}
|
||||
|
||||
// DeleteRealServer is part of Interface.
|
||||
// DeleteRealServer is part of ipvs.Interface.
|
||||
func (runner *runner) DeleteRealServer(vs *VirtualServer, rs *RealServer) error {
|
||||
bSvc, err := toBackendService(vs)
|
||||
svc, err := toIPVSService(vs)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
bDst, err := toBackendDestination(rs)
|
||||
dst, err := toIPVSDestination(rs)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return runner.ipvsHandle.DelDestination(bSvc, bDst)
|
||||
return runner.ipvsHandle.DelDestination(svc, dst)
|
||||
}
|
||||
|
||||
// GetRealServers is part of Interface.
|
||||
// GetRealServers is part of ipvs.Interface.
|
||||
func (runner *runner) GetRealServers(vs *VirtualServer) ([]*RealServer, error) {
|
||||
bSvc, err := toBackendService(vs)
|
||||
svc, err := toIPVSService(vs)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
bDestinations, err := runner.ipvsHandle.GetDestinations(bSvc)
|
||||
dsts, err := runner.ipvsHandle.GetDestinations(svc)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
realServers := make([]*RealServer, 0)
|
||||
for _, dest := range bDestinations {
|
||||
dst, err := toRealServer(dest)
|
||||
rss := make([]*RealServer, 0)
|
||||
for _, dst := range dsts {
|
||||
dst, err := toRealServer(dst)
|
||||
// TODO: aggregate errors?
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
realServers = append(realServers, dst)
|
||||
rss = append(rss, dst)
|
||||
}
|
||||
return realServers, nil
|
||||
return rss, nil
|
||||
}
|
||||
|
||||
// toVirtualServer converts an IPVS service representation to the equivalent virtual server structure.
|
||||
func toVirtualServer(svc *ipvs.Service) (*VirtualServer, error) {
|
||||
// toVirtualServer converts an IPVS Service to the equivalent VirtualServer structure.
|
||||
func toVirtualServer(svc *libipvs.Service) (*VirtualServer, error) {
|
||||
if svc == nil {
|
||||
return nil, errors.New("ipvs svc should not be empty")
|
||||
}
|
||||
|
@ -172,7 +175,7 @@ func toVirtualServer(svc *ipvs.Service) (*VirtualServer, error) {
|
|||
Address: svc.Address,
|
||||
Port: svc.Port,
|
||||
Scheduler: svc.SchedName,
|
||||
Protocol: protocolNumbeToString(ProtoType(svc.Protocol)),
|
||||
Protocol: protocolToString(Protocol(svc.Protocol)),
|
||||
Timeout: svc.Timeout,
|
||||
}
|
||||
|
||||
|
@ -194,8 +197,8 @@ func toVirtualServer(svc *ipvs.Service) (*VirtualServer, error) {
|
|||
return vs, nil
|
||||
}
|
||||
|
||||
// toRealServer converts an IPVS destination representation to the equivalent real server structure.
|
||||
func toRealServer(dst *ipvs.Destination) (*RealServer, error) {
|
||||
// toRealServer converts an IPVS Destination to the equivalent RealServer structure.
|
||||
func toRealServer(dst *libipvs.Destination) (*RealServer, error) {
|
||||
if dst == nil {
|
||||
return nil, errors.New("ipvs destination should not be empty")
|
||||
}
|
||||
|
@ -206,14 +209,14 @@ func toRealServer(dst *ipvs.Destination) (*RealServer, error) {
|
|||
}, nil
|
||||
}
|
||||
|
||||
// toBackendService converts an IPVS real server representation to the equivalent "backend" service structure.
|
||||
func toBackendService(vs *VirtualServer) (*ipvs.Service, error) {
|
||||
// toIPVSService converts a VirtualServer to the equivalent IPVS Service structure.
|
||||
func toIPVSService(vs *VirtualServer) (*libipvs.Service, error) {
|
||||
if vs == nil {
|
||||
return nil, errors.New("virtual server should not be empty")
|
||||
}
|
||||
bakSvc := &ipvs.Service{
|
||||
ipvsSvc := &libipvs.Service{
|
||||
Address: vs.Address,
|
||||
Protocol: stringToProtocolNumber(vs.Protocol),
|
||||
Protocol: stringToProtocol(vs.Protocol),
|
||||
Port: vs.Port,
|
||||
SchedName: vs.Scheduler,
|
||||
Flags: uint32(vs.Flags),
|
||||
|
@ -221,29 +224,29 @@ func toBackendService(vs *VirtualServer) (*ipvs.Service, error) {
|
|||
}
|
||||
|
||||
if ip4 := vs.Address.To4(); ip4 != nil {
|
||||
bakSvc.AddressFamily = syscall.AF_INET
|
||||
bakSvc.Netmask = 0xffffffff
|
||||
ipvsSvc.AddressFamily = syscall.AF_INET
|
||||
ipvsSvc.Netmask = 0xffffffff
|
||||
} else {
|
||||
bakSvc.AddressFamily = syscall.AF_INET6
|
||||
bakSvc.Netmask = 128
|
||||
ipvsSvc.AddressFamily = syscall.AF_INET6
|
||||
ipvsSvc.Netmask = 128
|
||||
}
|
||||
return bakSvc, nil
|
||||
return ipvsSvc, nil
|
||||
}
|
||||
|
||||
// toBackendDestination converts an IPVS real server representation to the equivalent "backend" destination structure.
|
||||
func toBackendDestination(rs *RealServer) (*ipvs.Destination, error) {
|
||||
// toIPVSDestination converts a RealServer to the equivalent IPVS Destination structure.
|
||||
func toIPVSDestination(rs *RealServer) (*libipvs.Destination, error) {
|
||||
if rs == nil {
|
||||
return nil, errors.New("real server should not be empty")
|
||||
}
|
||||
return &ipvs.Destination{
|
||||
return &libipvs.Destination{
|
||||
Address: rs.Address,
|
||||
Port: rs.Port,
|
||||
Weight: rs.Weight,
|
||||
}, nil
|
||||
}
|
||||
|
||||
// stringToProtocolNumber returns the protocol value for the given name
|
||||
func stringToProtocolNumber(protocol string) uint16 {
|
||||
// stringToProtocolType returns the protocol type for the given name
|
||||
func stringToProtocol(protocol string) uint16 {
|
||||
switch strings.ToLower(protocol) {
|
||||
case "tcp":
|
||||
return uint16(syscall.IPPROTO_TCP)
|
||||
|
@ -253,8 +256,8 @@ func stringToProtocolNumber(protocol string) uint16 {
|
|||
return uint16(0)
|
||||
}
|
||||
|
||||
// protocolNumbeToString returns the name for the given protocol value.
|
||||
func protocolNumbeToString(proto ProtoType) string {
|
||||
// protocolTypeToString returns the name for the given protocol.
|
||||
func protocolToString(proto Protocol) string {
|
||||
switch proto {
|
||||
case syscall.IPPROTO_TCP:
|
||||
return "TCP"
|
||||
|
@ -263,6 +266,3 @@ func protocolNumbeToString(proto ProtoType) string {
|
|||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
// ProtoType is IPVS service protocol type
|
||||
type ProtoType uint16
|
||||
|
|
|
@ -25,18 +25,18 @@ import (
|
|||
"syscall"
|
||||
"testing"
|
||||
|
||||
"github.com/docker/libnetwork/ipvs"
|
||||
libipvs "github.com/docker/libnetwork/ipvs"
|
||||
)
|
||||
|
||||
func Test_toVirtualServer(t *testing.T) {
|
||||
Tests := []struct {
|
||||
ipvsService ipvs.Service
|
||||
ipvsService libipvs.Service
|
||||
virtualServer VirtualServer
|
||||
expectError bool
|
||||
reason string
|
||||
}{
|
||||
{
|
||||
ipvs.Service{
|
||||
libipvs.Service{
|
||||
Flags: 0x0,
|
||||
},
|
||||
VirtualServer{},
|
||||
|
@ -44,7 +44,7 @@ func Test_toVirtualServer(t *testing.T) {
|
|||
fmt.Sprintf("IPVS Service Flags should be >= %d, got 0x0", FlagHashed),
|
||||
},
|
||||
{
|
||||
ipvs.Service{
|
||||
libipvs.Service{
|
||||
Flags: 0x1,
|
||||
},
|
||||
VirtualServer{},
|
||||
|
@ -52,7 +52,7 @@ func Test_toVirtualServer(t *testing.T) {
|
|||
fmt.Sprintf("IPVS Service Flags should be >= %d, got 0x1", FlagHashed),
|
||||
},
|
||||
{
|
||||
ipvs.Service{
|
||||
libipvs.Service{
|
||||
Protocol: syscall.IPPROTO_TCP,
|
||||
Port: 80,
|
||||
FWMark: 0,
|
||||
|
@ -76,7 +76,7 @@ func Test_toVirtualServer(t *testing.T) {
|
|||
"",
|
||||
},
|
||||
{
|
||||
ipvs.Service{
|
||||
libipvs.Service{
|
||||
Protocol: syscall.IPPROTO_UDP,
|
||||
Port: 33434,
|
||||
FWMark: 0,
|
||||
|
@ -100,7 +100,7 @@ func Test_toVirtualServer(t *testing.T) {
|
|||
"",
|
||||
},
|
||||
{
|
||||
ipvs.Service{
|
||||
libipvs.Service{
|
||||
Protocol: 0,
|
||||
Port: 0,
|
||||
FWMark: 0,
|
||||
|
@ -124,7 +124,7 @@ func Test_toVirtualServer(t *testing.T) {
|
|||
"",
|
||||
},
|
||||
{
|
||||
ipvs.Service{
|
||||
libipvs.Service{
|
||||
Protocol: 0,
|
||||
Port: 0,
|
||||
FWMark: 0,
|
||||
|
@ -165,13 +165,13 @@ func Test_toVirtualServer(t *testing.T) {
|
|||
}
|
||||
}
|
||||
|
||||
func Test_toBackendService(t *testing.T) {
|
||||
func Test_toIPVSService(t *testing.T) {
|
||||
Tests := []struct {
|
||||
ipvsService ipvs.Service
|
||||
ipvsService libipvs.Service
|
||||
virtualServer VirtualServer
|
||||
}{
|
||||
{
|
||||
ipvs.Service{
|
||||
libipvs.Service{
|
||||
Protocol: syscall.IPPROTO_TCP,
|
||||
Port: 80,
|
||||
FWMark: 0,
|
||||
|
@ -193,7 +193,7 @@ func Test_toBackendService(t *testing.T) {
|
|||
},
|
||||
},
|
||||
{
|
||||
ipvs.Service{
|
||||
libipvs.Service{
|
||||
Protocol: syscall.IPPROTO_UDP,
|
||||
Port: 33434,
|
||||
FWMark: 0,
|
||||
|
@ -215,7 +215,7 @@ func Test_toBackendService(t *testing.T) {
|
|||
},
|
||||
},
|
||||
{
|
||||
ipvs.Service{
|
||||
libipvs.Service{
|
||||
Protocol: 0,
|
||||
Port: 0,
|
||||
FWMark: 0,
|
||||
|
@ -237,7 +237,7 @@ func Test_toBackendService(t *testing.T) {
|
|||
},
|
||||
},
|
||||
{
|
||||
ipvs.Service{
|
||||
libipvs.Service{
|
||||
Protocol: 0,
|
||||
Port: 0,
|
||||
FWMark: 0,
|
||||
|
@ -261,7 +261,7 @@ func Test_toBackendService(t *testing.T) {
|
|||
}
|
||||
|
||||
for i := range Tests {
|
||||
got, err := toBackendService(&Tests[i].virtualServer)
|
||||
got, err := toIPVSService(&Tests[i].virtualServer)
|
||||
if err != nil {
|
||||
t.Errorf("case: %d, unexpected error: %v", i, err)
|
||||
}
|
||||
|
@ -271,13 +271,13 @@ func Test_toBackendService(t *testing.T) {
|
|||
}
|
||||
}
|
||||
|
||||
func Test_toFrontendDestination(t *testing.T) {
|
||||
func Test_toRealServer(t *testing.T) {
|
||||
Tests := []struct {
|
||||
ipvsDestination ipvs.Destination
|
||||
ipvsDestination libipvs.Destination
|
||||
realServer RealServer
|
||||
}{
|
||||
{
|
||||
ipvs.Destination{
|
||||
libipvs.Destination{
|
||||
Port: 54321,
|
||||
ConnectionFlags: 0,
|
||||
Weight: 1,
|
||||
|
@ -290,7 +290,7 @@ func Test_toFrontendDestination(t *testing.T) {
|
|||
},
|
||||
},
|
||||
{
|
||||
ipvs.Destination{
|
||||
libipvs.Destination{
|
||||
Port: 53,
|
||||
ConnectionFlags: 0,
|
||||
Weight: 1,
|
||||
|
@ -314,10 +314,10 @@ func Test_toFrontendDestination(t *testing.T) {
|
|||
}
|
||||
}
|
||||
|
||||
func Test_toBackendDestination(t *testing.T) {
|
||||
func Test_toIPVSDestination(t *testing.T) {
|
||||
Tests := []struct {
|
||||
realServer RealServer
|
||||
ipvsDestination ipvs.Destination
|
||||
ipvsDestination libipvs.Destination
|
||||
}{
|
||||
{
|
||||
RealServer{
|
||||
|
@ -325,7 +325,7 @@ func Test_toBackendDestination(t *testing.T) {
|
|||
Port: 54321,
|
||||
Weight: 1,
|
||||
},
|
||||
ipvs.Destination{
|
||||
libipvs.Destination{
|
||||
Port: 54321,
|
||||
ConnectionFlags: 0,
|
||||
Weight: 1,
|
||||
|
@ -338,7 +338,7 @@ func Test_toBackendDestination(t *testing.T) {
|
|||
Port: 53,
|
||||
Weight: 1,
|
||||
},
|
||||
ipvs.Destination{
|
||||
libipvs.Destination{
|
||||
Port: 53,
|
||||
ConnectionFlags: 0,
|
||||
Weight: 1,
|
||||
|
@ -347,44 +347,44 @@ func Test_toBackendDestination(t *testing.T) {
|
|||
},
|
||||
}
|
||||
for i := range Tests {
|
||||
got, err := toBackendDestination(&Tests[i].realServer)
|
||||
got, err := toIPVSDestination(&Tests[i].realServer)
|
||||
if err != nil {
|
||||
t.Errorf("case %d unexpected error: %d", i, err)
|
||||
}
|
||||
if !reflect.DeepEqual(*got, Tests[i].ipvsDestination) {
|
||||
t.Errorf("case %d Failed to translate Destination - got %#v, want %#v", i, *got, Tests[i].ipvsDestination)
|
||||
t.Errorf("case %d failed to translate Destination - got %#v, want %#v", i, *got, Tests[i].ipvsDestination)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func Test_stringToProtocolNumber(t *testing.T) {
|
||||
func Test_stringToProtocol(t *testing.T) {
|
||||
tests := []string{
|
||||
"TCP", "UDP", "ICMP",
|
||||
}
|
||||
expecteds := []uint16{
|
||||
expected := []uint16{
|
||||
uint16(syscall.IPPROTO_TCP), uint16(syscall.IPPROTO_UDP), uint16(0),
|
||||
}
|
||||
for i := range tests {
|
||||
got := stringToProtocolNumber(tests[i])
|
||||
if got != expecteds[i] {
|
||||
t.Errorf("stringToProtocolNumber() failed - got %#v, want %#v",
|
||||
got, expecteds[i])
|
||||
got := stringToProtocol(tests[i])
|
||||
if got != expected[i] {
|
||||
t.Errorf("stringToProtocol() failed - got %#v, want %#v",
|
||||
got, expected[i])
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func Test_protocolNumberToString(t *testing.T) {
|
||||
tests := []ProtoType{
|
||||
syscall.IPPROTO_TCP, syscall.IPPROTO_UDP, ProtoType(0),
|
||||
func Test_protocolToString(t *testing.T) {
|
||||
tests := []Protocol{
|
||||
syscall.IPPROTO_TCP, syscall.IPPROTO_UDP, Protocol(0),
|
||||
}
|
||||
expecteds := []string{
|
||||
expected := []string{
|
||||
"TCP", "UDP", "",
|
||||
}
|
||||
for i := range tests {
|
||||
got := protocolNumbeToString(tests[i])
|
||||
if got != expecteds[i] {
|
||||
t.Errorf("protocolNumbeToString() failed - got %#v, want %#v",
|
||||
got, expecteds[i])
|
||||
got := protocolToString(tests[i])
|
||||
if got != expected[i] {
|
||||
t.Errorf("protocolToString() failed - got %#v, want %#v",
|
||||
got, expected[i])
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue