Merge pull request #168 from lonegunmanb/f-201-aks-acr-identity

Fix 201-aks-acr-identity
This commit is contained in:
Mark Gray (MSFT) 2023-02-23 10:39:30 -08:00 committed by GitHub
commit eb63491dc1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 47 additions and 55 deletions

View File

@ -2,9 +2,9 @@ locals {
acr_name = "${replace(var.dns_prefix, "-", "")}${replace(var.name, "-", "")}acr" acr_name = "${replace(var.dns_prefix, "-", "")}${replace(var.name, "-", "")}acr"
} }
resource "azurerm_container_registry" "default" { resource "azurerm_container_registry" "default" {
name = "${local.acr_name}" name = local.acr_name
resource_group_name = "${azurerm_resource_group.default.name}" resource_group_name = azurerm_resource_group.default.name
location = "${azurerm_resource_group.default.location}" location = azurerm_resource_group.default.location
sku = "Standard" sku = "Standard"
admin_enabled = false admin_enabled = false
} }

View File

@ -1,24 +1,20 @@
resource "azurerm_kubernetes_cluster" "default" { resource "azurerm_kubernetes_cluster" "default" {
name = "${var.name}-aks" name = "${var.name}-aks"
location = "${azurerm_resource_group.default.location}" location = azurerm_resource_group.default.location
resource_group_name = "${azurerm_resource_group.default.name}" resource_group_name = azurerm_resource_group.default.name
dns_prefix = "${var.dns_prefix}-${var.name}-aks-${var.environment}" dns_prefix = "${var.dns_prefix}-${var.name}-aks-${var.environment}"
depends_on = ["azurerm_role_assignment.aks_network", "azurerm_role_assignment.aks_acr"] role_based_access_control_enabled = true
agent_pool_profile { default_node_pool {
name = "default" name = "default"
count = "${var.node_count}" vm_size = var.node_type
vm_size = "${var.node_type}" node_count = var.node_count
os_type = "Linux"
os_disk_size_gb = 30 os_disk_size_gb = 30
} }
identity {
service_principal { type = "UserAssigned"
client_id = "${azuread_application.default.application_id}" identity_ids = [azurerm_user_assigned_identity.aks.id]
client_secret = "${azuread_service_principal_password.default.value}"
} }
role_based_access_control { depends_on = [azurerm_role_assignment.aks_network, azurerm_role_assignment.aks_acr]
enabled = true
}
} }

View File

@ -1,30 +1,17 @@
resource "azuread_application" "default" { resource "azurerm_user_assigned_identity" "aks" {
name = "${var.name}-${var.environment}" location = azurerm_resource_group.default.location
} name = "${random_pet.rg.id}-uai"
resource_group_name = azurerm_resource_group.default.name
resource "azuread_service_principal" "default" {
application_id = "${azuread_application.default.application_id}"
}
resource "random_string" "password" {
length = 32
special = true
}
resource "azuread_service_principal_password" "default" {
service_principal_id = "${azuread_service_principal.default.id}"
value = "${random_string.password.result}"
end_date = "2099-01-01T01:00:00Z"
} }
resource "azurerm_role_assignment" "aks_network" { resource "azurerm_role_assignment" "aks_network" {
scope = "${data.azurerm_subscription.current.id}/resourceGroups/${azurerm_resource_group.default.name}" scope = azurerm_resource_group.default.id
role_definition_name = "Network Contributor" role_definition_name = "Network Contributor"
principal_id = "${azuread_service_principal.default.id}" principal_id = azurerm_user_assigned_identity.aks.principal_id
} }
resource "azurerm_role_assignment" "aks_acr" { resource "azurerm_role_assignment" "aks_acr" {
scope = "${data.azurerm_subscription.current.id}/resourceGroups/${azurerm_resource_group.default.name}/providers/Microsoft.ContainerRegistry/registries/${azurerm_container_registry.default.name}" scope = azurerm_container_registry.default.id
role_definition_name = "AcrPull" role_definition_name = "AcrPull"
principal_id = "${azuread_service_principal.default.id}" principal_id = azurerm_user_assigned_identity.aks.principal_id
} }

View File

@ -1,18 +1,27 @@
# The Azure Active Resource Manager Terraform provider terraform {
provider "azurerm" { required_version = ">= 1.3"
version = "=1.36.0" required_providers {
azurerm = {
source = "hashicorp/azurerm"
version = "~> 3.0"
}
}
} }
# The Azure Active Directory Terraform provider provider "azurerm" {
provider "azuread" { features {}
version = "=0.6.0"
} }
# Reference to the current subscription. Used when creating role assignments # Reference to the current subscription. Used when creating role assignments
data "azurerm_subscription" "current" {} data "azurerm_subscription" "current" {}
resource "random_pet" "rg" {
length = 1
prefix = var.name
}
# The main resource group for this deployment # The main resource group for this deployment
resource "azurerm_resource_group" "default" { resource "azurerm_resource_group" "default" {
name = "${var.name}-${var.environment}-rg" name = "${random_pet.rg.id}-${var.environment}-rg"
location = "${var.location}" location = var.location
} }

View File

@ -1,12 +1,12 @@
// Naming // Naming
variable "name" { variable "name" {
type = "string" type = string
description = "Location of the azure resource group." description = "Location of the azure resource group."
default = "demo-tfquickstart" default = "demo-tfquickstart"
} }
variable "environment" { variable "environment" {
type = "string" type = string
description = "Name of the deployment environment" description = "Name of the deployment environment"
default = "dev" default = "dev"
} }
@ -14,7 +14,7 @@ variable "environment" {
// Resource information // Resource information
variable "location" { variable "location" {
type = "string" type = string
description = "Location of the azure resource group." description = "Location of the azure resource group."
default = "WestUS2" default = "WestUS2"
} }
@ -22,19 +22,19 @@ variable "location" {
// Node type information // Node type information
variable "node_count" { variable "node_count" {
type = "string" type = number
description = "The number of K8S nodes to provision." description = "The number of K8S nodes to provision."
default = 3 default = 3
} }
variable "node_type" { variable "node_type" {
type = "string" type = string
description = "The size of each node." description = "The size of each node."
default = "Standard_D1_v2" default = "Standard_D2s_v3"
} }
variable "dns_prefix" { variable "dns_prefix" {
type = "string" type = string
description = "DNS Prefix" description = "DNS Prefix"
default = "tfq" default = "tfq"
} }