diff --git a/pkg/client/cluster.go b/pkg/client/cluster.go index d21b6f07a..aa721ba49 100644 --- a/pkg/client/cluster.go +++ b/pkg/client/cluster.go @@ -386,7 +386,7 @@ ClusterCreatOpts: /* * Docker Machine Special Configuration */ - if cluster.KubeAPI.Host == k3d.DefaultAPIHost && runtime == k3drt.Docker { + if (cluster.KubeAPI.Host == k3d.DefaultAPIHost || cluster.KubeAPI.Host == "0.0.0.0") && runtime == k3drt.Docker { // If the runtime is docker, attempt to use the docker host if runtime == k3drt.Docker { dockerHost := runtime.GetHost() diff --git a/pkg/client/kubeconfig_test.go b/pkg/client/kubeconfig_test.go new file mode 100644 index 000000000..aacca6fbd --- /dev/null +++ b/pkg/client/kubeconfig_test.go @@ -0,0 +1,38 @@ +/* +Copyright © 2020-2023 The k3d Author(s) + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. +*/ + +package client + +import ( + "testing" + + k3d "github.com/k3d-io/k3d/v5/pkg/types" +) + +func TestDefaultAPIHostIsLoopback(t *testing.T) { + // Verify that DefaultAPIHost is set to 127.0.0.1 (loopback) instead of 0.0.0.0 + // This ensures that kubeconfig files use a valid client-connectable address + expectedHost := "127.0.0.1" + if k3d.DefaultAPIHost != expectedHost { + t.Errorf("DefaultAPIHost should be '%s' for client connectivity, but got '%s'", expectedHost, k3d.DefaultAPIHost) + } +} diff --git a/pkg/client/registry.go b/pkg/client/registry.go index 9bbff86e5..18398d95d 100644 --- a/pkg/client/registry.go +++ b/pkg/client/registry.go @@ -312,7 +312,7 @@ func RegistryGenerateLocalRegistryHostingConfigMapYAML(ctx context.Context, runt } // if the host is now 0.0.0.0, check if we can set it to the IP of the docker-machine, if it's used - if host == k3d.DefaultAPIHost && runtime == runtimes.Docker { + if (host == k3d.DefaultAPIHost || host == "0.0.0.0") && runtime == runtimes.Docker { if gort.GOOS == "windows" || gort.GOOS == "darwin" { l.Log().Tracef("Running on %s: checking if it's using docker-machine", gort.GOOS) machineIP, err := runtime.(docker.Docker).GetDockerMachineIP() @@ -328,7 +328,7 @@ func RegistryGenerateLocalRegistryHostingConfigMapYAML(ctx context.Context, runt } // if host is still 0.0.0.0, use localhost instead - if host == k3d.DefaultAPIHost { + if host == k3d.DefaultAPIHost || host == "0.0.0.0" { host = "localhost" // we prefer localhost over 0.0.0.0 } diff --git a/pkg/types/defaults.go b/pkg/types/defaults.go index e343b3ef3..0f058b7fd 100644 --- a/pkg/types/defaults.go +++ b/pkg/types/defaults.go @@ -83,7 +83,7 @@ const DefaultKubeconfigPrefix = DefaultObjectNamePrefix + "-kubeconfig" const DefaultAPIPort = "6443" // DefaultAPIHost defines the default host (IP) for the Kubernetes API -const DefaultAPIHost = "0.0.0.0" +const DefaultAPIHost = "127.0.0.1" // GetDefaultObjectName prefixes the passed name with the default prefix func GetDefaultObjectName(name string) string {