-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: deploy a sharded MongoDB (#168)
- Loading branch information
Showing
22 changed files
with
1,029 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
#------------------------------------------------------------------------------ | ||
# Certificate Authority | ||
#------------------------------------------------------------------------------ | ||
resource "tls_private_key" "root_mongodb" { | ||
algorithm = "RSA" | ||
ecdsa_curve = "P384" | ||
rsa_bits = "4096" | ||
} | ||
|
||
resource "tls_self_signed_cert" "root_mongodb" { | ||
private_key_pem = tls_private_key.root_mongodb.private_key_pem | ||
is_ca_certificate = true | ||
validity_period_hours = var.validity_period_hours | ||
allowed_uses = [ | ||
"cert_signing", | ||
"key_encipherment", | ||
"digital_signature" | ||
] | ||
subject { | ||
organization = "ArmoniK mongodb Root (NonTrusted)" | ||
common_name = "ArmoniK mongodb Root (NonTrusted) Private Certificate Authority" | ||
country = "France" | ||
} | ||
} | ||
|
||
#------------------------------------------------------------------------------ | ||
# Certificate | ||
#------------------------------------------------------------------------------ | ||
resource "tls_private_key" "mongodb_private_key" { | ||
algorithm = "RSA" | ||
ecdsa_curve = "P384" | ||
rsa_bits = "4096" | ||
} | ||
|
||
resource "tls_cert_request" "mongodb_cert_request" { | ||
private_key_pem = tls_private_key.mongodb_private_key.private_key_pem | ||
subject { | ||
country = "France" | ||
common_name = "127.0.0.1" | ||
# organization = "127.0.0.1" | ||
} | ||
} | ||
|
||
resource "tls_locally_signed_cert" "mongodb_certificate" { | ||
cert_request_pem = tls_cert_request.mongodb_cert_request.cert_request_pem | ||
ca_private_key_pem = tls_private_key.root_mongodb.private_key_pem | ||
ca_cert_pem = tls_self_signed_cert.root_mongodb.cert_pem | ||
validity_period_hours = var.validity_period_hours | ||
allowed_uses = [ | ||
"key_encipherment", | ||
"digital_signature", | ||
"server_auth", | ||
"client_auth", | ||
"any_extended", | ||
] | ||
} | ||
|
||
#------------------------------------------------------------------------------ | ||
# Kubernetes Secrets with certificates | ||
#------------------------------------------------------------------------------ | ||
|
||
resource "kubernetes_secret" "mongodb_certificate" { | ||
metadata { | ||
name = "${var.name}-server-certificates" | ||
namespace = var.namespace | ||
} | ||
data = { | ||
"mongodb.pem" = format("%s\n%s", tls_locally_signed_cert.mongodb_certificate.cert_pem, tls_private_key.mongodb_private_key.private_key_pem) | ||
"chain.pem" = format("%s\n%s", tls_locally_signed_cert.mongodb_certificate.cert_pem, tls_self_signed_cert.root_mongodb.cert_pem) | ||
} | ||
} | ||
|
||
resource "local_sensitive_file" "mongodb_client_certificate" { | ||
content = format("%s\n%s", tls_locally_signed_cert.mongodb_certificate.cert_pem, tls_self_signed_cert.root_mongodb.cert_pem) | ||
filename = "${path.root}/generated/certificates/${var.name}/chain.pem" | ||
file_permission = "0600" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
locals { | ||
database_init_script = <<EOF | ||
db = db.getSiblingDB("${var.mongodb.database_name}"); | ||
db.createCollection("sample") | ||
db.sample.insertOne({test:1}) | ||
db.createUser( | ||
{ | ||
user: "${random_string.mongodb_application_user.result}", | ||
pwd: "${random_password.mongodb_application_password.result}", | ||
roles: [ | ||
{role: "readWrite", db: "${var.mongodb.database_name}" }, | ||
{ role: "dbAdmin", db: "${var.mongodb.database_name}" } | ||
] | ||
} | ||
); | ||
db.sample.drop() | ||
db.adminCommand( | ||
{ | ||
enableSharding: "${var.mongodb.database_name}" | ||
} | ||
); | ||
EOF | ||
} | ||
|
||
resource "kubernetes_secret" "database_init_script" { | ||
metadata { | ||
name = "${var.name}-database-init-script" | ||
namespace = var.namespace | ||
} | ||
data = { | ||
"initDatabase.js" = local.database_init_script | ||
} | ||
} | ||
|
||
resource "local_sensitive_file" "init_script_file" { | ||
content = local.database_init_script | ||
filename = "${path.root}/generated/${var.name}/configmaps/database_init_script.js" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
resource "random_string" "mongodb_application_user" { | ||
length = 8 | ||
special = false | ||
numeric = false | ||
} | ||
|
||
resource "random_password" "mongodb_application_password" { | ||
length = 16 | ||
special = false | ||
} |
106 changes: 106 additions & 0 deletions
106
storage/onpremise/mongodb-sharded/examples/complete/main.tf
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
module "sharded_mongodb" { | ||
source = "../../" | ||
namespace = var.namespace | ||
name = "mongodb-sharded" | ||
timeout = 300 | ||
|
||
labels = { | ||
shards = { | ||
"role" = "dataHolder" | ||
} | ||
|
||
router = { | ||
"role" = "distributer" | ||
} | ||
|
||
configsvr = { | ||
"role" = "metadataHolder" | ||
} | ||
} | ||
|
||
mongodb = { | ||
helm_chart_repository = "oci://registry-1.docker.io/bitnamicharts" | ||
helm_chart_version = "8.3.8" | ||
image_pull_secrets = [""] | ||
node_selector = {} | ||
registry = "docker.io" | ||
tag = "7.0.14-debian-12-r0" | ||
} | ||
|
||
sharding = { | ||
shards = { | ||
quantity = 2 | ||
replicas = 2 | ||
} | ||
|
||
configsvr = { | ||
replicas = 2 | ||
} | ||
|
||
router = { | ||
replicas = 2 | ||
} | ||
} | ||
|
||
persistence = { | ||
shards = { | ||
access_mode = ["ReadWriteOnce"] | ||
reclaim_policy = "Retain" | ||
storage_provisioner = "rancher.io/local-path" | ||
resources = { | ||
requests = { | ||
storage = "8Gi" | ||
} | ||
} | ||
} | ||
|
||
configsvr = { | ||
access_mode = ["ReadWriteOnce"] | ||
reclaim_policy = "Delete" | ||
storage_provisioner = "rancher.io/local-path" | ||
resources = { | ||
requests = { | ||
storage = "1Gi" | ||
} | ||
} | ||
} | ||
} | ||
|
||
resources = { | ||
shards = { | ||
limits = { | ||
"cpu" = "1" | ||
"memory" = "2Gi" | ||
} | ||
requests = { | ||
"cpu" = "500m" | ||
"memory" = "1Gi" | ||
} | ||
} | ||
|
||
arbiter = { | ||
limits = { | ||
"cpu" = "500m" | ||
"memory" = "500Mi" | ||
} | ||
} | ||
|
||
configsvr = { | ||
limits = { | ||
"cpu" = "1" | ||
"memory" = "1Gi" | ||
} | ||
requests = { | ||
"cpu" = "200m" | ||
"memory" = "400Mi" | ||
} | ||
} | ||
|
||
router = { | ||
requests = { | ||
"cpu" = "400m" | ||
"memory" = "700Mi" | ||
} | ||
} | ||
} | ||
} |
52 changes: 52 additions & 0 deletions
52
storage/onpremise/mongodb-sharded/examples/complete/outputs.tf
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
output "host" { | ||
description = "Hostname or IP address of MongoDB server" | ||
value = module.sharded_mongodb.host | ||
} | ||
|
||
output "port" { | ||
description = "Port of MongoDB server" | ||
value = module.sharded_mongodb.port | ||
} | ||
|
||
output "url" { | ||
description = "URL of MongoDB server" | ||
value = module.sharded_mongodb.url | ||
} | ||
|
||
output "number_of_shards" { | ||
description = "Number of MongoDB shards" | ||
value = module.sharded_mongodb.number_of_shards | ||
} | ||
|
||
output "number_of_replicas" { | ||
description = "Number of replicas for each shard" | ||
value = module.sharded_mongodb.number_of_replicas | ||
} | ||
|
||
# SENSITIVE OUTPUTS | ||
output "user_credentials" { | ||
description = "User credentials of MongoDB" | ||
value = module.sharded_mongodb.user_credentials | ||
sensitive = true | ||
} | ||
|
||
output "endpoints" { | ||
description = "Endpoints of MongoDB" | ||
value = module.sharded_mongodb.endpoints | ||
sensitive = true | ||
} | ||
|
||
output "env" { | ||
description = "Environment variables passed down to ArmoniK Core" | ||
value = module.sharded_mongodb.env | ||
} | ||
|
||
output "mount_secrets" { | ||
description = "Secrets to be mounted as volumes" | ||
value = module.sharded_mongodb.mount_secret | ||
} | ||
|
||
output "env_from_secret" { | ||
description = "Environment variables from secrets" | ||
value = module.sharded_mongodb.env_from_secret | ||
} |
9 changes: 9 additions & 0 deletions
9
storage/onpremise/mongodb-sharded/examples/complete/providers.tf
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
provider "helm" { | ||
kubernetes { | ||
config_path = var.kube_config_path | ||
} | ||
} | ||
|
||
provider "kubernetes" { | ||
config_path = var.kube_config_path | ||
} |
11 changes: 11 additions & 0 deletions
11
storage/onpremise/mongodb-sharded/examples/complete/variables.tf
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
variable "namespace" { | ||
description = "Namespace of ArmoniK resources" | ||
type = string | ||
default = "default" | ||
} | ||
|
||
variable "kube_config_path" { | ||
description = "The kubernetes configuration file path you want to specify" | ||
type = string | ||
default = "~/.kube/config" | ||
} |
13 changes: 13 additions & 0 deletions
13
storage/onpremise/mongodb-sharded/examples/complete/versions.tf
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
terraform { | ||
required_version = ">= 1.2" | ||
required_providers { | ||
helm = { | ||
source = "hashicorp/helm" | ||
version = ">= 2.10.1" | ||
} | ||
kubernetes = { | ||
source = "hashicorp/kubernetes" | ||
version = ">= 2.21.1" | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
module "sharded_mongodb" { | ||
source = "../../" | ||
namespace = var.namespace | ||
name = "mongodb-sharded" | ||
timeout = 300 | ||
|
||
mongodb = { | ||
helm_chart_version = "8.3.8" | ||
tag = "7.0.14-debian-12-r0" | ||
} | ||
} |
Oops, something went wrong.