k3d icon indicating copy to clipboard operation
k3d copied to clipboard

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

Open sanzoghenzo opened this issue 3 years ago • 2 comments

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:

  • check k3d cluster list to see if a cluster with that name already exists
  • call `k3d cluster create -c if it doesn't exist
  • if it already existed (not yet implemented):
    • check the nodes already present and create or delete nodes if there is a change in the number;
    • check the configured ports and call k3d node edit if changes are needed

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.

sanzoghenzo avatar Nov 27 '22 10:11 sanzoghenzo

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.

chasecaleb avatar Dec 02 '22 01:12 chasecaleb

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

sanzoghenzo avatar Apr 29 '23 08:04 sanzoghenzo