From 19e5fd7830dfc0aad23b35eb73d65b7a81a65253 Mon Sep 17 00:00:00 2001 From: Sean Knox Date: Thu, 29 Mar 2018 21:31:11 -0700 Subject: [PATCH] (terraform): build VM image from VHD blob and pass as module --- terraform/Makefile | 2 +- terraform/io.tf | 2 +- terraform/main.tf | 41 +++++++++++++++++----------- terraform/modules/bastion/bastion.tf | 7 +---- terraform/modules/bastion/io.tf | 2 +- terraform/modules/image/image.tf | 18 ++++++++++++ terraform/modules/image/io.tf | 12 ++++++++ terraform/modules/master/io.tf | 3 +- terraform/modules/master/master.tf | 7 +---- terraform/modules/node/io.tf | 2 +- terraform/modules/node/node.tf | 7 +---- terraform/scripts/init-variables | 4 +-- 12 files changed, 65 insertions(+), 42 deletions(-) create mode 100644 terraform/modules/image/image.tf create mode 100644 terraform/modules/image/io.tf diff --git a/terraform/Makefile b/terraform/Makefile index 58c0316..f371621 100644 --- a/terraform/Makefile +++ b/terraform/Makefile @@ -26,7 +26,7 @@ export CLUSTER_NAME ?= mycluster export AZURE_VM_KEY_NAME ?= acstack-$(CLUSTER_NAME) export AZURE_VM_KEY_PATH := ${DIR_KEY_PAIR}/${AZURE_VM_KEY_NAME}.pem -export AZURE_IMAGE_NAME ?= acstack-ubuntu-17.10-1522339580 +export AZURE_VHD_URI ?= https://acstackimages.blob.core.windows.net/system/Microsoft.Compute/Images/acs-vhds/acstack-1522124889-osDisk.8d0e099c-0ed0-483e-9d53-053057eb13b0.vhd export INTERNAL_TLD := ${CLUSTER_NAME}.acs export HYPERKUBE_IMAGE ?= quay.io/coreos/hyperkube diff --git a/terraform/io.tf b/terraform/io.tf index de0ab65..7dc949c 100644 --- a/terraform/io.tf +++ b/terraform/io.tf @@ -26,7 +26,7 @@ variable "node_count" { } variable "name" {} -variable "azure_image_name" {} +variable "azure_vhd_uri" {} variable "cidr" { default = { diff --git a/terraform/main.tf b/terraform/main.tf index f77849e..dddcaea 100644 --- a/terraform/main.tf +++ b/terraform/main.tf @@ -44,18 +44,28 @@ module "dns" { name = "${ var.name }" } -module "bastion" { - source = "./modules/bastion" - depends-id = "${ module.vnet.depends-id }" +module "image" { + source = "./modules/image" + depends-id = "${ module.rg.depends-id }" # variables - name = "${ var.name }" - azure_image_name = "${ var.azure_image_name }" - location = "${ var.location }" + name = "${ var.name }" + location = "${ var.location }" + azure_vhd_uri = "${ var.azure_vhd_uri }" +} + +module "bastion" { + source = "./modules/bastion" + depends-id = "${ module.image.depends-id }" + + # variables + name = "${ var.name }" + location = "${ var.location }" # modules private-subnet-id = "${ module.vnet.private-subnet-id }" storage_endpoint = "${ module.storage_account.primary_blob_endpoint }" + image_id = "${ module.image.image_id }" } module "master" { @@ -63,15 +73,14 @@ module "master" { depends-id = "${ module.bastion.depends-id }" # variables - name = "${ var.name }" - azure_image_name = "${ var.azure_image_name }" - location = "${ var.location }" - instances = "${ length( split(",", var.etcd-ips) ) }" - etcd-ips = "${ var.etcd-ips }" + name = "${ var.name }" + location = "${ var.location }" + etcd-ips = "${ var.etcd-ips }" # modules private-subnet-id = "${ module.vnet.private-subnet-id }" storage_endpoint = "${ module.storage_account.primary_blob_endpoint }" + image_id = "${ module.image.image_id }" } module "node" { @@ -79,14 +88,14 @@ module "node" { depends-id = "${ module.bastion.depends-id }" # variables - name = "${ var.name }" - azure_image_name = "${ var.azure_image_name }" - location = "${ var.location }" - node_count = "${ var.node_count }" - etcd-ips = "${ var.etcd-ips }" + name = "${ var.name }" + location = "${ var.location }" + node_count = "${ var.node_count }" + etcd-ips = "${ var.etcd-ips }" # modules private-subnet-id = "${ module.vnet.private-subnet-id }" bastion-ip = "${ module.bastion.public-ip }" storage_endpoint = "${ module.storage_account.primary_blob_endpoint }" + image_id = "${ module.image.image_id }" } diff --git a/terraform/modules/bastion/bastion.tf b/terraform/modules/bastion/bastion.tf index 1718844..97f96a4 100644 --- a/terraform/modules/bastion/bastion.tf +++ b/terraform/modules/bastion/bastion.tf @@ -2,11 +2,6 @@ data "azurerm_resource_group" "image" { name = "ACStackImages" } -data "azurerm_image" "image" { - name = "${ var.azure_image_name }" - resource_group_name = "${data.azurerm_resource_group.image.name}" -} - resource "azurerm_public_ip" "bastion" { name = "bastion" location = "${ var.location }" @@ -45,7 +40,7 @@ resource "azurerm_virtual_machine" "bastion" { delete_data_disks_on_termination = true storage_image_reference { - id = "${data.azurerm_image.image.id}" + id = "${ var.image_id }" } storage_os_disk { diff --git a/terraform/modules/bastion/io.tf b/terraform/modules/bastion/io.tf index 3d927fc..661a96c 100644 --- a/terraform/modules/bastion/io.tf +++ b/terraform/modules/bastion/io.tf @@ -3,7 +3,7 @@ variable "location" {} variable "private-subnet-id" {} variable "depends-id" {} variable "storage_endpoint" {} -variable "azure_image_name" {} +variable "image_id" {} output "depends-id" { value = "${null_resource.dummy_dependency.id}" diff --git a/terraform/modules/image/image.tf b/terraform/modules/image/image.tf new file mode 100644 index 0000000..21d5aa6 --- /dev/null +++ b/terraform/modules/image/image.tf @@ -0,0 +1,18 @@ +resource "azurerm_image" "acs" { + name = "${ var.name }" + location = "${ var.location }" + resource_group_name = "${ var.name }" + + os_disk { + os_type = "Linux" + os_state = "Generalized" + blob_uri = "${ var.azure_vhd_uri }" + size_gb = 30 + } +} + +resource "null_resource" "dummy_dependency" { + depends_on = [ + "azurerm_image.acs", + ] +} diff --git a/terraform/modules/image/io.tf b/terraform/modules/image/io.tf new file mode 100644 index 0000000..c37944f --- /dev/null +++ b/terraform/modules/image/io.tf @@ -0,0 +1,12 @@ +variable "name" {} +variable "location" {} +variable "depends-id" {} +variable "azure_vhd_uri" {} + +output "depends-id" { + value = "${null_resource.dummy_dependency.id}" +} + +output "image_id" { + value = "${ azurerm_image.acs.id }" +} diff --git a/terraform/modules/master/io.tf b/terraform/modules/master/io.tf index 6f5e282..1731d2d 100644 --- a/terraform/modules/master/io.tf +++ b/terraform/modules/master/io.tf @@ -7,10 +7,9 @@ variable "etcd-ips" {} variable "name" {} variable "location" {} variable "private-subnet-id" {} -variable "instances" {} variable "depends-id" {} variable "storage_endpoint" {} -variable "azure_image_name" {} +variable "image_id" {} output "depends-id" { value = "${null_resource.dummy_dependency.id}" diff --git a/terraform/modules/master/master.tf b/terraform/modules/master/master.tf index 72cc688..62e4ad0 100644 --- a/terraform/modules/master/master.tf +++ b/terraform/modules/master/master.tf @@ -2,11 +2,6 @@ data "azurerm_resource_group" "image" { name = "ACStackImages" } -data "azurerm_image" "image" { - name = "${ var.azure_image_name }" - resource_group_name = "${data.azurerm_resource_group.image.name}" -} - resource "azurerm_network_interface" "master" { name = "master${ count.index + 1 }" location = "${ var.location }" @@ -38,7 +33,7 @@ resource "azurerm_virtual_machine" "master" { delete_data_disks_on_termination = true storage_image_reference { - id = "${data.azurerm_image.image.id}" + id = "${ var.image_id }" } storage_os_disk { diff --git a/terraform/modules/node/io.tf b/terraform/modules/node/io.tf index 2c09340..5c24f55 100644 --- a/terraform/modules/node/io.tf +++ b/terraform/modules/node/io.tf @@ -11,7 +11,7 @@ variable "node_count" {} variable "depends-id" {} variable "bastion-ip" {} variable "storage_endpoint" {} -variable "azure_image_name" {} +variable "image_id" {} output "depends-id" { value = "${null_resource.dummy_dependency.id}" diff --git a/terraform/modules/node/node.tf b/terraform/modules/node/node.tf index 672040e..64ce430 100644 --- a/terraform/modules/node/node.tf +++ b/terraform/modules/node/node.tf @@ -2,11 +2,6 @@ data "azurerm_resource_group" "image" { name = "ACStackImages" } -data "azurerm_image" "image" { - name = "${ var.azure_image_name }" - resource_group_name = "${data.azurerm_resource_group.image.name}" -} - resource "azurerm_network_interface" "node" { name = "node${ count.index + 1 }" location = "${ var.location }" @@ -37,7 +32,7 @@ resource "azurerm_virtual_machine" "node" { delete_data_disks_on_termination = true storage_image_reference { - id = "${data.azurerm_image.image.id}" + id = "${ var.image_id }" } storage_os_disk { diff --git a/terraform/scripts/init-variables b/terraform/scripts/init-variables index a838f69..c8e66a1 100755 --- a/terraform/scripts/init-variables +++ b/terraform/scripts/init-variables @@ -6,7 +6,7 @@ set -x echo $AZURE_LOCATION echo $AZURE_VM_KEY_NAME -echo $AZURE_IMAGE_NAME +echo $AZURE_VHD_URI echo $INTERNAL_TLD echo $CLUSTER_NAME CIDR_ALLOW_SSH=`$CDIR/myip` @@ -48,7 +48,7 @@ dns-service-ip = "${K8S_DNS_IP}" internal-tld = "${INTERNAL_TLD}" k8s-service-ip = "${K8S_SERVICE_IP}" name = "${CLUSTER_NAME}" -azure_image_name = "${AZURE_IMAGE_NAME}" +azure_vhd_uri = "${AZURE_VHD_URI}" pki-ip = "${PKI_IP}" etcd-ips = "$ETCD_IPS" EOF