diff --git a/terraform/main.tf b/terraform/main.tf index 796caf1..468daed 100644 --- a/terraform/main.tf +++ b/terraform/main.tf @@ -11,55 +11,25 @@ module "mimir_coordinator" { units = var.coordinator_units } -module "mimir_backend" { - source = "../worker/terraform" - depends_on = [module.mimir_coordinator] - - app_name = var.backend_name - channel = var.channel - constraints = var.anti_affinity ? "arch=amd64 tags=anti-pod.app.kubernetes.io/name=${var.backend_name},anti-pod.topology-key=kubernetes.io/hostname" : var.worker_constraints - config = merge({ - role-backend = true - }, var.backend_config) - model_uuid = var.model_uuid - resources = var.worker_resources - revision = var.worker_revision - storage_directives = var.backend_worker_storage_directives - units = var.backend_units -} - -module "mimir_read" { - source = "../worker/terraform" - depends_on = [module.mimir_coordinator] - - app_name = var.read_name - channel = var.channel - config = merge({ - role-read = true - }, var.read_config) - constraints = var.anti_affinity ? "arch=amd64 tags=anti-pod.app.kubernetes.io/name=${var.read_name},anti-pod.topology-key=kubernetes.io/hostname" : var.worker_constraints - model_uuid = var.model_uuid - resources = var.worker_resources - revision = var.worker_revision - storage_directives = var.read_worker_storage_directives - units = var.read_units +locals { + workers = { for k, v in var.workers : k => merge(v, { + app_name = coalesce(v.app_name, "mimir-${k}") + }) } } -module "mimir_write" { - source = "../worker/terraform" - depends_on = [module.mimir_coordinator] +module "mimir_worker" { + for_each = local.workers + source = "../worker/terraform" - app_name = var.write_name - channel = var.channel - config = merge({ - role-write = true - }, var.write_config) - constraints = var.anti_affinity ? "arch=amd64 tags=anti-pod.app.kubernetes.io/name=${var.write_name},anti-pod.topology-key=kubernetes.io/hostname" : var.worker_constraints + app_name = each.value.app_name + channel = var.channel + config = merge({ "role-${each.key}" = "true" }, each.value.config) + constraints = var.anti_affinity ? "arch=amd64 tags=anti-pod.app.kubernetes.io/name=${each.value.app_name},anti-pod.topology-key=kubernetes.io/hostname" : each.value.constraints model_uuid = var.model_uuid resources = var.worker_resources revision = var.worker_revision - storage_directives = var.write_worker_storage_directives - units = var.write_units + storage_directives = each.value.storage_directives + units = each.value.units } # -------------- # S3-integrator -------------- @@ -117,35 +87,8 @@ resource "juju_integration" "coordinator_to_s3_integrator" { } } -resource "juju_integration" "coordinator_to_read" { - model_uuid = var.model_uuid - - application { - name = module.mimir_coordinator.app_name - endpoint = module.mimir_coordinator.provides.mimir_cluster - } - - application { - name = module.mimir_read.app_name - endpoint = module.mimir_read.requires.mimir_cluster - } -} - -resource "juju_integration" "coordinator_to_write" { - model_uuid = var.model_uuid - - application { - name = module.mimir_coordinator.app_name - endpoint = module.mimir_coordinator.provides.mimir_cluster - } - - application { - name = module.mimir_write.app_name - endpoint = module.mimir_write.requires.mimir_cluster - } -} - -resource "juju_integration" "coordinator_to_backend" { +resource "juju_integration" "coordinator_to_worker" { + for_each = local.workers model_uuid = var.model_uuid application { @@ -154,7 +97,7 @@ resource "juju_integration" "coordinator_to_backend" { } application { - name = module.mimir_backend.app_name - endpoint = module.mimir_backend.requires.mimir_cluster + name = module.mimir_worker[each.key].app_name + endpoint = module.mimir_worker[each.key].requires.mimir_cluster } } diff --git a/terraform/outputs.tf b/terraform/outputs.tf index 5d769af..0985858 100644 --- a/terraform/outputs.tf +++ b/terraform/outputs.tf @@ -3,10 +3,8 @@ output "app_names" { { mimir_s3_integrator = juju_application.s3_integrator.name, mimir_coordinator = module.mimir_coordinator.app_name, - mimir_read = module.mimir_read.app_name, - mimir_write = module.mimir_write.app_name, - mimir_backend = module.mimir_backend.app_name, - } + }, + { for k, v in module.mimir_worker : "mimir_${k}" => v.app_name } ) description = "All application names which make up this product module" } diff --git a/terraform/variables.tf b/terraform/variables.tf index 4a161a3..f69f67e 100644 --- a/terraform/variables.tf +++ b/terraform/variables.tf @@ -49,24 +49,37 @@ variable "s3_endpoint" { type = string } -# -------------- # App Names -------------- +# -------------- # Workers -------------- + +variable "workers" { + description = "Map of worker roles to deploy. Keys must be one of: all, backend, read, write. When 'all' is used, a single worker with role-all is created." + type = map(object({ + units = optional(number, 1) + config = optional(map(string), {}) + constraints = optional(string, "arch=amd64") + storage_directives = optional(map(string), {}) + app_name = optional(string) + })) + default = { + backend = {} + read = {} + write = {} + } -variable "read_name" { - description = "Name of the Mimir read (meta role) app" - type = string - default = "mimir-read" -} + validation { + condition = alltrue([for k in keys(var.workers) : contains(["all", "backend", "read", "write"], k)]) + error_message = "Worker keys must be one of: all, backend, read, write." + } -variable "write_name" { - description = "Name of the Mimir write (meta role) app" - type = string - default = "mimir-write" -} + validation { + condition = !(contains(keys(var.workers), "all") && length(var.workers) > 1) + error_message = "When using role 'all', no other worker roles may be specified." + } -variable "backend_name" { - description = "Name of the Mimir backend (meta role) app" - type = string - default = "mimir-backend" + validation { + condition = alltrue([for k, v in var.workers : v.units >= 1]) + error_message = "The number of units for each worker must be greater than or equal to 1." + } } variable "s3_integrator_name" { @@ -83,24 +96,6 @@ variable "coordinator_config" { default = {} } -variable "backend_config" { - description = "Map of the backend worker configuration options" - type = map(string) - default = {} -} - -variable "read_config" { - description = "Map of the read worker configuration options" - type = map(string) - default = {} -} - -variable "write_config" { - description = "Map of the write worker configuration options" - type = map(string) - default = {} -} - variable "s3_integrator_config" { description = "Map of the s3-integrator configuration options" type = map(string) @@ -127,17 +122,6 @@ variable "coordinator_constraints" { } } -variable "worker_constraints" { - description = "String listing constraints for the worker application" - type = string - default = "arch=amd64" - - validation { - condition = !(var.anti_affinity && var.worker_constraints != "arch=amd64") - error_message = "Setting both custom charm constraints and anti-affinity to true is not allowed." - } -} - variable "s3_integrator_constraints" { description = "String listing constraints for the s3-integrator application" type = string @@ -191,24 +175,6 @@ variable "coordinator_storage_directives" { default = {} } -variable "backend_worker_storage_directives" { - description = "Map of storage used by the backend worker application, which defaults to 1 GB, allocated by Juju" - type = map(string) - default = {} -} - -variable "read_worker_storage_directives" { - description = "Map of storage used by the read worker application, which defaults to 1 GB, allocated by Juju" - type = map(string) - default = {} -} - -variable "write_worker_storage_directives" { - description = "Map of storage used by the write worker application, which defaults to 1 GB, allocated by Juju" - type = map(string) - default = {} -} - variable "s3_integrator_storage_directives" { description = "Map of storage used by the s3-integrator application, which defaults to 1 GB, allocated by Juju" type = map(string) @@ -217,36 +183,6 @@ variable "s3_integrator_storage_directives" { # -------------- # Units Per App -------------- -variable "read_units" { - description = "Number of Mimir worker units with the read meta role" - type = number - default = 1 - validation { - condition = var.read_units >= 1 - error_message = "The number of units must be greater than or equal to 1." - } -} - -variable "write_units" { - description = "Number of Mimir worker units with the write meta role" - type = number - default = 1 - validation { - condition = var.write_units >= 1 - error_message = "The number of units must be greater than or equal to 1." - } -} - -variable "backend_units" { - description = "Number of Mimir worker units with the backend meta role" - type = number - default = 1 - validation { - condition = var.backend_units >= 1 - error_message = "The number of units must be greater than or equal to 1." - } -} - variable "coordinator_units" { description = "Number of Mimir coordinator units" type = number