Skip to content

Commit 87a5ab2

Browse files
committed
Makes linux_profile dynamic and NO linux_profile block by default. All existing cluster need to assign admin_username(which default value is azureuser) explicitly OR THE CLUSTER WILL BE REPLACED!
1 parent a3f637c commit 87a5ab2

4 files changed

Lines changed: 15 additions & 10 deletions

File tree

main.tf

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,15 @@ resource "azurerm_kubernetes_cluster" "main" {
2222
sku_tier = var.sku_tier
2323
private_cluster_enabled = var.private_cluster_enabled
2424

25-
linux_profile {
26-
admin_username = var.admin_username
25+
dynamic "linux_profile" {
26+
for_each = var.admin_username == null ? [] : ["linux_profile"]
27+
content {
28+
admin_username = var.admin_username
2729

28-
ssh_key {
29-
# remove any new lines using the replace interpolation function
30-
key_data = replace(coalesce(var.public_ssh_key, tls_private_key.ssh.public_key_openssh), "\n", "")
30+
ssh_key {
31+
# remove any new lines using the replace interpolation function
32+
key_data = replace(coalesce(var.public_ssh_key, tls_private_key.ssh.public_key_openssh), "\n", "")
33+
}
3134
}
3235
}
3336

outputs.tf

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -132,11 +132,11 @@ output "open_service_mesh_enabled" {
132132

133133
output "generated_cluster_public_ssh_key" {
134134
description = "The cluster will use this generated public key as ssh key when `var.public_ssh_key` is empty or null."
135-
value = var.public_ssh_key == "" || var.public_ssh_key == null ? tls_private_key.ssh.public_key_openssh : null
135+
value = try(azurerm_kubernetes_cluster.main.linux_profile[0], null) != null ? (var.public_ssh_key == "" || var.public_ssh_key == null ? tls_private_key.ssh.public_key_openssh : null) : null
136136
}
137137

138138
output "generated_cluster_private_ssh_key" {
139139
description = "The cluster will use this generated private key as ssh key when `var.public_ssh_key` is empty or null."
140140
sensitive = true
141-
value = var.public_ssh_key == "" || var.public_ssh_key == null ? tls_private_key.ssh.private_key_pem : null
141+
value = try(azurerm_kubernetes_cluster.main.linux_profile[0], null) != null ? (var.public_ssh_key == "" || var.public_ssh_key == null ? tls_private_key.ssh.private_key_pem : null) : null
142142
}

test/fixture/main.tf

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,8 @@ module "aks_cluster_name" {
8888
prefix = "prefix"
8989
resource_group_name = azurerm_resource_group.main.name
9090
enable_log_analytics_workspace = true
91+
# Not necessary, just for demo purpose.
92+
admin_username = "azureuser"
9193
cluster_log_analytics_workspace_name = "test-cluster"
9294
net_profile_pod_cidr = "10.1.0.0/16"
9395
identity_type = "UserAssigned"

variables.tf

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,8 @@ variable "client_secret" {
3939
}
4040

4141
variable "admin_username" {
42-
default = "azureuser"
43-
description = "The username of the local administrator to be created on the Kubernetes cluster"
42+
default = null
43+
description = "The username of the local administrator to be created on the Kubernetes cluster. Set this variable to `null` to turn off the cluster's `linux_profile`. Changing this forces a new resource to be created."
4444
type = string
4545
}
4646

@@ -69,7 +69,7 @@ variable "agents_count" {
6969
}
7070

7171
variable "public_ssh_key" {
72-
description = "A custom ssh key to control access to the AKS cluster"
72+
description = "A custom ssh key to control access to the AKS cluster. Changing this forces a new resource to be created."
7373
type = string
7474
default = ""
7575
}

0 commit comments

Comments
 (0)