Skip to content

Commit

Permalink
Terraform files for VMs creation on vSphere for Assisted Installer de…
Browse files Browse the repository at this point in the history
…ployment automation (for HCI) (#8912)

* add terraform directory (including README and .gitignore files)
* add terraform files for AI deployment on vSphere

Signed-off-by: Daniel Horak <[email protected]>
  • Loading branch information
dahorak authored Nov 30, 2023
1 parent 6d3f891 commit 3afe420
Show file tree
Hide file tree
Showing 9 changed files with 378 additions and 0 deletions.
5 changes: 5 additions & 0 deletions terraform/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
terraform.tfvars
.terraform.lock.hcl
.terraform
*.tfstate
*.backup
4 changes: 4 additions & 0 deletions terraform/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# terraform directory

This directory contain terraform configuration and modules used for some
deployments (as of now for deployment on vSphere via Assisted Installer).
7 changes: 7 additions & 0 deletions terraform/ai/vsphere/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# vsphere/ai directory

This directory contain terraform configuration and modules used for deployment
on vSphere via Assisted Installer.

The code is inspired by
[Example vSphere UPI deployment from openshift installer repo](https://github.com/openshift/installer/tree/master/upi/vsphere)
96 changes: 96 additions & 0 deletions terraform/ai/vsphere/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
// prepare some local variables
locals {
control_planes = [for idx in range(var.control_plane_count) : "${var.cluster_id}-control-plane-${idx}"]
compute_nodes = [for idx in range(var.compute_count) : "${var.cluster_id}-compute-${idx}"]
guest_id = "rhel8_64Guest"
}

// configure connection to vSphere
provider "vsphere" {
user = var.vsphere_user
password = var.vsphere_password
vsphere_server = var.vsphere_server
allow_unverified_ssl = true
}

// get vSphere DC
data "vsphere_datacenter" "dc" {
name = var.vsphere_datacenter
}

// get vSphere Cluster
data "vsphere_compute_cluster" "compute_cluster" {
name = var.vsphere_cluster
datacenter_id = data.vsphere_datacenter.dc.id
}

// get vSphere Data Store
data "vsphere_datastore" "datastore" {
name = var.vsphere_datastore
datacenter_id = data.vsphere_datacenter.dc.id
}

// get vSphere Network
data "vsphere_network" "network" {
name = var.vm_network
datacenter_id = data.vsphere_datacenter.dc.id
distributed_virtual_switch_uuid = ""
}

// create Resource Pool for VMs
resource "vsphere_resource_pool" "resource_pool" {
name = var.cluster_id
parent_resource_pool_id = data.vsphere_compute_cluster.compute_cluster.resource_pool_id
}

// create Folder for VMs
resource "vsphere_folder" "folder" {
path = var.cluster_id
type = "vm"
datacenter_id = data.vsphere_datacenter.dc.id
}

// upload discovery iso to vSphere data store to /iso directory
resource "vsphere_file" "discovery_iso" {
datacenter = var.vsphere_datacenter
datastore = var.vsphere_datastore
source_file = var.iso_image
destination_file = "/iso/${var.cluster_id}-discovery.iso"
create_directories = true
}

// create Control Plane VMs
module "control_plane_vm" {
count = var.control_plane_count
source = "./vm"
vmname = local.control_planes[count.index]
resource_pool_id = vsphere_resource_pool.resource_pool.id
datastore_id = data.vsphere_datastore.datastore.id
network_id = data.vsphere_network.network.id
folder_id = vsphere_folder.folder.path
guest_id = local.guest_id
num_cpus = var.control_plane_num_cpus
memory = var.control_plane_memory
system_disk_size = var.system_disk_size
iso_image = vsphere_file.discovery_iso.destination_file
nested_hv_enabled = true
}

// create Compute VMs
module "compute_vm" {
count = var.compute_count
source = "./vm"
vmname = local.compute_nodes[count.index]
resource_pool_id = vsphere_resource_pool.resource_pool.id
datastore_id = data.vsphere_datastore.datastore.id
network_id = data.vsphere_network.network.id
folder_id = vsphere_folder.folder.path
guest_id = local.guest_id
num_cpus = var.compute_num_cpus
memory = var.compute_memory
system_disk_size = var.system_disk_size
data_disks_count = var.compute_data_disks_count
data_disks_size = var.compute_data_disks_size
iso_image = vsphere_file.discovery_iso.destination_file
nested_hv_enabled = true
}
58 changes: 58 additions & 0 deletions terraform/ai/vsphere/terraform.tfvars.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
// ID identifying the cluster to create. Use your username so that resources
// created can be tracked back to you.
cluster_id = "example-cluster"

// Name (URL) of the vSphere server.
vsphere_server = "VSPHERE_SERVER_URL"

// User on the vSphere server.
vsphere_user = "VSPHERE_USER"

// Password of the user on the vSphere server.
vsphere_password = "VSPHERE_PASSWORD"

// Name of the vSphere cluster.
vsphere_cluster = "Cluster-1"

// Name of the vSphere data center.
vsphere_datacenter = "DatacenterName"

// Name of the vSphere data store.
vsphere_datastore = "vsanDatastore"

// Name of the publicly accessible network for cluster ingress and access.
vm_network = "VM Network"

// Local path to the Assisted Installer discovery iso.
iso_image = "/path/to/discovery.iso"

// The size of system disk for both Control plane and Compute VMs, in GB.
// Default 120 GB.
system_disk_size = "120"

// The number of control plane VMs to create. Default is 3.
control_plane_count = "3"

// The number of cores for Control plane VMs. Default is 4.
control_plane_num_cpus = "4"

// Control plane VMs memory size, in MB.
// Default is 16384 (16 GB)
control_plane_memory = "16384"

// The number of Compute VMs to create. Default is 3.
compute_count = "3"

// The number of cores for Compute VMs. Default is 16.
compute_num_cpus = "16"

// Compute VMs memory size, in MB.
// Default is 65536 (64 GB)
compute_memory = "65536"

// The number of data disks for Compute VMs. Default is 2.
compute_data_disks_count = "2"

// The size of data disks for Compute VMs, in GB.
// Default 256 GB.
compute_data_disks_size = "256"
107 changes: 107 additions & 0 deletions terraform/ai/vsphere/variables.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
//////
// vSphere variables
//////

variable "vsphere_server" {
type = string
description = "This is the vSphere server for the environment."
}

variable "vsphere_user" {
type = string
description = "vSphere server user for the environment."
}

variable "vsphere_password" {
type = string
description = "vSphere server password"
}

variable "vsphere_cluster" {
type = string
default = ""
description = "This is the name of the vSphere cluster."
}

variable "vsphere_datacenter" {
type = string
default = ""
description = "This is the name of the vSphere data center."
}

variable "vsphere_datastore" {
type = string
default = ""
description = "This is the name of the vSphere data store."
}
variable "vm_network" {
type = string
description = "This is the name of the publicly accessible network for cluster ingress and access."
default = "VM Network"
}

///////////
// cluster/all nodes related variables
///////////

variable "cluster_id" {
type = string
description = "This cluster id must be of max length 27 and must have only alphanumeric or hyphen characters."
}

variable "iso_image" {
type = string
}

variable "system_disk_size" {
type = string
default = "120"
}

///////////
// control-plane machine variables
///////////

variable "control_plane_count" {
type = string
default = "3"
}

variable "control_plane_memory" {
type = string
default = "16384"
}

variable "control_plane_num_cpus" {
type = string
default = "4"
}

//////////
// compute machine variables
//////////

variable "compute_count" {
type = string
default = "3"
}

variable "compute_memory" {
type = string
default = "65536"
}

variable "compute_num_cpus" {
type = string
default = "16"
}

variable "compute_data_disks_count" {
type = string
default = "2"
}

variable "compute_data_disks_size" {
type = string
default = "256"
}
43 changes: 43 additions & 0 deletions terraform/ai/vsphere/vm/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
resource "vsphere_virtual_machine" "vm" {
name = var.vmname
resource_pool_id = var.resource_pool_id
datastore_id = var.datastore_id
num_cpus = var.num_cpus
memory = var.memory
guest_id = var.guest_id
folder = var.folder_id
enable_disk_uuid = "true"
nested_hv_enabled = var.nested_hv_enabled
wait_for_guest_net_timeout = "0"
wait_for_guest_net_routable = "false"

network_interface {
network_id = var.network_id
}

disk {
label = "disk0"
size = 120
thin_provisioned = true
}

# creates variable number of data disks for VM
dynamic "disk" {
for_each = [for idx in range(var.data_disks_count) : idx + 1]
content {
label = "disk${disk.value}"
unit_number = disk.value
size = 256
thin_provisioned = true
}
}

cdrom {
datastore_id = var.datastore_id
path = var.iso_image
}

extra_config = {
"stealclock.enable" = "TRUE"
}
}
55 changes: 55 additions & 0 deletions terraform/ai/vsphere/vm/variables.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
variable "vmname" {
type = string
}

variable "guest_id" {
type = string
}

variable "resource_pool_id" {
type = string
}

variable "folder_id" {
type = string
}

variable "datastore_id" {
type = string
}

variable "network_id" {
type = string
}

variable "memory" {
type = string
}

variable "num_cpus" {
type = string
}

variable "iso_image" {
type = string
}

variable "system_disk_size" {
type = string
default = "120"
}

variable "data_disks_count" {
type = string
default = "0"
}

variable "data_disks_size" {
type = string
default = "100"
}

variable "nested_hv_enabled" {
type = bool
default = false
}
3 changes: 3 additions & 0 deletions terraform/ai/vsphere/vm/versions.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
terraform {
required_version = ">= 0.12"
}

0 comments on commit 3afe420

Please sign in to comment.