From 894af0607759e71da23208ca856ff9771c95709f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=9F=A5=E8=B0=A8?= Date: Thu, 25 Oct 2018 15:35:49 +0800 Subject: [PATCH 1/2] bugfix: get ip address by `hostname -i` get ip address by `hostname -i`, in alibaba server, these is no default route, so when enable pouch-cri, it can't ip address by default route to initialize the cri of pouchd. Signed-off-by: Rudy Zhang --- pkg/netutils/interface.go | 39 +++++++++++++++++++++++++++++++++++---- 1 file changed, 35 insertions(+), 4 deletions(-) diff --git a/pkg/netutils/interface.go b/pkg/netutils/interface.go index 4d27ba80c..d4737a799 100644 --- a/pkg/netutils/interface.go +++ b/pkg/netutils/interface.go @@ -7,10 +7,13 @@ import ( "io" "net" "os" - "strings" + "github.com/alibaba/pouch/pkg/exec" + "github.com/sirupsen/logrus" + "github.com/golang/glog" + "github.com/pkg/errors" ) // AddressFamily is the ip address type. @@ -362,6 +365,28 @@ func chooseHostInterfaceFromRoute(routes []Route, nw networkInterfacer) (net.IP, return nil, fmt.Errorf("unable to select an IP from default routes") } +func getAddrByHostname() (net.IP, error) { + exit, stdout, stderr, err := exec.Run(0, "hostname", "-i") + if err != nil { + return nil, errors.Wrapf(err, "failed to get ip address, stdout: (%s), stderr: (%s), exit: (%s)", + stdout, stderr, exit) + } + + if exit != 0 { + return nil, errors.Errorf("failed to get ip address, stdout: (%s), stderr: (%s), exit: (%s)", + stdout, stderr, exit) + } + + addr := net.ParseIP(strings.TrimSpace(stdout)) + if addr == nil { + return nil, errors.Errorf("failed to get ip address, invalid format: (%s)", stdout) + } + + logrus.Infof("get host ip address: (%s)", stdout) + + return addr, nil +} + // ChooseBindAddress is used to choose an Host IP address for binding. // If bind-address is usable, return it directly // If bind-address is not usable (unset, 0.0.0.0, or loopback), we will use the host's default @@ -369,10 +394,16 @@ func chooseHostInterfaceFromRoute(routes []Route, nw networkInterfacer) (net.IP, func ChooseBindAddress(bindAddress net.IP) (net.IP, error) { if bindAddress == nil || bindAddress.IsUnspecified() || bindAddress.IsLoopback() { hostIP, err := ChooseHostInterface() - if err != nil { - return nil, err + if err == nil { + bindAddress = hostIP + } else { + hostIP, err = getAddrByHostname() + if err != nil { + return nil, err + } + bindAddress = hostIP } - bindAddress = hostIP + } return bindAddress, nil } From 180aca8126b67171d63f781302c8fb0d6e3edddd Mon Sep 17 00:00:00 2001 From: zhuangqh Date: Wed, 31 Oct 2018 10:15:03 +0800 Subject: [PATCH 2/2] remove redundant log Signed-off-by: zhuangqh --- pkg/netutils/interface.go | 3 --- 1 file changed, 3 deletions(-) diff --git a/pkg/netutils/interface.go b/pkg/netutils/interface.go index d4737a799..9f7aa7e35 100644 --- a/pkg/netutils/interface.go +++ b/pkg/netutils/interface.go @@ -10,7 +10,6 @@ import ( "strings" "github.com/alibaba/pouch/pkg/exec" - "github.com/sirupsen/logrus" "github.com/golang/glog" "github.com/pkg/errors" @@ -382,8 +381,6 @@ func getAddrByHostname() (net.IP, error) { return nil, errors.Errorf("failed to get ip address, invalid format: (%s)", stdout) } - logrus.Infof("get host ip address: (%s)", stdout) - return addr, nil }