Skip to content

Commit e2504ab

Browse files
Merge pull request openshift#7642 from AnnaZivkovic/azure_duplicate_image_gallery
CORS-2525: Azure: remove storage account with bootstrap destroy
2 parents 04a22e6 + 178280a commit e2504ab

File tree

10 files changed

+251
-104
lines changed

10 files changed

+251
-104
lines changed

data/data/azure/bootstrap/main.tf

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ provider "azurerm" {
1717
}
1818

1919
data "azurerm_storage_account" "storage_account" {
20-
name = var.storage_account_name
20+
name = azurerm_storage_account.cluster.name
2121
resource_group_name = var.resource_group_name
2222
}
2323

@@ -57,13 +57,13 @@ data "azurerm_storage_account_sas" "ignition" {
5757

5858
resource "azurerm_storage_container" "ignition" {
5959
name = "ignition"
60-
storage_account_name = var.storage_account_name
60+
storage_account_name = azurerm_storage_account.cluster.name
6161
}
6262

6363
resource "azurerm_storage_blob" "ignition" {
6464
name = "bootstrap.ign"
6565
source = var.ignition_bootstrap_file
66-
storage_account_name = var.storage_account_name
66+
storage_account_name = azurerm_storage_account.cluster.name
6767
storage_container_name = azurerm_storage_container.ignition.name
6868
type = var.azure_keyvault_key_name != "" ? "Page" : "Block"
6969
}
@@ -223,7 +223,7 @@ resource "azurerm_linux_virtual_machine" "bootstrap" {
223223
}
224224

225225
# Either source_image_id or source_image_reference must be defined
226-
source_image_id = ! var.azure_use_marketplace_image ? var.vm_image : null
226+
source_image_id = azurerm_shared_image_version.bootstrap_image_version.id
227227

228228
dynamic "source_image_reference" {
229229
for_each = var.azure_use_marketplace_image ? [1] : []
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,12 @@
11
output "bootstrap_ip" {
22
value = var.azure_private ? azurerm_network_interface.bootstrap.private_ip_address : azurerm_public_ip.bootstrap_public_ip_v4[0].ip_address
33
}
4+
5+
output "storage_account_id" {
6+
value = azurerm_storage_account.cluster.id
7+
}
8+
9+
output "storage_rhcos_image_url" {
10+
value = azurerm_storage_blob.rhcos_image.url
11+
}
12+
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
locals {
2+
tags = merge(
3+
{
4+
"kubernetes.io_cluster.${var.cluster_id}" = "owned"
5+
},
6+
var.azure_extra_tags,
7+
)
8+
9+
# At this time min_tls_version is only supported in the Public Cloud and US Government Cloud.
10+
environments_with_min_tls_version = ["public", "usgovernment"]
11+
}
12+
13+
resource "azurerm_storage_account" "cluster" {
14+
name = "cluster${var.random_storage_account_suffix}"
15+
resource_group_name = var.resource_group_name
16+
location = var.azure_region
17+
account_tier = var.azure_keyvault_name != "" ? "Premium" : "Standard"
18+
account_replication_type = "LRS"
19+
min_tls_version = contains(local.environments_with_min_tls_version, var.azure_environment) ? "TLS1_2" : null
20+
allow_nested_items_to_be_public = var.azure_keyvault_name != "" ? true : false
21+
tags = var.azure_extra_tags
22+
23+
dynamic "customer_managed_key" {
24+
for_each = var.azure_keyvault_name != "" ? [1] : []
25+
content {
26+
key_vault_key_id = var.key_vault_key_id
27+
user_assigned_identity_id = user_assigned_identity_id
28+
}
29+
}
30+
31+
dynamic identity {
32+
for_each = var.azure_keyvault_name != "" ? [1] : []
33+
content {
34+
type = "UserAssigned"
35+
identity_ids = [user_assigned_identity_id]
36+
}
37+
}
38+
}
39+
40+
# copy over the vhd to cluster resource group and create an image using that
41+
resource "azurerm_storage_container" "vhd" {
42+
name = "vhd"
43+
storage_account_name = azurerm_storage_account.cluster.name
44+
}
45+
46+
resource "azurerm_storage_blob" "rhcos_image" {
47+
name = "rhcos${var.random_storage_account_suffix}.vhd"
48+
storage_account_name = azurerm_storage_account.cluster.name
49+
storage_container_name = azurerm_storage_container.vhd.name
50+
type = "Page"
51+
source_uri = var.azure_image_url
52+
metadata = tomap({ source_uri = var.azure_image_url })
53+
}
54+
55+
resource "azurerm_shared_image" "bootstrap_gen2" {
56+
name = "${var.cluster_id}-bootstrap-gen2"
57+
gallery_name = var.image_version_gallery_name
58+
resource_group_name = var.resource_group_name
59+
location = var.azure_region
60+
os_type = "Linux"
61+
hyper_v_generation = "V2"
62+
architecture = var.azure_vm_architecture
63+
64+
identifier {
65+
publisher = "RedHat-gen2"
66+
offer = "rhcos-gen2"
67+
sku = "bootstrap"
68+
}
69+
70+
tags = var.azure_extra_tags
71+
}
72+
73+
resource "azurerm_shared_image_version" "bootstrap_image_version" {
74+
name = var.azure_image_release
75+
gallery_name = azurerm_shared_image.bootstrap_gen2.gallery_name
76+
image_name = azurerm_shared_image.bootstrap_gen2.name
77+
resource_group_name = var.resource_group_name
78+
location = var.azure_region
79+
80+
blob_uri = azurerm_storage_blob.rhcos_image.url
81+
storage_account_id = azurerm_storage_account.cluster.id
82+
83+
target_region {
84+
name = var.azure_region
85+
regional_replica_count = 1
86+
}
87+
88+
tags = var.azure_extra_tags
89+
}

data/data/azure/bootstrap/variables.tf

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,14 +37,24 @@ variable "resource_group_name" {
3737
description = "The resource group name for the deployment."
3838
}
3939

40-
variable "storage_account_name" {
40+
variable "image_version_gallery_name" {
4141
type = string
42-
description = "the name of the storage account for the cluster. It can be used for boot diagnostics."
42+
description = "The name of the image gallery used to set up shared images."
4343
}
4444

45-
variable "vm_image" {
45+
variable "image_version_gen2_gallery_name" {
4646
type = string
47-
description = "The resource id of the vm image used for bootstrap."
47+
description = "The name of the gen2 image gallery used to set up shared images."
48+
}
49+
50+
variable "image_version_name" {
51+
type = string
52+
description = "The name of shared image used to set up shared images."
53+
}
54+
55+
variable "image_version_gen2_name" {
56+
type = string
57+
description = "The name of the gen2 shared image used to set up shared images."
4858
}
4959

5060
variable "identity" {

data/data/azure/cluster/main.tf

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ module "master" {
2525
vm_size = var.azure_master_vm_type
2626
disk_encryption_set_id = var.azure_master_disk_encryption_set_id
2727
encryption_at_host_enabled = var.azure_master_encryption_at_host_enabled
28-
vm_image = var.vm_image
2928
identity = var.identity
3029
ignition = var.ignition_master
3130
elb_backend_pool_v4_id = var.elb_backend_pool_v4_id
@@ -53,6 +52,16 @@ module "master" {
5352
secure_boot = var.azure_master_secure_boot
5453
virtualized_trusted_platform_module = var.azure_master_virtualized_trusted_platform_module
5554

55+
storage_account_id = var.storage_account_id
56+
storage_rhcos_image_url = var.storage_rhcos_image_url
57+
image_version_gallery_name = var.image_version_gallery_name
58+
image_version_gen2_gallery_name = var.image_version_gen2_gallery_name
59+
image_version_name = var.image_version_name
60+
image_version_gen2_name = var.image_version_gen2_name
61+
azure_image_release = var.azure_image_release
62+
azure_region = var.azure_region
63+
azure_hypervgeneration_version = var.azure_hypervgeneration_version
64+
5665
use_ipv4 = var.use_ipv4
5766
use_ipv6 = var.use_ipv6
5867
}

data/data/azure/cluster/master/master.tf

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,44 @@ locals {
44
ip_v4_configuration_name = "pipConfig"
55
// TODO: Azure machine provider probably needs to look for pipConfig-v6 as well (or a different name like pipConfig-secondary)
66
ip_v6_configuration_name = "pipConfig-v6"
7+
8+
vm_image = var.azure_hypervgeneration_version == "V2" ? azurerm_shared_image_version.clustergen2_image_version.id : azurerm_shared_image_version.cluster_image_version.id
9+
10+
}
11+
resource "azurerm_shared_image_version" "cluster_image_version" {
12+
name = var.azure_image_release
13+
gallery_name = var.image_version_gallery_name
14+
image_name = var.image_version_name
15+
resource_group_name = var.resource_group_name
16+
location = var.azure_region
17+
18+
blob_uri = var.storage_rhcos_image_url
19+
storage_account_id = var.storage_account_id
20+
21+
target_region {
22+
name = var.azure_region
23+
regional_replica_count = 1
24+
}
25+
26+
tags = var.azure_extra_tags
27+
}
28+
29+
resource "azurerm_shared_image_version" "clustergen2_image_version" {
30+
name = var.azure_image_release
31+
gallery_name = var.image_version_gen2_gallery_name
32+
image_name = var.image_version_gen2_name
33+
resource_group_name = var.resource_group_name
34+
location = var.azure_region
35+
36+
blob_uri = var.storage_rhcos_image_url
37+
storage_account_id = var.storage_account_id
38+
39+
target_region {
40+
name = var.azure_region
41+
regional_replica_count = 1
42+
}
43+
44+
tags = var.azure_extra_tags
745
}
846

947
resource "azurerm_network_interface" "master" {
@@ -124,8 +162,7 @@ resource "azurerm_linux_virtual_machine" "master" {
124162
}
125163

126164
# Either source_image_id or source_image_reference must be defined
127-
source_image_id = ! var.use_marketplace_image ? var.vm_image : null
128-
165+
source_image_id = ! var.use_marketplace_image ? local.vm_image : null
129166
dynamic "source_image_reference" {
130167
for_each = var.use_marketplace_image ? [1] : []
131168

data/data/azure/cluster/master/variables.tf

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,46 @@ variable "encryption_at_host_enabled" {
2727
description = "Enables encryption at the VM host."
2828
}
2929

30-
variable "vm_image" {
30+
variable "storage_account_id" {
3131
type = string
32-
description = "The resource id of the vm image used for masters."
32+
description = "The storage account ID for the cluster. It can be used for boot diagnostics"
33+
}
34+
35+
variable "storage_rhcos_image_url" {
36+
type = string
37+
description = "The rhcos image url used to identify the vm image for bootstrap and cluster."
38+
}
39+
40+
variable "image_version_gallery_name" {
41+
type = string
42+
description = "The name of the image gallery used to set up shared images."
43+
}
44+
45+
variable "image_version_gen2_gallery_name" {
46+
type = string
47+
description = "The name of the gen2 image gallery used to set up shared images."
48+
}
49+
50+
variable "image_version_name" {
51+
type = string
52+
description = "The name of shared image used to set up shared images."
53+
}
54+
55+
variable "image_version_gen2_name" {
56+
type = string
57+
description = "The name of the gen2 shared image used to set up shared images."
58+
}
59+
60+
variable "azure_region" {
61+
type = string
62+
}
63+
64+
variable "azure_image_release" {
65+
type = string
66+
}
67+
68+
variable "azure_hypervgeneration_version" {
69+
type = string
3370
}
3471

3572
variable "use_marketplace_image" {

data/data/azure/cluster/variables.tf

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,34 @@ variable "resource_group_name" {
6262
description = "The resource group name for the deployment."
6363
}
6464

65-
variable "vm_image" {
65+
variable "storage_account_id" {
6666
type = string
67-
description = "The resource id of the vm image used for bootstrap."
67+
description = "The storage account ID for the cluster. It can be used for boot diagnostics"
68+
}
69+
70+
variable "storage_rhcos_image_url" {
71+
type = string
72+
description = "The rhcos image url used to identify the vm image for bootstrap and cluster."
73+
}
74+
75+
variable "image_version_gallery_name" {
76+
type = string
77+
description = "The name of the image gallery used to set up shared images."
78+
}
79+
80+
variable "image_version_gen2_gallery_name" {
81+
type = string
82+
description = "The name of the gen2 image gallery used to set up shared images."
83+
}
84+
85+
variable "image_version_name" {
86+
type = string
87+
description = "The name of shared image used to set up shared images."
88+
}
89+
90+
variable "image_version_gen2_name" {
91+
type = string
92+
description = "The name of the gen2 shared image used to set up shared images."
6893
}
6994

7095
variable "identity" {

0 commit comments

Comments
 (0)