Skip to content

Commit

Permalink
Merge pull request #10 from Justin-DynamicD/add-labels
Browse files Browse the repository at this point in the history
Add custom label support
  • Loading branch information
Justin-DynamicD authored Sep 7, 2023
2 parents 05a51f3 + cf6abc4 commit ebfdf22
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 32 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,8 @@ node_default_pool = {
| min_count | number | no | 3 | minimum number of nodes |
| name | string | no | "system" | sets the name of the default node pool |
| node_count | number | no | 3 | sets the initial node count |
| node_labels | map | no | null | add labels to the nodes |
| node_taints | list | no | null | add taints to the nodes |
| only_critical_addons_enabled | bool | no | true | sets the node pool as type "system" restricting user workloads |
| os_disk_size_gb | number | no | 70 | size of node disks in GB |
| os_disk_type | string | no | "Ephemeral" | type of disk |
Expand Down Expand Up @@ -167,12 +169,16 @@ node_user_pool = {
| mode | string | no | "User" | sets pool mode between User/System |
| name | string | no | "user" | sets the name of the node pool |
| node_count | number | no | 2 | sets the initial node count |
| node_labels | map | no | {}[^1] | add labels to the nodes |
| node_taints | list | no | [][^1] | add taints to the nodes |
| os_disk_size_gb | number | no | 120 | size of node disks in GB |
| os_disk_type | string | no | "Ephemeral" | type of disk |
| priority | string | no | "Regular" | the type of nodes |
| spot_max_price | number | no | -1 | used with spot instances, set a price limit on server cost, -1 means no limit |
| vm_size | string | no | "Standard_D4ds_v5" | set the node type |

[^1]: `node_labels` and `node_taints` are merged with default labels as [recomended by Microsoft](https://docs.microsoft.com/en-us/azure/aks/spot-node-pool). As of this writing, this is specific to Spot instances.

### oms

While log analytics and workspaces are beyond the reach of this module, it _does_ include the ability to configure the diagnostic logging for both the cluster and AGW (if exists). All of the logs currently use the same `retention_days` setting.
Expand Down
30 changes: 5 additions & 25 deletions aks.tf
Original file line number Diff line number Diff line change
@@ -1,25 +1,3 @@
# this local block follows Azure Documentation for node labels + taints
# and contains thier configuration which is applied by priority
# details: https://docs.microsoft.com/en-us/azure/aks/spot-node-pool

locals {
aks_node_extra = {
Regular = {
labels = {}
taints = []
}
Spot = {
labels = {
"kubernetes.azure.com/scalesetpriority" = "spot"
}
taints = [
"kubernetes.azure.com/scalesetpriority=spot:NoSchedule"
]
}
}
}


resource "azurerm_kubernetes_cluster" "main" {
lifecycle {
# due to auto-scaling we need to ignore the nodecount after launch
Expand Down Expand Up @@ -57,6 +35,8 @@ resource "azurerm_kubernetes_cluster" "main" {
min_count = local.node_default_pool.min_count
name = local.node_default_pool.name
node_count = local.node_default_pool.node_count
node_labels = local.node_default_pool.node_labels
node_taints = local.node_default_pool.node_taints
only_critical_addons_enabled = local.node_default_pool.only_critical_addons_enabled
os_disk_size_gb = local.node_default_pool.os_disk_size_gb
os_disk_type = local.node_default_pool.os_disk_type
Expand Down Expand Up @@ -90,8 +70,8 @@ resource "azurerm_kubernetes_cluster_node_pool" "user" {
mode = local.node_user_pool.mode
name = local.node_user_pool.name
node_count = local.node_user_pool.node_count
node_labels = local.aks_node_extra[local.node_user_pool.priority].labels
node_taints = local.aks_node_extra[local.node_user_pool.priority].taints
node_labels = local.node_user_pool_merged.node_labels
node_taints = local.node_user_pool_merged.node_taints
os_disk_size_gb = local.node_user_pool.os_disk_size_gb
os_disk_type = local.node_user_pool.os_disk_type
priority = local.node_user_pool.priority
Expand All @@ -101,4 +81,4 @@ resource "azurerm_kubernetes_cluster_node_pool" "user" {
vm_size = local.node_user_pool.vm_size
vnet_subnet_id = local.subnet_id # must be defined or terraform will redeploy despite documentation stating optional
zones = local.zones != [] ? local.zones : null
}
}
2 changes: 1 addition & 1 deletion diagnostics.tf
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,4 @@ resource "azurerm_monitor_diagnostic_setting" "agw" {
days = local.oms.retention_days
}
}
}
}
36 changes: 34 additions & 2 deletions main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

locals {
# ensure agw priority is set if sku is of type "v2"
# if nothing is provided, we will set to 1 for v2, or -1 to omit
# if nothing is provided, we will set to 1 for v2, or -1 to omit
detect_priority = length(regexall("v2$", var.app_gateway.sku_tier)) > 0 ? 10 : null
priority = coalesce(var.app_gateway.priority, local.detect_priority, -1)

Expand All @@ -15,7 +15,39 @@ locals {
agw = coalesce(var.app_gateway.name, "${var.name_prefix}-agw")
}

# these are unmodified, just dropped into locals for cconsistency
# This block follows Azure Documentation for default node labels + taints
# which is unique to each priority type.
# details: https://docs.microsoft.com/en-us/azure/aks/spot-node-pool
node_user_pool_defaults = {
Regular = {
node_labels = {}
node_taints = []
}
Spot = {
node_labels = {
"kubernetes.azure.com/scalesetpriority" = "spot"
}
node_taints = [
"kubernetes.azure.com/scalesetpriority=spot:NoSchedule"
]
}
}

# merges the node_user_pool_defaults with the node_user_pool via
# priority type (see above). Allows user to add values.
# node_user_pool = var.node_user_pool
node_user_pool_merged = {
node_labels = merge(
var.node_user_pool.node_labels,
local.node_user_pool_defaults[var.node_user_pool.priority].node_labels
)
node_taints = concat(
var.node_user_pool.node_taints,
local.node_user_pool_defaults[var.node_user_pool.priority].node_taints
)
}

# these are unmodified, just dropped into locals for consistency
acr_list = var.acr_list
app_gateway = var.app_gateway
automatic_channel_upgrade = var.automatic_channel_upgrade
Expand Down
2 changes: 1 addition & 1 deletion outputs.tf
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,4 @@

# output "identity_client_id" {
# value = azurerm_user_assigned_identity.testIdentity.client_id
# }
# }
8 changes: 6 additions & 2 deletions variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ variable "node_default_pool" {
min_count = optional(number, 3)
name = optional(string, "system")
node_count = optional(number, 3)
node_labels = optional(map(any))
node_taints = optional(list(string))
only_critical_addons_enabled = optional(bool, true)
os_disk_size_gb = optional(number, 70)
os_disk_type = optional(string, "Ephemeral")
Expand All @@ -57,6 +59,8 @@ variable "node_user_pool" {
mode = optional(string, "User")
name = optional(string, "user")
node_count = optional(number, 2)
node_labels = optional(map(any), {}) # needs defaults as we merge it later
node_taints = optional(list(string), []) # needs defaults as we concat it later
os_disk_size_gb = optional(number, 120)
os_disk_type = optional(string, "Ephemeral")
priority = optional(string, "Regular")
Expand All @@ -74,7 +78,7 @@ variable "oms" {
ApplicationGatewayAccessLog = optional(bool, true)
ApplicationGatewayPerformanceLog = optional(bool, true)
ApplicationGatewayFirewallLog = optional(bool, true)
}))
}), {})
agw_metrics = optional(bool, true)
aks_logs = optional(object({
cloud-controller-manager = optional(bool, false)
Expand All @@ -88,7 +92,7 @@ variable "oms" {
kube-audit-admin = optional(bool, true)
kube-controller-manager = optional(bool, true)
kube-scheduler = optional(bool, false)
}))
}), {})
aks_metrics = optional(bool, true)
retention_days = optional(number, 30)
storage_account_id = optional(string)
Expand Down
2 changes: 1 addition & 1 deletion versions.tf
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# required provider versions
######
terraform {
required_version = ">= 1.3.0"
required_version = ">= 1.4.0"
required_providers {
azurerm = ">= 3.32.0"
random = ">= 3.4.0"
Expand Down

0 comments on commit ebfdf22

Please sign in to comment.