-
Notifications
You must be signed in to change notification settings - Fork 40k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support ChooseHostInterface on Mac OS X #5497
Closed
Closed
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -378,68 +378,50 @@ func getIPFromInterface(intfName string, nw networkInterfacer) (net.IP, error) { | |
return nil, nil | ||
} | ||
|
||
func flagsSet(flags net.Flags, test net.Flags) bool { | ||
return flags&test != 0 | ||
} | ||
|
||
func flagsClear(flags net.Flags, test net.Flags) bool { | ||
return flags&test == 0 | ||
} | ||
|
||
func chooseHostInterfaceNativeGo() (net.IP, error) { | ||
intfs, err := net.Interfaces() | ||
//chooseHostInterfaceNativeGo is a method used to fetch an IP for a daemon. | ||
//It iterates over the interfaces returned by Interfaces() method | ||
//and picks the first interface which is not Loopback. | ||
//This method may pick docker or other bridges | ||
func chooseHostInterfaceNativeGo(nw networkInterfacer) (net.IP, error) { | ||
intfs, err := nw.Interfaces() | ||
if err != nil { | ||
return nil, err | ||
} | ||
i := 0 | ||
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 err != nil { | ||
return nil, err | ||
} | ||
if len(addrs) > 0 { | ||
// This interface should suffice. | ||
break | ||
} | ||
finalIP, err := getIPFromInterface(intfs[i].Name, nw) | ||
if err != nil { | ||
return nil, err | ||
} | ||
if finalIP != nil { | ||
glog.V(4).Infof("Choosing IP %v ", finalIP) | ||
return finalIP, nil | ||
} | ||
} | ||
if i == len(intfs) { | ||
return nil, err | ||
} | ||
glog.V(2).Infof("Choosing interface %s for from-host portals", intfs[i].Name) | ||
addrs, err := intfs[i].Addrs() | ||
if err != nil { | ||
return nil, err | ||
} | ||
glog.V(2).Infof("Interface %s = %s", intfs[i].Name, addrs[0].String()) | ||
ip, _, err := net.ParseCIDR(addrs[0].String()) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return ip, nil | ||
return nil, nil | ||
} | ||
|
||
//ChooseHostInterface is a method used fetch an IP for a daemon. | ||
//It uses data from /proc/net/route file. | ||
//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 chooseHostInterfaceNativeGo(nw) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. log this happening - it's important in prod |
||
} | ||
return nil, err | ||
} | ||
defer inFile.Close() | ||
var nw networkInterfacer = networkInterface{} | ||
return chooseHostInterfaceFromRoute(inFile, nw) | ||
} | ||
|
||
type networkInterfacer interface { | ||
InterfaceByName(intfName string) (*net.Interface, error) | ||
Addrs(intf *net.Interface) ([]net.Addr, error) | ||
Interfaces() ([]net.Interface, error) | ||
} | ||
|
||
type networkInterface struct{} | ||
|
@@ -460,6 +442,14 @@ func (_ networkInterface) Addrs(intf *net.Interface) ([]net.Addr, error) { | |
return addrs, nil | ||
} | ||
|
||
func (_ networkInterface) Interfaces() ([]net.Interface, error) { | ||
intfs, err := net.Interfaces() | ||
if err != nil { | ||
return nil, err | ||
} | ||
return intfs, nil | ||
} | ||
|
||
func chooseHostInterfaceFromRoute(inFile io.Reader, nw networkInterfacer) (net.IP, error) { | ||
routes, err := getRoutes(inFile) | ||
if err != nil { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this significantly better than just returning 127.0.0.1 ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it is.
This was for supporting non Linux based systems , like mac and maybe windows.
should 127.0.0.1 be default if there is no other interface on the device ?I thought not.(you have replied it wont work in another PR)
As of now , if a developer is is using his system without any internet connection he cannot start proxy and apiserver .So he has to have a interface , a fake one atleast.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
127.0.0.1 will not work for actual use. It might be enough for tests to pass. Let me run some manual tests tomorrow.
@quinton-hoole who is also picking up a bunch of networking related topics (FYI, no action required yet :)