From 1852589dd7c1684d98f3c386ff1b3825891ac732 Mon Sep 17 00:00:00 2001 From: Kebe Date: Wed, 6 Nov 2024 10:01:38 +0800 Subject: [PATCH] Setting the default hostIP for `get kubeconfig` Update pkg/cluster/internal/providers/podman/provider.go Co-authored-by: Antonio Ojea --- pkg/cluster/internal/providers/podman/provider.go | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/pkg/cluster/internal/providers/podman/provider.go b/pkg/cluster/internal/providers/podman/provider.go index 856b07b046..fa311617d9 100644 --- a/pkg/cluster/internal/providers/podman/provider.go +++ b/pkg/cluster/internal/providers/podman/provider.go @@ -171,6 +171,15 @@ func (p *provider) DeleteNodes(n []nodes.Node) error { return deleteVolumes(nodeVolumes) } +// getHostIPOrDefault defaults HostIP to localhost if is not set +// xref: https://github.com/kubernetes-sigs/kind/issues/3777 +func getHostIPOrDefault(hostIP string) string { + if hostIP == "" { + return "127.0.0.1" + } + return hostIP +} + // GetAPIServerEndpoint is part of the providers.Provider interface func (p *provider) GetAPIServerEndpoint(cluster string) (string, error) { // locate the node that hosts this @@ -266,7 +275,7 @@ func (p *provider) GetAPIServerEndpoint(cluster string) (string, error) { } for _, pm := range v { if containerPort == common.APIServerInternalPort && protocol == "tcp" { - return net.JoinHostPort(pm.HostIP, pm.HostPort), nil + return net.JoinHostPort(getHostIPOrDefault(pm.HostIP), pm.HostPort), nil } } } @@ -278,7 +287,7 @@ func (p *provider) GetAPIServerEndpoint(cluster string) (string, error) { } for _, pm := range portMappings19 { if pm.ContainerPort == common.APIServerInternalPort && pm.Protocol == "tcp" { - return net.JoinHostPort(pm.HostIP, strconv.Itoa(int(pm.HostPort))), nil + return net.JoinHostPort(getHostIPOrDefault(pm.HostIP), strconv.Itoa(int(pm.HostPort))), nil } }