Skip to content

Commit

Permalink
Add artificial resource in order to wait for cluster to be in ready s… (
Browse files Browse the repository at this point in the history
#35)

* 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.
  • Loading branch information
apasyniuk authored Jul 31, 2023
1 parent 01be6a2 commit e1b24a1
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
26 changes: 26 additions & 0 deletions main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
13 changes: 13 additions & 0 deletions variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -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."
Expand Down Expand Up @@ -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
}

0 comments on commit e1b24a1

Please sign in to comment.