Skip to content

Commit

Permalink
Update spacelift component. Add `settings.spacelift.space_name_patt…
Browse files Browse the repository at this point in the history
  • Loading branch information
aknysh authored Feb 12, 2024
1 parent df3e6ca commit 5c70585
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ components:
| <a name="input_root_stack_policy_attachments"></a> [root\_stack\_policy\_attachments](#input\_root\_stack\_policy\_attachments) | List of policy attachments to attach to the root admin stack | `set(string)` | `[]` | no |
| <a name="input_runner_image"></a> [runner\_image](#input\_runner\_image) | The full image name and tag of the Docker image to use in Spacelift | `string` | `null` | no |
| <a name="input_showcase"></a> [showcase](#input\_showcase) | Showcase settings | `map(any)` | `null` | no |
| <a name="input_space_id"></a> [space\_id](#input\_space\_id) | Place the stack in the specified space\_id. | `string` | `"root"` | no |
| <a name="input_space_id"></a> [space\_id](#input\_space\_id) | Place the stack in the specified space\_id | `string` | `"root"` | no |
| <a name="input_spacelift_run_enabled"></a> [spacelift\_run\_enabled](#input\_spacelift\_run\_enabled) | Enable/disable creation of the `spacelift_run` resource | `bool` | `false` | no |
| <a name="input_spacelift_spaces_component_name"></a> [spacelift\_spaces\_component\_name](#input\_spacelift\_spaces\_component\_name) | The component name of the spacelift spaces component | `string` | `"spacelift/spaces"` | no |
| <a name="input_spacelift_spaces_environment_name"></a> [spacelift\_spaces\_environment\_name](#input\_spacelift\_spaces\_environment\_name) | The environment name of the spacelift spaces component | `string` | `null` | no |
Expand Down
28 changes: 27 additions & 1 deletion src/child-stacks.tf
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,6 @@ module "child_stack" {
protect_from_deletion = try(each.value.settings.spacelift.protect_from_deletion, var.protect_from_deletion)
repository = var.repository
runner_image = try(each.value.settings.spacelift.runner_image, var.runner_image)
space_id = local.spaces[try(each.value.settings.spacelift.space_name, var.space_id)]
spacelift_run_enabled = try(each.value.settings.spacelift.spacelift_run_enabled, var.spacelift_run_enabled)
spacelift_stack_dependency_enabled = try(each.value.settings.spacelift.spacelift_stack_dependency_enabled, var.spacelift_stack_dependency_enabled)
stack_destructor_enabled = try(each.value.settings.spacelift.stack_destructor_enabled, var.stack_destructor_enabled)
Expand All @@ -130,6 +129,33 @@ module "child_stack" {
pulumi = try(each.value.settings.spacelift.pulumi, var.pulumi)
showcase = try(each.value.settings.spacelift.showcase, var.showcase)

# Process `spacelift.space_name` and `spacelift.space_name_pattern`
space_id = local.spaces[
try(
coalesce(
# if `space_name` is specified, use it
each.value.settings.spacelift.space_name,
# otherwise, try to replace the context tokens in `space_name_template` and use it
# `space_name_template` accepts the following context tokens: {namespace}, {tenant}, {environment}, {stage}
each.value.settings.spacelift.space_name_pattern != "" && each.value.settings.spacelift.space_name_pattern != null ? (
replace(
replace(
replace(
replace(
each.value.settings.spacelift.space_name_pattern,
"{namespace}", module.this.namespace
),
"{tenant}", module.this.tenant
),
"{environment}", module.this.environment
),
"{stage}", module.this.stage)
) : ""
),
var.space_id
)
]

depends_on = [
null_resource.spaces_precondition,
null_resource.workers_precondition,
Expand Down
26 changes: 24 additions & 2 deletions src/spaces.tf
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,29 @@ locals {
# spacelift.settings metadata. It then creates a set of all of the unique space_names so we can use that to look up
# their IDs from remote state.
unique_spaces_from_config = toset([for k, v in {
for k, v in module.child_stacks_config.spacelift_stacks : k => try(v.settings.spacelift.space_name, "root")
for k, v in module.child_stacks_config.spacelift_stacks : k => try(
coalesce(
# if `space_name` is specified, use it
v.settings.spacelift.space_name,
# otherwise, try to replace the context tokens in `space_name_template` and use it
# `space_name_template` accepts the following context tokens: {namespace}, {tenant}, {environment}, {stage}
v.settings.spacelift.space_name_pattern != "" && v.settings.spacelift.space_name_pattern != null ? (
replace(
replace(
replace(
replace(
v.settings.spacelift.space_name_pattern,
"{namespace}", module.this.namespace
),
"{tenant}", module.this.tenant
),
"{environment}", module.this.environment
),
"{stage}", module.this.stage)
) : ""
),
"root"
)
if try(v.settings.spacelift.workspace_enabled, false) == true
} : v if v != "root"])

Expand All @@ -18,7 +40,7 @@ locals {
missing_spaces = setunion(setsubtract(local.unique_spaces_from_config, keys(local.spaces)))
}

# Ensure all of the spaces referenced in the atmos config exist in Spacelift
# Ensure all of the spaces referenced in the Atmos config exist in Spacelift
resource "null_resource" "spaces_precondition" {
count = local.enabled ? 1 : 0

Expand Down
2 changes: 1 addition & 1 deletion src/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ variable "showcase" {

variable "space_id" {
type = string
description = "Place the stack in the specified space_id."
description = "Place the stack in the specified space_id"
default = "root"
}

Expand Down

0 comments on commit 5c70585

Please sign in to comment.