Update go dependencies
This commit is contained in:
parent
060e449056
commit
4fb61c73d1
1192 changed files with 185874 additions and 302749 deletions
51
vendor/k8s.io/apimachinery/pkg/util/net/http.go
generated
vendored
51
vendor/k8s.io/apimachinery/pkg/util/net/http.go
generated
vendored
|
|
@ -150,13 +150,13 @@ func GetHTTPClient(req *http.Request) string {
|
|||
return "unknown"
|
||||
}
|
||||
|
||||
// Extracts and returns the clients IP from the given request.
|
||||
// Looks at X-Forwarded-For header, X-Real-Ip header and request.RemoteAddr in that order.
|
||||
// Returns nil if none of them are set or is set to an invalid value.
|
||||
func GetClientIP(req *http.Request) net.IP {
|
||||
// SourceIPs splits the comma separated X-Forwarded-For header or returns the X-Real-Ip header or req.RemoteAddr,
|
||||
// in that order, ignoring invalid IPs. It returns nil if all of these are empty or invalid.
|
||||
func SourceIPs(req *http.Request) []net.IP {
|
||||
hdr := req.Header
|
||||
// First check the X-Forwarded-For header for requests via proxy.
|
||||
hdrForwardedFor := hdr.Get("X-Forwarded-For")
|
||||
forwardedForIPs := []net.IP{}
|
||||
if hdrForwardedFor != "" {
|
||||
// X-Forwarded-For can be a csv of IPs in case of multiple proxies.
|
||||
// Use the first valid one.
|
||||
|
|
@ -164,17 +164,20 @@ func GetClientIP(req *http.Request) net.IP {
|
|||
for _, part := range parts {
|
||||
ip := net.ParseIP(strings.TrimSpace(part))
|
||||
if ip != nil {
|
||||
return ip
|
||||
forwardedForIPs = append(forwardedForIPs, ip)
|
||||
}
|
||||
}
|
||||
}
|
||||
if len(forwardedForIPs) > 0 {
|
||||
return forwardedForIPs
|
||||
}
|
||||
|
||||
// Try the X-Real-Ip header.
|
||||
hdrRealIp := hdr.Get("X-Real-Ip")
|
||||
if hdrRealIp != "" {
|
||||
ip := net.ParseIP(hdrRealIp)
|
||||
if ip != nil {
|
||||
return ip
|
||||
return []net.IP{ip}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -182,11 +185,43 @@ func GetClientIP(req *http.Request) net.IP {
|
|||
// Remote Address in Go's HTTP server is in the form host:port so we need to split that first.
|
||||
host, _, err := net.SplitHostPort(req.RemoteAddr)
|
||||
if err == nil {
|
||||
return net.ParseIP(host)
|
||||
if remoteIP := net.ParseIP(host); remoteIP != nil {
|
||||
return []net.IP{remoteIP}
|
||||
}
|
||||
}
|
||||
|
||||
// Fallback if Remote Address was just IP.
|
||||
return net.ParseIP(req.RemoteAddr)
|
||||
if remoteIP := net.ParseIP(req.RemoteAddr); remoteIP != nil {
|
||||
return []net.IP{remoteIP}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// Extracts and returns the clients IP from the given request.
|
||||
// Looks at X-Forwarded-For header, X-Real-Ip header and request.RemoteAddr in that order.
|
||||
// Returns nil if none of them are set or is set to an invalid value.
|
||||
func GetClientIP(req *http.Request) net.IP {
|
||||
ips := SourceIPs(req)
|
||||
if len(ips) == 0 {
|
||||
return nil
|
||||
}
|
||||
return ips[0]
|
||||
}
|
||||
|
||||
// Prepares the X-Forwarded-For header for another forwarding hop by appending the previous sender's
|
||||
// IP address to the X-Forwarded-For chain.
|
||||
func AppendForwardedForHeader(req *http.Request) {
|
||||
// Copied from net/http/httputil/reverseproxy.go:
|
||||
if clientIP, _, err := net.SplitHostPort(req.RemoteAddr); err == nil {
|
||||
// If we aren't the first proxy retain prior
|
||||
// X-Forwarded-For information as a comma+space
|
||||
// separated list and fold multiple headers into one.
|
||||
if prior, ok := req.Header["X-Forwarded-For"]; ok {
|
||||
clientIP = strings.Join(prior, ", ") + ", " + clientIP
|
||||
}
|
||||
req.Header.Set("X-Forwarded-For", clientIP)
|
||||
}
|
||||
}
|
||||
|
||||
var defaultProxyFuncPointer = fmt.Sprintf("%p", http.ProxyFromEnvironment)
|
||||
|
|
|
|||
209
vendor/k8s.io/apimachinery/pkg/util/net/interface.go
generated
vendored
209
vendor/k8s.io/apimachinery/pkg/util/net/interface.go
generated
vendored
|
|
@ -29,6 +29,13 @@ import (
|
|||
"github.com/golang/glog"
|
||||
)
|
||||
|
||||
type AddressFamily uint
|
||||
|
||||
const (
|
||||
familyIPv4 AddressFamily = 4
|
||||
familyIPv6 AddressFamily = 6
|
||||
)
|
||||
|
||||
type Route struct {
|
||||
Interface string
|
||||
Destination net.IP
|
||||
|
|
@ -36,6 +43,7 @@ type Route struct {
|
|||
// TODO: add more fields here if needed
|
||||
}
|
||||
|
||||
// getRoutes obtains the IPv4 routes, and filters out non-default routes.
|
||||
func getRoutes(input io.Reader) ([]Route, error) {
|
||||
routes := []Route{}
|
||||
if input == nil {
|
||||
|
|
@ -52,24 +60,30 @@ func getRoutes(input io.Reader) ([]Route, error) {
|
|||
continue
|
||||
}
|
||||
fields := strings.Fields(line)
|
||||
routes = append(routes, Route{})
|
||||
route := &routes[len(routes)-1]
|
||||
route.Interface = fields[0]
|
||||
ip, err := parseIP(fields[1])
|
||||
dest, err := parseHexToIPv4(fields[1])
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
route.Destination = ip
|
||||
ip, err = parseIP(fields[2])
|
||||
gw, err := parseHexToIPv4(fields[2])
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
route.Gateway = ip
|
||||
if !dest.Equal(net.IPv4zero) {
|
||||
continue
|
||||
}
|
||||
routes = append(routes, Route{
|
||||
Interface: fields[0],
|
||||
Destination: dest,
|
||||
Gateway: gw,
|
||||
})
|
||||
}
|
||||
return routes, nil
|
||||
}
|
||||
|
||||
func parseIP(str string) (net.IP, error) {
|
||||
// parseHexToIPv4 takes the hex IP address string from route file and converts it
|
||||
// from little endian to big endian for creation of a net.IP address.
|
||||
// a net.IP, using big endian ordering.
|
||||
func parseHexToIPv4(str string) (net.IP, error) {
|
||||
if str == "" {
|
||||
return nil, fmt.Errorf("input is nil")
|
||||
}
|
||||
|
|
@ -77,12 +91,10 @@ func parseIP(str string) (net.IP, error) {
|
|||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
//TODO add ipv6 support
|
||||
if len(bytes) != net.IPv4len {
|
||||
return nil, fmt.Errorf("only IPv4 is supported")
|
||||
return nil, fmt.Errorf("invalid IPv4 address in route")
|
||||
}
|
||||
bytes[0], bytes[1], bytes[2], bytes[3] = bytes[3], bytes[2], bytes[1], bytes[0]
|
||||
return net.IP(bytes), nil
|
||||
return net.IP([]byte{bytes[3], bytes[2], bytes[1], bytes[0]}), nil
|
||||
}
|
||||
|
||||
func isInterfaceUp(intf *net.Interface) bool {
|
||||
|
|
@ -96,10 +108,22 @@ func isInterfaceUp(intf *net.Interface) bool {
|
|||
return false
|
||||
}
|
||||
|
||||
//getFinalIP method receives all the IP addrs of a Interface
|
||||
//and returns a nil if the address is Loopback, Ipv6, link-local or nil.
|
||||
//It returns a valid IPv4 if an Ipv4 address is found in the array.
|
||||
func getFinalIP(addrs []net.Addr) (net.IP, error) {
|
||||
func isLoopbackOrPointToPoint(intf *net.Interface) bool {
|
||||
return intf.Flags&(net.FlagLoopback|net.FlagPointToPoint) != 0
|
||||
}
|
||||
|
||||
func inFamily(ip net.IP, expectedFamily AddressFamily) bool {
|
||||
ipFamily := familyIPv4
|
||||
if ip.To4() == nil {
|
||||
ipFamily = familyIPv6
|
||||
}
|
||||
return ipFamily == expectedFamily
|
||||
}
|
||||
|
||||
// getMatchingGlobalIP method checks all the IP addresses of a Interface looking
|
||||
// for a valid non-loopback/link-local address of the requested family and returns
|
||||
// it, if found.
|
||||
func getMatchingGlobalIP(addrs []net.Addr, family AddressFamily) (net.IP, error) {
|
||||
if len(addrs) > 0 {
|
||||
for i := range addrs {
|
||||
glog.V(4).Infof("Checking addr %s.", addrs[i].String())
|
||||
|
|
@ -107,17 +131,15 @@ func getFinalIP(addrs []net.Addr) (net.IP, error) {
|
|||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
//Only IPv4
|
||||
//TODO : add IPv6 support
|
||||
if ip.To4() != nil {
|
||||
if !ip.IsLoopback() && !ip.IsLinkLocalMulticast() && !ip.IsLinkLocalUnicast() {
|
||||
if inFamily(ip, family) {
|
||||
if ip.IsGlobalUnicast() {
|
||||
glog.V(4).Infof("IP found %v", ip)
|
||||
return ip, nil
|
||||
} else {
|
||||
glog.V(4).Infof("Loopback/link-local found %v", ip)
|
||||
glog.V(4).Infof("non-global IP found %v", ip)
|
||||
}
|
||||
} else {
|
||||
glog.V(4).Infof("%v is not a valid IPv4 address", ip)
|
||||
glog.V(4).Infof("%v is not an IPv%d address", ip, int(family))
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -125,7 +147,7 @@ func getFinalIP(addrs []net.Addr) (net.IP, error) {
|
|||
return nil, nil
|
||||
}
|
||||
|
||||
func getIPFromInterface(intfName string, nw networkInterfacer) (net.IP, error) {
|
||||
func getIPFromInterface(intfName string, forFamily AddressFamily, nw networkInterfacer) (net.IP, error) {
|
||||
intf, err := nw.InterfaceByName(intfName)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
|
|
@ -136,63 +158,77 @@ func getIPFromInterface(intfName string, nw networkInterfacer) (net.IP, error) {
|
|||
return nil, err
|
||||
}
|
||||
glog.V(4).Infof("Interface %q has %d addresses :%v.", intfName, len(addrs), addrs)
|
||||
finalIP, err := getFinalIP(addrs)
|
||||
matchingIP, err := getMatchingGlobalIP(addrs, forFamily)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if finalIP != nil {
|
||||
glog.V(4).Infof("valid IPv4 address for interface %q found as %v.", intfName, finalIP)
|
||||
return finalIP, nil
|
||||
if matchingIP != nil {
|
||||
glog.V(4).Infof("Found valid IPv%d address %v for interface %q.", int(forFamily), matchingIP, intfName)
|
||||
return matchingIP, nil
|
||||
}
|
||||
}
|
||||
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
func flagsSet(flags net.Flags, test net.Flags) bool {
|
||||
return flags&test != 0
|
||||
// memberOF tells if the IP is of the desired family. Used for checking interface addresses.
|
||||
func memberOf(ip net.IP, family AddressFamily) bool {
|
||||
if ip.To4() != nil {
|
||||
return family == familyIPv4
|
||||
} else {
|
||||
return family == familyIPv6
|
||||
}
|
||||
}
|
||||
|
||||
func flagsClear(flags net.Flags, test net.Flags) bool {
|
||||
return flags&test == 0
|
||||
}
|
||||
|
||||
func chooseHostInterfaceNativeGo() (net.IP, error) {
|
||||
intfs, err := net.Interfaces()
|
||||
// chooseIPFromHostInterfaces looks at all system interfaces, trying to find one that is up that
|
||||
// has a global unicast address (non-loopback, non-link local, non-point2point), and returns the IP.
|
||||
// Searches for IPv4 addresses, and then IPv6 addresses.
|
||||
func chooseIPFromHostInterfaces(nw networkInterfacer) (net.IP, error) {
|
||||
intfs, err := nw.Interfaces()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
i := 0
|
||||
var ip net.IP
|
||||
for i = range intfs {
|
||||
if flagsSet(intfs[i].Flags, net.FlagUp) && flagsClear(intfs[i].Flags, net.FlagLoopback|net.FlagPointToPoint) {
|
||||
addrs, err := intfs[i].Addrs()
|
||||
if len(intfs) == 0 {
|
||||
return nil, fmt.Errorf("no interfaces found on host.")
|
||||
}
|
||||
for _, family := range []AddressFamily{familyIPv4, familyIPv6} {
|
||||
glog.V(4).Infof("Looking for system interface with a global IPv%d address", uint(family))
|
||||
for _, intf := range intfs {
|
||||
if !isInterfaceUp(&intf) {
|
||||
glog.V(4).Infof("Skipping: down interface %q", intf.Name)
|
||||
continue
|
||||
}
|
||||
if isLoopbackOrPointToPoint(&intf) {
|
||||
glog.V(4).Infof("Skipping: LB or P2P interface %q", intf.Name)
|
||||
continue
|
||||
}
|
||||
addrs, err := nw.Addrs(&intf)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if len(addrs) > 0 {
|
||||
for _, addr := range addrs {
|
||||
if addrIP, _, err := net.ParseCIDR(addr.String()); err == nil {
|
||||
if addrIP.To4() != nil {
|
||||
ip = addrIP.To4()
|
||||
if !ip.IsLinkLocalMulticast() && !ip.IsLinkLocalUnicast() {
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
if len(addrs) == 0 {
|
||||
glog.V(4).Infof("Skipping: no addresses on interface %q", intf.Name)
|
||||
continue
|
||||
}
|
||||
for _, addr := range addrs {
|
||||
ip, _, err := net.ParseCIDR(addr.String())
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("Unable to parse CIDR for interface %q: %s", intf.Name, err)
|
||||
}
|
||||
if ip != nil {
|
||||
// This interface should suffice.
|
||||
break
|
||||
if !memberOf(ip, family) {
|
||||
glog.V(4).Infof("Skipping: no address family match for %q on interface %q.", ip, intf.Name)
|
||||
continue
|
||||
}
|
||||
// TODO: Decide if should open up to allow IPv6 LLAs in future.
|
||||
if !ip.IsGlobalUnicast() {
|
||||
glog.V(4).Infof("Skipping: non-global address %q on interface %q.", ip, intf.Name)
|
||||
continue
|
||||
}
|
||||
glog.V(4).Infof("Found global unicast address %q on interface %q.", ip, intf.Name)
|
||||
return ip, nil
|
||||
}
|
||||
}
|
||||
}
|
||||
if ip == nil {
|
||||
return nil, fmt.Errorf("no acceptable interface from host")
|
||||
}
|
||||
glog.V(4).Infof("Choosing interface %s (IP %v) as default", intfs[i].Name, ip)
|
||||
return ip, nil
|
||||
return nil, fmt.Errorf("no acceptable interface with global unicast address found on host")
|
||||
}
|
||||
|
||||
//ChooseHostInterface is a method used fetch an IP for a daemon.
|
||||
|
|
@ -200,39 +236,41 @@ func chooseHostInterfaceNativeGo() (net.IP, error) {
|
|||
//For a node with no internet connection ,it returns error
|
||||
//For a multi n/w interface node it returns the IP of the interface with gateway on it.
|
||||
func ChooseHostInterface() (net.IP, error) {
|
||||
var nw networkInterfacer = networkInterface{}
|
||||
inFile, err := os.Open("/proc/net/route")
|
||||
if err != nil {
|
||||
if os.IsNotExist(err) {
|
||||
return chooseHostInterfaceNativeGo()
|
||||
return chooseIPFromHostInterfaces(nw)
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
defer inFile.Close()
|
||||
var nw networkInterfacer = networkInterface{}
|
||||
return chooseHostInterfaceFromRoute(inFile, nw)
|
||||
}
|
||||
|
||||
// networkInterfacer defines an interface for several net library functions. Production
|
||||
// code will forward to net library functions, and unit tests will override the methods
|
||||
// for testing purposes.
|
||||
type networkInterfacer interface {
|
||||
InterfaceByName(intfName string) (*net.Interface, error)
|
||||
Addrs(intf *net.Interface) ([]net.Addr, error)
|
||||
Interfaces() ([]net.Interface, error)
|
||||
}
|
||||
|
||||
// networkInterface implements the networkInterfacer interface for production code, just
|
||||
// wrapping the underlying net library function calls.
|
||||
type networkInterface struct{}
|
||||
|
||||
func (_ networkInterface) InterfaceByName(intfName string) (*net.Interface, error) {
|
||||
intf, err := net.InterfaceByName(intfName)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return intf, nil
|
||||
return net.InterfaceByName(intfName)
|
||||
}
|
||||
|
||||
func (_ networkInterface) Addrs(intf *net.Interface) ([]net.Addr, error) {
|
||||
addrs, err := intf.Addrs()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return addrs, nil
|
||||
return intf.Addrs()
|
||||
}
|
||||
|
||||
func (_ networkInterface) Interfaces() ([]net.Interface, error) {
|
||||
return net.Interfaces()
|
||||
}
|
||||
|
||||
func chooseHostInterfaceFromRoute(inFile io.Reader, nw networkInterfacer) (net.IP, error) {
|
||||
|
|
@ -240,27 +278,30 @@ func chooseHostInterfaceFromRoute(inFile io.Reader, nw networkInterfacer) (net.I
|
|||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
zero := net.IP{0, 0, 0, 0}
|
||||
var finalIP net.IP
|
||||
for i := range routes {
|
||||
//find interface with gateway
|
||||
if routes[i].Destination.Equal(zero) {
|
||||
glog.V(4).Infof("Default route transits interface %q", routes[i].Interface)
|
||||
finalIP, err := getIPFromInterface(routes[i].Interface, nw)
|
||||
if len(routes) == 0 {
|
||||
return nil, fmt.Errorf("No default routes.")
|
||||
}
|
||||
// TODO: append IPv6 routes for processing - currently only have IPv4 routes
|
||||
for _, family := range []AddressFamily{familyIPv4, familyIPv6} {
|
||||
glog.V(4).Infof("Looking for default routes with IPv%d addresses", uint(family))
|
||||
for _, route := range routes {
|
||||
// TODO: When have IPv6 routes, filter here to speed up processing
|
||||
// if route.Family != family {
|
||||
// continue
|
||||
// }
|
||||
glog.V(4).Infof("Default route transits interface %q", route.Interface)
|
||||
finalIP, err := getIPFromInterface(route.Interface, family, nw)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if finalIP != nil {
|
||||
glog.V(4).Infof("Choosing IP %v ", finalIP)
|
||||
glog.V(4).Infof("Found active IP %v ", finalIP)
|
||||
return finalIP, nil
|
||||
}
|
||||
}
|
||||
}
|
||||
glog.V(4).Infof("No valid IP found")
|
||||
if finalIP == nil {
|
||||
return nil, fmt.Errorf("Unable to select an IP.")
|
||||
}
|
||||
return nil, nil
|
||||
glog.V(4).Infof("No active IP found by looking at default routes")
|
||||
return nil, fmt.Errorf("unable to select an IP from default routes.")
|
||||
}
|
||||
|
||||
// If bind-address is usable, return it directly
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue