Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
218 changes: 110 additions & 108 deletions README.md

Large diffs are not rendered by default.

24 changes: 16 additions & 8 deletions main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,14 @@ data "azurerm_resource_group" "main" {
name = var.resource_group_name
}

module "ssh-key" {
source = "./modules/ssh-key"
public_ssh_key = var.public_ssh_key == "" ? "" : var.public_ssh_key
moved {
from = module.ssh-key.tls_private_key.ssh
to = tls_private_key.ssh
}

resource "tls_private_key" "ssh" {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not adding here this ?

count = var.admin_username == null ? 0 : 1

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah that works, but it will also require using index when we reference the resource, and I think an unused private key resource won't do harm, so I choose a shortcut.

algorithm = "RSA"
rsa_bits = 2048
}

resource "azurerm_kubernetes_cluster" "main" {
Expand All @@ -17,12 +22,15 @@ resource "azurerm_kubernetes_cluster" "main" {
sku_tier = var.sku_tier
private_cluster_enabled = var.private_cluster_enabled

linux_profile {
admin_username = var.admin_username
dynamic "linux_profile" {
for_each = var.admin_username == null ? [] : ["linux_profile"]
content {
admin_username = var.admin_username

ssh_key {
# remove any new lines using the replace interpolation function
key_data = replace(var.public_ssh_key == "" ? module.ssh-key.public_ssh_key : var.public_ssh_key, "\n", "")
ssh_key {
# remove any new lines using the replace interpolation function
key_data = replace(coalesce(var.public_ssh_key, tls_private_key.ssh.public_key_openssh), "\n", "")
}
}
}

Expand Down
21 changes: 0 additions & 21 deletions modules/ssh-key/main.tf

This file was deleted.

11 changes: 11 additions & 0 deletions outputs.tf
Original file line number Diff line number Diff line change
Expand Up @@ -133,3 +133,14 @@ output "open_service_mesh_enabled" {
output "oidc_issuer_url" {
value = azurerm_kubernetes_cluster.main.oidc_issuer_url
}

output "generated_cluster_public_ssh_key" {
description = "The cluster will use this generated public key as ssh key when `var.public_ssh_key` is empty or null."
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
}

output "generated_cluster_private_ssh_key" {
description = "The cluster will use this generated private key as ssh key when `var.public_ssh_key` is empty or null."
sensitive = true
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
}
2 changes: 2 additions & 0 deletions test/fixture/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,8 @@ module "aks_cluster_name" {
prefix = "prefix"
resource_group_name = azurerm_resource_group.main.name
enable_log_analytics_workspace = true
# Not necessary, just for demo purpose.
admin_username = "azureuser"
cluster_log_analytics_workspace_name = "test-cluster"
net_profile_pod_cidr = "10.1.0.0/16"
identity_type = "UserAssigned"
Expand Down
2 changes: 1 addition & 1 deletion test/fixture/outputs.tf
Original file line number Diff line number Diff line change
Expand Up @@ -67,4 +67,4 @@ output "test_password" {
output "test_kube_raw" {
sensitive = true
value = module.aks.kube_config_raw
}
}
6 changes: 3 additions & 3 deletions variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ variable "client_secret" {
}

variable "admin_username" {
default = "azureuser"
description = "The username of the local administrator to be created on the Kubernetes cluster"
default = null
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."
type = string
}

Expand Down Expand Up @@ -69,7 +69,7 @@ variable "agents_count" {
}

variable "public_ssh_key" {
description = "A custom ssh key to control access to the AKS cluster"
description = "A custom ssh key to control access to the AKS cluster. Changing this forces a new resource to be created."
type = string
default = ""
}
Expand Down
2 changes: 1 addition & 1 deletion versions.tf
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ terraform {
required_providers {
azurerm = {
source = "hashicorp/azurerm"
version = "~> 3.11"
version = "~> 3.3"
}
}

Expand Down