From e18c6b72055ed16d110f8f44f3ac4d398012b21e Mon Sep 17 00:00:00 2001 From: Philipp Strube Date: Mon, 24 Aug 2020 16:04:02 +0200 Subject: [PATCH] Extend configuration inheritance implementation This commit moves the implementation of the configuration inheritance into a shared module and extends it to support two commonly requested use-cases. 1. Support for custom names instead of apps and ops, fix #126 2. Support for more than two infrastructure environments The module expects two input variables, `configuration` and `base_key`. `configuration` is a map of maps, and `base_key` sets the key in the configuration map that all other keys will inherit from. So, with the frameworks default apps and ops inheritance and configuration, it has the exact same behavior as before. Ops inherits from apps and overwrites values. But given a Terraform configuration with different and more keys: ``` locals { cfg = { "a" = { "a" = "a" "b" = null } "b" = { "b" = "b" } "c" = { "c" = "c" } } } module "configuration" { source = "../common/configuration" configuration = local.cfg base_key = "a" } output "merged_cfg" { value = module.configuration.merged } ``` The merged_cfg output will be: ``` merged_cfg = { "a" = { "a" = "a" } "b" = { "a" = "a" "b" = "b" } "c" = { "a" = "a" "c" = "c" } } ``` Until futher notice the framework documents and tests only the apps and ops case. So, here be dragons, you've been warned. But users can now adjust environment names and number to their liking. Provided they also align the the tagging strategy and triggers in the pipeline accordingly. --- aws/cluster/configuration.tf | 15 ++++++++------- aws/cluster/variables.tf | 6 ++++++ azurerm/cluster/configuration.tf | 15 ++++++++------- azurerm/cluster/variables.tf | 6 ++++++ common/configuration/outputs.tf | 15 +++++++++++++++ common/configuration/variables.tf | 9 +++++++++ common/configuration/versions.tf | 4 ++++ google/cluster/configuration.tf | 15 ++++++++------- google/cluster/variables.tf | 6 ++++++ kind/cluster/configuration.tf | 15 ++++++++------- kind/cluster/variables.tf | 6 ++++++ 11 files changed, 84 insertions(+), 28 deletions(-) create mode 100644 common/configuration/outputs.tf create mode 100644 common/configuration/variables.tf create mode 100644 common/configuration/versions.tf diff --git a/aws/cluster/configuration.tf b/aws/cluster/configuration.tf index 77dc4990..cfc65192 100644 --- a/aws/cluster/configuration.tf +++ b/aws/cluster/configuration.tf @@ -1,12 +1,13 @@ -locals { - # apps config and merged ops config - workspaces = { - apps = var.configuration["apps"] - ops = merge(var.configuration["apps"], var.configuration["ops"]) - } +module "configuration" { + source = "../../common/configuration" + + configuration = var.configuration + base_key = var.base_key +} +locals { # current workspace config - cfg = local.workspaces[terraform.workspace] + cfg = module.configuration.merged[terraform.workspace] name_prefix = local.cfg["name_prefix"] diff --git a/aws/cluster/variables.tf b/aws/cluster/variables.tf index 89d2c16d..907dbd4f 100644 --- a/aws/cluster/variables.tf +++ b/aws/cluster/variables.tf @@ -8,3 +8,9 @@ variable "manifest_path" { description = "Path to Kustomize overlay to build." default = null } + +variable "base_key" { + type = string + description = "The key in the configuration map all other keys inherit from." + default = "apps" +} diff --git a/azurerm/cluster/configuration.tf b/azurerm/cluster/configuration.tf index 408613e3..eb86b5d6 100644 --- a/azurerm/cluster/configuration.tf +++ b/azurerm/cluster/configuration.tf @@ -1,12 +1,13 @@ -locals { - # apps config and merged ops config - workspaces = { - apps = var.configuration["apps"] - ops = merge(var.configuration["apps"], var.configuration["ops"]) - } +module "configuration" { + source = "../../common/configuration" + + configuration = var.configuration + base_key = var.base_key +} +locals { # current workspace config - cfg = local.workspaces[terraform.workspace] + cfg = module.configuration.merged[terraform.workspace] name_prefix = local.cfg["name_prefix"] diff --git a/azurerm/cluster/variables.tf b/azurerm/cluster/variables.tf index 89d2c16d..907dbd4f 100644 --- a/azurerm/cluster/variables.tf +++ b/azurerm/cluster/variables.tf @@ -8,3 +8,9 @@ variable "manifest_path" { description = "Path to Kustomize overlay to build." default = null } + +variable "base_key" { + type = string + description = "The key in the configuration map all other keys inherit from." + default = "apps" +} diff --git a/common/configuration/outputs.tf b/common/configuration/outputs.tf new file mode 100644 index 00000000..81761d83 --- /dev/null +++ b/common/configuration/outputs.tf @@ -0,0 +1,15 @@ +locals { + base = { + (var.base_key) = var.configuration[var.base_key] + } + + overlays = { + for name, _ in var.configuration : + name => merge(var.configuration[var.base_key], var.configuration[name]) + if name != var.base_key + } +} + +output "merged" { + value = merge(local.base, local.overlays) +} diff --git a/common/configuration/variables.tf b/common/configuration/variables.tf new file mode 100644 index 00000000..3dfd4c83 --- /dev/null +++ b/common/configuration/variables.tf @@ -0,0 +1,9 @@ +variable "configuration" { + type = map(map(string)) + description = "Map with per workspace cluster configuration." +} + +variable "base_key" { + type = string + description = "The key in the configuration map all other keys inherit from." +} diff --git a/common/configuration/versions.tf b/common/configuration/versions.tf new file mode 100644 index 00000000..ac97c6ac --- /dev/null +++ b/common/configuration/versions.tf @@ -0,0 +1,4 @@ + +terraform { + required_version = ">= 0.12" +} diff --git a/google/cluster/configuration.tf b/google/cluster/configuration.tf index c62f98c1..aeb414fc 100644 --- a/google/cluster/configuration.tf +++ b/google/cluster/configuration.tf @@ -1,12 +1,13 @@ -locals { - # apps config and merged ops config - workspaces = { - apps = var.configuration["apps"] - ops = merge(var.configuration["apps"], var.configuration["ops"]) - } +module "configuration" { + source = "../../common/configuration" + + configuration = var.configuration + base_key = var.base_key +} +locals { # current workspace config - cfg = local.workspaces[terraform.workspace] + cfg = module.configuration.merged[terraform.workspace] name_prefix = local.cfg["name_prefix"] diff --git a/google/cluster/variables.tf b/google/cluster/variables.tf index 89d2c16d..907dbd4f 100644 --- a/google/cluster/variables.tf +++ b/google/cluster/variables.tf @@ -8,3 +8,9 @@ variable "manifest_path" { description = "Path to Kustomize overlay to build." default = null } + +variable "base_key" { + type = string + description = "The key in the configuration map all other keys inherit from." + default = "apps" +} diff --git a/kind/cluster/configuration.tf b/kind/cluster/configuration.tf index 733562b4..aea7f9bb 100644 --- a/kind/cluster/configuration.tf +++ b/kind/cluster/configuration.tf @@ -1,12 +1,13 @@ -locals { - # apps config and merged ops config - workspaces = { - apps = var.configuration["apps"] - ops = merge(var.configuration["apps"], var.configuration["ops"]) - } +module "configuration" { + source = "../../common/configuration" + + configuration = var.configuration + base_key = var.base_key +} +locals { # current workspace config - cfg = local.workspaces[terraform.workspace] + cfg = module.configuration.merged[terraform.workspace] name_prefix = local.cfg["name_prefix"] diff --git a/kind/cluster/variables.tf b/kind/cluster/variables.tf index 89d2c16d..907dbd4f 100644 --- a/kind/cluster/variables.tf +++ b/kind/cluster/variables.tf @@ -8,3 +8,9 @@ variable "manifest_path" { description = "Path to Kustomize overlay to build." default = null } + +variable "base_key" { + type = string + description = "The key in the configuration map all other keys inherit from." + default = "apps" +}