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

Add component attribute to stack configs. Allow configuring and provisioning many instances of the same component. #15

Merged
merged 8 commits into from
Jan 15, 2021
4 changes: 4 additions & 0 deletions example/stacks/ue2-dev.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ components:
vpc:
vars:
cidr_block: "10.100.0.0/18"
vpc2:
component: vpc
vars:
cidr_block: "10.108.0.0/18"
eks:
vars:
test-map:
Expand Down
5 changes: 5 additions & 0 deletions example/stacks/ue2-prod.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ components:
command: "/usr/bin/terraform-0.13"
vars:
cidr_block: "10.102.0.0/18"
vpc2:
command: "/usr/bin/terraform-0.13"
component: vpc
vars:
cidr_block: "10.109.0.0/18"
eks:
vars: {}

Expand Down
4 changes: 4 additions & 0 deletions example/stacks/ue2-staging.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ components:
vpc:
vars:
cidr_block: "10.104.0.0/18"
vpc2:
component: vpc
vars:
cidr_block: "10.110.0.0/18"
eks:
vars: {}

Expand Down
1 change: 1 addition & 0 deletions modules/terraform/terraform-apply.variant
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ job "terraform apply" {
step "apply clean" {
run "terraform shell" {
component = param.component
stack = opt.stack

commands = [
"rm",
Expand Down
1 change: 1 addition & 0 deletions modules/terraform/terraform-clean.variant
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ job "terraform clean" {
step "clean shell" {
run "terraform shell" {
component = param.component
stack = opt.stack

commands = [
"rm -rf .terraform *.planfile"
Expand Down
92 changes: 86 additions & 6 deletions modules/terraform/terraform-core.variant
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,15 @@ job "terraform subcommand" {
}

option "args" {
type = list(string)
default = []
description = "args to pass to the subcommand"
type = list(string)
}

option "env" {
type = map(string)
default = {}
description = "Environment variables for the subcommand"
type = map(string)
}

option "interactive" {
Expand Down Expand Up @@ -67,10 +67,27 @@ job "terraform subcommand" {
value = compact(concat(list(opt.dry-run ? conf.terraform-command.command : ""), list(opt.subcommand), opt.args))
}

config "config-component" {
source job {
name = "stack config"
args = {
component-type = "terraform"
config-type = "component"
component = param.component
stack = opt.stack
format = "json"
}
}
}

variable "component" {
value = coalesce(conf.config-component.component, param.component)
}

exec {
command = var.exec-cmd
args = var.exec-args
dir = "${opt.terraform-dir}/${param.component}"
dir = "${opt.terraform-dir}/${var.component}"
env = opt.env
interactive = true # currently `variant` does not support interpolation in the `interactive` attribute, so `opt.interactive` does not work
}
Expand All @@ -85,11 +102,34 @@ job "terraform shell" {
description = "Component"
}

option "stack" {
type = string
description = "Stack"
short = "s"
}

option "commands" {
description = "List of commands to execute"
type = list(string)
}

config "config-component" {
source job {
name = "stack config"
args = {
component-type = "terraform"
config-type = "component"
component = param.component
stack = opt.stack
format = "json"
}
}
}

variable "component" {
value = coalesce(conf.config-component.component, param.component)
}

run "shell" {
command = "bash"

Expand All @@ -98,7 +138,7 @@ job "terraform shell" {
join(" ", opt.commands)
]

dir = "${opt.terraform-dir}/${param.component}"
dir = "${opt.terraform-dir}/${var.component}"
}
}

Expand Down Expand Up @@ -130,6 +170,23 @@ job "terraform write varfile" {
}
}

config "config-component" {
source job {
name = "stack config"
args = {
component-type = "terraform"
config-type = "component"
component = param.component
stack = opt.stack
format = "json"
}
}
}

variable "component" {
value = coalesce(conf.config-component.component, param.component)
}

exec {
command = "echo"

Expand All @@ -144,7 +201,7 @@ job "terraform write varfile" {
format = event.exec.args[0]
}

file = "${opt.terraform-dir}/${param.component}/${opt.stack}-${param.component}.terraform.tfvars.json"
file = "${opt.terraform-dir}/${var.component}/${opt.stack}-${param.component}.terraform.tfvars.json"
}
}

Expand Down Expand Up @@ -181,6 +238,12 @@ job "terraform write override" {
description = "Component"
}

option "stack" {
type = string
description = "Stack"
short = "s"
}

option "role" {
type = string
description = "Role to assume"
Expand All @@ -195,6 +258,23 @@ job "terraform write override" {
}
}

config "config-component" {
source job {
name = "stack config"
args = {
component-type = "terraform"
config-type = "component"
component = param.component
stack = opt.stack
format = "json"
}
}
}

variable "component" {
value = coalesce(conf.config-component.component, param.component)
}

exec {
command = "echo"
args = [
Expand All @@ -208,7 +288,7 @@ job "terraform write override" {
format = event.exec.args[0]
}

file = "${opt.terraform-dir}/${param.component}/override.tf.json"
file = "${opt.terraform-dir}/${var.component}/override.tf.json"
}
}

Expand Down
1 change: 1 addition & 0 deletions modules/terraform/terraform-destroy.variant
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ job "terraform destroy" {
step "destroy clean" {
run "terraform shell" {
component = param.component
stack = opt.stack

commands = [
"rm",
Expand Down
3 changes: 3 additions & 0 deletions modules/terraform/terraform-import.variant
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ job "terraform import" {
step "write provider override" {
run "terraform write override" {
component = param.component
stack = opt.stack
role = opt.role
}
}
Expand All @@ -96,6 +97,8 @@ job "terraform import" {
step "apply clean" {
run "terraform shell" {
component = param.component
stack = opt.stack

commands = ["rm", "override.tf.json"]
}
}
Expand Down
21 changes: 20 additions & 1 deletion modules/terraform/terraform-workspace.variant
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,29 @@ job "terraform workspace" {
}
}

config "config-component" {
source job {
name = "stack config"
args = {
component-type = "terraform"
config-type = "component"
component = param.component
stack = opt.stack
format = "json"
}
}
}

variable "workspace" {
value = conf.config-component.component != null ? format("%s-%s", opt.stack, param.component) : opt.stack
}

run "terraform shell" {
component = param.component
stack = opt.stack

commands = [
"${conf.terraform-command.command} workspace select ${opt.stack} || ${conf.terraform-command.command} workspace new ${opt.stack}"
"${conf.terraform-command.command} workspace select ${var.workspace} || ${conf.terraform-command.command} workspace new ${var.workspace}"
]
}
}
18 changes: 16 additions & 2 deletions modules/utils/stack-config.variant
Original file line number Diff line number Diff line change
Expand Up @@ -77,20 +77,33 @@ job "stack config" {
}
}

# Deep-merge all variables in this order: global-scoped, component-type-scoped, and component-scoped
variable "component" {
value = try(conf.all.components[opt.component-type][param.component]["component"], null)
}

# Deep-merge all variables in this order: global-scoped, component-type-scoped, component-scoped, component-instance-scoped
config "vars" {
source job {
name = "stack-config-echo"
args = {
opt1 = yamlencode(try(conf.all["vars"], {}))
}
}
# Component type vars override global vars
source job {
name = "stack-config-echo"
args = {
opt1 = yamlencode(try(conf.all[opt.component-type]["vars"], {}))
}
}
# Component vars override component type and global vars
source job {
name = "stack-config-echo"
args = {
opt1 = yamlencode(try(conf.all.components[opt.component-type][var.component]["vars"], {}))
}
}
# Component instance vars override component, component type and global vars
source job {
name = "stack-config-echo"
args = {
Expand Down Expand Up @@ -145,7 +158,8 @@ job "stack config" {
command = { command = var.command },
backend = { "${var.backend-type}" = conf.backend },
backend-type = var.backend-type,
all = conf.all
all = conf.all,
component = { component = var.component }
}
}

Expand Down