🏭️ Second pass of Terraform refactoring

Break down provider-specific configuration into two files:
- config.tf (actual configuration, e.g. credentials, that cannot be
  included in submodules)
- variables.tf (per-provider knobs and settings, e.g. mapping logical
  VM size like S/M/L to actual cloud SKUs)
This commit is contained in:
Jérôme Petazzoni
2023-04-09 09:45:05 +02:00
parent f69a9d3eb8
commit abca33af29
64 changed files with 118 additions and 105 deletions

View File

@@ -120,11 +120,19 @@ Legend:
│ ├── 📄common.tf
│ ├── 📁🌍digitalocean
│ └── ...
├── 📁provider-config
│ ├── 📄aws.tf
│ ├── 📄azure.tf
├── 📄civo.tf
│ ├── 📄digitalocean.tf
├── 📁providers
│ ├── 📁aws
│ ├── 📄config.tf
│ └── 📄variables.tf
│ ├── 📁azure
│ │ ├── 📄config.tf
│ │ └── 📄variables.tf
│ ├── 📁civo
│ │ ├── 📄config.tf
│ │ └── 📄variables.tf
│ ├── 📁digitalocean
│ │ ├── 📄config.tf
│ │ └── 📄variables.tf
│ └── ...
├── 📁tags
│ │ (contains Terraform configurations + other files
@@ -150,7 +158,7 @@ The directory structure can feel a bit overwhelming at first, but it's built wit
**Don't repeat yourself.** As much as possible, common variables, definitions, and logic has been factored in the `common.tf` file that you can see in `one-kubernetes` and `virtual-machines`. That file is then symlinked in each provider-specific directory, to make sure that all providers use the same version of the `common.tf` file.
**Don't repeat yourself (again).** The things that are specific to each provider (e.g. how to obtain the credentials; the size of the VMs to use...) have been placed in the `provider-config` directory, and are shared between the `one-kubernetes` and the `virtual-machines` configurations.
**Don't repeat yourself (again).** The things that are specific to each provider have been placed in the `providers` directory, and are shared between the `one-kubernetes` and the `virtual-machines` configurations. Specifically, for each provider, there is `config.tf` (which contains provider configuration, e.g. how to obtain the credentials for that provider) and `variables.tf` (which contains default values like which location and which VM size to use).
**Terraform configurations should work in `labctl` or standalone, without extra work.** The Terraform configurations (identified by 🌍 in the directory tree above) can be used directly. Just go to one of these directories, `terraform init`, `terraform apply`, and you're good to go. But they can also be used from `labctl`. `labctl` shouldn't barf out if you did a `terraform apply` in one of these directories (because it will only copy the `*.tf` files, and leave alone the other files, like the Terraform state).

View File

@@ -4,7 +4,6 @@ module "clusters" {
cluster_name = each.value.cluster_name
min_nodes_per_pool = local.min_nodes_per_pool
max_nodes_per_pool = local.max_nodes_per_pool
enable_arm_pool = var.enable_arm_pool
node_size = var.node_size
common_tags = local.common_tags
location = each.value.location

View File

@@ -17,13 +17,8 @@ variable "node_size" {
default = "M"
}
variable "enable_arm_pool" {
type = bool
default = false
}
variable "location" {
type = string
type = string
default = null
}

View File

@@ -1 +1 @@
../../provider-config/aws.tf
../../providers/aws/config.tf

View File

@@ -0,0 +1 @@
../../providers/aws/variables.tf

View File

@@ -1 +1 @@
../../provider-config/civo.tf
../../providers/civo/config.tf

View File

@@ -4,4 +4,4 @@ terraform {
source = "civo/civo"
}
}
}
}

View File

@@ -0,0 +1 @@
../../providers/civo/variables.tf

View File

@@ -1 +1 @@
../../provider-config/digitalocean.tf
../../providers/digitalocean/config.tf

View File

@@ -0,0 +1 @@
../../providers/digitalocean/variables.tf

View File

@@ -1 +1 @@
../../provider-config/exoscale.tf
../../providers/exoscale/config.tf

View File

@@ -0,0 +1 @@
../../providers/exoscale/variables.tf

View File

@@ -1 +1 @@
../../provider-config/googlecloud.tf
../../providers/googlecloud/config.tf

View File

@@ -0,0 +1,12 @@
locals {
location = var.location != null ? var.location : "europe-north1-a"
region = replace(local.location, "/-[a-z]$/", "")
# Unfortunately, the following line doesn't work
# (that attribute just returns an empty string)
# so we have to hard-code the project name.
#project = data.google_client_config._.project
project = "prepare-tf"
}
data "google_client_config" "_" {}

View File

@@ -1,12 +0,0 @@
locals {
location = var.location != null ? var.location : "europe-north1-a"
region = replace(local.location, "/-[a-z]$/", "")
# Unfortunately, the following line doesn't work
# (that attribute just returns an empty string)
# so we have to hard-code the project name.
#project = data.google_client_config._.project
project = "prepare-tf"
}
data "google_client_config" "_" {}

View File

@@ -0,0 +1 @@
../../providers/googlecloud/variables.tf

View File

@@ -1 +1 @@
../../provider-config/linode.tf
../../providers/linode/config.tf

View File

@@ -0,0 +1 @@
../../providers/linode/variables.tf

View File

@@ -1 +1 @@
../../provider-config/oci.tf
../../providers/oci/config.tf

View File

@@ -0,0 +1 @@
../../providers/oci/variables.tf

View File

@@ -1 +1 @@
../../provider-config/scaleway.tf
../../providers/scaleway/config.tf

View File

@@ -0,0 +1 @@
../../providers/scaleway/variables.tf

View File

@@ -1 +1 @@
../../provider-config/vcluster.tf
../../providers/vcluster/config.tf

View File

@@ -1,9 +0,0 @@
variable "node_sizes" {
type = map(string)
default = {}
}
variable "location" {
type = string
default = null
}

View File

@@ -0,0 +1 @@
../../providers/vcluster/variables.tf

View File

@@ -0,0 +1,3 @@
provider "aws" {
region = var.location
}

View File

@@ -1,7 +1,3 @@
provider "aws" {
region = var.location
}
variable "node_sizes" {
type = map(any)
default = {

View File

@@ -0,0 +1,3 @@
provider "azurerm" {
features {}
}

View File

@@ -1,7 +1,3 @@
provider "azurerm" {
features {}
}
/*
Available sizes:
"Standard_D11_v2" # CPU=2 RAM=14

View File

@@ -7,17 +7,3 @@ locals {
civo_current = local.civo_config.meta.current_apikey
civo_apikey = local.civo_config.apikeys[local.civo_current]
}
variable "node_sizes" {
type = map(any)
default = {
S = "g4s.kube.small"
M = "g4s.kube.medium"
L = "g4s.kube.large"
}
}
variable "location" {
type = string
default = "lon1"
}

View File

@@ -0,0 +1,13 @@
variable "node_sizes" {
type = map(any)
default = {
S = "g4s.kube.small"
M = "g4s.kube.medium"
L = "g4s.kube.large"
}
}
variable "location" {
type = string
default = "lon1"
}

View File

@@ -0,0 +1,3 @@
provider "digitalocean" {
token = yamldecode(file("~/.config/doctl/config.yaml"))["access-token"]
}

View File

@@ -1,7 +1,3 @@
provider "digitalocean" {
token = yamldecode(file("~/.config/doctl/config.yaml"))["access-token"]
}
variable "node_sizes" {
type = map(any)
default = {

View File

@@ -2,17 +2,3 @@ provider "exoscale" {
key = regex("\n key *= *\"([^\"]+)\"\n", file("~/.config/exoscale/exoscale.toml"))[0]
secret = regex("\n secret *= *\"([^\"]+)\"\n", file("~/.config/exoscale/exoscale.toml"))[0]
}
variable "node_sizes" {
type = map(any)
default = {
S = "standard.small"
M = "standard.medium"
L = "standard.large"
}
}
variable "location" {
type = string
default = "ch-gva-2"
}

View File

@@ -0,0 +1,13 @@
variable "node_sizes" {
type = map(any)
default = {
S = "standard.small"
M = "standard.medium"
L = "standard.large"
}
}
variable "location" {
type = string
default = "ch-gva-2"
}

View File

@@ -0,0 +1,9 @@
/*
Okay, the following is pretty gross - it uses the first token found in the hcloud CLI
configuration file. We don't use Hetzner much anyway, and when we do, we only have one
profile ever, and we want this thing to Just Work; so this should do for now, but might
need to be improved if others actively use Hetzner to provision training labs.
*/
provider "hcloud" {
token = regex("token = \"([A-Za-z0-9]+)\"", file("~/.config/hcloud/cli.toml"))[0]
}

View File

@@ -1,13 +1,3 @@
/*
Okay, the following is pretty gross - it uses the first token found in the hcloud CLI
configuration file. We don't use Hetzner much anyway, and when we do, we only have one
profile ever, and we want this thing to Just Work; so this should do for now, but might
need to be improved if others actively use Hetzner to provision training labs.
*/
provider "hcloud" {
token = regex("token = \"([A-Za-z0-9]+)\"", file("~/.config/hcloud/cli.toml"))[0]
}
/*
$ hcloud server-type list | grep shared
1 cx11 1 shared 2.0 GB 20 GB local

View File

@@ -0,0 +1,3 @@
provider "linode" {
token = regex("\ntoken *= *([0-9a-f]+)\n", file("~/.config/linode-cli"))[0]
}

View File

@@ -1,7 +1,3 @@
provider "linode" {
token = regex("\ntoken *= *([0-9a-f]+)\n", file("~/.config/linode-cli"))[0]
}
/*
Available sizes:
"g6-standard-1" # CPU=1 RAM=2

View File

@@ -0,0 +1,9 @@
variable "node_sizes" {
type = map(any)
default = {}
}
variable "location" {
type = string
default = null
}

View File

@@ -1 +1 @@
../../provider-config/aws.tf
../../providers/aws/config.tf

View File

@@ -0,0 +1 @@
../../providers/aws/variables.tf

View File

@@ -1 +1 @@
../../provider-config/azure.tf
../../providers/azure/config.tf

View File

@@ -0,0 +1 @@
../../providers/azure/variables.tf

View File

@@ -1 +1 @@
../../provider-config/digitalocean.tf
../../providers/digitalocean/config.tf

View File

@@ -0,0 +1 @@
../../providers/digitalocean/variables.tf

View File

@@ -1 +1 @@
../../provider-config/hetzner.tf
../../providers/hetzner/config.tf

View File

@@ -0,0 +1 @@
../../providers/hetzner/variables.tf

View File

@@ -1 +1 @@
../../provider-config/linode.tf
../../providers/linode/config.tf

View File

@@ -0,0 +1 @@
../../providers/linode/variables.tf

View File

@@ -1 +1 @@
../../provider-config/oci.tf
../../providers/oci/config.tf

View File

@@ -0,0 +1 @@
../../providers/oci/variables.tf

View File

@@ -0,0 +1 @@
../../providers/openstack/config.tf

View File

@@ -0,0 +1 @@
../../providers/openstack/variables.tf

View File

@@ -1 +1 @@
../../provider-config/scaleway.tf
../../providers/scaleway/config.tf

View File

@@ -0,0 +1 @@
../../providers/scaleway/variables.tf