From 0d2990510ebb4685bc227f66705cce0317995e0c Mon Sep 17 00:00:00 2001 From: qvicksilver Date: Mon, 6 Apr 2020 11:41:46 +0200 Subject: [PATCH] Terraform/OpenStack: Enable usage of an existing router (#5890) --- contrib/terraform/openstack/README.md | 10 ++++++++++ contrib/terraform/openstack/kubespray.tf | 1 + contrib/terraform/openstack/modules/network/main.tf | 9 +++++++-- contrib/terraform/openstack/modules/network/outputs.tf | 6 +++--- .../terraform/openstack/modules/network/variables.tf | 2 ++ contrib/terraform/openstack/variables.tf | 5 +++++ 6 files changed, 28 insertions(+), 5 deletions(-) diff --git a/contrib/terraform/openstack/README.md b/contrib/terraform/openstack/README.md index 5d4f2e4dc..09c7c4ddb 100644 --- a/contrib/terraform/openstack/README.md +++ b/contrib/terraform/openstack/README.md @@ -38,6 +38,16 @@ hosts where that makes sense. You have the option of creating bastion hosts inside the private subnet to access the nodes there. Alternatively, a node with a floating IP can be used as a jump host to nodes without. +#### Using an existing router +It is possible to use an existing router instead of creating one. To use an +existing router set the router\_id variable to the uuid of the router you wish +to use. + +For example: +``` +router_id = "00c542e7-6f46-4535-ae95-984c7f0391a3" +``` + ### Kubernetes Nodes You can create many different kubernetes topologies by setting the number of different classes of hosts. For each class there are options for allocating diff --git a/contrib/terraform/openstack/kubespray.tf b/contrib/terraform/openstack/kubespray.tf index 33fbff762..746b5a550 100644 --- a/contrib/terraform/openstack/kubespray.tf +++ b/contrib/terraform/openstack/kubespray.tf @@ -12,6 +12,7 @@ module "network" { dns_nameservers = "${var.dns_nameservers}" network_dns_domain = "${var.network_dns_domain}" use_neutron = "${var.use_neutron}" + router_id = "${var.router_id}" } module "ips" { diff --git a/contrib/terraform/openstack/modules/network/main.tf b/contrib/terraform/openstack/modules/network/main.tf index 0ab9d4630..2fd65cd53 100644 --- a/contrib/terraform/openstack/modules/network/main.tf +++ b/contrib/terraform/openstack/modules/network/main.tf @@ -1,10 +1,15 @@ resource "openstack_networking_router_v2" "k8s" { name = "${var.cluster_name}-router" - count = "${var.use_neutron}" + count = "${var.use_neutron}" == 1 && "${var.router_id}" == null ? 1 : 0 admin_state_up = "true" external_network_id = "${var.external_net}" } +data "openstack_networking_router_v2" "k8s" { + router_id = "${var.router_id}" + count = "${var.use_neutron}" == 1 && "${var.router_id}" != null ? 1 : 0 +} + resource "openstack_networking_network_v2" "k8s" { name = "${var.network_name}" count = "${var.use_neutron}" @@ -23,6 +28,6 @@ resource "openstack_networking_subnet_v2" "k8s" { resource "openstack_networking_router_interface_v2" "k8s" { count = "${var.use_neutron}" - router_id = "${openstack_networking_router_v2.k8s[count.index].id}" + router_id = "%{if openstack_networking_router_v2.k8s != []}${openstack_networking_router_v2.k8s[count.index].id} %{else}${var.router_id} %{endif}" subnet_id = "${openstack_networking_subnet_v2.k8s[count.index].id}" } diff --git a/contrib/terraform/openstack/modules/network/outputs.tf b/contrib/terraform/openstack/modules/network/outputs.tf index 61aeef10d..5b62c75bb 100644 --- a/contrib/terraform/openstack/modules/network/outputs.tf +++ b/contrib/terraform/openstack/modules/network/outputs.tf @@ -1,11 +1,11 @@ output "router_id" { - value = "${element(concat(openstack_networking_router_v2.k8s.*.id, list("")), 0)}" + value = "%{if var.use_neutron == 1} ${var.router_id == null ? element(concat(openstack_networking_router_v2.k8s.*.id, [""]), 0) : var.router_id} %{else} %{endif}" } output "router_internal_port_id" { - value = "${element(concat(openstack_networking_router_interface_v2.k8s.*.id, list("")), 0)}" + value = "${element(concat(openstack_networking_router_interface_v2.k8s.*.id, [""]), 0)}" } output "subnet_id" { - value = "${element(concat(openstack_networking_subnet_v2.k8s.*.id, list("")), 0)}" + value = "${element(concat(openstack_networking_subnet_v2.k8s.*.id, [""]), 0)}" } diff --git a/contrib/terraform/openstack/modules/network/variables.tf b/contrib/terraform/openstack/modules/network/variables.tf index 99a7dd41d..1b2660f37 100644 --- a/contrib/terraform/openstack/modules/network/variables.tf +++ b/contrib/terraform/openstack/modules/network/variables.tf @@ -13,3 +13,5 @@ variable "dns_nameservers" { variable "subnet_cidr" {} variable "use_neutron" {} + +variable "router_id" {} diff --git a/contrib/terraform/openstack/variables.tf b/contrib/terraform/openstack/variables.tf index 1ab16e5e9..b0fe4a6ed 100644 --- a/contrib/terraform/openstack/variables.tf +++ b/contrib/terraform/openstack/variables.tf @@ -220,3 +220,8 @@ variable "use_access_ip" { variable "use_server_groups" { default = false } + +variable "router_id" { + description = "uuid of an externally defined router to use" + default = null +}