Skip to content

Commit

Permalink
address comments
Browse files Browse the repository at this point in the history
  • Loading branch information
fabriziopandini committed Aug 1, 2022
1 parent 19c43b9 commit 0f12635
Showing 1 changed file with 28 additions and 11 deletions.
39 changes: 28 additions & 11 deletions Tiltfile
Original file line number Diff line number Diff line change
Expand Up @@ -411,6 +411,16 @@ def prepare_all():
def cluster_templates():
substitutions = settings.get("kustomize_substitutions", {})

# Ensure we have default values for a small set of well-known variables
substitutions["NAMESPACE"] = substitutions.get("NAMESPACE", "default")
substitutions["KUBERNETES_VERSION"] = substitutions.get("KUBERNETES_VERSION", "v1.24.0")
substitutions["CONTROL_PLANE_MACHINE_COUNT"] = substitutions.get("CONTROL_PLANE_MACHINE_COUNT", "1")
substitutions["WORKER_MACHINE_COUNT"] = substitutions.get("WORKER_MACHINE_COUNT", "3")

# Note: this is a workaround to pass env variables to cmd buttons while this is not supported natively like in local_resource
for name, value in substitutions.items():
os.environ[name] = value

template_dirs = settings.get("template_dirs", {
"docker": ["./test/infrastructure/docker/templates"],
})
Expand Down Expand Up @@ -440,11 +450,12 @@ def deploy_templates(filename, label, substitutions):

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

local_resource(
name = clusterclass_name,
cmd = ["bash", "-c", apply_clusterclass_cmd],
env = substitutions,
auto_init = False,
trigger_mode = TRIGGER_MODE_MANUAL,
labels = [label + ".clusterclasses"],
Expand All @@ -455,9 +466,9 @@ def deploy_clusterclass(clusterclass_name, label, filename, substitutions):
argv = ["bash", "-c", apply_clusterclass_cmd],
resource = clusterclass_name,
icon_name = "note_add",
text = "Apply ClusterClass",
text = "Apply `" + clusterclass_name + "` ClusterClass",
inputs = [
text_input("NAMESPACE", default = substitutions.get("NAMESPACE", "default")),
text_input("NAMESPACE", default = substitutions.get("NAMESPACE")),
],
)

Expand All @@ -466,16 +477,20 @@ def deploy_clusterclass(clusterclass_name, label, filename, substitutions):
argv = ["bash", "-c", delete_clusterclass_cmd],
resource = clusterclass_name,
icon_name = "delete_forever",
text = "Delete ClusterClass",
text = "Delete `" + clusterclass_name + "` ClusterClass",
inputs = [
text_input("NAMESPACE", default = substitutions.get("NAMESPACE")),
],
)

def deploy_cluster_template(template_name, label, filename, substitutions):
apply_cluster_template_cmd = clusterctl_cmd + " generate cluster $CLUSTER_NAME --from " + filename + " | " + kubectl_cmd + " apply -f - && echo \"Cluster '$CLUSTER_NAME' created, don't forget to delete\n\""
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"'
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\""
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"'

local_resource(
name = template_name,
cmd = ["bash", "-c", apply_cluster_template_cmd],
env = substitutions,
auto_init = False,
trigger_mode = TRIGGER_MODE_MANUAL,
labels = [label + ".templates"],
Expand All @@ -488,11 +503,10 @@ def deploy_cluster_template(template_name, label, filename, substitutions):
icon_name = "add_box",
text = "Create `" + template_name + "` cluster",
inputs = [
text_input("CLUSTER_NAME", default = template_name + "-1"),
text_input("NAMESPACE", default = substitutions.get("NAMESPACE", "default")),
text_input("KUBERNETES_VERSION", default = substitutions.get("KUBERNETES_VERSION", "v1.24.0")),
text_input("CONTROL_PLANE_MACHINE_COUNT", default = substitutions.get("CONTROL_PLANE_MACHINE_COUNT", "1")),
text_input("WORKER_MACHINE_COUNT", default = substitutions.get("WORKER_MACHINE_COUNT", "1")),
text_input("NAMESPACE", default = substitutions.get("NAMESPACE")),
text_input("KUBERNETES_VERSION", default = substitutions.get("KUBERNETES_VERSION")),
text_input("CONTROL_PLANE_MACHINE_COUNT", default = substitutions.get("CONTROL_PLANE_MACHINE_COUNT")),
text_input("WORKER_MACHINE_COUNT", default = substitutions.get("WORKER_MACHINE_COUNT")),
],
)

Expand All @@ -502,6 +516,9 @@ def deploy_cluster_template(template_name, label, filename, substitutions):
resource = template_name,
icon_name = "delete_forever",
text = "Delete `" + template_name + "` clusters",
inputs = [
text_input("NAMESPACE", default = substitutions.get("NAMESPACE")),
],
)

cmd_button(
Expand Down

0 comments on commit 0f12635

Please sign in to comment.