diff --git a/cmd/create/createCluster.go b/cmd/create/createCluster.go index 8bff7773..89012ab0 100644 --- a/cmd/create/createCluster.go +++ b/cmd/create/createCluster.go @@ -170,6 +170,9 @@ func parseCreateClusterCmd(cmd *cobra.Command, args []string, createClusterOpts network.Name = networkName network.External = true } + if networkName == "host" && (masterCount+workerCount) > 1 { + log.Fatalln("Can only run a single node in hostnetwork mode") + } // --secret secret, err := cmd.Flags().GetString("secret") diff --git a/pkg/runtimes/docker/network.go b/pkg/runtimes/docker/network.go index e6e67286..9f8176b0 100644 --- a/pkg/runtimes/docker/network.go +++ b/pkg/runtimes/docker/network.go @@ -93,3 +93,14 @@ func (d Docker) DeleteNetwork(ID string) error { // (3) delete network return docker.NetworkRemove(ctx, ID) } + +// GetNetwork gets information about a network by its ID +func GetNetwork(ID string) (types.NetworkResource, error) { + ctx := context.Background() + docker, err := client.NewClientWithOpts(client.FromEnv, client.WithAPIVersionNegotiation()) + if err != nil { + log.Errorln("Failed to create docker client") + return types.NetworkResource{}, err + } + return docker.NetworkInspect(ctx, ID, types.NetworkInspectOptions{}) +} diff --git a/pkg/runtimes/docker/translate.go b/pkg/runtimes/docker/translate.go index 8bb5cd4d..de010eeb 100644 --- a/pkg/runtimes/docker/translate.go +++ b/pkg/runtimes/docker/translate.go @@ -86,11 +86,17 @@ func TranslateNodeToContainer(node *k3d.Node) (*NodeInDocker, error) { } containerConfig.ExposedPorts = exposedPorts hostConfig.PortBindings = portBindings - /* Network */ networkingConfig.EndpointsConfig = map[string]*network.EndpointSettings{ node.Network: {}, } + netInfo, err := GetNetwork(node.Network) + if err != nil { + log.Warnln("Failed to get network information") + log.Warnln(err) + } else if netInfo.Driver == "host" { + hostConfig.NetworkMode = "host" + } return &NodeInDocker{ ContainerConfig: containerConfig, diff --git a/tests/test_basic.sh b/tests/test_basic.sh index f4311e9d..d887882d 100755 --- a/tests/test_basic.sh +++ b/tests/test_basic.sh @@ -7,8 +7,8 @@ CURR_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )" source "$CURR_DIR/common.sh" info "Creating two clusters..." -$EXE --verbose create cluster c1 --wait --timeout 60 --api-port 6443 || failed "could not create cluster c1" -$EXE --verbose create cluster c2 --wait --timeout 60 --api-port 6444 || failed "could not create cluster c2" +$EXE --verbose create cluster c1 --wait --timeout 60s --api-port 6443 || failed "could not create cluster c1" +$EXE --verbose create cluster c2 --wait --timeout 60s --api-port 6444 || failed "could not create cluster c2" info "Checking we have access to both clusters..." check_k3d_clusters "c1" "c2" || failed "error checking cluster" diff --git a/tests/test_multi_master.sh b/tests/test_multi_master.sh index 2fe4f0d2..256c3d73 100755 --- a/tests/test_multi_master.sh +++ b/tests/test_multi_master.sh @@ -7,7 +7,7 @@ CURR_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )" source "$CURR_DIR/common.sh" info "Creating cluster multimaster..." -$EXE create cluster "multimaster" --masters 3 --api-port 6443 --wait --timeout 360 || failed "could not create cluster multimaster" +$EXE --verbose create cluster "multimaster" --masters 3 --api-port 6443 --wait --timeout 360s || failed "could not create cluster multimaster" info "Checking we have access to the cluster..." check_k3d_clusters "multimaster" || failed "error checking cluster"