# Deploy kubernetes via k0s (which installs a bundled containerd).
# $ limactl start ./k0s.yaml
# $ limactl shell k0s kubectl
#
# It can be accessed from the host by exporting the kubeconfig file;
# the ports are already forwarded automatically by lima:
#
# $ export KUBECONFIG=$(limactl list k0s --format 'unix://{{.Dir}}/copied-from-guest/kubeconfig.yaml')
# $ kubectl get no
# NAME       STATUS   ROLES                  AGE   VERSION
#  lima-k0s   Ready    control-plane   2m48s   v1.33.2+k0s

minimumLimaVersion: 2.0.0

base: template:_images/ubuntu-lts

# Mounts are disabled in this template, but can be enabled optionally.
mounts: []

# containerd is managed by k0s, not by Lima, so the values are set to false here.
containerd:
  system: false
  user: false

provision:
- mode: system
  script: |
    #!/bin/bash
    set -eux -o pipefail
    command -v k0s >/dev/null 2>&1 && exit 0

    # install k0s prerequisites
    curl -sfL https://get.k0s.sh | sh

- mode: system
  script: |
    #!/bin/bash
    set -eux -o pipefail

    #  start k0s as a single node cluster
    if ! systemctl status k0scontroller >/dev/null 2>&1; then
      k0s install controller --single
    fi

    systemctl start k0scontroller

probes:
- description: "k0s to be running"
  script: |
    #!/bin/bash
    set -eux -o pipefail
    if ! timeout 30s bash -c "until sudo test -f /var/lib/k0s/pki/admin.conf; do sleep 3; done"; then
      echo >&2 "k0s kubeconfig file has not yet been created"
      exit 1
    fi
  hint: |
    The k0s control plane is not ready yet.
    Run "limactl shell k0s sudo journalctl -u k0scontroller" to debug.

copyToHost:
- guest: "/var/lib/k0s/pki/admin.conf"
  host: "{{.Dir}}/copied-from-guest/kubeconfig.yaml"
  deleteOnStop: true
message: |
  To run `kubectl` on the host (assumes kubectl is installed), run the following commands:
  ------
  export KUBECONFIG="{{.Dir}}/copied-from-guest/kubeconfig.yaml"
  kubectl ...
  ------
