diff --git a/service/node.go b/service/node.go index a8d8a6d..45d67c7 100644 --- a/service/node.go +++ b/service/node.go @@ -1522,7 +1522,7 @@ func (s *service) addNodeInformationIntoArray(ctx context.Context, array *Storag log.Infof("Node %s does not have FC or iSCSI initiators and can only be used for NFS exports", s.opts.NodeName) } - nodeIps, err := utils.GetHostIP(ctx) + nodeIps, err := utils.GetHostIP() if err != nil { return status.Error(codes.Unknown, utils.GetMessageWithRunID(rid, "Unable to get node IP. Error: %v", err)) } diff --git a/service/utils/emcutils.go b/service/utils/emcutils.go index 70e491d..5fdadc4 100644 --- a/service/utils/emcutils.go +++ b/service/utils/emcutils.go @@ -145,8 +145,7 @@ func GetFCInitiators(ctx context.Context) ([]string, error) { } // GetHostIP - Utility method to extract Host IP -func GetHostIP(ctx context.Context) ([]string, error) { - log := GetRunidLogger(ctx) +func GetHostIP() ([]string, error) { cmd := exec.Command("hostname", "-I") cmdOutput := &bytes.Buffer{} cmd.Stdout = cmdOutput @@ -162,31 +161,21 @@ func GetHostIP(ctx context.Context) ([]string, error) { } output := string(cmdOutput.Bytes()) ips := strings.Split(strings.TrimSpace(output), " ") - log.Debugf("Host IPs: %v", ips) hostname, err := os.Hostname() - log.Debugf("Hostname: %v", hostname) if err != nil { return nil, err } var lookupIps []string for _, ip := range ips { - if !strings.Contains(ip, ":") { - lookupResp, err := net.LookupAddr(ip) - log.Debugf("lookupResp: %v for ip: %v", lookupResp, ip) - if err == nil { - for _, resp := range lookupResp { - log.Debugf("resp: %v, hostname: %v", resp, hostname) - if strings.Contains(resp, hostname) { - lookupIps = append(lookupIps, ip) - } - } - } + lookupResp, err := net.LookupAddr(ip) + if err == nil && strings.Contains(lookupResp[0], hostname) { + lookupIps = append(lookupIps, ip) } } if len(lookupIps) == 0 { - return nil, errors.New("host ip not found") + lookupIps = append(lookupIps, ips[0]) } return lookupIps, nil }