Skip to content

Commit

Permalink
fix netlink.AddrAdd file exist error.
Browse files Browse the repository at this point in the history
  • Loading branch information
jolestar committed Jul 13, 2017
1 parent fe308a9 commit 46e7d72
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
2 changes: 2 additions & 0 deletions hostnic/hostnic.go
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,7 @@ func getOrAllocateNicAsGateway(nicProvider provider.NicProvider, containernic *p
deleteNic(gateway.ID, nicProvider)
return nil, err
}
//TODO refactor to use pkg.ConfigureIface
iface, err := pkg.LinkByMacAddr(gateway.HardwareAddr)
if err != nil {
logger.Error("LinkByMacAddr err %s, delete Nic %s", err.Error(), gateway.ID)
Expand All @@ -226,6 +227,7 @@ func getOrAllocateNicAsGateway(nicProvider provider.NicProvider, containernic *p
return nil, err
}
//start to configure ip
pkg.ClearLinkAddr(iface)
addr := &netlink.Addr{IPNet: &net.IPNet{IP: net.ParseIP(gateway.Address), Mask: ipNet.Mask}, Label: ""}
if err := netlink.AddrAdd(iface, addr); err != nil {
logger.Error("AddrAdd err %s, delete Nic %s", err.Error(), gateway.ID)
Expand Down
18 changes: 17 additions & 1 deletion pkg/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ import (
"encoding/json"
"errors"
"io/ioutil"
"syscall"
"github.com/yunify/qingcloud-sdk-go/logger"
)

func StringPtr(str string) *string {
Expand All @@ -48,7 +50,7 @@ func ConfigureIface(ifName string, res *current.Result) error {
if err := netlink.LinkSetDown(link); err != nil {
return fmt.Errorf("failed to set link down: %v", err)
}

ClearLinkAddr(link)
var v4gw, v6gw net.IP
for _, ipc := range res.IPs {
if int(ipc.Interface) >= len(res.Interfaces) || res.Interfaces[ipc.Interface].Name != ifName {
Expand Down Expand Up @@ -128,4 +130,18 @@ func LoadNetConfFromFile(file string) (*NetConf, error){
return nil, err
}
return LoadNetConf(b)
}

func ClearLinkAddr(iface netlink.Link){
//clear old addr. os possible bind ip to nic before hostnic.
addrs, err := netlink.AddrList(iface, syscall.AF_INET)
if err == nil && len(addrs) >0 {
logger.Info("Del Nic exists addrs: %+v, Nic %s", addrs, iface.Attrs().HardwareAddr)
for _,addr := range addrs {
err := netlink.AddrDel(iface, &addr)
if err != nil {
logger.Error("AddrDel err %s addr:%+v, Nic %s", err.Error(), addr, iface.Attrs().HardwareAddr)
}
}
}
}

0 comments on commit 46e7d72

Please sign in to comment.