This repository has been archived by the owner on Oct 7, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 30
Add checks to quit if DefaultInterface address can't be resolved #133
Merged
Merged
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
ba5329e
Added checks to quit if DefaultInterface address can't be resolved
Mierdin 88c868d
No, I did not compile before the last commit. I had to go run an erra…
Mierdin e4bffad
Updated start-containers to respect slashes in branch name
Mierdin 3c9c509
Use underscore instead of hyphen in docker image tag
Mierdin 766a1af
Updated rabbitmq port in start-containers.sh
Mierdin 0066021
Fix bug with returning nil err all the time
Mierdin 5fb6243
Merge branch 'master' into fix/nil-defaultinterface
Mierdin 82f2be5
Moved defaultinterface stuff out of comms
Mierdin 3bc6930
Centralized as much logic as possible
Mierdin 419cfe4
Add comment on new function
Mierdin 7dec374
Simplified GetDefaultInterfaceIP
Mierdin dcaad51
Fixed erroneous spelling of function name
Mierdin c21de2d
Use net.InterfaceByName instead of reinventing that wheel
Mierdin b3de4cc
Merge branch 'master' into fix/nil-defaultinterface
Mierdin 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
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 |
---|---|---|
|
@@ -10,32 +10,75 @@ package hostresources | |
|
||
import ( | ||
"net" | ||
|
||
"github.com/pkg/errors" | ||
) | ||
|
||
// GetIPOfInt will iterate over all addresses for the given network interface, but will return only | ||
// GetDefaultInterfaceIP determines the appropriate IP address to use for either the server or agent | ||
// based on configuration parameters passed in as arguments | ||
// | ||
// The server uses this address to inform the agents of the URL they should use to download assets | ||
// | ||
// The agents use this address so that the server knows how to orchestrate tests. | ||
// (i.e. This agent publishes it's default address, and the server instructs other agents to target it in tests) | ||
func GetDefaultInterfaceIP(ifname, ipAddrOverride string) (string, error) { | ||
if ipAddrOverride != "" { | ||
return ipAddrOverride, nil | ||
} | ||
return getIPOfInt(ifname) | ||
} | ||
|
||
// getIPOfInt will get the first usable IP address on a network interface | ||
// | ||
// TODO(mierdin): Need to handle IPv6 here | ||
func getIPOfInt(ifname string) (string, error) { | ||
|
||
iface, err := net.InterfaceByName(ifname) | ||
if err != nil { | ||
return "", errors.Wrap(err, "Specified network interface not found") | ||
} | ||
|
||
addrs, err := iface.Addrs() | ||
if err != nil { | ||
return "", errors.Wrap(err, "Failed to retrieve addresses from network interface") | ||
} | ||
|
||
// Iterate over all the addresses and return the first one we find | ||
for _, addr := range addrs { | ||
if ipnet, ok := addr.(*net.IPNet); ok { | ||
if ipnet.IP.To4() != nil { | ||
return ipnet.IP.To4().String(), nil | ||
} | ||
} | ||
} | ||
|
||
return "", errors.New("No DefaultInterface address found") | ||
} | ||
|
||
// getIPOfInt will iterate over all addresses for the given network interface, but will return only | ||
// the first one it finds. TODO(mierdin): This has obvious drawbacks, particularly with IPv6. Need to figure out a better way. | ||
func GetIPOfInt(ifname string) net.IP { | ||
func oldgetIPOfInt(ifname string) (string, error) { | ||
interfaces, err := net.Interfaces() | ||
if err != nil { | ||
panic(err) | ||
return "", err | ||
} | ||
|
||
for _, iface := range interfaces { | ||
if iface.Name == ifname { | ||
|
||
addrs, err := iface.Addrs() | ||
if err != nil { | ||
panic(err) | ||
return "", err | ||
} | ||
for _, addr := range addrs { | ||
if ipnet, ok := addr.(*net.IPNet); ok { | ||
if ipnet.IP.To4() != nil { | ||
return ipnet.IP | ||
return ipnet.IP.To4().String(), nil | ||
} | ||
|
||
} | ||
} | ||
} | ||
} | ||
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. Looks like 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. Damn - and it looks like this has existed for some time. Can't believe I missed that! 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. Cool - implemented this and got rid of some of my wheel reinvention in c21de2d |
||
return nil | ||
return "", errors.New("No DefaultInterface address found") | ||
} |
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.
I would make this
ListenForAgent(_ assetProvider, assetURLPrefix string) error
instead of putting the description in the comments. Although it will be a bit inconsistent with the rest of the interface definition.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.
True, and I think we already discussed this in a previous review (so that's on me). I opened #137 to do this in a future PR