From b1dec5dc77d7c96481431f82cc2a6dbc04dd2a4c Mon Sep 17 00:00:00 2001 From: vasubabu Date: Mon, 8 May 2023 20:08:27 +0530 Subject: [PATCH] Add provider meta module_name in Equinix Metal TF configs --- main.tf | 90 ++++++++++++++++++++++----------------------- output.tf | 8 ++-- util/test_matrix.sh | 2 +- variables.tf | 12 +++--- versions.tf | 9 +++-- 5 files changed, 62 insertions(+), 59 deletions(-) diff --git a/main.tf b/main.tf index 556d08a..f6980e5 100644 --- a/main.tf +++ b/main.tf @@ -1,5 +1,5 @@ -provider "metal" { - auth_token = var.metal_auth_token +provider "equinix" { + auth_token = var.equinix_metal_auth_token } provider "google" { @@ -12,10 +12,10 @@ resource "random_string" "cluster_suffix" { upper = false } -resource "metal_project" "new_project" { - count = var.metal_create_project ? 1 : 0 - name = var.metal_project_name - organization_id = var.metal_organization_id +resource "equinix_metal_project" "new_project" { + count = var.equinix_metal_create_project ? 1 : 0 + name = var.equinix_metal_project_name + organization_id = var.equinix_metal_organization_id bgp_config { deployment_type = "local" asn = var.bgp_asn @@ -28,7 +28,7 @@ locals { timestamp = timestamp() timestamp_sanitized = replace(local.timestamp, "/[- TZ:]/", "") ssh_key_name = format("anthos-%s-%s", var.cluster_name, random_string.cluster_suffix.result) - metal_project_id = var.metal_create_project ? metal_project.new_project[0].id : var.metal_project_id + metal_project_id = var.equinix_metal_create_project ? equinix_metal_project.new_project[0].id : var.equinix_metal_project_id gcr_sa_key = var.gcp_keys_path == "" ? base64decode(google_service_account_key.gcr_sa_key[0].private_key) : file("${var.gcp_keys_path}/gcr.json") connect_sa_key = var.gcp_keys_path == "" ? base64decode(google_service_account_key.connect_sa_key[0].private_key) : file("${var.gcp_keys_path}/connect.json") register_sa_key = var.gcp_keys_path == "" ? base64decode(google_service_account_key.register_sa_key[0].private_key) : file("${var.gcp_keys_path}/register.json") @@ -42,7 +42,7 @@ resource "tls_private_key" "ssh_key_pair" { rsa_bits = 4096 } -resource "metal_ssh_key" "ssh_pub_key" { +resource "equinix_metal_ssh_key" "ssh_pub_key" { name = local.cluster_name public_key = chomp(tls_private_key.ssh_key_pair.public_key_openssh) } @@ -53,14 +53,14 @@ resource "local_file" "cluster_private_key_pem" { file_permission = "0600" } -resource "metal_reserved_ip_block" "cp_vip" { +resource "equinix_metal_reserved_ip_block" "cp_vip" { project_id = local.metal_project_id metro = var.metro quantity = 1 description = format("Cluster: '%s' Contol Plane VIP", local.cluster_name) } -resource "metal_reserved_ip_block" "ingress_vip" { +resource "equinix_metal_reserved_ip_block" "ingress_vip" { project_id = local.metal_project_id metro = var.metro quantity = 1 @@ -94,15 +94,15 @@ data "cloudinit_config" "cp_user_data" { kube_vip_install = templatefile("${path.module}/templates/kube_vip_install.sh", { cluster_name = local.cluster_name - eip = cidrhost(metal_reserved_ip_block.cp_vip.cidr_notation, 0) + eip = cidrhost(equinix_metal_reserved_ip_block.cp_vip.cidr_notation, 0) count = 0 kube_vip_ver = var.kube_vip_version - auth_token = var.metal_auth_token + auth_token = var.equinix_metal_auth_token project_id = local.metal_project_id }) ccm_secret = templatefile("${path.module}/templates/ccm_secret.yaml", { - auth_token = var.metal_auth_token + auth_token = var.equinix_metal_auth_token project_id = local.metal_project_id }) }) @@ -126,9 +126,9 @@ data "cloudinit_config" "worker_user_data" { } -resource "metal_device" "control_plane" { +resource "equinix_metal_device" "control_plane" { depends_on = [ - metal_ssh_key.ssh_pub_key + equinix_metal_ssh_key.ssh_pub_key ] count = local.cp_count hostname = format("%s-cp-%02d", local.cluster_name, count.index + 1) @@ -141,9 +141,9 @@ resource "metal_device" "control_plane" { tags = ["anthos", "baremetal", "control-plane"] } -resource "metal_device" "worker_nodes" { +resource "equinix_metal_device" "worker_nodes" { depends_on = [ - metal_ssh_key.ssh_pub_key + equinix_metal_ssh_key.ssh_pub_key ] count = var.worker_count hostname = format("%s-worker-%02d", local.cluster_name, count.index + 1) @@ -156,15 +156,15 @@ resource "metal_device" "worker_nodes" { tags = ["anthos", "baremetal", "worker"] } -resource "metal_bgp_session" "enable_cp_bgp" { +resource "equinix_metal_bgp_session" "enable_cp_bgp" { count = local.cp_count - device_id = element(metal_device.control_plane.*.id, count.index) + device_id = element(equinix_metal_device.control_plane.*.id, count.index) address_family = "ipv4" } -resource "metal_bgp_session" "enable_worker_bgp" { +resource "equinix_metal_bgp_session" "enable_worker_bgp" { count = var.worker_count - device_id = element(metal_device.worker_nodes.*.id, count.index) + device_id = element(equinix_metal_device.worker_nodes.*.id, count.index) address_family = "ipv4" } @@ -177,7 +177,7 @@ resource "null_resource" "prep_anthos_cluster" { type = "ssh" user = "root" private_key = chomp(tls_private_key.ssh_key_pair.private_key_pem) - host = metal_device.control_plane.0.access_public_ipv4 + host = equinix_metal_device.control_plane.0.access_public_ipv4 } @@ -185,12 +185,12 @@ resource "null_resource" "prep_anthos_cluster" { content = templatefile("${path.module}/templates/pre_reqs.sh", { cluster_name = local.cluster_name operating_system = var.operating_system - cp_vip = cidrhost(metal_reserved_ip_block.cp_vip.cidr_notation, 0) - ingress_vip = cidrhost(metal_reserved_ip_block.ingress_vip.cidr_notation, 0) - cp_ips = join(" ", metal_device.control_plane.*.access_private_ipv4) - cp_ids = join(" ", metal_device.control_plane.*.id) - worker_ips = join(" ", metal_device.worker_nodes.*.access_private_ipv4) - worker_ids = join(" ", metal_device.worker_nodes.*.id) + cp_vip = cidrhost(equinix_metal_reserved_ip_block.cp_vip.cidr_notation, 0) + ingress_vip = cidrhost(equinix_metal_reserved_ip_block.ingress_vip.cidr_notation, 0) + cp_ips = join(" ", equinix_metal_device.control_plane.*.access_private_ipv4) + cp_ids = join(" ", equinix_metal_device.control_plane.*.id) + worker_ips = join(" ", equinix_metal_device.worker_nodes.*.access_private_ipv4) + worker_ids = join(" ", equinix_metal_device.worker_nodes.*.id) anthos_ver = var.anthos_version }) destination = "/root/bootstrap/pre_reqs.sh" @@ -212,7 +212,7 @@ resource "null_resource" "deploy_anthos_cluster" { type = "ssh" user = "root" private_key = chomp(tls_private_key.ssh_key_pair.private_key_pem) - host = metal_device.control_plane.0.access_public_ipv4 + host = equinix_metal_device.control_plane.0.access_public_ipv4 } provisioner "remote-exec" { inline = [ @@ -225,21 +225,21 @@ resource "null_resource" "download_kube_config" { depends_on = [null_resource.deploy_anthos_cluster] provisioner "local-exec" { - command = "scp -i ~/.ssh/${local.ssh_key_name} -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null root@${metal_device.control_plane.0.access_public_ipv4}:/root/baremetal/bmctl-workspace/${local.cluster_name}/${local.cluster_name}-kubeconfig ." + command = "scp -i ~/.ssh/${local.ssh_key_name} -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null root@${equinix_metal_device.control_plane.0.access_public_ipv4}:/root/baremetal/bmctl-workspace/${local.cluster_name}/${local.cluster_name}-kubeconfig ." } } resource "null_resource" "kube_vip_install_first_cp" { depends_on = [ - metal_bgp_session.enable_cp_bgp, - metal_bgp_session.enable_worker_bgp, + equinix_metal_bgp_session.enable_cp_bgp, + equinix_metal_bgp_session.enable_worker_bgp, null_resource.prep_anthos_cluster, ] connection { type = "ssh" user = "root" private_key = chomp(tls_private_key.ssh_key_pair.private_key_pem) - host = metal_device.control_plane.0.access_public_ipv4 + host = equinix_metal_device.control_plane.0.access_public_ipv4 } provisioner "remote-exec" { inline = [ @@ -258,16 +258,16 @@ resource "null_resource" "add_remaining_cps" { type = "ssh" user = "root" private_key = chomp(tls_private_key.ssh_key_pair.private_key_pem) - host = metal_device.control_plane.0.access_public_ipv4 + host = equinix_metal_device.control_plane.0.access_public_ipv4 } provisioner "file" { content = templatefile("${path.module}/templates/add_remaining_cps.sh", { cluster_name = local.cluster_name - cp_ip_2 = metal_device.control_plane.1.access_private_ipv4 - cp_id_2 = metal_device.control_plane.1.id - cp_ip_3 = metal_device.control_plane.2.access_private_ipv4 - cp_id_3 = metal_device.control_plane.2.id + cp_ip_2 = equinix_metal_device.control_plane.1.access_private_ipv4 + cp_id_2 = equinix_metal_device.control_plane.1.id + cp_ip_3 = equinix_metal_device.control_plane.2.access_private_ipv4 + cp_id_3 = equinix_metal_device.control_plane.2.id }) destination = "/root/bootstrap/add_remaining_cps.sh" } @@ -287,7 +287,7 @@ resource "null_resource" "kube_vip_install_remaining_cp" { type = "ssh" user = "root" private_key = chomp(tls_private_key.ssh_key_pair.private_key_pem) - host = element(metal_device.control_plane.*.access_public_ipv4, count.index + 1) + host = element(equinix_metal_device.control_plane.*.access_public_ipv4, count.index + 1) } provisioner "remote-exec" { inline = ["mkdir -p /root/bootstrap"] @@ -295,10 +295,10 @@ resource "null_resource" "kube_vip_install_remaining_cp" { provisioner "file" { content = templatefile("${path.module}/templates/kube_vip_install.sh", { cluster_name = local.cluster_name - eip = cidrhost(metal_reserved_ip_block.cp_vip.cidr_notation, 0) + eip = cidrhost(equinix_metal_reserved_ip_block.cp_vip.cidr_notation, 0) count = 1 kube_vip_ver = var.kube_vip_version - auth_token = var.metal_auth_token + auth_token = var.equinix_metal_auth_token project_id = local.metal_project_id }) @@ -321,7 +321,7 @@ resource "null_resource" "install_ccm" { type = "ssh" user = "root" private_key = chomp(tls_private_key.ssh_key_pair.private_key_pem) - host = metal_device.control_plane.0.access_public_ipv4 + host = equinix_metal_device.control_plane.0.access_public_ipv4 } provisioner "remote-exec" { @@ -340,7 +340,7 @@ resource "null_resource" "install_kube_vip_daemonset" { type = "ssh" user = "root" private_key = chomp(tls_private_key.ssh_key_pair.private_key_pem) - host = metal_device.control_plane.0.access_public_ipv4 + host = equinix_metal_device.control_plane.0.access_public_ipv4 } provisioner "file" { content = templatefile("${path.module}/templates/kube_vip_ds.yaml", { @@ -363,11 +363,11 @@ module "storage" { ] ssh = { - host = metal_device.control_plane.0.access_public_ipv4 + host = equinix_metal_device.control_plane.0.access_public_ipv4 private_key = chomp(tls_private_key.ssh_key_pair.private_key_pem) user = "root" kubeconfig = "/root/baremetal/bmctl-workspace/${local.cluster_name}/${local.cluster_name}-kubeconfig" - worker_addresses = metal_device.worker_nodes.*.access_public_ipv4 + worker_addresses = equinix_metal_device.worker_nodes.*.access_public_ipv4 } cluster_name = local.cluster_name diff --git a/output.tf b/output.tf index 2c2f458..e541d4a 100644 --- a/output.tf +++ b/output.tf @@ -1,10 +1,10 @@ output "Control_Plane_Public_IPs" { - value = metal_device.control_plane.*.access_public_ipv4 + value = equinix_metal_device.control_plane.*.access_public_ipv4 description = "Control Plane Public IPs" } output "Worker_Public_IPs" { - value = metal_device.worker_nodes.*.access_public_ipv4 + value = equinix_metal_device.worker_nodes.*.access_public_ipv4 description = "Worker Node Public IPs" } @@ -14,12 +14,12 @@ output "ssh_key_location" { } output "Control_Plane_VIP" { - value = cidrhost(metal_reserved_ip_block.cp_vip.cidr_notation, 0) + value = cidrhost(equinix_metal_reserved_ip_block.cp_vip.cidr_notation, 0) description = "The Virtual IP for the Control Plane" } output "Ingress_VIP" { - value = cidrhost(metal_reserved_ip_block.ingress_vip.cidr_notation, 0) + value = cidrhost(equinix_metal_reserved_ip_block.ingress_vip.cidr_notation, 0) description = "The Virtual IP for Ingress" } diff --git a/util/test_matrix.sh b/util/test_matrix.sh index c736d20..3d4b5f2 100755 --- a/util/test_matrix.sh +++ b/util/test_matrix.sh @@ -6,7 +6,7 @@ if [ "$1" = "apply" ]; then for os in "${operating_systems[@]}"; do mkdir -p $os short_name=`echo $os |sed "s/_//g"` - nohup terraform apply --auto-approve -state="./$os/terraform.tfstate" -var "operating_system=$os" -var "facility=sv15" -var "plan=c3.small.x86" -var "hostname=$short_name-anothos-baermetal" > ./$os/terraform.log & + nohup terraform apply --auto-approve -state="./$os/terraform.tfstate" -var "operating_system=$os" -var "metro=da" -var "plan=c3.small.x86" -var "hostname=$short_name-anothos-baermetal" > ./$os/terraform.log & done elif [ "$1" = "destroy" ]; then for os in "${operating_systems[@]}"; do diff --git a/variables.tf b/variables.tf index 4570c50..22eef26 100644 --- a/variables.tf +++ b/variables.tf @@ -1,15 +1,15 @@ -variable "metal_auth_token" { +variable "equinix_metal_auth_token" { type = string description = "Equinix Metal API Key" } -variable "metal_project_id" { +variable "equinix_metal_project_id" { type = string default = "null" description = "Equinix Metal Project ID" } -variable "metal_organization_id" { +variable "equinix_metal_organization_id" { type = string default = "null" description = "Equinix Metal Organization ID" @@ -73,13 +73,13 @@ variable "cluster_name" { } } -variable "metal_create_project" { +variable "equinix_metal_create_project" { type = bool default = true - description = "Create a Metal Project if this is 'true'. Else use provided 'metal_project_id'" + description = "Create a Metal Project if this is 'true'. Else use provided 'equinix_metal_project_id'" } -variable "metal_project_name" { +variable "equinix_metal_project_name" { type = string default = "baremetal-anthos" description = "The name of the Metal project if 'create_project' is 'true'." diff --git a/versions.tf b/versions.tf index 77be853..087c77c 100644 --- a/versions.tf +++ b/versions.tf @@ -3,8 +3,9 @@ terraform { null = { source = "hashicorp/null" } - metal = { - source = "equinix/metal" + equinix = { + source = "equinix/equinix" + version = "~> 1.14" } random = { source = "hashicorp/random" @@ -23,5 +24,7 @@ terraform { version = "~>3.53.0" } } - required_version = ">= 0.13" + provider_meta "equinix" { + module_name = "equinix-metal-anthos-on-baremetal" + } }