Skip to content

Commit

Permalink
Extend configuration inheritance implementation
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
pst committed Aug 24, 2020
1 parent d785eee commit e18c6b7
Show file tree
Hide file tree
Showing 11 changed files with 84 additions and 28 deletions.
15 changes: 8 additions & 7 deletions aws/cluster/configuration.tf
Original file line number Diff line number Diff line change
@@ -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"]

Expand Down
6 changes: 6 additions & 0 deletions aws/cluster/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
15 changes: 8 additions & 7 deletions azurerm/cluster/configuration.tf
Original file line number Diff line number Diff line change
@@ -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"]

Expand Down
6 changes: 6 additions & 0 deletions azurerm/cluster/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
15 changes: 15 additions & 0 deletions common/configuration/outputs.tf
Original file line number Diff line number Diff line change
@@ -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)
}
9 changes: 9 additions & 0 deletions common/configuration/variables.tf
Original file line number Diff line number Diff line change
@@ -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."
}
4 changes: 4 additions & 0 deletions common/configuration/versions.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@

terraform {
required_version = ">= 0.12"
}
15 changes: 8 additions & 7 deletions google/cluster/configuration.tf
Original file line number Diff line number Diff line change
@@ -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"]

Expand Down
6 changes: 6 additions & 0 deletions google/cluster/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
15 changes: 8 additions & 7 deletions kind/cluster/configuration.tf
Original file line number Diff line number Diff line change
@@ -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"]

Expand Down
6 changes: 6 additions & 0 deletions kind/cluster/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}

0 comments on commit e18c6b7

Please sign in to comment.