Skip to content

Commit 804ffc4

Browse files
improve-templates-in-tilt
1 parent 2e73a26 commit 804ffc4

File tree

2 files changed

+51
-30
lines changed

2 files changed

+51
-30
lines changed

Tiltfile

Lines changed: 44 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -411,74 +411,89 @@ def prepare_all():
411411
def cluster_templates():
412412
substitutions = settings.get("kustomize_substitutions", {})
413413

414+
# Ensure we have default values for a small set of well-known variables
415+
substitutions["NAMESPACE"] = substitutions.get("NAMESPACE", "default")
416+
substitutions["KUBERNETES_VERSION"] = substitutions.get("KUBERNETES_VERSION", "v1.24.0")
417+
substitutions["CONTROL_PLANE_MACHINE_COUNT"] = substitutions.get("CONTROL_PLANE_MACHINE_COUNT", "1")
418+
substitutions["WORKER_MACHINE_COUNT"] = substitutions.get("WORKER_MACHINE_COUNT", "3")
419+
420+
# Note: this is a workaround to pass env variables to cmd buttons while this is not supported natively like in local_resource
421+
for name, value in substitutions.items():
422+
os.environ[name] = value
423+
414424
template_dirs = settings.get("template_dirs", {
415425
"docker": ["./test/infrastructure/docker/templates"],
416426
})
417427

418428
for provider, provider_dirs in template_dirs.items():
429+
p = providers.get(provider)
430+
label = p.get("label", provider)
431+
419432
for template_dir in provider_dirs:
420433
template_list = [filename for filename in listdir(template_dir) if os.path.basename(filename).endswith("yaml")]
421434
for filename in template_list:
422-
deploy_templates(filename, provider, substitutions)
435+
deploy_templates(filename, label, substitutions)
423436

424-
def deploy_templates(filename, provider, substitutions):
437+
def deploy_templates(filename, label, substitutions):
425438
# validate filename exists
426439
if not os.path.exists(filename):
427440
fail(filename + " not found")
428441

429-
os.environ["NAMESPACE"] = substitutions.get("NAMESPACE", "default")
430-
os.environ["KUBERNETES_VERSION"] = substitutions.get("KUBERNETES_VERSION", "v1.24.0")
431-
os.environ["CONTROL_PLANE_MACHINE_COUNT"] = substitutions.get("CONTROL_PLANE_MACHINE_COUNT", "1")
432-
os.environ["WORKER_MACHINE_COUNT"] = substitutions.get("WORKER_MACHINE_COUNT", "3")
433-
434442
basename = os.path.basename(filename)
435443
if basename.endswith(".yaml"):
436444
if basename.startswith("clusterclass-"):
437445
template_name = basename.replace("clusterclass-", "").replace(".yaml", "")
438-
deploy_clusterclass(template_name, provider, filename)
446+
deploy_clusterclass(template_name, label, filename, substitutions)
439447
elif basename.startswith("cluster-template-"):
440448
clusterclass_name = basename.replace("cluster-template-", "").replace(".yaml", "")
441-
deploy_cluster_template(clusterclass_name, provider, filename)
449+
deploy_cluster_template(clusterclass_name, label, filename, substitutions)
442450

443-
def deploy_clusterclass(clusterclass_name, provider, filename):
444-
apply_clusterclass_cmd = "cat " + filename + " | " + envsubst_cmd + " | " + kubectl_cmd + " apply -f - && echo \"ClusterClass created from\'" + filename + "\', don't forget to delete\n\""
445-
delete_clusterclass_cmd = kubectl_cmd + " delete clusterclass " + clusterclass_name + ' --ignore-not-found=true; echo "\n"'
451+
def deploy_clusterclass(clusterclass_name, label, filename, substitutions):
452+
apply_clusterclass_cmd = "cat " + filename + " | " + envsubst_cmd + " | " + kubectl_cmd + " apply --namespace=$NAMESPACE -f - && echo \"ClusterClass created from\'" + filename + "\', don't forget to delete\n\""
453+
delete_clusterclass_cmd = kubectl_cmd + " --namespace=$NAMESPACE delete clusterclass " + clusterclass_name + ' --ignore-not-found=true; echo "\n"'
446454

447455
local_resource(
448456
name = clusterclass_name,
449457
cmd = ["bash", "-c", apply_clusterclass_cmd],
458+
env = substitutions,
450459
auto_init = False,
451460
trigger_mode = TRIGGER_MODE_MANUAL,
452-
labels = [provider + "-clusterclasses"],
461+
labels = [label + ".clusterclasses"],
453462
)
454463

455464
cmd_button(
456465
clusterclass_name + ":apply",
457466
argv = ["bash", "-c", apply_clusterclass_cmd],
458467
resource = clusterclass_name,
459468
icon_name = "note_add",
460-
text = "Apply ClusterClass",
469+
text = "Apply `" + clusterclass_name + "` ClusterClass",
470+
inputs = [
471+
text_input("NAMESPACE", default = substitutions.get("NAMESPACE")),
472+
],
461473
)
462474

463475
cmd_button(
464476
clusterclass_name + ":delete",
465477
argv = ["bash", "-c", delete_clusterclass_cmd],
466478
resource = clusterclass_name,
467479
icon_name = "delete_forever",
468-
text = "Delete ClusterClass",
480+
text = "Delete `" + clusterclass_name + "` ClusterClass",
481+
inputs = [
482+
text_input("NAMESPACE", default = substitutions.get("NAMESPACE")),
483+
],
469484
)
470485

471-
def deploy_cluster_template(template_name, provider, filename):
472-
apply_cluster_template_cmd = "CLUSTER_NAME=" + template_name + "-$RANDOM; " + clusterctl_cmd + " generate cluster $CLUSTER_NAME --from " + filename + " | " + kubectl_cmd + " apply -f - && echo \"Cluster '$CLUSTER_NAME' created, don't forget to delete\n\""
473-
474-
delete_clusters_cmd = 'DELETED=$(echo "$(bash -c "' + kubectl_cmd + ' get clusters --no-headers -o custom-columns=":metadata.name"")" | grep -E "^' + template_name + '-[[:digit:]]{1,5}$"); if [ -z "$DELETED" ]; then echo "Nothing to delete for cluster template ' + template_name + '"; else echo "Deleting clusters:\n$DELETED\n"; echo $DELETED | xargs -L1 ' + kubectl_cmd + ' delete cluster; fi; echo "\n"'
486+
def deploy_cluster_template(template_name, label, filename, substitutions):
487+
apply_cluster_template_cmd = "CLUSTER_NAME=" + template_name + "-$RANDOM;" + clusterctl_cmd + " generate cluster $CLUSTER_NAME --from " + filename + " | " + kubectl_cmd + " apply -f - && echo \"Cluster '$CLUSTER_NAME' created, don't forget to delete\n\""
488+
delete_clusters_cmd = 'DELETED=$(echo "$(bash -c "' + kubectl_cmd + ' --namespace=$NAMESPACE get clusters -A --no-headers -o custom-columns=":metadata.name"")" | grep -E "^' + template_name + '-[[:digit:]]{1,5}$"); if [ -z "$DELETED" ]; then echo "Nothing to delete for cluster template ' + template_name + '"; else echo "Deleting clusters:\n$DELETED\n"; echo $DELETED | xargs -L1 ' + kubectl_cmd + ' delete cluster; fi; echo "\n"'
475489

476490
local_resource(
477491
name = template_name,
478492
cmd = ["bash", "-c", apply_cluster_template_cmd],
493+
env = substitutions,
479494
auto_init = False,
480495
trigger_mode = TRIGGER_MODE_MANUAL,
481-
labels = [provider + "-cluster-templates"],
496+
labels = [label + ".templates"],
482497
)
483498

484499
cmd_button(
@@ -487,6 +502,12 @@ def deploy_cluster_template(template_name, provider, filename):
487502
resource = template_name,
488503
icon_name = "add_box",
489504
text = "Create `" + template_name + "` cluster",
505+
inputs = [
506+
text_input("NAMESPACE", default = substitutions.get("NAMESPACE")),
507+
text_input("KUBERNETES_VERSION", default = substitutions.get("KUBERNETES_VERSION")),
508+
text_input("CONTROL_PLANE_MACHINE_COUNT", default = substitutions.get("CONTROL_PLANE_MACHINE_COUNT")),
509+
text_input("WORKER_MACHINE_COUNT", default = substitutions.get("WORKER_MACHINE_COUNT")),
510+
],
490511
)
491512

492513
cmd_button(
@@ -495,6 +516,9 @@ def deploy_cluster_template(template_name, provider, filename):
495516
resource = template_name,
496517
icon_name = "delete_forever",
497518
text = "Delete `" + template_name + "` clusters",
519+
inputs = [
520+
text_input("NAMESPACE", default = substitutions.get("NAMESPACE")),
521+
],
498522
)
499523

500524
cmd_button(

docs/book/src/developer/tilt.md

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -278,15 +278,16 @@ These can be customized for your specific needs.
278278

279279
### Deploying a workload cluster
280280

281-
After your kind management cluster is up and running with Tilt, you can deploy a workload clusters in the Tilt web UI based off of YAML templates from specified directories. By default, templates are read from `./test/infrastructure/docker/templates`.
281+
After your kind management cluster is up and running with Tilt, you can deploy a workload clusters in the Tilt web UI based off of YAML templates from the directories specified in
282+
the `template_dirs` field from the [tilt-settings.yaml](#tilt-settings-fields) file (default `./test/infrastructure/docker/templates`).
282283

283-
These deployment resources are found in the Tilt web UI under the label grouping `<provider>-cluster-templates` and `<provider>-clusterclasses` for each specified provider, i.e. `docker-cluster-templates` and `docker-clusterclasses`.
284+
Templates should be named according to clusterctl conventions:
284285

285-
The `<provider>-cluster-templates` category contains cluster templates, you can create a cluster by clicking "Create cluster" or the clockwise arrow icon ⟳. Note that each time a cluster template is deployed, it deploys a new workload cluster in addition to the existing ones. To delete all clusters based off of a template, click on "Delete \<cluster-template\> cluster," and click on "Delete all workload clusters" to delete all workload clusters.
286+
- template files must be named `cluster-template-{name}.yaml`; those files will be accessible in the Tilt web UI under the label grouping `{provider-label}.templates`, i.e. `CAPD.templates`.
287+
- cluster class files must be named `clusterclass-{name}.yaml`; those file will be accessible in the Tilt web UI under the label grouping `{provider-label}.clusterclasses`, i.e. `CAPD.clusterclasses`.
286288

287-
The `<provider>-clusterclasses` category contains ClusterClass definitions and you can create them by clicking on the "Create clusterclass" or the clockwise arrow icon ⟳ and delete them by clicking on "Delete clusterclass".
288-
289-
Variables in a cluster template are substituted with values from `kustomize_substitutions` in `tilt-settings.yaml`. The default substitutions are:
289+
By selecting one of those items in the Tilt web UI set of buttons will appear, allowing to create - with a dropdown for customizing variable substitutions - or delete clusters.
290+
Custom values for variable substitutions can be set using `kustomize_substitutions` in `tilt-settings.yaml`, e.g.
290291

291292
```yaml
292293
kustomize_substitutions:
@@ -296,8 +297,6 @@ kustomize_substitutions:
296297
WORKER_MACHINE_COUNT: 3
297298
```
298299

299-
Lastly, cluster template directories can be specified from the `template_dirs` field in `tilt-settings.yaml`. See [tilt-settings fields](#tilt-settings-fields) for an example.
300-
301300
<h1>Use of clusterctl</h1>
302301

303302
When the worker cluster has been created using tilt, `clusterctl` should not be used for management
@@ -308,8 +307,6 @@ This limitation is an acceptable trade-off while executing fast dev-test iterati
308307
you are interested in testing clusterctl workflows, you should refer to the
309308
[clusterctl developer instructions](../clusterctl/developers.md).
310309

311-
</aside>
312-
313310
## Available providers
314311

315312
The following providers are currently defined in the Tiltfile:

0 commit comments

Comments
 (0)