keepwsl #43
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
on: | |
push: | |
branches: | |
- win-wsl | |
jobs: | |
wsl: | |
runs-on: windows-2022 | |
steps: | |
- name: Prepare WSL | |
run: | | |
$ErrorActionPreference = "Stop" | |
$PSNativeCommandUseErrorActionPreference = $true | |
& wsl --update | |
& wsl --set-default-version 2 | |
& wsl --install Ubuntu | |
& wsl --set-default Ubuntu | |
- name: Configure WSL | |
# https://github.com/microsoft/WSL/issues/10138#issuecomment-2380431569 | |
shell: powershell -Command 'Get-Content -Path {0} -Encoding utf8 | & wsl -- bash -euo pipefail' | |
run: | | |
cat <<EOF >/etc/wsl.conf | |
[boot] | |
systemd=true | |
[automount] | |
enabled = true | |
EOF | |
mkdir -p /usr/local/lib/systemd/system | |
cat <<EOF >/usr/local/lib/systemd/keepwsl.conf | |
[Unit] | |
Description=A simple service to keep WSL alive | |
Documentation=https://github.com/gardengim/keepwsl | |
[Service] | |
ExecStart=/mnt/c/Windows/System32/wsl.exe sleep infinity | |
[Install] | |
WantedBy=default.target | |
EOF | |
- name: Restart WSL | |
run: "& wsl --shutdown" | |
- name: Start k0s controller in WSL | |
id: start-controller | |
run: | | |
$ErrorActionPreference = "Stop" | |
$PSNativeCommandUseErrorActionPreference = $true | |
(Invoke-WebRequest -Uri https://get.k0s.sh).Content | & wsl -- sh | |
& wsl mkdir /etc/k0s | |
@" | |
spec: | |
api: | |
address: $((& wsl hostname -i).Split(" ")[-1]) | |
storage: | |
type: kine | |
network | |
provider: calico | |
"@ | & wsl tee /etc/k0s/k0s.yaml | |
& wsl -- k0s install controller --debug --disable-components metrics-server | |
& wsl -- k0s start | |
- name: Download k0s for Windows | |
run: | | |
$ErrorActionPreference = "Stop" | |
$PSNativeCommandUseErrorActionPreference = $true | |
$k0sVersion = (Invoke-WebRequest -Uri https://docs.k0sproject.io/latest.txt).Content.Trim() | |
$url = "https://github.com/k0sproject/k0s/releases/download/$k0sVersion/k0s-${k0sVersion}-amd64.exe" | |
Write-Host "Downloading k0s $k0sVersion" | |
Invoke-WebRequest -Uri $url -OutFile k0s.exe | |
& .\k0s version | |
- name: Prepare kubeconfig | |
run: | | |
$ErrorActionPreference = "Stop" | |
$PSNativeCommandUseErrorActionPreference = $true | |
# Set-PSRepository -Name PSGallery -InstallationPolicy Trusted | |
# Install-Module -Name powershell-yaml -Scope CurrentUser | |
for ($attempt = 1; $true; $attempt++) { | |
try { | |
Write-Host "Getting kubeconfig (attempt $attempt) ..." | |
$kubeconfig = & wsl k0s kubeconfig admin | |
# $kubeconfig = & wsl k0s kubeconfig admin | ConvertFrom-Yaml | |
# foreach ($cluster in $kubeconfig.clusters) { | |
# $cluster.cluster.server = "https://127.0.0.1:6443" | |
# } | |
# $kubeconfig | ConvertTo-Yaml | Out-File -FilePath kubeconfig -Encoding utf8 | |
& wsl k0s kubeconfig admin | Out-File -FilePath kubeconfig -Encoding utf8 | |
} | |
catch { | |
Write-Warning "Failed to get kubeconfig: $_" | |
if ($attempt -le 5) { | |
Start-Sleep -Seconds 5 | |
continue | |
} | |
throw | |
} | |
Write-Host "Kubeconfig written to file" | |
break | |
} | |
"KUBECONFIG=" + (Resolve-Path "kubeconfig").Path | Out-File -FilePath $env:GITHUB_ENV -Append -Encoding utf8 | |
- name: Try to connect | |
run: | | |
Write-Host $env:KUBECONFIG | |
Get-Content -Path $env:KUBECONFIG | |
for ($i = 1; $i -le 72; $i++) { | |
& kubectl auth whoami | |
Start-Sleep -Seconds 5 | |
} | |
- name: Get join token | |
run: | | |
$ErrorActionPreference = "Stop" | |
$PSNativeCommandUseErrorActionPreference = $true | |
for ($attempt = 1; $true; $attempt++) { | |
try { | |
Write-Host "Getting join token (attempt $attempt) ..." | |
& wsl k0s token create --role=worker | Out-File -FilePath k0s-worker-token -Encoding utf8 | |
} | |
catch { | |
Write-Warning "Failed to get join token: $_" | |
if ($attempt -le 15) { | |
Start-Sleep -Seconds 5 | |
continue | |
} | |
throw | |
} | |
Write-Host "Join token written to file" | |
break | |
} | |
- name: Start k0s worker | |
timeout-minutes: 7 | |
run: | | |
$ErrorActionPreference = "Stop" | |
$PSNativeCommandUseErrorActionPreference = $true | |
& .\k0s worker --token-file=k0s-worker-token | |
- name: Monitor cluster a bit | |
run: | | |
for ($i = 1; $i -le 5; $i++) { | |
& kubectl get node,po -A -owide | |
Start-Sleep -Seconds 5 | |
} | |
- name: k0 controller logs | |
if: always() && steps.start-controller.conclusion == 'success' | |
run: | | |
& wsl systemctl status k0scontroller.service | |
& wsl journalctl -u k0scontroller.service |