From 163b5ee9a68f063f8f5029ff854c64b5ea23fa2f Mon Sep 17 00:00:00 2001 From: Alex O'Regan Date: Mon, 22 Jul 2024 16:17:47 +0100 Subject: [PATCH] rework error handling in node.IP() --- pkg/cluster/internal/providers/docker/node.go | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/pkg/cluster/internal/providers/docker/node.go b/pkg/cluster/internal/providers/docker/node.go index 0e545a51d9..5003f5db0b 100644 --- a/pkg/cluster/internal/providers/docker/node.go +++ b/pkg/cluster/internal/providers/docker/node.go @@ -20,6 +20,7 @@ import ( "context" "fmt" "io" + "net" "strings" "sigs.k8s.io/kind/pkg/errors" @@ -53,9 +54,12 @@ func (n *node) Role() (string, error) { func (n *node) IP() (ipv4 string, ipv6 string, err error) { // Give the node a chance to indicate its own canonical IPs. output, err := exec.Output(exec.Command("docker", "exec", n.name, "/get-ips.sh")) - if err == nil { - ips := strings.Split(strings.TrimSpace(string(output)), " ") - fmt.Printf("Node %v provided IPs: %v\n", n.name, ips) + if err != nil { + return "", "", errors.Wrap(err, fmt.Sprintf("get-ips.sh failed. Output: %s", output)) + } + + ips := strings.Split(strings.TrimSpace(string(output)), " ") + if net.ParseIP(ips[0]) != nil && net.ParseIP(ips[1]) != nil { return ips[0], ips[1], nil } @@ -71,7 +75,7 @@ func (n *node) IP() (ipv4 string, ipv6 string, err error) { if len(lines) != 1 { return "", "", errors.Errorf("file should only be one line, got %d lines", len(lines)) } - ips := strings.Split(lines[0], ",") + ips = strings.Split(lines[0], ",") if len(ips) != 2 { return "", "", errors.Errorf("container addresses should have 2 values, got %d values", len(ips)) }