Skip to content

Commit

Permalink
fix: deploy to existing projects in selected metro
Browse files Browse the repository at this point in the history
Signed-off-by: Marques Johansson <[email protected]>
  • Loading branch information
displague committed Sep 7, 2024
1 parent 00b1aec commit eb44ad3
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 18 deletions.
11 changes: 8 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,27 @@ The user needs to provide two environment variables:

`METAL_AUTH_TOKEN` API token to access your Equinix account

`TF_VAR_api_key` API token to access your Equinix account.
`TF_VAR_api_key` API token to access your Equinix account (needed when Spot Market is enabled)

`TF_VAR_project_name` Terraform variable to identify project in your Equinix account.
`TF_VAR_project_name` or `TF_VAR_project_id` Terraform variable to identify project in your Equinix account.

Optionally the user can also provide:

`TF_VAR_metal_create_project` Terraform variable to create a project of name `TF_VAR_project_name` if it does not exist.

You can overwrite any values in variables.tf by using .tfvars files or [other means](https://www.terraform.io/language/values/variables#assigning-values-to-root-module-variables)
You can overwrite any values in `variables.tf` by using `.tfvars` files or [other means](https://www.terraform.io/language/values/variables#assigning-values-to-root-module-variables)

By default the module will create a 3 node Harvester cluster.

The Harvester console can be accessed using an Elastic IP created by the sample.

A random token and password will be generated for your example.

```sh
terraform output -raw harvester_admin_password
terraform output -raw harvester_api_token
```

If you provide a Rancher API URL and keys, your Harvester environment can be managed by Rancher and a kubeconfig file will be saved locally.

### Using `terraform.tfvars.example` to Override Variable Values
Expand Down
7 changes: 4 additions & 3 deletions data.tf
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
data "equinix_metal_project" "project" {
name = var.metal_create_project ? equinix_metal_project.new_project[0].name : var.project_name
name = var.metal_create_project ? equinix_metal_project.new_project[0].name : (var.project_name == "" ? null : var.project_name)
project_id = var.metal_create_project ? null : (var.project_id == "" ? null : var.project_id)
}

data "equinix_metal_ip_block_ranges" "address_block" {
project_id = local.project_id
metro = var.use_cheapest_metro ? local.cheapest_metro_price.metro : var.metro
metro = (var.spot_instance && var.use_cheapest_metro) ? local.cheapest_metro_price.metro : var.metro
}


Expand All @@ -30,7 +31,7 @@ data "equinix_metal_device" "join_devices" {
}

data "http" "prices" {
count = var.use_cheapest_metro ? 1 : 0
count = var.spot_instance && var.use_cheapest_metro ? 1 : 0
url = "https://api.equinix.com/metal/v1/market/spot/prices/metros"
method = "GET"
request_headers = {
Expand Down
9 changes: 5 additions & 4 deletions main.tf
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
locals {
project_id = var.metal_create_project ? equinix_metal_project.new_project[0].id : data.equinix_metal_project.project.project_id
metro = var.use_cheapest_metro ? local.cheapest_metro_price.metro : lower(var.metro)
metro = (var.spot_instance && var.use_cheapest_metro) ? local.cheapest_metro_price.metro : lower(var.metro)
}

// IP attachment to be added to seed node, and this is subsequently assigned as Harvester vip
Expand All @@ -16,13 +16,14 @@ resource "random_password" "token" {
}

resource "equinix_metal_project" "new_project" {
count = var.metal_create_project ? 1 : 0
name = var.project_name
count = var.metal_create_project ? 1 : 0
name = var.project_name
organization_id = var.organization_id == "" ? null : var.organization_id
}

locals {
machine_size = var.plan
pricing_data = var.use_cheapest_metro ? try(jsondecode(data.http.prices[0].response_body), null) : null
pricing_data = (var.spot_instance && var.use_cheapest_metro) ? try(jsondecode(data.http.prices[0].response_body), null) : null
least_bid_price_metro = can(local.pricing_data) && can(local.pricing_data.spot_market_prices) ? flatten([for metro, machines in local.pricing_data.spot_market_prices : [
for machine, details in machines : {
metro = metro
Expand Down
12 changes: 12 additions & 0 deletions outputs.tf
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,15 @@ output "out_of_band_hostnames" {
value = concat([data.equinix_metal_device.seed_device.sos_hostname], data.equinix_metal_device.join_devices.*.sos_hostname)
description = "Out of band hostnames for SSH access to the console"
}

output "harvester_admin_password" {
value = random_password.password.result
description = "The password for the Harvester 'admin' user"
sensitive = true
}

output "harvester_api_token" {
value = random_password.token.result
description = "The token for the Harvester API"
sensitive = true
}
3 changes: 3 additions & 0 deletions terraform.tfvars.example
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ harvester_version = "v1.3.1"
hostname_prefix = "test-harvester"
node_count = "3"
project_name = "Harvester Labs"
# metal_create_project = false
# project_id = "uuid"
# organization_id = "uuid"
plan = "m3.small.x86"
#max_bid_price = "0.17"
metro = "SV"
Expand Down
26 changes: 18 additions & 8 deletions variables.tf
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
variable "harvester_version" {
default = "v1.3.1"
description = "Harvester version to be installed"
description = "Harvester version to be installed (Must be a valid version tag from https://github.com/rancherlabs/terraform-harvester-equinix/tree/main/ipxe)"
}

variable "node_count" {
Expand All @@ -10,7 +10,17 @@ variable "node_count" {

variable "project_name" {
default = ""
description = "Name of the Equinix metal project"
description = "Name of the Equinix Metal project to deploy into, when not looking up by project_id"
}

variable "organization_id" {
description = "Equinix Metal organization ID to create or find a project in"
default = ""
}

variable "project_id" {
description = "Equinix Metal project ID to deploy into, if not creating a new project or looking up by name"
default = ""
}

variable "metal_create_project" {
Expand All @@ -21,22 +31,22 @@ variable "metal_create_project" {

variable "plan" {
default = "c3.small.x86"
description = "Size of the servers to be deployed on Equinix metal"
description = "Size of the servers to be deployed on Equinix metal (https://deploy.equinix.com/developers/docs/metal/hardware/standard-servers/)"
}

variable "billing_cycle" {
default = "hourly"
description = "Equinix metal billing/invoice generation schedule"
description = "Equinix metal billing/invoice generation schedule (hourly/daily/monthly/yearly)"
}

variable "metro" {
default = "SG"
description = "Equinix metal data center location. Examples: SG,SV,AM,MA,Ny,LA,etc."
description = "Equinix metal data center location (https://deploy.equinix.com/developers/docs/metal/locations/metros/). Examples: SG,SV,AM,MA,Ny,LA,etc."
}

variable "ipxe_script" {
default = "https://raw.githubusercontent.com/rancherlabs/terraform-harvester-equinix/main/ipxe/ipxe-"
description = "URL for booting the servers with IPXE"
description = "URL to the iPXE script to use for booting the server (harvester_version will be appended to this without the 'v' prefix)"
}

variable "hostname_prefix" {
Expand All @@ -46,7 +56,7 @@ variable "hostname_prefix" {

variable "spot_instance" {
default = false
description = "Set to true to use spot instance instead of on demand. Also set you max bid price if true."
description = "Set to true to use spot instance instead of on demand. Also set your max bid price if true."
}

variable "max_bid_price" {
Expand Down Expand Up @@ -92,5 +102,5 @@ variable "api_key" {
variable "use_cheapest_metro" {
type = bool
default = true
description = "A boolean variable to control cheapest metro selection"
description = "Equinix Metal authentication token. Required when using Spot Instances for HTTP pricing lookups. METAL_AUTH_TOKEN should always be set as an environment variable."
}

0 comments on commit eb44ad3

Please sign in to comment.