Merge pull request #107001 from mk46/unix_lib

replace syscall with sys/unix pkg in pkg/util/ipvs
This commit is contained in:
Kubernetes Prow Robot 2022-01-05 13:54:13 -08:00 committed by GitHub
commit 098b0e05d1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 28 additions and 27 deletions

View File

@ -24,12 +24,12 @@ import (
"net" "net"
"strings" "strings"
"sync" "sync"
"syscall"
"time" "time"
"errors" "errors"
libipvs "github.com/moby/ipvs" libipvs "github.com/moby/ipvs"
"golang.org/x/sys/unix"
"k8s.io/klog/v2" "k8s.io/klog/v2"
) )
@ -234,7 +234,7 @@ func toVirtualServer(svc *libipvs.Service) (*VirtualServer, error) {
vs.Flags = ServiceFlags(svc.Flags &^ uint32(FlagHashed)) vs.Flags = ServiceFlags(svc.Flags &^ uint32(FlagHashed))
if vs.Address == nil { if vs.Address == nil {
if svc.AddressFamily == syscall.AF_INET { if svc.AddressFamily == unix.AF_INET {
vs.Address = net.IPv4zero vs.Address = net.IPv4zero
} else { } else {
vs.Address = net.IPv6zero vs.Address = net.IPv6zero
@ -272,10 +272,10 @@ func toIPVSService(vs *VirtualServer) (*libipvs.Service, error) {
} }
if ip4 := vs.Address.To4(); ip4 != nil { if ip4 := vs.Address.To4(); ip4 != nil {
ipvsSvc.AddressFamily = syscall.AF_INET ipvsSvc.AddressFamily = unix.AF_INET
ipvsSvc.Netmask = 0xffffffff ipvsSvc.Netmask = 0xffffffff
} else { } else {
ipvsSvc.AddressFamily = syscall.AF_INET6 ipvsSvc.AddressFamily = unix.AF_INET6
ipvsSvc.Netmask = 128 ipvsSvc.Netmask = 128
} }
return ipvsSvc, nil return ipvsSvc, nil
@ -297,11 +297,11 @@ func toIPVSDestination(rs *RealServer) (*libipvs.Destination, error) {
func stringToProtocol(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(unix.IPPROTO_TCP)
case "udp": case "udp":
return uint16(syscall.IPPROTO_UDP) return uint16(unix.IPPROTO_UDP)
case "sctp": case "sctp":
return uint16(syscall.IPPROTO_SCTP) return uint16(unix.IPPROTO_SCTP)
} }
return uint16(0) return uint16(0)
} }
@ -309,11 +309,11 @@ func stringToProtocol(protocol string) uint16 {
// protocolTypeToString returns the name for the given protocol. // protocolTypeToString returns the name for the given protocol.
func protocolToString(proto Protocol) string { func protocolToString(proto Protocol) string {
switch proto { switch proto {
case syscall.IPPROTO_TCP: case unix.IPPROTO_TCP:
return "TCP" return "TCP"
case syscall.IPPROTO_UDP: case unix.IPPROTO_UDP:
return "UDP" return "UDP"
case syscall.IPPROTO_SCTP: case unix.IPPROTO_SCTP:
return "SCTP" return "SCTP"
} }
return "" return ""

View File

@ -22,12 +22,13 @@ package ipvs
import ( import (
"fmt" "fmt"
"reflect" "reflect"
"syscall"
"testing" "testing"
netutils "k8s.io/utils/net" netutils "k8s.io/utils/net"
libipvs "github.com/moby/ipvs" libipvs "github.com/moby/ipvs"
"golang.org/x/sys/unix"
) )
func Test_toVirtualServer(t *testing.T) { func Test_toVirtualServer(t *testing.T) {
@ -55,14 +56,14 @@ func Test_toVirtualServer(t *testing.T) {
}, },
{ {
libipvs.Service{ libipvs.Service{
Protocol: syscall.IPPROTO_TCP, Protocol: unix.IPPROTO_TCP,
Port: 80, Port: 80,
FWMark: 0, FWMark: 0,
SchedName: "", SchedName: "",
Flags: uint32(FlagPersistent + FlagHashed), Flags: uint32(FlagPersistent + FlagHashed),
Timeout: 0, Timeout: 0,
Netmask: 0xffffffff, Netmask: 0xffffffff,
AddressFamily: syscall.AF_INET, AddressFamily: unix.AF_INET,
Address: nil, Address: nil,
PEName: "", PEName: "",
}, },
@ -79,14 +80,14 @@ func Test_toVirtualServer(t *testing.T) {
}, },
{ {
libipvs.Service{ libipvs.Service{
Protocol: syscall.IPPROTO_UDP, Protocol: unix.IPPROTO_UDP,
Port: 33434, Port: 33434,
FWMark: 0, FWMark: 0,
SchedName: "wlc", SchedName: "wlc",
Flags: uint32(0 + FlagHashed), Flags: uint32(0 + FlagHashed),
Timeout: 100, Timeout: 100,
Netmask: 128, Netmask: 128,
AddressFamily: syscall.AF_INET6, AddressFamily: unix.AF_INET6,
Address: netutils.ParseIPSloppy("2012::beef"), Address: netutils.ParseIPSloppy("2012::beef"),
PEName: "", PEName: "",
}, },
@ -110,7 +111,7 @@ func Test_toVirtualServer(t *testing.T) {
Flags: uint32(0 + FlagHashed), Flags: uint32(0 + FlagHashed),
Timeout: 0, Timeout: 0,
Netmask: 0xffffffff, Netmask: 0xffffffff,
AddressFamily: syscall.AF_INET, AddressFamily: unix.AF_INET,
Address: netutils.ParseIPSloppy("1.2.3.4"), Address: netutils.ParseIPSloppy("1.2.3.4"),
PEName: "", PEName: "",
}, },
@ -134,7 +135,7 @@ func Test_toVirtualServer(t *testing.T) {
Flags: uint32(FlagPersistent + FlagHashed), Flags: uint32(FlagPersistent + FlagHashed),
Timeout: 0, Timeout: 0,
Netmask: 128, Netmask: 128,
AddressFamily: syscall.AF_INET6, AddressFamily: unix.AF_INET6,
Address: nil, Address: nil,
PEName: "", PEName: "",
}, },
@ -151,14 +152,14 @@ func Test_toVirtualServer(t *testing.T) {
}, },
{ {
libipvs.Service{ libipvs.Service{
Protocol: syscall.IPPROTO_SCTP, Protocol: unix.IPPROTO_SCTP,
Port: 80, Port: 80,
FWMark: 0, FWMark: 0,
SchedName: "", SchedName: "",
Flags: uint32(FlagPersistent + FlagHashed), Flags: uint32(FlagPersistent + FlagHashed),
Timeout: 0, Timeout: 0,
Netmask: 0xffffffff, Netmask: 0xffffffff,
AddressFamily: syscall.AF_INET, AddressFamily: unix.AF_INET,
Address: nil, Address: nil,
PEName: "", PEName: "",
}, },
@ -198,14 +199,14 @@ func Test_toIPVSService(t *testing.T) {
}{ }{
{ {
libipvs.Service{ libipvs.Service{
Protocol: syscall.IPPROTO_TCP, Protocol: unix.IPPROTO_TCP,
Port: 80, Port: 80,
FWMark: 0, FWMark: 0,
SchedName: "", SchedName: "",
Flags: 0, Flags: 0,
Timeout: 0, Timeout: 0,
Netmask: 0xffffffff, Netmask: 0xffffffff,
AddressFamily: syscall.AF_INET, AddressFamily: unix.AF_INET,
Address: netutils.ParseIPSloppy("0.0.0.0"), Address: netutils.ParseIPSloppy("0.0.0.0"),
PEName: "", PEName: "",
}, },
@ -220,14 +221,14 @@ func Test_toIPVSService(t *testing.T) {
}, },
{ {
libipvs.Service{ libipvs.Service{
Protocol: syscall.IPPROTO_UDP, Protocol: unix.IPPROTO_UDP,
Port: 33434, Port: 33434,
FWMark: 0, FWMark: 0,
SchedName: "wlc", SchedName: "wlc",
Flags: 1234, Flags: 1234,
Timeout: 100, Timeout: 100,
Netmask: 128, Netmask: 128,
AddressFamily: syscall.AF_INET6, AddressFamily: unix.AF_INET6,
Address: netutils.ParseIPSloppy("2012::beef"), Address: netutils.ParseIPSloppy("2012::beef"),
PEName: "", PEName: "",
}, },
@ -249,7 +250,7 @@ func Test_toIPVSService(t *testing.T) {
Flags: 0, Flags: 0,
Timeout: 0, Timeout: 0,
Netmask: 0xffffffff, Netmask: 0xffffffff,
AddressFamily: syscall.AF_INET, AddressFamily: unix.AF_INET,
Address: netutils.ParseIPSloppy("1.2.3.4"), Address: netutils.ParseIPSloppy("1.2.3.4"),
PEName: "", PEName: "",
}, },
@ -271,7 +272,7 @@ func Test_toIPVSService(t *testing.T) {
Flags: 0, Flags: 0,
Timeout: 0, Timeout: 0,
Netmask: 128, Netmask: 128,
AddressFamily: syscall.AF_INET6, AddressFamily: unix.AF_INET6,
Address: netutils.ParseIPSloppy("::0"), Address: netutils.ParseIPSloppy("::0"),
PEName: "", PEName: "",
}, },
@ -388,7 +389,7 @@ func Test_stringToProtocol(t *testing.T) {
"TCP", "UDP", "ICMP", "SCTP", "TCP", "UDP", "ICMP", "SCTP",
} }
expected := []uint16{ expected := []uint16{
uint16(syscall.IPPROTO_TCP), uint16(syscall.IPPROTO_UDP), uint16(0), uint16(syscall.IPPROTO_SCTP), uint16(unix.IPPROTO_TCP), uint16(unix.IPPROTO_UDP), uint16(0), uint16(unix.IPPROTO_SCTP),
} }
for i := range tests { for i := range tests {
got := stringToProtocol(tests[i]) got := stringToProtocol(tests[i])
@ -401,7 +402,7 @@ func Test_stringToProtocol(t *testing.T) {
func Test_protocolToString(t *testing.T) { func Test_protocolToString(t *testing.T) {
tests := []Protocol{ tests := []Protocol{
syscall.IPPROTO_TCP, syscall.IPPROTO_UDP, Protocol(0), syscall.IPPROTO_SCTP, unix.IPPROTO_TCP, unix.IPPROTO_UDP, Protocol(0), unix.IPPROTO_SCTP,
} }
expected := []string{ expected := []string{
"TCP", "UDP", "", "SCTP", "TCP", "UDP", "", "SCTP",