Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pkg/client/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
38 changes: 38 additions & 0 deletions pkg/client/kubeconfig_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
}
4 changes: 2 additions & 2 deletions pkg/client/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/types/defaults.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down