Skip to content

Commit c6a2fec

Browse files
authored
Merge pull request #8454 from marosset/azure-terraform-retention-updates
Reconciling azure terraform state with what is actually deployed to azure
2 parents 69ae4cf + 14891bd commit c6a2fec

File tree

10 files changed

+239
-48
lines changed

10 files changed

+239
-48
lines changed

infra/azure/terraform/capz/.terraform.lock.hcl

Lines changed: 40 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
File renamed without changes.

infra/azure/terraform/capz/capz-monitoring/main.tf

Lines changed: 62 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,22 @@ variable "subscription_id" {
2626
type = string
2727
}
2828

29+
locals {
30+
# reproduce the previous pattern seen in state:
31+
# "<first-10-of-rg>-<rg>-<first-6-of-subscription>"
32+
computed_dns_prefix = format("%s-%s-%s",
33+
substr(var.resource_group_name, 0, 10),
34+
var.resource_group_name,
35+
substr(var.subscription_id, 0, 6)
36+
)
37+
}
38+
2939
# Create the "capz-monitoring" resource group
3040
resource "azurerm_resource_group" "capz-monitoring" {
3141
location = var.location
3242
name = var.resource_group_name
3343
tags = {
3444
DO-NOT-DELETE = "contact capz"
35-
creationTimestamp = timestamp()
3645
}
3746
}
3847

@@ -49,8 +58,24 @@ resource "azurerm_role_assignment" "monitoring_reader" {
4958
depends_on = [ azurerm_user_assigned_identity.capz_monitoring_user_identity ]
5059
}
5160

61+
# lookups for AKS-created user assigned identities and DNS zone (do not create new identities)
62+
data "azurerm_user_assigned_identity" "aks_akv" {
63+
name = "azurekeyvaultsecretsprovider-capz-monitoring"
64+
resource_group_name = "MC_capz-monitoring_capz-monitoring_eastus"
65+
}
66+
67+
data "azurerm_user_assigned_identity" "aks_webapp" {
68+
name = "webapprouting-capz-monitoring"
69+
resource_group_name = "MC_capz-monitoring_capz-monitoring_eastus"
70+
}
71+
72+
data "azurerm_dns_zone" "capz_monitoring" {
73+
name = "capz-monitoring.org"
74+
resource_group_name = "capz-monitoring"
75+
}
76+
5277
resource "azurerm_kubernetes_cluster" "capz-monitoring" {
53-
dns_prefix = var.resource_group_name
78+
dns_prefix = local.computed_dns_prefix
5479
location = var.location
5580
name = var.resource_group_name
5681
resource_group_name = var.resource_group_name
@@ -64,16 +89,48 @@ resource "azurerm_kubernetes_cluster" "capz-monitoring" {
6489
]
6590
kubelet_identity {
6691
user_assigned_identity_id = azurerm_user_assigned_identity.capz_monitoring_user_identity.id
92+
client_id = azurerm_user_assigned_identity.capz_monitoring_user_identity.client_id
93+
object_id = azurerm_user_assigned_identity.capz_monitoring_user_identity.principal_id
6794
}
6895
identity {
6996
type = "UserAssigned"
7097
identity_ids = [
7198
azurerm_user_assigned_identity.capz_monitoring_user_identity.id
7299
]
73100
}
101+
102+
# keep AKS addon-managed identities and the DNS zone referenced via data sources
103+
key_vault_secrets_provider {
104+
secret_rotation_enabled = false
105+
secret_rotation_interval = "2m"
106+
107+
# secret_identity is computed by the AKS provider; do not set it here.
108+
}
109+
110+
web_app_routing {
111+
default_nginx_controller = "AnnotationControlled"
112+
dns_zone_ids = [
113+
data.azurerm_dns_zone.capz_monitoring.id,
114+
]
115+
116+
# web_app_routing_identity is created/linked by AKS and is computed; do not set it here.
117+
}
118+
74119
default_node_pool {
75-
name = "default"
76-
node_count = 1
77-
vm_size = "Standard_Ds2_v2"
120+
name = "nodepool1"
121+
node_count = 3
122+
vm_size = "Standard_DS2_v2"
123+
124+
upgrade_settings {
125+
drain_timeout_in_minutes = 0
126+
max_surge = "10%"
127+
node_soak_duration_in_minutes = 0
128+
}
129+
}
130+
131+
lifecycle {
132+
ignore_changes = [
133+
linux_profile
134+
]
78135
}
79136
}

infra/azure/terraform/capz/cluster-api-gallery/main.tf

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ resource "azurerm_resource_group" "cluster-api-gallery" {
2828
name = var.resource_group_name
2929
tags = {
3030
DO-NOT-DELETE = "UpstreamInfra"
31-
creationTimestamp = "2024-10-24T00:00:00Z"
31+
creationTimestamp = "2024-10-03T15:53:21Z"
3232
}
3333
}
3434

@@ -39,8 +39,9 @@ resource "azurerm_shared_image_gallery" "community_gallery" {
3939
name = "community_gallery"
4040
resource_group_name = "cluster-api-gallery"
4141
tags = {
42-
creationTimestamp = "2024-10-24T00:00:00Z"
42+
creationTimestamp = "2024-10-24T17:36:37Z"
4343
jobName = "image-builder-sig-ubuntu-2404"
44+
DO-NOT-DELETE = "UpstreamInfra"
4445
}
4546
sharing {
4647
permission = "Community"
@@ -62,6 +63,7 @@ resource "azurerm_user_assigned_identity" "pipelines_user_identity" {
6263
name = "ado-pipeline-mi"
6364
resource_group_name = var.resource_group_name
6465
tags = {
66+
DO-NOT-DELETE = "UpstreamInfra"
6567
creationTimestamp = "2024-10-24T00:00:00Z"
6668
}
6769
depends_on = [

infra/azure/terraform/capz/container-registry/main.tf

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,7 @@ resource "azurerm_container_registry" "capzci_registry" {
2929
sku = "Premium"
3030
anonymous_pull_enabled = true
3131

32-
retention_policy {
33-
days = 7
34-
enabled = true
35-
}
32+
retention_policy_in_days = 7
3633

3734
tags = {
3835
RetentionPolicy = "7days"
@@ -87,10 +84,7 @@ resource "azurerm_container_registry" "e2eprivate_registry" {
8784
resource_group_name = var.resource_group_name
8885
sku = "Premium"
8986

90-
retention_policy {
91-
days = 7
92-
enabled = true
93-
}
87+
retention_policy_in_days = 7
9488

9589
tags = {
9690
RetentionPolicy = "7days"

infra/azure/terraform/capz/identities/main.tf

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,18 @@ variable "location" {
2222
type = string
2323
}
2424

25+
variable "subscription_id" {
26+
type = string
27+
}
28+
29+
variable "container_registry_scope" {
30+
type = string
31+
}
32+
33+
variable "e2eprivate_registry_scope" {
34+
type = string
35+
}
36+
2537
resource "azurerm_user_assigned_identity" "cloud_provider_user_identity" {
2638
name = "cloud-provider-user-identity"
2739
location = var.location
@@ -40,6 +52,31 @@ resource "azurerm_user_assigned_identity" "gmsa_user_identity" {
4052
resource_group_name = var.resource_group_name
4153
}
4254

55+
resource "azurerm_role_definition" "gmsa_custom_role" {
56+
name = "gMSA"
57+
scope = "/subscriptions/${var.subscription_id}"
58+
description = "Required permissions for gmsa to read properties of subscriptions and managed identities"
59+
60+
permissions {
61+
actions = [
62+
"Microsoft.Resources/subscriptions/read",
63+
"Microsoft.ManagedIdentity/userAssignedIdentities/read"
64+
]
65+
not_actions = []
66+
}
67+
68+
assignable_scopes = [
69+
"/subscriptions/${var.subscription_id}"
70+
]
71+
}
72+
73+
resource "azurerm_role_assignment" "gmsa_role_assignment" {
74+
principal_id = azurerm_user_assigned_identity.domain_vm_identity.principal_id
75+
role_definition_name = azurerm_role_definition.gmsa_custom_role.name
76+
scope = "/subscriptions/${var.subscription_id}"
77+
depends_on = [azurerm_user_assigned_identity.domain_vm_identity]
78+
}
79+
4380
output "cloud_provider_user_identity_id" {
4481
value = azurerm_user_assigned_identity.cloud_provider_user_identity.principal_id
4582
}

infra/azure/terraform/capz/main.tf

Lines changed: 40 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ limitations under the License.
1616

1717
provider "azurerm" {
1818
features {}
19+
subscription_id = var.subscription_id
1920
}
2021

2122
terraform {
@@ -36,10 +37,6 @@ resource "azurerm_resource_provider_registration" "provider-k8s-config" {
3637
}
3738
}
3839

39-
resource "azurerm_resource_provider_registration" "provider-container-service" {
40-
name = "Microsoft.ContainerService"
41-
}
42-
4340
resource "azurerm_marketplace_agreement" "traefik-agreement" {
4441
publisher = "containous"
4542
offer = "traefik-proxy"
@@ -62,7 +59,6 @@ resource "azurerm_marketplace_agreement" "kinvolk-stable2-agreement" {
6259
# Data source to get the current client configuration
6360
data "azurerm_client_config" "current" {}
6461

65-
6662
# Resource group for CAPZ CI resources
6763
resource "azurerm_resource_group" "capz_ci" {
6864
location = var.location
@@ -80,45 +76,58 @@ resource "azurerm_storage_account" "k8sprowstorage" {
8076
min_tls_version = "TLS1_0"
8177
account_replication_type = "RAGRS"
8278
cross_tenant_replication_enabled = true
83-
depends_on = [azurerm_resource_group.capz_ci]
79+
depends_on = [
80+
azurerm_resource_group.capz_ci
81+
]
8482
}
8583

86-
# Import identities module
87-
module "identities" {
88-
source = "./identities"
84+
# Import container registry module
85+
module "container_registry" {
86+
source = "./container-registry"
8987
resource_group_name = var.resource_group_name
9088
location = var.location
91-
depends_on = [azurerm_resource_group.capz_ci]
89+
depends_on = [
90+
azurerm_resource_group.capz_ci
91+
]
92+
}
93+
94+
# Import identities module
95+
module "identities" {
96+
source = "./identities"
97+
resource_group_name = var.resource_group_name
98+
location = var.location
99+
subscription_id = data.azurerm_client_config.current.subscription_id
100+
container_registry_scope = module.container_registry.container_registry_id
101+
e2eprivate_registry_scope = module.container_registry.e2eprivate_registry_id
102+
depends_on = [
103+
azurerm_resource_group.capz_ci
104+
]
92105
}
93106

94107
# Import key vault module
95108
module "key_vault" {
96-
source = "./key-vault"
97-
resource_group_name = var.resource_group_name
98-
location = var.location
99-
tenant_id = data.azurerm_client_config.current.tenant_id
109+
source = "./key-vault"
110+
resource_group_name = var.resource_group_name
111+
location = var.location
112+
tenant_id = data.azurerm_client_config.current.tenant_id
100113
identities = {
101-
cloud_provider_user_identity_id = module.identities.cloud_provider_user_identity_id
102114
domain_vm_identity_id = module.identities.domain_vm_identity_id
103115
gmsa_user_identity_id = module.identities.gmsa_user_identity_id
104116
}
105-
depends_on = [azurerm_resource_group.capz_ci]
106-
}
107-
108-
# Import container registry module
109-
module "container_registry" {
110-
source = "./container-registry"
111-
resource_group_name = var.resource_group_name
112-
location = var.location
113-
depends_on = [azurerm_resource_group.capz_ci]
117+
depends_on = [
118+
azurerm_resource_group.capz_ci
119+
]
114120
}
115121

116122
# Import role assignments module
117123
module "role_assignments" {
118124
source = "./role-assignments"
119125
resource_group_name = var.resource_group_name
120126
container_registry_scope = module.container_registry.container_registry_id
121-
subscription_id = data.azurerm_client_config.current.subscription_id
127+
e2eprivate_registry_scope = module.container_registry.e2eprivate_registry_id
128+
cloud_provider_user_identity_id = module.identities.cloud_provider_user_identity_id
129+
subscription_id = data.azurerm_client_config.current.subscription_id
130+
key_vault_id = module.key_vault.key_vault_id
122131
depends_on = [
123132
azurerm_resource_group.capz_ci,
124133
azurerm_storage_account.k8sprowstorage,
@@ -129,15 +138,17 @@ module "role_assignments" {
129138
# Import Cluster API gallery module
130139
module "cluster_api_gallery" {
131140
source = "./cluster-api-gallery"
132-
resource_group_name = var.resource_group_name
133-
location = var.location
134-
depends_on = module.role_assignments
141+
resource_group_name = "cluster-api-gallery"
142+
location = "northcentralus"
143+
depends_on = [
144+
module.role_assignments
145+
]
135146
}
136147

137148
# Import CAPZ monitoring module
138149
module "capz_monitoring" {
139150
source = "./capz-monitoring"
140-
resource_group_name = var.resource_group_name
151+
resource_group_name = "capz-monitoring"
141152
location = var.location
142153
subscription_id = data.azurerm_client_config.current.subscription_id
143154
}

0 commit comments

Comments
 (0)