From e1b24a1c9af81b7275d62bcc82d27eb7a9adaf99 Mon Sep 17 00:00:00 2001 From: apasyniuk <58225282+apasyniuk@users.noreply.github.com> Date: Mon, 31 Jul 2023 20:27:22 +0200 Subject: [PATCH] =?UTF-8?q?Add=20artificial=20resource=20in=20order=20to?= =?UTF-8?q?=20wait=20for=20cluster=20to=20be=20in=20ready=20s=E2=80=A6=20(?= =?UTF-8?q?#35)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Add artificial resource in order to wait for cluster to be in ready state This commit introduces a change that will block module completion up until created cluster is in the Ready state. Natural candidate for cluster ready state is castai_xxx_cluster resource creation step. Hoverer as cluster resource needs to finish its creation in order to run helm charts that actually deploys components that makes cluster to transit to ready state we need to introduce custom resource that will depend on helm charts resources that will pool ready state. To accomplish this null_resource was introduced that pools created cluster status until it is in ready state. --- main.tf | 26 ++++++++++++++++++++++++++ variables.tf | 13 +++++++++++++ 2 files changed, 39 insertions(+) diff --git a/main.tf b/main.tf index f47d88f..7edf4b4 100644 --- a/main.tf +++ b/main.tf @@ -216,6 +216,32 @@ resource "helm_release" "castai_cluster_controller" { } } +resource "null_resource" "wait_for_cluster" { + count = var.wait_for_cluster_ready ? 1 : 0 + depends_on = [helm_release.castai_cluster_controller, helm_release.castai_agent] + + provisioner "local-exec" { + quiet = true + environment = { + API_KEY = var.castai_api_token + } + command = <<-EOT + RETRY_COUNT=30 + POOLING_INTERVAL=30 + + for i in $(seq 1 $RETRY_COUNT); do + sleep $POOLING_INTERVAL + curl -s ${var.api_url}/v1/kubernetes/external-clusters/${castai_aks_cluster.castai_cluster.id} -H "x-api-key: $API_KEY" | grep '"status"\s*:\s*"ready"' && exit 0 + done + + echo "Cluster is not ready after 15 minutes" + exit 1 + EOT + + interpreter = ["bash", "-c"] + } +} + resource "helm_release" "castai_spot_handler" { name = "castai-spot-handler" repository = "https://castai.github.io/helm-charts" diff --git a/variables.tf b/variables.tf index ffa3db4..69d9c86 100644 --- a/variables.tf +++ b/variables.tf @@ -4,6 +4,13 @@ variable "api_url" { default = "https://api.cast.ai" } +variable "castai_api_token" { + type = string + description = "Optional CAST AI API token created in console.cast.ai API Access keys section. Used only when `wait_for_cluster_ready` is set to true" + sensitive = true + default = "" +} + variable "aks_cluster_name" { type = string description = "Name of the cluster to be connected to CAST AI." @@ -136,3 +143,9 @@ variable "kvisor_version" { type = string default = null } + +variable "wait_for_cluster_ready" { + type = bool + description = "Wait for cluster to be ready before finishing the module execution, this option requires `castai_api_token` to be set" + default = false +}