Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Init terraform #1

Merged
merged 10 commits into from
Aug 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions .terraform.lock.hcl

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

61 changes: 61 additions & 0 deletions example/.terraform.lock.hcl

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 16 additions & 0 deletions example/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
provider "google" {
project = var.project_id
region = var.region
zone = var.zone
}

provider "google-beta" {
project = var.project_id
region = var.region
zone = var.zone
}

module "ctrlplane" {
source = "../"
namespace = var.namespace
}
19 changes: 19 additions & 0 deletions example/variables.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
variable "project_id" {
type = string
description = "Project ID"
}

variable "region" {
type = string
description = "Google region"
}

variable "zone" {
type = string
description = "Google zone"
}

variable "namespace" {
type = string
description = "Namespace prefix used for resources"
}
62 changes: 62 additions & 0 deletions main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
module "project_factory_project_services" {
source = "terraform-google-modules/project-factory/google//modules/project_services"
version = "~> 16.0"
project_id = null
disable_dependent_services = false
disable_services_on_destroy = false
activate_apis = [
"sqladmin.googleapis.com", // Database
"networkmanagement.googleapis.com", // Networking
"servicenetworking.googleapis.com", // Networking
]
}

module "networking" {
source = "./modules/networking"
namespace = var.namespace

depends_on = [module.project_factory_project_services]
}

module "database" {
source = "./modules/database"
namespace = var.namespace

network_connection_string = module.networking.network_connection_string

postgres_tier = var.postgres_tier
postgres_version = var.postgres_version

deletion_protection = var.deletion_protection

depends_on = [module.networking]
}

module "redis" {
source = "./modules/redis"
namespace = var.namespace

tier = var.redis_tier
memory_size_gb = var.redis_memory_size_gb

network_id = module.networking.network_id
}

module "service_accounts" {
source = "./modules/service_accounts"
namespace = var.namespace
}

module "gke" {
source = "./modules/gke"
namespace = var.namespace

deletion_protection = var.deletion_protection

network_self_link = module.networking.network_self_link
subnetwork_self_link = module.networking.subnetwork_self_link

service_account_email = module.service_accounts.service_account_email

depends_on = [module.networking, module.service_accounts]
}
43 changes: 43 additions & 0 deletions modules/database/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
resource "random_string" "this" {
length = 32
special = false
}

resource "random_pet" "this" {
length = 2
keepers = {
namespace = var.postgres_version
}
}

locals {
database_name = "ctrlplane"
master_username = "ctrlplane"
master_password = random_string.this.result
master_instance_name = "${var.namespace}-${random_pet.this.id}"
}

resource "google_sql_database_instance" "this" {
name = local.master_instance_name
database_version = var.postgres_version

settings {
tier = var.postgres_tier
ip_configuration {
ipv4_enabled = false
private_network = var.network_connection_string
}
}
}

resource "google_sql_database" "this" {
name = local.database_name
instance = google_sql_database_instance.this.name
}

resource "google_sql_user" "this" {
instance = google_sql_database_instance.this.name
name = local.master_username
password = local.master_password
}

19 changes: 19 additions & 0 deletions modules/database/outputs.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
output "database_name" {
value = google_sql_database.this.name
description = "The name of the database."
}

output "sql_user_username" {
value = google_sql_user.this.name
description = "The name of the database user."
}

output "sql_user_password" {
value = google_sql_user.this.password
description = "The password of the database user."
}

output "database_instance_private_ip_address" {
value = google_sql_database_instance.this.private_ip_address
description = "The private IP address of the database instance."
}
26 changes: 26 additions & 0 deletions modules/database/variables.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
variable "namespace" {
description = "Namespace for the database"
type = string
}

variable "postgres_version" {
description = "Version for Postgres"
type = string
default = "POSTGRES_16"
}

variable "network_connection_string" {
description = "The private service networking connection string that will connect Postgres to the network."
type = string
}

variable "postgres_tier" {
description = "The tier for the Postgres instance"
type = string
}

variable "deletion_protection" {
description = "Whether to enable deletion protection for the database instance."
type = bool
default = true
}
24 changes: 24 additions & 0 deletions modules/gke/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
resource "google_container_cluster" "this" {
name = "${var.namespace}-cluster"

network = var.network_self_link
subnetwork = var.subnetwork_self_link

enable_autopilot = true

deletion_protection = var.deletion_protection

node_config {
service_account = var.service_account_email
}

release_channel {
channel = "STABLE"
}

master_auth {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

kept this because the comment seemed like we should keep it

client_certificate_config {
issue_client_certificate = false
}
}
}
1 change: 1 addition & 0 deletions modules/gke/outputs.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

25 changes: 25 additions & 0 deletions modules/gke/variables.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
variable "namespace" {
description = "Namespace for the GKE cluster"
type = string
}

variable "service_account_email" {
description = "The service account email associated with the GKE cluster instances to host Ctrlplane."
type = string
}

variable "network_self_link" {
description = "The network self link."
type = string
}

variable "subnetwork_self_link" {
description = "The subnetwork self link."
type = string
}

variable "deletion_protection" {
description = "Whether to enable deletion protection for the database instance."
type = bool
default = true
}
25 changes: 25 additions & 0 deletions modules/networking/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
resource "google_compute_network" "this" {
name = "${var.namespace}-vpc"
description = "Ctrlplane VPC Network"
auto_create_subnetworks = false
}

resource "google_compute_subnetwork" "this" {
name = "${var.namespace}-subnet"
ip_cidr_range = "10.10.0.0/16"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

network = google_compute_network.this.self_link
}

resource "google_compute_global_address" "this" {
name = "${var.namespace}-private-ip-address"
purpose = "VPC_PEERING"
address_type = "INTERNAL"
prefix_length = 16
network = google_compute_network.this.id
}

resource "google_service_networking_connection" "this" {
network = google_compute_network.this.id
service = "servicenetworking.googleapis.com"
reserved_peering_ranges = [google_compute_global_address.this.name]
}
19 changes: 19 additions & 0 deletions modules/networking/outputs.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
output "network_id" {
value = google_compute_network.this.id
description = "The network id."
}

output "network_self_link" {
value = google_compute_network.this.self_link
description = "The network self link."
}

output "subnetwork_self_link" {
value = google_compute_subnetwork.this.self_link
description = "The subnetwork self link."
}

output "network_connection_string" {
description = "The private connection string between the network and GCP services."
value = google_service_networking_connection.this.network
}
4 changes: 4 additions & 0 deletions modules/networking/variables.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
variable "namespace" {
type = string
description = "The name prefix for all resources created."
}
13 changes: 13 additions & 0 deletions modules/redis/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
resource "google_redis_instance" "this" {
name = "${var.namespace}-redis"
display_name = "${var.namespace} Ctrlplane Instance"

tier = var.tier
memory_size_gb = var.memory_size_gb

authorized_network = var.network_id

auth_enabled = true

transit_encryption_mode = "SERVER_AUTHENTICATION"
}
Loading