feat: Linode Provider v2 migration and infrastructure cleanup #35

Merged
bobby merged 3 commits from feat/01a-linode-terraform-cleanup into main 2026-11-17 23:44:08 -05:00
15 changed files with 240 additions and 158 deletions
Showing only changes of commit 494f38c1f3 - Show all commits

No files matched your search

+13 -6
View File
@@ -5,6 +5,7 @@
# Environment defaults # Environment defaults
ENV ?= staging ENV ?= staging
REGION ?= us-east REGION ?= us-east
OBJECT_STORAGE_CLUSTER ?= us-east-1
# Initialize Terraform # Initialize Terraform
init: init:
@@ -17,38 +18,39 @@ init-backend:
terraform init -reconfigure \ terraform init -reconfigure \
-backend-config=bucket=hapi-terraform-state-${ENV} \ -backend-config=bucket=hapi-terraform-state-${ENV} \
-backend-config=key=terraform/${ENV}/terraform.tfstate \ -backend-config=key=terraform/${ENV}/terraform.tfstate \
-backend-config=region=${REGION} -backend-config=region=${OBJECT_STORAGE_CLUSTER}
# Generate plan # Generate plan
plan: plan:
@echo "Generating Terraform plan for ${ENV}..." @echo "Generating Terraform plan for ${ENV}..."
terraform plan -var="environment=${ENV}" terraform plan -var="environment=${ENV}" -var="linode_object_storage_cluster=${OBJECT_STORAGE_CLUSTER}"
# Apply changes # Apply changes
apply: apply:
@echo "Applying Terraform changes for ${ENV}..." @echo "Applying Terraform changes for ${ENV}..."
terraform apply -var="environment=${ENV}" terraform apply -var="environment=${ENV}" -var="linode_object_storage_cluster=${OBJECT_STORAGE_CLUSTER}"
# Apply with auto-approve # Apply with auto-approve
apply-auto: apply-auto:
@echo "Applying Terraform changes for ${ENV} (auto-approve)..." @echo "Applying Terraform changes for ${ENV} (auto-approve)..."
terraform apply -var="environment=${ENV}" -auto-approve terraform apply -var="environment=${ENV}" -var="linode_object_storage_cluster=${OBJECT_STORAGE_CLUSTER}" -auto-approve
# Destroy infrastructure # Destroy infrastructure
destroy: destroy:
@echo "Destroying Terraform infrastructure for ${ENV}..." @echo "Destroying Terraform infrastructure for ${ENV}..."
terraform destroy -var="environment=${ENV}" terraform destroy -var="environment=${ENV}" -var="linode_object_storage_cluster=${OBJECT_STORAGE_CLUSTER}"
# Show outputs # Show outputs
output: output:
@echo "Terraform outputs:" @echo "Terraform outputs:"
terraform output terraform output -json | jq -r 'to_entries | .[] | "\(.key) = \(.value.value)"' 2>/dev/null || terraform output
# Load environment variables # Load environment variables
env: env:
@echo "Environment variables:" @echo "Environment variables:"
@echo " ENV=${ENV}" @echo " ENV=${ENV}"
@echo " REGION=${REGION}" @echo " REGION=${REGION}"
@echo " OBJECT_STORAGE_CLUSTER=${OBJECT_STORAGE_CLUSTER}"
@echo " LINODE_TOKEN=${LINODE_TOKEN}" @echo " LINODE_TOKEN=${LINODE_TOKEN}"
@echo " OBJECT_STORAGE_ACCESS_KEY=${OBJECT_STORAGE_ACCESS_KEY}" @echo " OBJECT_STORAGE_ACCESS_KEY=${OBJECT_STORAGE_ACCESS_KEY}"
@echo " OBJECT_STORAGE_SECRET_KEY=${OBJECT_STORAGE_SECRET_KEY}" @echo " OBJECT_STORAGE_SECRET_KEY=${OBJECT_STORAGE_SECRET_KEY}"
@@ -56,6 +58,11 @@ env:
# Run all checks # Run all checks
check: init plan apply check: init plan apply
# Validate configuration
validate:
@echo "Validating Terraform configuration..."
terraform validate
# Clean up local state # Clean up local state
clean: clean:
@echo "Cleaning local Terraform files..." @echo "Cleaning local Terraform files..."
+15 -4
View File
@@ -1,9 +1,22 @@
# Backend configuration for Terraform state in Linode Object Storage # Backend configuration for Terraform state in Linode Object Storage
# This module provides the configuration needed for remote state storage # This module provides the configuration needed for remote state storage
terraform {
required_providers {
linode = {
source = "linode/linode"
version = "~> 2.0"
}
random = {
source = "hashicorp/random"
version = "~> 3.0"
}
}
}
resource "linode_object_storage_bucket" "terraform_state" { resource "linode_object_storage_bucket" "terraform_state" {
label = "hapi-terraform-state-${var.environment}" label = "hapi-terraform-state-${var.environment}"
cluster = var.linode_object_storage_cluster region = var.linode_object_storage_cluster
access_key = var.object_storage_access_key access_key = var.object_storage_access_key
secret_key = var.object_storage_secret_key secret_key = var.object_storage_secret_key
} }
@@ -38,7 +51,5 @@ output "backend_s3_config" {
secret_key = local.backend_config.secret_key secret_key = local.backend_config.secret_key
skip_region_validation = local.backend_config.skip_region_validation skip_region_validation = local.backend_config.skip_region_validation
} }
sensitive = { sensitive = true
secret_key = local.backend_config.secret_key
}
} }
+2 -1
View File
@@ -1,4 +1,5 @@
# Backend module variables # Backend module example configuration
linode_region = "us-east" linode_region = "us-east"
linode_object_storage_cluster = "us-east-1" linode_object_storage_cluster = "us-east-1"
object_storage_access_key = "your-access-key" object_storage_access_key = "your-access-key"
+41
View File
@@ -1,21 +1,62 @@
# Main Terraform configuration for HAPI infrastructure # Main Terraform configuration for HAPI infrastructure
# This file orchestrates all infrastructure components # This file orchestrates all infrastructure components
terraform {
required_version = ">= 1.9.0"
required_providers {
linode = {
source = "linode/linode"
version = "~> 2.0"
}
random = {
source = "hashicorp/random"
version = "~> 3.0"
}
}
}
# Provider configurations are in providers.tf # Provider configurations are in providers.tf
# Modules # Modules
module "postgres" { module "postgres" {
source = "./postgres" source = "./postgres"
linode_region = var.linode_region
linode_object_storage_cluster = var.linode_object_storage_cluster
object_storage_access_key = var.object_storage_access_key
object_storage_secret_key = var.object_storage_secret_key
environment = var.environment
} }
module "storage" { module "storage" {
source = "./storage" source = "./storage"
linode_region = var.linode_region
linode_object_storage_cluster = var.linode_object_storage_cluster
environment = var.environment
object_storage_access_key = var.object_storage_access_key
object_storage_secret_key = var.object_storage_secret_key
} }
module "network" { module "network" {
source = "./network" source = "./network"
linode_region = var.linode_region
environment = var.environment
} }
# Backend configuration for Terraform state # Backend configuration for Terraform state
# State will be stored in Linode Object Storage # State will be stored in Linode Object Storage
# Backend configuration is defined in infra/backend/ # Backend configuration is defined in infra/backend/
# Backend module (for terraform state storage configuration)
module "backend" {
source = "./backend"
linode_region = var.linode_region
linode_object_storage_cluster = var.linode_object_storage_cluster
object_storage_access_key = var.object_storage_access_key
object_storage_secret_key = var.object_storage_secret_key
environment = var.environment
}
+64 -56
View File
@@ -1,75 +1,83 @@
# Networking module for HAPI # Networking module for HAPI
# Creates firewall rules for API, PostgreSQL, and Adminer # Creates firewall rules for API, PostgreSQL, and Adminer
terraform {
required_providers {
linode = {
source = "linode/linode"
version = "~> 2.0"
}
random = {
source = "hashicorp/random"
version = "~> 3.0"
}
}
}
resource "linode_firewall" "hapi" { resource "linode_firewall" "hapi" {
label = "hapi-firewall" label = "hapi-firewall-${var.environment}"
tags = var.tags tags = toset([for k, v in var.tags : "${k}=${v}"])
# Inbound policy
inbound_policy = "DROP"
# Outbound policy
outbound_policy = "DROP"
# Inbound rules # Inbound rules
inbound = [ inbound {
{ action = "ACCEPT"
label = "allow-api" label = "allow-api-${var.environment}"
protocol = "tcp" protocol = "TCP"
ports = "8000" ports = "8000"
cidrs = ["0.0.0.0/0", "::/0"] ipv4 = ["0.0.0.0/0"]
}, ipv6 = ["::/0"]
{ }
label = "allow-postgres"
protocol = "tcp" inbound {
action = "ACCEPT"
label = "allow-postgres-${var.environment}"
protocol = "TCP"
ports = "5432" ports = "5432"
cidrs = ["10.0.0.0/8", "172.16.0.0/12", "192.168.0.0/16"] ipv4 = ["10.0.0.0/8", "172.16.0.0/12", "192.168.0.0/16"]
}, }
{
label = "allow-adminer" inbound {
protocol = "tcp" action = "ACCEPT"
label = "allow-adminer-${var.environment}"
protocol = "TCP"
ports = "8080" ports = "8080"
cidrs = ["0.0.0.0/0", "::/0"] ipv4 = ["0.0.0.0/0"]
}, ipv6 = ["::/0"]
{ }
label = "allow-https"
protocol = "tcp" inbound {
action = "ACCEPT"
label = "allow-https-${var.environment}"
protocol = "TCP"
ports = "443" ports = "443"
cidrs = ["0.0.0.0/0", "::/0"] ipv4 = ["0.0.0.0/0"]
}, ipv6 = ["::/0"]
{ }
label = "allow-http"
protocol = "tcp" inbound {
action = "ACCEPT"
label = "allow-http-${var.environment}"
protocol = "TCP"
ports = "80" ports = "80"
cidrs = ["0.0.0.0/0", "::/0"] ipv4 = ["0.0.0.0/0"]
ipv6 = ["::/0"]
} }
]
# Outbound rules # Outbound rules
outbound = [ outbound {
{ action = "ACCEPT"
label = "allow-all-outbound" label = "allow-all-outbound-${var.environment}"
protocol = "tcp" protocol = "TCP"
ports = "0-65535" ports = "0-65535"
cidrs = ["0.0.0.0/0", "::/0"] ipv4 = ["0.0.0.0/0"]
ipv6 = ["::/0"]
} }
]
}
resource "linode_firewall_rule" "api_health" {
for_each = toset(["staging", "production"])
firewall_id = linode_firewall.hapi.id
label = "api-health-${each.key}"
direction = "inbound"
protocol = "tcp"
ports = "8000"
cidrs = ["0.0.0.0/0", "::/0"]
}
resource "linode_firewall_rule" "adminer_access" {
for_each = toset(["staging", "production"])
firewall_id = linode_firewall.hapi.id
label = "adminer-${each.key}"
direction = "inbound"
protocol = "tcp"
ports = "8080"
cidrs = ["0.0.0.0/0", "::/0"]
} }
# Firewall rule configurations # Firewall rule configurations
+3 -1
View File
@@ -1,3 +1,5 @@
# Network module variables # Network module example configuration
linode_region = "us-east" linode_region = "us-east"
linode_object_storage_cluster = "us-east-1"
environment = "staging" environment = "staging"
+6
View File
@@ -15,6 +15,12 @@ variable "tags" {
} }
} }
variable "linode_object_storage_cluster" {
description = "Linode Object Storage cluster for backend state storage"
type = string
default = "us-east-1"
}
variable "environment" { variable "environment" {
description = "Deployment environment (staging or production)" description = "Deployment environment (staging or production)"
type = string type = string
+18 -5
View File
@@ -1,12 +1,25 @@
# PostgreSQL module for HAPI # PostgreSQL module for HAPI
# Creates separate database instances for staging and production # Creates separate database instances for staging and production
terraform {
required_providers {
linode = {
source = "linode/linode"
version = "~> 2.0"
}
random = {
source = "hashicorp/random"
version = "~> 3.0"
}
}
}
# Object Storage buckets for PostgreSQL state # Object Storage buckets for PostgreSQL state
resource "linode_object_storage_bucket" "state" { resource "linode_object_storage_bucket" "state" {
for_each = toset(["staging", "production"]) for_each = toset(["staging", "production"])
label = "hapi-postgres-state-${each.key}" label = "hapi-postgres-state-${each.key}"
cluster = var.linode_object_storage_cluster region = var.linode_object_storage_cluster
access_key = var.object_storage_access_key access_key = var.object_storage_access_key
secret_key = var.object_storage_secret_key secret_key = var.object_storage_secret_key
} }
@@ -17,9 +30,9 @@ resource "linode_lke_cluster" "postgres" {
region = var.linode_region region = var.linode_region
label = "hapi-postgres-${each.key}" label = "hapi-postgres-${each.key}"
k8s_version = var.linode_lke_version k8s_version = var.linode_lke_version
tags = merge(var.tags, { Environment = each.key }) tags = toset([for k, v in merge(var.tags, { Environment = each.key }) : "${k}=${v}"])
pools { pool {
count = each.key == "production" ? 2 : 1 count = each.key == "production" ? 2 : 1
type = var.linode_node_type type = var.linode_node_type
} }
@@ -40,8 +53,8 @@ locals {
} }
output "endpoint" { output "endpoint" {
description = "PostgreSQL endpoint" description = "PostgreSQL endpoint (Kubernetes API endpoint)"
value = linode_lke_cluster.postgres[var.environment].hostname value = linode_lke_cluster.postgres[var.environment].api_endpoints[0]
} }
output "port" { output "port" {
+5 -1
View File
@@ -1,5 +1,9 @@
# Postgres module variables # PostgreSQL module example configuration
linode_region = "us-east" linode_region = "us-east"
linode_lke_version = "1.30" linode_lke_version = "1.30"
linode_node_type = "g6-standard-4" linode_node_type = "g6-standard-4"
linode_object_storage_cluster = "us-east-1" linode_object_storage_cluster = "us-east-1"
object_storage_access_key = "your-access-key"
object_storage_secret_key = "your-secret-key"
environment = "staging"
+9
View File
@@ -43,3 +43,12 @@ variable "tags" {
Project = "hapi" Project = "hapi"
} }
} }
variable "environment" {
description = "Deployment environment (staging or production)"
type = string
validation {
condition = contains(["staging", "production"], var.environment)
error_message = "Environment must be 'staging' or 'production'."
}
}
+2 -4
View File
@@ -1,8 +1,6 @@
# Provider configurations for HAPI Terraform infrastructure # Provider configurations for HAPI Terraform infrastructure
provider "linode" { # Provider configurations
token = var.linode_token # These use the sources defined in main.tf terraform block
region = var.linode_region
}
provider "random" {} provider "random" {}
+15 -79
View File
@@ -1,97 +1,33 @@
# Object Storage module for HAPI # Object Storage module for HAPI
# Creates buckets for uploads and Terraform state # Creates buckets for uploads and Terraform state
terraform {
required_providers {
linode = {
source = "linode/linode"
version = "~> 2.0"
}
random = {
source = "hashicorp/random"
version = "~> 3.0"
}
}
}
resource "linode_object_storage_bucket" "uploads" { resource "linode_object_storage_bucket" "uploads" {
for_each = toset(["staging", "production"]) for_each = toset(["staging", "production"])
label = "hapi-uploads-${each.key}" label = "hapi-uploads-${each.key}"
cluster = var.linode_object_storage_cluster region = var.linode_object_storage_cluster
access_key = var.object_storage_access_key access_key = var.object_storage_access_key
secret_key = var.object_storage_secret_key secret_key = var.object_storage_secret_key
depends_on = [linode_object_storage_cluster.this]
} }
resource "linode_object_storage_bucket" "state" { resource "linode_object_storage_bucket" "state" {
for_each = toset(["staging", "production"]) for_each = toset(["staging", "production"])
label = "hapi-terraform-state-${each.key}" label = "hapi-terraform-state-${each.key}"
cluster = var.linode_object_storage_cluster region = var.linode_object_storage_cluster
access_key = var.object_storage_access_key access_key = var.object_storage_access_key
secret_key = var.object_storage_secret_key secret_key = var.object_storage_secret_key
depends_on = [linode_object_storage_cluster.this]
}
resource "linode_object_storage_cluster" "this" {
label = "hapi-object-storage"
region = var.linode_region
}
# Bucket configurations
locals {
bucket_config = {
staging = {
uploads = {
label = "hapi-uploads-staging"
policy = jsonencode({
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Principal": { "AWS": ["*"] },
"Action": ["s3:GetObject", "s3:PutObject"],
"Resource": ["arn:aws:s3:::hapi-uploads-staging/*"]
}
]
})
}
state = {
label = "hapi-terraform-state-staging"
}
}
production = {
uploads = {
label = "hapi-uploads-production"
policy = jsonencode({
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Principal": { "AWS": ["*"] },
"Action": ["s3:GetObject", "s3:PutObject"],
"Resource": ["arn:aws:s3:::hapi-uploads-production/*"]
}
]
})
}
state = {
label = "hapi-terraform-state-production"
}
}
}
}
output "endpoint" {
description = "Object Storage endpoint"
value = "https://${var.linode_region}.linodeobjects.com"
}
output "access_key" {
description = "Object Storage access key"
value = var.object_storage_access_key
}
output "secret_key" {
description = "Object Storage secret key"
value = var.object_storage_secret_key
sensitive = true
}
output "state_bucket_name" {
description = "Name of the Terraform state bucket"
value = "hapi-terraform-state-${var.environment}"
}
output "uploads_bucket_name" {
description = "Name of the uploads bucket"
value = "hapi-uploads-${var.environment}"
} }
+27
View File
@@ -0,0 +1,27 @@
# Outputs for Object Storage module
output "endpoint" {
description = "Object Storage endpoint"
value = var.linode_object_storage_cluster
}
output "access_key" {
description = "Object Storage access key"
value = var.object_storage_access_key
}
output "secret_key" {
description = "Object Storage secret key (sensitive)"
value = var.object_storage_secret_key
sensitive = true
}
output "state_bucket_name" {
description = "Name of the Terraform state bucket"
value = "hapi-terraform-state-${var.environment}"
}
output "uploads_bucket_name" {
description = "Name of the uploads bucket"
value = "hapi-uploads-${var.environment}"
}
+2 -1
View File
@@ -1,4 +1,5 @@
# Storage module variables # Storage module example configuration
linode_region = "us-east" linode_region = "us-east"
linode_object_storage_cluster = "us-east-1" linode_object_storage_cluster = "us-east-1"
object_storage_access_key = "your-access-key" object_storage_access_key = "your-access-key"
+18
View File
@@ -36,3 +36,21 @@ variable "tags" {
Project = "hapi" Project = "hapi"
} }
} }
# Backend configuration variables
variable "object_storage_access_key" {
description = "Linode Object Storage access key for backend state storage"
type = string
}
variable "object_storage_secret_key" {
description = "Linode Object Storage secret key for backend state storage"
type = string
sensitive = true
}
variable "linode_object_storage_cluster" {
description = "Linode Object Storage cluster for backend state storage"
type = string
default = "us-east-1"
}