k3d-io / k3d

Little helper to run CNCF's k3s in Docker
https://k3d.io/
MIT License
5.51k stars 463 forks source link

[FEATURE] add "k3d cluster apply" command to create and update cluster from config #1184

Open sanzoghenzo opened 2 years ago

sanzoghenzo commented 2 years ago

Is your feature request related to a problem or a Pull Request

I can't find any related issues or PRs.

Scope of your request

New "apply" verb for "cluster" noun

Describe the solution you'd like

Be able to use the configuration file not only to create a cluster, but also to edit it, so that a configuration file can hold the desired state of the cluster.

Something like kubectl apply, but for k3d resources.

I'm trying to setup a pyinfra deployment (something like an Ansible playbook) to be able to deploy a k3d cluster on my homelab; In order to have some idempotency I have to:

Describe alternatives you've considered

I could accept destroying and re-create the cluster if the changes are big/to complex to handle, but it still needs a handier way to compare the state against the config file.

chasecaleb commented 2 years ago

This would be excellent! I'm struggling with the same pain, although I'm using Nix to manage my k3d config file.

it still needs a handier way to compare the state against the config file.

Is exactly my pain. I can destroy and re-create the cluster easily with k3d cluster commands, but there isn't a way to figure out when the config file differs from the running cluster.

sanzoghenzo commented 1 year ago

FYI, I've limited my goals to a local k3d deployment and switched to Taskfile. The built-in checksum on the sources files allows me to run the destroy+create the cluster only if I edit the file. It is still overkill if I only need to change, for example, the loadbalancer port, but it is somehting.

version: "3"

vars:
  K3D_CLUSTER_NAME:
    sh: grep 'name:' k3d-cluster.yml | awk '{ print $2 }' | tr -d '\"'

tasks:
  k3d:
    desc: Install k3d. Use -f to upgrade
    cmds:
      - wget -q -O - https://raw.githubusercontent.com/k3d-io/k3d/main/install.sh | bash
    status:
      - command -v k3d

  delete-cluster:
    desc: Delete k3d cluster if it exists
    cmds:
      - k3d cluster delete {{.K3D_CLUSTER_NAME}} || true
    silent: true

  create-cluster:
    desc: Create k3d cluster using the k3d-cluster.yml configuration
    deps:
      - k3d
    sources:
      - k3d-cluster.yml
    cmds:
      - task: delete-cluster
      - k3d cluster create -c k3d-cluster.yml