Skip to content

Commit

Permalink
getHostIP: do not emit confusing warning that doesn't affect usability
Browse files Browse the repository at this point in the history
  • Loading branch information
iwilltry42 committed Nov 23, 2021
1 parent 7bde77c commit d78ef48
Showing 1 changed file with 10 additions and 9 deletions.
19 changes: 10 additions & 9 deletions pkg/client/host.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,15 +84,15 @@ func GetHostIP(ctx context.Context, runtime runtimes.Runtime, cluster *k3d.Clust
return ip, nil
}

l.Log().Warnf("failed to resolve 'host.docker.internal' from inside the k3d-tools node: %v", err)
l.Log().Debugf("[GetHostIP on Docker Desktop] failed to resolve 'host.docker.internal' from inside the k3d-tools node: %v", err)

}

l.Log().Infof("HostIP: using network gateway...")
ip, err := runtime.GetHostIP(ctx, cluster.Network.Name)
if err != nil {
return nil, fmt.Errorf("runtime failed to get host IP: %w", err)
}
l.Log().Infof("HostIP: using network gateway %s address", ip)

return ip, nil

Expand All @@ -105,20 +105,22 @@ func GetHostIP(ctx context.Context, runtime runtimes.Runtime, cluster *k3d.Clust

func resolveHostnameFromInside(ctx context.Context, rtime runtimes.Runtime, node *k3d.Node, hostname string, cmd ResolveHostCmd) (net.IP, error) {

errPrefix := fmt.Errorf("error resolving hostname %s from inside node %s", hostname, node.Name)

logreader, execErr := rtime.ExecInNodeGetLogs(ctx, node, []string{"sh", "-c", fmt.Sprintf(cmd.Cmd, hostname)})

if logreader == nil {
if execErr != nil {
return nil, execErr
return nil, fmt.Errorf("%v: %w", errPrefix, execErr)
}
return nil, fmt.Errorf("Failed to get logs from exec process")
return nil, fmt.Errorf("%w: failed to get logs from exec process", errPrefix)
}

submatches := map[string]string{}
scanner := bufio.NewScanner(logreader)
if scanner == nil {
if execErr != nil {
return nil, execErr
return nil, fmt.Errorf("%v: %w", errPrefix, execErr)
}
return nil, fmt.Errorf("Failed to scan logs for host IP: Could not create scanner from logreader")
}
Expand All @@ -132,19 +134,18 @@ func resolveHostnameFromInside(ctx context.Context, rtime runtimes.Runtime, node
if len(match) == 0 {
continue
}
l.Log().Tracef("-> Match(es): '%+v'", match)
submatches = util.MapSubexpNames(cmd.LogMatcher.SubexpNames(), match)
l.Log().Tracef(" -> Submatch(es): %+v", submatches)
l.Log().Tracef("-> Match(es): '%+v' -> Submatch(es): %+v", match, submatches)
break
}
if _, ok := submatches["ip"]; !ok {
if execErr != nil {
l.Log().Errorln(execErr)
}
return nil, fmt.Errorf("Failed to read address for '%s' from command output", hostname)
return nil, fmt.Errorf("%w: failed to read address for '%s' from command output", errPrefix, hostname)
}

l.Log().Debugf("Hostname '%s' -> Address '%s'", hostname, submatches["ip"])
l.Log().Debugf("Hostname '%s' resolved to address '%s' inside node %s", hostname, submatches["ip"], node.Name)

return net.ParseIP(submatches["ip"]), nil

Expand Down

0 comments on commit d78ef48

Please sign in to comment.