-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Switched from shell commands to netlink commands
- Loading branch information
Showing
469 changed files
with
232,609 additions
and
90 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,18 @@ | ||
module github.com/darxkies/virtual-ip | ||
|
||
require ( | ||
github.com/armon/go-metrics v0.0.0-20180917152333-f0300d1749da // indirect | ||
github.com/armon/go-metrics v0.0.0-20180917152333-f0300d1749da | ||
github.com/cespare/reflex v0.2.0 // indirect | ||
github.com/fsnotify/fsnotify v1.4.7 // indirect | ||
github.com/hashicorp/go-immutable-radix v1.0.0 // indirect | ||
github.com/hashicorp/go-msgpack v0.0.0-20150518234257-fa3f63826f7c // indirect | ||
github.com/hashicorp/go-msgpack v0.0.0-20150518234257-fa3f63826f7c | ||
github.com/hashicorp/raft v1.0.0 | ||
github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51 // indirect | ||
github.com/kr/pty v1.1.3 // indirect | ||
github.com/ogier/pflag v0.0.1 // indirect | ||
github.com/pkg/errors v0.8.0 | ||
github.com/sirupsen/logrus v1.2.0 | ||
golang.org/x/sys v0.0.0-20181213200352-4d1cda033e06 // indirect | ||
github.com/vishvananda/netlink v1.0.0 | ||
github.com/vishvananda/netns v0.0.0-20180720170159-13995c7128cc | ||
golang.org/x/sys v0.0.0-20181213200352-4d1cda033e06 | ||
) |
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 was deleted.
Oops, something went wrong.
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 |
---|---|---|
@@ -0,0 +1,92 @@ | ||
package pkg | ||
|
||
import ( | ||
"github.com/pkg/errors" | ||
"github.com/vishvananda/netlink" | ||
) | ||
|
||
type NetworkConfigurator interface { | ||
AddIP() error | ||
DeleteIP() error | ||
IsSet() (bool, error) | ||
} | ||
|
||
type NetlinkNetworkConfigurator struct { | ||
address *netlink.Addr | ||
link netlink.Link | ||
} | ||
|
||
func NewNetlinkNetworkConfigurator(_address, _interface string) (result NetlinkNetworkConfigurator, error error) { | ||
result = NetlinkNetworkConfigurator{} | ||
|
||
result.address, error = netlink.ParseAddr(_address + "/32") | ||
if error != nil { | ||
error = errors.Wrapf(error, "could not parse address '%s'", _address) | ||
|
||
return | ||
} | ||
|
||
result.link, error = netlink.LinkByName(_interface) | ||
if error != nil { | ||
error = errors.Wrapf(error, "could not get link for interface '%s'", _interface) | ||
|
||
return | ||
} | ||
|
||
return | ||
} | ||
|
||
func (configurator NetlinkNetworkConfigurator) AddIP() error { | ||
result, error := configurator.IsSet() | ||
if error != nil { | ||
return errors.Wrap(error, "ip check in AddIP failed") | ||
} | ||
|
||
// Already set | ||
if result { | ||
return nil | ||
} | ||
|
||
if error = netlink.AddrAdd(configurator.link, configurator.address); error != nil { | ||
return errors.Wrap(error, "could not add ip") | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (configurator NetlinkNetworkConfigurator) DeleteIP() error { | ||
result, error := configurator.IsSet() | ||
if error != nil { | ||
return errors.Wrap(error, "ip check in DeleteIP failed") | ||
} | ||
|
||
// Nothing to delete | ||
if !result { | ||
return nil | ||
} | ||
|
||
if error = netlink.AddrDel(configurator.link, configurator.address); error != nil { | ||
return errors.Wrap(error, "could not delete ip") | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (configurator NetlinkNetworkConfigurator) IsSet() (result bool, error error) { | ||
var addresses []netlink.Addr | ||
|
||
addresses, error = netlink.AddrList(configurator.link, 0) | ||
if error != nil { | ||
error = errors.Wrap(error, "could not list addresses") | ||
|
||
return | ||
} | ||
|
||
for _, address := range addresses { | ||
if address.Equal(*configurator.address) { | ||
return true, nil | ||
} | ||
} | ||
|
||
return false, nil | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.