allow users to set image_uuid instead of name, this allows the use of openstack community images (#7283)

This commit is contained in:
Hugo Blom
2021-02-16 16:05:06 +01:00
committed by GitHub
parent 796d3fb975
commit f2d10e9465
4 changed files with 85 additions and 31 deletions

View File

@@ -52,7 +52,11 @@ module "compute" {
master_volume_type = var.master_volume_type master_volume_type = var.master_volume_type
public_key_path = var.public_key_path public_key_path = var.public_key_path
image = var.image image = var.image
image_uuid = var.image_uuid
image_gfs = var.image_gfs image_gfs = var.image_gfs
image_master = var.image_master
image_master_uuid = var.image_master_uuid
image_gfs_uuid = var.image_gfs_uuid
ssh_user = var.ssh_user ssh_user = var.ssh_user
ssh_user_gfs = var.ssh_user_gfs ssh_user_gfs = var.ssh_user_gfs
flavor_k8s_master = var.flavor_k8s_master flavor_k8s_master = var.flavor_k8s_master

View File

@@ -1,11 +1,18 @@
data "openstack_images_image_v2" "vm_image" { data "openstack_images_image_v2" "vm_image" {
count = var.image_uuid == "" ? 1 : 0
name = var.image name = var.image
} }
data "openstack_images_image_v2" "gfs_image" { data "openstack_images_image_v2" "gfs_image" {
count = var.image_gfs_uuid == "" ? var.image_uuid == "" ? 1 : 0 : 0
name = var.image_gfs == "" ? var.image : var.image_gfs name = var.image_gfs == "" ? var.image : var.image_gfs
} }
data "openstack_images_image_v2" "image_master" {
count = var.image_master_uuid == "" ? var.image_uuid == "" ? 1 : 0 : 0
name = var.image_master == "" ? var.image : var.image_master
}
resource "openstack_compute_keypair_v2" "k8s" { resource "openstack_compute_keypair_v2" "k8s" {
name = "kubernetes-${var.cluster_name}" name = "kubernetes-${var.cluster_name}"
public_key = chomp(file(var.public_key_path)) public_key = chomp(file(var.public_key_path))
@@ -151,19 +158,26 @@ locals {
openstack_networking_secgroup_v2.worker.name, openstack_networking_secgroup_v2.worker.name,
var.extra_sec_groups ? openstack_networking_secgroup_v2.worker_extra[0].name : "", var.extra_sec_groups ? openstack_networking_secgroup_v2.worker_extra[0].name : "",
]) ])
# Image uuid
image_to_use_node = var.image_uuid != "" ? var.image_uuid : data.openstack_images_image_v2.vm_image[0].id
# Image_gfs uuid
image_to_use_gfs = var.image_gfs_uuid != "" ? var.image_gfs_uuid : var.image_uuid != "" ? var.image_uuid : data.openstack_images_image_v2.gfs_image[0].id
# image_master uuidimage_gfs_uuid
image_to_use_master = var.image_master_uuid != "" ? var.image_master_uuid : var.image_uuid != "" ? var.image_uuid : data.openstack_images_image_v2.image_master[0].id
} }
resource "openstack_compute_instance_v2" "bastion" { resource "openstack_compute_instance_v2" "bastion" {
name = "${var.cluster_name}-bastion-${count.index + 1}" name = "${var.cluster_name}-bastion-${count.index + 1}"
count = var.number_of_bastions count = var.number_of_bastions
image_name = var.image image_id = var.bastion_root_volume_size_in_gb == 0 ? local.image_to_use_node : null
flavor_id = var.flavor_bastion flavor_id = var.flavor_bastion
key_pair = openstack_compute_keypair_v2.k8s.name key_pair = openstack_compute_keypair_v2.k8s.name
dynamic "block_device" { dynamic "block_device" {
for_each = var.bastion_root_volume_size_in_gb > 0 ? [var.image] : [] for_each = var.bastion_root_volume_size_in_gb > 0 ? [local.image_to_use_node] : []
content { content {
uuid = data.openstack_images_image_v2.vm_image.id uuid = local.image_to_use_node
source_type = "image" source_type = "image"
volume_size = var.bastion_root_volume_size_in_gb volume_size = var.bastion_root_volume_size_in_gb
boot_index = 0 boot_index = 0
@@ -196,15 +210,15 @@ resource "openstack_compute_instance_v2" "k8s_master" {
name = "${var.cluster_name}-k8s-master-${count.index + 1}" name = "${var.cluster_name}-k8s-master-${count.index + 1}"
count = var.number_of_k8s_masters count = var.number_of_k8s_masters
availability_zone = element(var.az_list, count.index) availability_zone = element(var.az_list, count.index)
image_name = var.image image_id = var.master_root_volume_size_in_gb == 0 ? local.image_to_use_master : null
flavor_id = var.flavor_k8s_master flavor_id = var.flavor_k8s_master
key_pair = openstack_compute_keypair_v2.k8s.name key_pair = openstack_compute_keypair_v2.k8s.name
dynamic "block_device" { dynamic "block_device" {
for_each = var.master_root_volume_size_in_gb > 0 ? [var.image] : [] for_each = var.master_root_volume_size_in_gb > 0 ? [local.image_to_use_master] : []
content { content {
uuid = data.openstack_images_image_v2.vm_image.id uuid = local.image_to_use_master
source_type = "image" source_type = "image"
volume_size = var.master_root_volume_size_in_gb volume_size = var.master_root_volume_size_in_gb
volume_type = var.master_volume_type volume_type = var.master_volume_type
@@ -243,15 +257,15 @@ resource "openstack_compute_instance_v2" "k8s_master_no_etcd" {
name = "${var.cluster_name}-k8s-master-ne-${count.index + 1}" name = "${var.cluster_name}-k8s-master-ne-${count.index + 1}"
count = var.number_of_k8s_masters_no_etcd count = var.number_of_k8s_masters_no_etcd
availability_zone = element(var.az_list, count.index) availability_zone = element(var.az_list, count.index)
image_name = var.image image_id = var.master_root_volume_size_in_gb == 0 ? local.image_to_use_master : null
flavor_id = var.flavor_k8s_master flavor_id = var.flavor_k8s_master
key_pair = openstack_compute_keypair_v2.k8s.name key_pair = openstack_compute_keypair_v2.k8s.name
dynamic "block_device" { dynamic "block_device" {
for_each = var.master_root_volume_size_in_gb > 0 ? [var.image] : [] for_each = var.master_root_volume_size_in_gb > 0 ? [local.image_to_use_master] : []
content { content {
uuid = data.openstack_images_image_v2.vm_image.id uuid = local.image_to_use_master
source_type = "image" source_type = "image"
volume_size = var.master_root_volume_size_in_gb volume_size = var.master_root_volume_size_in_gb
volume_type = var.master_volume_type volume_type = var.master_volume_type
@@ -290,14 +304,14 @@ resource "openstack_compute_instance_v2" "etcd" {
name = "${var.cluster_name}-etcd-${count.index + 1}" name = "${var.cluster_name}-etcd-${count.index + 1}"
count = var.number_of_etcd count = var.number_of_etcd
availability_zone = element(var.az_list, count.index) availability_zone = element(var.az_list, count.index)
image_name = var.image image_id = var.etcd_root_volume_size_in_gb == 0 ? local.image_to_use_master : null
flavor_id = var.flavor_etcd flavor_id = var.flavor_etcd
key_pair = openstack_compute_keypair_v2.k8s.name key_pair = openstack_compute_keypair_v2.k8s.name
dynamic "block_device" { dynamic "block_device" {
for_each = var.etcd_root_volume_size_in_gb > 0 ? [var.image] : [] for_each = var.etcd_root_volume_size_in_gb > 0 ? [local.image_to_use_master] : []
content { content {
uuid = data.openstack_images_image_v2.vm_image.id uuid = local.image_to_use_master
source_type = "image" source_type = "image"
volume_size = var.etcd_root_volume_size_in_gb volume_size = var.etcd_root_volume_size_in_gb
boot_index = 0 boot_index = 0
@@ -331,14 +345,14 @@ resource "openstack_compute_instance_v2" "k8s_master_no_floating_ip" {
name = "${var.cluster_name}-k8s-master-nf-${count.index + 1}" name = "${var.cluster_name}-k8s-master-nf-${count.index + 1}"
count = var.number_of_k8s_masters_no_floating_ip count = var.number_of_k8s_masters_no_floating_ip
availability_zone = element(var.az_list, count.index) availability_zone = element(var.az_list, count.index)
image_name = var.image image_id = var.master_root_volume_size_in_gb == 0 ? local.image_to_use_master : null
flavor_id = var.flavor_k8s_master flavor_id = var.flavor_k8s_master
key_pair = openstack_compute_keypair_v2.k8s.name key_pair = openstack_compute_keypair_v2.k8s.name
dynamic "block_device" { dynamic "block_device" {
for_each = var.master_root_volume_size_in_gb > 0 ? [var.image] : [] for_each = var.master_root_volume_size_in_gb > 0 ? [local.image_to_use_master] : []
content { content {
uuid = data.openstack_images_image_v2.vm_image.id uuid = local.image_to_use_master
source_type = "image" source_type = "image"
volume_size = var.master_root_volume_size_in_gb volume_size = var.master_root_volume_size_in_gb
volume_type = var.master_volume_type volume_type = var.master_volume_type
@@ -373,14 +387,14 @@ resource "openstack_compute_instance_v2" "k8s_master_no_floating_ip_no_etcd" {
name = "${var.cluster_name}-k8s-master-ne-nf-${count.index + 1}" name = "${var.cluster_name}-k8s-master-ne-nf-${count.index + 1}"
count = var.number_of_k8s_masters_no_floating_ip_no_etcd count = var.number_of_k8s_masters_no_floating_ip_no_etcd
availability_zone = element(var.az_list, count.index) availability_zone = element(var.az_list, count.index)
image_name = var.image image_id = var.master_root_volume_size_in_gb == 0 ? local.image_to_use_master : null
flavor_id = var.flavor_k8s_master flavor_id = var.flavor_k8s_master
key_pair = openstack_compute_keypair_v2.k8s.name key_pair = openstack_compute_keypair_v2.k8s.name
dynamic "block_device" { dynamic "block_device" {
for_each = var.master_root_volume_size_in_gb > 0 ? [var.image] : [] for_each = var.master_root_volume_size_in_gb > 0 ? [local.image_to_use_master] : []
content { content {
uuid = data.openstack_images_image_v2.vm_image.id uuid = local.image_to_use_master
source_type = "image" source_type = "image"
volume_size = var.master_root_volume_size_in_gb volume_size = var.master_root_volume_size_in_gb
volume_type = var.master_volume_type volume_type = var.master_volume_type
@@ -415,14 +429,14 @@ resource "openstack_compute_instance_v2" "k8s_node" {
name = "${var.cluster_name}-k8s-node-${count.index + 1}" name = "${var.cluster_name}-k8s-node-${count.index + 1}"
count = var.number_of_k8s_nodes count = var.number_of_k8s_nodes
availability_zone = element(var.az_list_node, count.index) availability_zone = element(var.az_list_node, count.index)
image_name = var.image image_id = var.node_root_volume_size_in_gb == 0 ? local.image_to_use_node : null
flavor_id = var.flavor_k8s_node flavor_id = var.flavor_k8s_node
key_pair = openstack_compute_keypair_v2.k8s.name key_pair = openstack_compute_keypair_v2.k8s.name
dynamic "block_device" { dynamic "block_device" {
for_each = var.node_root_volume_size_in_gb > 0 ? [var.image] : [] for_each = var.node_root_volume_size_in_gb > 0 ? [local.image_to_use_node] : []
content { content {
uuid = data.openstack_images_image_v2.vm_image.id uuid = local.image_to_use_node
source_type = "image" source_type = "image"
volume_size = var.node_root_volume_size_in_gb volume_size = var.node_root_volume_size_in_gb
boot_index = 0 boot_index = 0
@@ -460,14 +474,14 @@ resource "openstack_compute_instance_v2" "k8s_node_no_floating_ip" {
name = "${var.cluster_name}-k8s-node-nf-${count.index + 1}" name = "${var.cluster_name}-k8s-node-nf-${count.index + 1}"
count = var.number_of_k8s_nodes_no_floating_ip count = var.number_of_k8s_nodes_no_floating_ip
availability_zone = element(var.az_list_node, count.index) availability_zone = element(var.az_list_node, count.index)
image_name = var.image image_id = var.node_root_volume_size_in_gb == 0 ? local.image_to_use_node : null
flavor_id = var.flavor_k8s_node flavor_id = var.flavor_k8s_node
key_pair = openstack_compute_keypair_v2.k8s.name key_pair = openstack_compute_keypair_v2.k8s.name
dynamic "block_device" { dynamic "block_device" {
for_each = var.node_root_volume_size_in_gb > 0 ? [var.image] : [] for_each = var.node_root_volume_size_in_gb > 0 ? [local.image_to_use_node] : []
content { content {
uuid = data.openstack_images_image_v2.vm_image.id uuid = local.image_to_use_node
source_type = "image" source_type = "image"
volume_size = var.node_root_volume_size_in_gb volume_size = var.node_root_volume_size_in_gb
boot_index = 0 boot_index = 0
@@ -501,14 +515,14 @@ resource "openstack_compute_instance_v2" "k8s_nodes" {
for_each = var.number_of_k8s_nodes == 0 && var.number_of_k8s_nodes_no_floating_ip == 0 ? var.k8s_nodes : {} for_each = var.number_of_k8s_nodes == 0 && var.number_of_k8s_nodes_no_floating_ip == 0 ? var.k8s_nodes : {}
name = "${var.cluster_name}-k8s-node-${each.key}" name = "${var.cluster_name}-k8s-node-${each.key}"
availability_zone = each.value.az availability_zone = each.value.az
image_name = var.image image_id = var.node_root_volume_size_in_gb == 0 ? local.image_to_use_node : null
flavor_id = each.value.flavor flavor_id = each.value.flavor
key_pair = openstack_compute_keypair_v2.k8s.name key_pair = openstack_compute_keypair_v2.k8s.name
dynamic "block_device" { dynamic "block_device" {
for_each = var.node_root_volume_size_in_gb > 0 ? [var.image] : [] for_each = var.node_root_volume_size_in_gb > 0 ? [local.image_to_use_node] : []
content { content {
uuid = data.openstack_images_image_v2.vm_image.id uuid = local.image_to_use_node
source_type = "image" source_type = "image"
volume_size = var.node_root_volume_size_in_gb volume_size = var.node_root_volume_size_in_gb
boot_index = 0 boot_index = 0
@@ -546,14 +560,14 @@ resource "openstack_compute_instance_v2" "glusterfs_node_no_floating_ip" {
name = "${var.cluster_name}-gfs-node-nf-${count.index + 1}" name = "${var.cluster_name}-gfs-node-nf-${count.index + 1}"
count = var.number_of_gfs_nodes_no_floating_ip count = var.number_of_gfs_nodes_no_floating_ip
availability_zone = element(var.az_list, count.index) availability_zone = element(var.az_list, count.index)
image_name = var.image_gfs image_name = var.gfs_root_volume_size_in_gb == 0 ? local.image_to_use_gfs : null
flavor_id = var.flavor_gfs_node flavor_id = var.flavor_gfs_node
key_pair = openstack_compute_keypair_v2.k8s.name key_pair = openstack_compute_keypair_v2.k8s.name
dynamic "block_device" { dynamic "block_device" {
for_each = var.gfs_root_volume_size_in_gb > 0 ? [var.image] : [] for_each = var.gfs_root_volume_size_in_gb > 0 ? [local.image_to_use_gfs] : []
content { content {
uuid = data.openstack_images_image_v2.vm_image.id uuid = local.image_to_use_gfs
source_type = "image" source_type = "image"
volume_size = var.gfs_root_volume_size_in_gb volume_size = var.gfs_root_volume_size_in_gb
boot_index = 0 boot_index = 0

View File

@@ -135,3 +135,19 @@ variable "extra_sec_groups" {
variable "extra_sec_groups_name" { variable "extra_sec_groups_name" {
type = string type = string
} }
variable "image_uuid" {
type = string
}
variable "image_gfs_uuid" {
type = string
}
variable "image_master" {
type = string
}
variable "image_master_uuid" {
type = string
}

View File

@@ -258,3 +258,23 @@ variable "extra_sec_groups" {
variable "extra_sec_groups_name" { variable "extra_sec_groups_name" {
default = "custom" default = "custom"
} }
variable "image_uuid" {
description = "uuid of image inside openstack to use"
default = ""
}
variable "image_gfs_uuid" {
description = "uuid of image to be used on gluster fs nodes. If empty defaults to image_uuid"
default = ""
}
variable "image_master" {
description = "uuid of image inside openstack to use"
default = ""
}
variable "image_master_uuid" {
description = "uuid of image to be used on master nodes. If empty defaults to image_uuid"
default = ""
}