Skip to content

Instantly share code, notes, and snippets.

@jstangroome
Created August 21, 2018 08:19
Show Gist options
  • Save jstangroome/bb72c365ca7b4199e17f7b2e72d64d14 to your computer and use it in GitHub Desktop.
Save jstangroome/bb72c365ca7b4199e17f7b2e72d64d14 to your computer and use it in GitHub Desktop.
Rewrite your Windows .kube/config to be usable by Linux kubectl inside WSL
#!/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