Skip to content

Commit 696e2a9

Browse files
committed
hack/local-up-karmada.sh supports WSL2
added log when getting ip address on wsl2 environment replaced special character that is only available on macos with =; verify ip address for WSL2 environment Signed-off-by: Yifan <[email protected]>
1 parent a2c2057 commit 696e2a9

File tree

2 files changed

+35
-4
lines changed

2 files changed

+35
-4
lines changed

hack/setup-dev-base.sh

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -83,10 +83,15 @@ else
8383
fi
8484

8585
#step1. create host cluster and member clusters in parallel
86-
# host IP address: script parameter ahead of macOS IP
86+
# host IP address: script parameter ahead of WSL2 or macOS IP
8787
if [[ -z "${HOST_IPADDRESS}" ]]; then
88-
util::get_macos_ipaddress # Adapt for macOS
89-
HOST_IPADDRESS=${MAC_NIC_IPADDRESS:-}
88+
if util::is_wsl2; then
89+
util::get_wsl2_ipaddress # adapt for WSL2
90+
HOST_IPADDRESS=${WSL2_HOST_IP_ADDRESS:-}
91+
else
92+
util::get_macos_ipaddress # Adapt for macOS
93+
HOST_IPADDRESS=${MAC_NIC_IPADDRESS:-}
94+
fi
9095
fi
9196
#prepare for kindClusterConfig
9297
TEMP_PATH=$(mktemp -d)

hack/util.sh

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -533,6 +533,16 @@ function util::get_docker_host_ip_port(){
533533
docker inspect --format='{{range $key, $value := index .NetworkSettings.Ports "6443/tcp"}}{{if eq $key 0}}{{$value.HostIp}}:{{$value.HostPort}}{{end}}{{end}}' "${container_name}"
534534
}
535535

536+
# util::is_wsl2 checks if the current environment is WSL2.
537+
# Returns:
538+
# 0 if running in WSL2, 1 otherwise.
539+
# Usage:
540+
# util::is_wsl2 && echo "WSL2 detected" || echo "Not WSL2"
541+
function util::is_wsl2(){
542+
# /proc/sys/fs/binfmt_misc/WSLInterop exists only in WSL2 environments
543+
[[ -f /proc/sys/fs/binfmt_misc/WSLInterop ]] || \
544+
( [[ -f /proc/version ]] && grep -q "WSL2" /proc/version )
545+
}
536546
# util::check_clusters_ready checks if a cluster is ready, if not, wait until timeout
537547
function util::check_clusters_ready() {
538548
local kubeconfig_path=${1}
@@ -548,7 +558,13 @@ function util::check_clusters_ready() {
548558
os_name=$(go env GOOS)
549559
local container_ip_port
550560
case $os_name in
551-
linux) container_ip_port=$(util::get_docker_native_ipaddress "${context_name}-control-plane")":6443"
561+
linux)
562+
if util::is_wsl2; then
563+
# WSL2 uses a different method to get the container IP address
564+
container_ip_port=$(util::get_docker_host_ip_port "${context_name}-control-plane")
565+
else # normal linux environment
566+
container_ip_port=$(util::get_docker_native_ipaddress "${context_name}-control-plane")":6443"
567+
fi
552568
;;
553569
darwin) container_ip_port=$(util::get_docker_host_ip_port "${context_name}-control-plane")
554570
;;
@@ -683,6 +699,16 @@ function util::add_routes() {
683699
unset IFS
684700
}
685701

702+
# util::get_wsl2_ipaddress will get ip address on wsl2, store to 'WSL2_HOST_IP_ADDRESS' if available
703+
WSL2_HOST_IP_ADDRESS=''
704+
function util::get_wsl2_ipaddress() {
705+
echo "===================================================="
706+
echo "= Detected that you are installing Karmada on WSL2 ="
707+
echo "===================================================="
708+
WSL2_HOST_IP_ADDRESS=$(hostname -I | awk '{print $1}')
709+
util::verify_ip_address "${WSL2_HOST_IP_ADDRESS}"
710+
}
711+
686712
# util::get_macos_ipaddress will get ip address on macos interactively, store to 'MAC_NIC_IPADDRESS' if available
687713
MAC_NIC_IPADDRESS=''
688714
function util::get_macos_ipaddress() {

0 commit comments

Comments
 (0)