Rewrite your Windows .kube/config to be usable by Linux kubectl inside WSL
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
win_profile_dir=$(wslpath "$(cmd.exe /c "echo %USERPROFILE%" | tr -d '\r')") | |
win_dot_kube_dir="${win_profile_dir}/.kube" | |
win_kube_config="${win_dot_kube_dir}/config" | |
wsl_dot_kube_dir="${HOME}/.kube" | |
mkdir -p "${wsl_dot_kube_dir}" | |
wsl_kube_config="${wsl_dot_kube_dir}/config" | |
embed=0 | |
if [ "$1" = '--embed' ] | |
then | |
embed=1 | |
fi | |
inline () { | |
local line="$1" key value wsl_file | |
IFS=: read -r key value <<<"${line}" | |
value="${value# }" | |
wsl_file=$(wslpath "${value}") | |
if [ -n "${wsl_file}" ] && [ -s "${wsl_file}" ] | |
then | |
value="${wsl_file}" | |
if [ "${embed}" -eq 1 ] | |
then | |
value=$(base64 -w0 <"${wsl_file}") | |
key="${key}-data" | |
fi | |
fi | |
printf '%s: %s\n' "${key}" "${value}" | |
} | |
while IFS= read -r line | |
do | |
case "${line}" in | |
*' certificate-authority:'*|*' client-certificate:'*|*' client-key:'*) | |
inline "${line}" | |
;; | |
*) | |
printf '%s\n' "${line}" | |
;; | |
esac | |
done <"${win_kube_config}" >"${wsl_kube_config}" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment