Skip to content

Instantly share code, notes, and snippets.

@birjj
Last active March 5, 2023 19:44
Show Gist options
  • Save birjj/9f7e89f896756ddf14a282ac96560513 to your computer and use it in GitHub Desktop.
Save birjj/9f7e89f896756ddf14a282ac96560513 to your computer and use it in GitHub Desktop.
Merge Windows kubectl config into WSL config
#!/usr/bin/env zsh
# Written for ZSH - if you use another shell, adapt as needed
# Copies the kubectl config from the default Windows location, converts it to a Unix-compatible format,
# merges it with existing configs and saves it to the default Unix location
# copy over the kubeconfig from Windows, replacing Windows paths with WSL paths
WindowsUser='johan'
WindowsConfig=$(cat "/mnt/c/Users/$WindowsUser/.kube/config")
re='(.*?)([A-Z]:\\[a-zA-Z\\ \.]+)(.*)'
while [[ $WindowsConfig =~ $re ]]; do
PreStr="${match[1]}"
Path="${match[2]}"
PostStr="${match[3]}"
NewPath=$(wslpath "$Path")
WindowsConfig="$PreStr$NewPath$PostStr"
done
echo "$WindowsConfig" > ~/.kube/config-windows
# merge all the configs into one
KubeConfigs=~/.kube/config-rpis:~/.kube/config-windows
KUBECONFIG=$KubeConfigs kubectl config view --flatten > ~/.kube/config
#!/usr/bin/env zsh
# Written for ZSH - if you use another shell, adapt as needed. Depends on merge-kubeconfigs being available on PATH
# Starts the Minikube cluster, sets up Docker CLI to target the Minikube instance and sets up kubectl config
if [[ ! $ZSH_EVAL_CONTEXT =~ :file$ ]]; then
echo -e "\033[1;31mThe script must be ran as sourced, for env variables to work\033[0m"
echo -e "\033[0;31mUse '. start-minikube'\033[0m"
exit 1
fi
# Check if we need to start minikube first
MinikubeStatus="$(/mnt/c/Program\ Files/Kubernetes/Minikube/minikube.exe status)"
if [[ $MinikubeStatus == *"Stopped"* ]]; then
echo "Starting Minikube"
/mnt/c/Program\ Files/Kubernetes/Minikube/minikube.exe start
else
echo -e "\033[1;32mMinikube is already running\033[0m"
fi
# then use the minikube docker-env
echo "Using minikube docker-env"
MinikubeDockerEnv="$(/mnt/c/Program\ Files/Kubernetes/Minikube/minikube.exe docker-env --shell=zsh)"
re='(.*?)([A-Z]:\\[a-zA-Z\\ \.]+)(.*)'
while [[ $MinikubeDockerEnv =~ $re ]]; do
PreStr="${match[1]}"
Path="${match[2]}"
PostStr="${match[3]}"
NewPath=$(wslpath "$Path")
MinikubeDockerEnv="$PreStr$NewPath$PostStr"
done
eval "$MinikubeDockerEnv"
echo -e "Finished executing:\n\033[2m$MinikubeDockerEnv\033[0m\n"
echo "Merging kubeconfigs"
merge-kubeconfig
echo "Done"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment