Skip to content

Commit

Permalink
feat: introduce node configuration (#11)
Browse files Browse the repository at this point in the history
  • Loading branch information
varnastadeus authored Sep 16, 2022
1 parent 55725d9 commit 0f04114
Show file tree
Hide file tree
Showing 11 changed files with 221 additions and 28 deletions.
13 changes: 12 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,13 @@
terraform.d
.idea/

# terraform examples/
examples/**/.terraform/
examples/**/terraform.d/
examples/**/crash.log
examples/**/*.tfstate
examples/**/*.tfstate.backup
examples/**/.terraform.lock.hcl
examples/**/local.auto.tfvars
examples/**/.kube/*
terraform.tfstate
.terraform*
42 changes: 29 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,18 @@ module "castai-aks-cluster" {
subscription_id = data.azurerm_subscription.current.subscription_id
tenant_id = data.azurerm_subscription.current.tenant_id
default_node_configuration = module.castai-aks-cluster.castai_node_configurations["default"]
node_configurations = {
default = {
disk_cpu_ratio = 25
subnets = [azurerm_subnet.internal.id]
tags = {
"node-config" : "default"
}
}
}
}
```

Expand All @@ -44,18 +56,18 @@ module "castai-aks-cluster" {
| Name | Version |
|------|---------|
| <a name="requirement_terraform"></a> [terraform](#requirement\_terraform) | >= 0.13 |
| <a name="requirement_azuread"></a> [azuread](#requirement\_azuread) | 2.22.0 |
| <a name="requirement_azurerm"></a> [azurerm](#requirement\_azurerm) | 3.7.0 |
| <a name="requirement_castai"></a> [castai](#requirement\_castai) | >= 0.18.0 |
| <a name="requirement_azuread"></a> [azuread](#requirement\_azuread) | >=2.22.0 |
| <a name="requirement_azurerm"></a> [azurerm](#requirement\_azurerm) | >=3.7.0 |
| <a name="requirement_castai"></a> [castai](#requirement\_castai) | >= 0.26.3 |
| <a name="requirement_helm"></a> [helm](#requirement\_helm) | >=2.0.0 |

## Providers

| Name | Version |
|------|---------|
| <a name="provider_azuread"></a> [azuread](#provider\_azuread) | 2.22.0 |
| <a name="provider_azurerm"></a> [azurerm](#provider\_azurerm) | 3.7.0 |
| <a name="provider_castai"></a> [castai](#provider\_castai) | >= 0.18.0 |
| <a name="provider_azuread"></a> [azuread](#provider\_azuread) | >=2.22.0 |
| <a name="provider_azurerm"></a> [azurerm](#provider\_azurerm) | >=3.7.0 |
| <a name="provider_castai"></a> [castai](#provider\_castai) | >= 0.26.3 |
| <a name="provider_helm"></a> [helm](#provider\_helm) | >=2.0.0 |

## Modules
Expand All @@ -66,19 +78,20 @@ No modules.

| Name | Type |
|------|------|
| [azuread_application.castai](https://registry.terraform.io/providers/hashicorp/azuread/2.22.0/docs/resources/application) | resource |
| [azuread_application_password.castai](https://registry.terraform.io/providers/hashicorp/azuread/2.22.0/docs/resources/application_password) | resource |
| [azuread_service_principal.castai](https://registry.terraform.io/providers/hashicorp/azuread/2.22.0/docs/resources/service_principal) | resource |
| [azurerm_role_assignment.castai_node_resource_group](https://registry.terraform.io/providers/hashicorp/azurerm/3.7.0/docs/resources/role_assignment) | resource |
| [azurerm_role_assignment.castai_resource_group](https://registry.terraform.io/providers/hashicorp/azurerm/3.7.0/docs/resources/role_assignment) | resource |
| [azurerm_role_definition.castai](https://registry.terraform.io/providers/hashicorp/azurerm/3.7.0/docs/resources/role_definition) | resource |
| [azuread_application.castai](https://registry.terraform.io/providers/hashicorp/azuread/latest/docs/resources/application) | resource |
| [azuread_application_password.castai](https://registry.terraform.io/providers/hashicorp/azuread/latest/docs/resources/application_password) | resource |
| [azuread_service_principal.castai](https://registry.terraform.io/providers/hashicorp/azuread/latest/docs/resources/service_principal) | resource |
| [azurerm_role_assignment.castai_node_resource_group](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/role_assignment) | resource |
| [azurerm_role_assignment.castai_resource_group](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/role_assignment) | resource |
| [azurerm_role_definition.castai](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/role_definition) | resource |
| [castai_aks_cluster.castai_cluster](https://registry.terraform.io/providers/castai/castai/latest/docs/resources/aks_cluster) | resource |
| [castai_autoscaler.castai_autoscaler_policies](https://registry.terraform.io/providers/castai/castai/latest/docs/resources/autoscaler) | resource |
| [castai_node_configuration.this](https://registry.terraform.io/providers/castai/castai/latest/docs/resources/node_configuration) | resource |
| [helm_release.castai_agent](https://registry.terraform.io/providers/hashicorp/helm/latest/docs/resources/release) | resource |
| [helm_release.castai_cluster_controller](https://registry.terraform.io/providers/hashicorp/helm/latest/docs/resources/release) | resource |
| [helm_release.castai_evictor](https://registry.terraform.io/providers/hashicorp/helm/latest/docs/resources/release) | resource |
| [helm_release.castai_spot_handler](https://registry.terraform.io/providers/hashicorp/helm/latest/docs/resources/release) | resource |
| [azuread_client_config.current](https://registry.terraform.io/providers/hashicorp/azuread/2.22.0/docs/data-sources/client_config) | data source |
| [azuread_client_config.current](https://registry.terraform.io/providers/hashicorp/azuread/latest/docs/data-sources/client_config) | data source |

## Inputs

Expand All @@ -89,7 +102,9 @@ No modules.
| <a name="input_api_url"></a> [api\_url](#input\_api\_url) | URL of alternative CAST AI API to be used during development or testing | `string` | `"https://api.cast.ai"` | no |
| <a name="input_autoscaler_policies_json"></a> [autoscaler\_policies\_json](#input\_autoscaler\_policies\_json) | Optional json object to override CAST AI cluster autoscaler policies | `string` | `""` | no |
| <a name="input_castai_components_labels"></a> [castai\_components\_labels](#input\_castai\_components\_labels) | Optional additional Kubernetes labels for CAST AI pods | `map` | `{}` | no |
| <a name="input_default_node_configuration"></a> [default\_node\_configuration](#input\_default\_node\_configuration) | ID of the default node configuration | `string` | n/a | yes |
| <a name="input_delete_nodes_on_disconnect"></a> [delete\_nodes\_on\_disconnect](#input\_delete\_nodes\_on\_disconnect) | Optionally delete Cast AI created nodes when the cluster is destroyed | `bool` | `false` | no |
| <a name="input_node_configurations"></a> [node\_configurations](#input\_node\_configurations) | Map of AKS node configurations to create | <pre>map(object({<br> disk_cpu_ratio = optional(number)<br> subnets = list(string)<br> ssh_public_key = optional(string)<br> image = optional(string)<br> tags = optional(map(string))<br> max_pods_per_node = optional(number)<br> }))</pre> | `{}` | no |
| <a name="input_node_resource_group"></a> [node\_resource\_group](#input\_node\_resource\_group) | n/a | `string` | n/a | yes |
| <a name="input_resource_group"></a> [resource\_group](#input\_resource\_group) | n/a | `string` | n/a | yes |
| <a name="input_subscription_id"></a> [subscription\_id](#input\_subscription\_id) | Azure subscription ID | `string` | n/a | yes |
Expand All @@ -99,5 +114,6 @@ No modules.

| Name | Description |
|------|-------------|
| <a name="output_castai_node_configurations"></a> [castai\_node\_configurations](#output\_castai\_node\_configurations) | Map of node configurations ids by name |
| <a name="output_cluster_id"></a> [cluster\_id](#output\_cluster\_id) | CAST.AI cluster id, which can be used for accessing cluster data using API |
<!-- END_TF_DOCS -->
50 changes: 50 additions & 0 deletions examples/basic/aks.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
provider "helm" {
kubernetes {
host = azurerm_kubernetes_cluster.this.kube_config.0.host
client_certificate = base64decode(azurerm_kubernetes_cluster.this.kube_config.0.client_certificate)
client_key = base64decode(azurerm_kubernetes_cluster.this.kube_config.0.client_key)
cluster_ca_certificate = base64decode(azurerm_kubernetes_cluster.this.kube_config.0.cluster_ca_certificate)
}
}

resource "azurerm_resource_group" "this" {
name = var.aks_cluster_name
location = var.aks_cluster_region
}

resource "azurerm_virtual_network" "this" {
name = "${var.aks_cluster_name}-network"
location = azurerm_resource_group.this.location
resource_group_name = azurerm_resource_group.this.name
address_space = ["10.1.0.0/16"]
}

resource "azurerm_subnet" "internal" {
name = "internal"
virtual_network_name = azurerm_virtual_network.this.name
resource_group_name = azurerm_resource_group.this.name
address_prefixes = ["10.1.0.0/22"]
}

resource "azurerm_kubernetes_cluster" "this" {
name = var.aks_cluster_name
resource_group_name = azurerm_resource_group.this.name
location = azurerm_resource_group.this.location
dns_prefix = var.aks_cluster_name
node_resource_group = "${var.aks_cluster_name}-ng"

default_node_pool {
name = "default"
node_count = 1
vm_size = "Standard_D2_v2"
vnet_subnet_id = azurerm_subnet.internal.id
}

identity {
type = "SystemAssigned"
}

tags = {
Environment = "Production"
}
}
38 changes: 38 additions & 0 deletions examples/basic/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
provider "castai" {
api_token = var.castai_api_token
}

provider "azurerm" {
features {}
}

data "azurerm_subscription" "current" {}

provider "azuread" {
tenant_id = data.azurerm_subscription.current.tenant_id
}

module "castai-aks-cluster" {
source = "../../"

aks_cluster_name = var.aks_cluster_name
aks_cluster_region = var.aks_cluster_region
subscription_id = data.azurerm_subscription.current.subscription_id
tenant_id = data.azurerm_subscription.current.tenant_id

node_resource_group = azurerm_kubernetes_cluster.this.node_resource_group
resource_group = azurerm_kubernetes_cluster.this.resource_group_name
delete_nodes_on_disconnect = true

default_node_configuration = module.castai-aks-cluster.castai_node_configurations["default"]

node_configurations = {
default = {
disk_cpu_ratio = 25
subnets = [azurerm_subnet.internal.id]
tags = {
"node-config" : "default"
}
}
}
}
16 changes: 16 additions & 0 deletions examples/basic/variables.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
variable "castai_api_token" {
type = string
description = "CAST AI API token used for user authentication"
}

variable "aks_cluster_name" {
type = string
description = "Name of the AKS cluster, resources will be created for."
default = "aks-basic"
}

variable "aks_cluster_region" {
type = string
description = "Region of the AKS cluster, resources will be created for."
default = "westeurope"
}
18 changes: 18 additions & 0 deletions examples/basic/versions.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
terraform {
required_version = ">= 0.13"

required_providers {
azurerm = {
source = "hashicorp/azurerm"
version = "3.7.0"
}
azuread = {
source = "hashicorp/azuread"
version = "2.22.0"
}
castai = {
source = "castai/castai"
version = ">= 0.26.3"
}
}
}
30 changes: 24 additions & 6 deletions main.tf
Original file line number Diff line number Diff line change
@@ -1,14 +1,32 @@
resource "castai_aks_cluster" "castai_cluster" {
name = var.aks_cluster_name

region = var.aks_cluster_region
region = var.aks_cluster_region
subscription_id = var.subscription_id
tenant_id = var.tenant_id
client_id = azuread_application.castai.application_id
client_secret = azuread_application_password.castai.value

node_resource_group = var.node_resource_group
delete_nodes_on_disconnect = var.delete_nodes_on_disconnect

subscription_id = var.subscription_id
node_resource_group = var.node_resource_group
tenant_id = var.tenant_id
client_id = azuread_application.castai.application_id
client_secret = azuread_application_password.castai.value
}

resource "castai_node_configuration" "this" {
for_each = {for k, v in var.node_configurations : k => v}

cluster_id = castai_aks_cluster.castai_cluster.id

name = try(each.value.name, each.key)
disk_cpu_ratio = try(each.value.disk_cpu_ratio, 25)
subnets = try(each.value.subnets, null)
ssh_public_key = try(each.value.ssh_public_key, null)
image = try(each.value.image, null)
tags = try(each.value.tags, {})

aks {
max_pods_per_node = try(each.value.max_pods_per_node, 30)
}
}

resource "helm_release" "castai_agent" {
Expand Down
5 changes: 0 additions & 5 deletions output.tf

This file was deleted.

12 changes: 12 additions & 0 deletions outputs.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
output "cluster_id" {
value = castai_aks_cluster.castai_cluster.id
description = "CAST.AI cluster id, which can be used for accessing cluster data using API"
sensitive = true
}

output "castai_node_configurations" {
description = "Map of node configurations ids by name"
value = {
for k, v in castai_node_configuration.this : v.name => v.id
}
}
22 changes: 20 additions & 2 deletions variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,25 @@ variable "tenant_id" {
}

variable "castai_components_labels" {
type = map
type = map
description = "Optional additional Kubernetes labels for CAST AI pods"
default = {}
default = {}
}

variable "node_configurations" {
type = map(object({
disk_cpu_ratio = optional(number)
subnets = list(string)
ssh_public_key = optional(string)
image = optional(string)
tags = optional(map(string))
max_pods_per_node = optional(number)
}))
description = "Map of AKS node configurations to create"
default = {}
}

variable "default_node_configuration" {
type = string
description = "ID of the default node configuration"
}
3 changes: 2 additions & 1 deletion version.tf → versions.tf
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
terraform {
required_version = ">= 0.13"
experiments = [module_variable_optional_attrs]

required_providers {
azurerm = {
Expand All @@ -12,7 +13,7 @@ terraform {
}
castai = {
source = "castai/castai"
version = ">= 0.18.0"
version = ">= 0.26.3"
}
helm = {
source = "hashicorp/helm"
Expand Down

0 comments on commit 0f04114

Please sign in to comment.