@@ -12,31 +12,100 @@ resource "azurerm_container_registry" "this_container_registry" {
1212 depends_on = [azurerm_resource_group . this_resource_group ]
1313}
1414
15- # resource "azurerm_kubernetes_cluster" "this_aks_cluster" {
16- # name = var.aks_name
17- # location = var.location
18- # resource_group_name = azurerm_resource_group.this_resource_group.name
19- # dns_prefix = "DSB"
20-
21-
22- # default_node_pool {
23- # name = "default"
24- # node_count = 1
25- # vm_size = "Standard_A2_v2"
26- # }
27-
28- # identity {
29- # type = "UserAssigned"
30- # identity_ids = [azurerm_user_assigned_identity.this_uaid.id]
31- # }
32-
33- # tags = {
34- # Environment = "Production"
35- # }
36- # depends_on = [
37- # azurerm_role_assignment.uaid_contributor,
38- # azurerm_role_assignment.acr_pull,
39- # azurerm_role_assignment.acr_push
40- # ]
41- # }
15+ resource "azuread_application" "sp_app" {
16+ display_name = " ado-aks-deployer"
17+ }
18+
19+ resource "azuread_service_principal" "sp" {
20+ client_id = azuread_application. sp_app . client_id
21+ }
22+
23+ resource "azuread_application_password" "sp_secret" {
24+ application_id = azuread_application. sp_app . object_id
25+ display_name = " ado-aks-deployer-secret"
26+ rotate_when_changed = { rotation = timestamp () } # forces regen if you ever need
27+ }
28+
29+ # Get the AKS and RG
30+ data "azurerm_resource_group" "rg" {
31+ name = var. resource_group_name
32+ }
33+
34+ data "azurerm_kubernetes_cluster" "aks" {
35+ name = var. aks_name
36+ resource_group_name = data. azurerm_resource_group . rg . name
37+ }
38+
39+ # Look up built-in roles
40+ data "azurerm_role_definition" "contrib" {
41+ name = " Contributor"
42+ scope = data. azurerm_kubernetes_cluster . aks . id
43+ }
44+
45+ data "azurerm_role_definition" "aks_admin" {
46+ name = " Azure Kubernetes Service Cluster Admin Role"
47+ scope = data. azurerm_kubernetes_cluster . aks . id
48+ }
49+
50+ # Assign roles to the SPN
51+ resource "azurerm_role_assignment" "ra_contrib" {
52+ scope = data. azurerm_kubernetes_cluster . aks . id
53+ role_definition_id = data. azurerm_role_definition . contrib . role_definition_id
54+ principal_id = azuread_service_principal. sp . object_id
55+ principal_type = " ServicePrincipal"
56+ }
57+
58+ resource "azurerm_role_assignment" "ra_aks_admin" {
59+ scope = data. azurerm_kubernetes_cluster . aks . id
60+ role_definition_id = data. azurerm_role_definition . aks_admin . role_definition_id
61+ principal_id = azuread_service_principal. sp . object_id
62+ principal_type = " ServicePrincipal"
63+ }
64+
65+ resource "azuredevops_serviceendpoint_azurerm" "arm_sc" {
66+ project_id = azuredevops_project. this . id
67+ service_endpoint_name = " ARM Service Connection"
68+ description = " ARM service connection for AKS deploys"
69+
70+ # Azure environment; use AzureCloud, AzureUSGovernment, AzureChinaCloud, etc.
71+ environment = " AzureCloud"
72+
73+ # Subscription context shown in ADO UI
74+ azurerm_spn_tenantid = var. TFC_AZ_TENANT_ID
75+ azurerm_subscription_id = var. TFC_AZ_SUBSCRIPTION_ID
76+
77+ # Auth with SP + secret
78+ credentials {
79+ serviceprincipalid = azuread_application. sp_app . client_id
80+ serviceprincipalkey = azuread_application_password. sp_secret . value
81+ }
82+ }
83+
84+ resource "azurerm_kubernetes_cluster" "this_aks_cluster" {
85+ name = var. aks_name
86+ location = var. location
87+ resource_group_name = azurerm_resource_group. this_resource_group . name
88+ dns_prefix = " DSB"
89+
90+
91+ default_node_pool {
92+ name = " default"
93+ node_count = 1
94+ vm_size = " Standard_A2_v2"
95+ }
96+
97+ identity {
98+ type = " UserAssigned"
99+ identity_ids = [azurerm_user_assigned_identity . this_uaid . id ]
100+ }
101+
102+ tags = {
103+ Environment = " Production"
104+ }
105+ depends_on = [
106+ azurerm_role_assignment . uaid_contributor ,
107+ azurerm_role_assignment . acr_pull ,
108+ azurerm_role_assignment . acr_push
109+ ]
110+ }
42111
0 commit comments