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

Update spacelift component. Add settings.spacelift.space_name_pattern attribute #980

Merged
merged 4 commits into from
Feb 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ Please use the [issue tracker](https://github.com/cloudposse/terraform-aws-compo

### 💻 Developing

If you are interested in being a contributor and want to get involved in developing this project or help out with Cloud Posse's other projects, we would love to hear from you!
If you are interested in being a contributor and want to get involved in developing this project or help out with Cloud Posse's other projects, we would love to hear from you!
Hit us up in [Slack](https://cpco.io/slack?utm_source=github&utm_medium=readme&utm_campaign=cloudposse/terraform-aws-components&utm_content=slack), in the `#cloudposse` channel.

In general, PRs are welcome. We follow the typical "fork-and-pull" Git workflow.
Expand Down
2 changes: 1 addition & 1 deletion modules/spacelift/admin-stack/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 modules/spacelift/admin-stack/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 modules/spacelift/admin-stack/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 modules/spacelift/admin-stack/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
2 changes: 1 addition & 1 deletion modules/spacelift/spaces/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ No providers.

| Name | Source | Version |
|------|--------|---------|
| <a name="module_policy"></a> [policy](#module\_policy) | cloudposse/cloud-infrastructure-automation/spacelift//modules/spacelift-policy | 1.4.0 |
| <a name="module_policy"></a> [policy](#module\_policy) | cloudposse/cloud-infrastructure-automation/spacelift//modules/spacelift-policy | 1.6.0 |
| <a name="module_space"></a> [space](#module\_space) | cloudposse/cloud-infrastructure-automation/spacelift//modules/spacelift-space | 1.6.0 |
| <a name="module_this"></a> [this](#module\_this) | cloudposse/label/null | 0.25.0 |

Expand Down
2 changes: 1 addition & 1 deletion modules/spacelift/spaces/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ module "space" {

module "policy" {
source = "cloudposse/cloud-infrastructure-automation/spacelift//modules/spacelift-policy"
version = "1.4.0"
version = "1.6.0"

for_each = local.all_policies_inputs

Expand Down
Loading