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

feat: adds the runtime_overrides variable + tests #44

Open
wants to merge 13 commits into
base: main
Choose a base branch
from

Conversation

Gowiem
Copy link
Member

@Gowiem Gowiem commented Jan 23, 2025

what

  • Adds the runtime_overrides variable + tests to check usage
    • NOTE: The new overrides test is currently failing due to what I'm calling the "Optional Type Defaults Problem". I'm posting this to share the full use-case for what we're running into so I can see if others have an elegant way to handle this problem. Below is the output of the failing tests just so it's easy to find. Check out the OpenTofu Slack thread linked below for full details.
    • (Previously) Failing Test Output
  • Introduces StackConfig schema validation using the https://registry.terraform.io/providers/bpedman/jsonschema/latest/docs
  • Adds terraform-docs as a trunk action + config for that + removes pre-commit-config

why

  • runtime_overrides allows consumers of this child module to do logic within their root module to manage the final Stack config. This gives them additional flexibility beyond our static StackConfig configuration.

references

@Gowiem Gowiem self-assigned this Jan 23, 2025
@Gowiem Gowiem requested a review from a team as a code owner January 23, 2025 19:10
@Gowiem Gowiem force-pushed the feature/runtime-overrides branch from 4babc73 to 96d325e Compare January 28, 2025 06:19
Copy link
Member

@gberenice gberenice left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM! The only thing that confuses me - this PR contains the changes introduced in the parallel PR #42. Is that intended?

examples/complete/root-modules/network/stacks/dev.yaml Outdated Show resolved Hide resolved
versions.tf Show resolved Hide resolved
@Gowiem
Copy link
Member Author

Gowiem commented Jan 29, 2025

@gberenice mind giving me another review? I added a new test to confirm the schema is working as expected 👍

gberenice
gberenice previously approved these changes Jan 29, 2025
Copy link
Member

@gberenice gberenice left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great, I like it! Thanks, Matt 👏

@Gowiem Gowiem force-pushed the feature/runtime-overrides branch from 3aadae6 to f18df2b Compare January 30, 2025 18:55
@Gowiem Gowiem enabled auto-merge (squash) January 30, 2025 19:08
Copy link
Member

@westonplatter westonplatter left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will come back and look deeper after a client meeting

}

# Test the default-example stack with runtime overrides
run "test_default_example_stack_runtime_overrides" {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After watching your rubber 🦆 loom and reading through the open tofu slack thread, I'm wondering if we have a test in this PR for this logic? I didn't see it after a first pass, but I may be overlooking test logic for that condition.

# variables.tf
variable "runtime_overrides" {
  type = object({
    administrative = optional(bool)
    branch = optional_rJemove(string)
  })

  default = {}
}

# terraform.tfvars
runtime_overrides = {
  administrative = false
}

# At runtime
var.runtime_overrides => { administrative = false }

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@westonplatter This test that you commented on provides just that:

  1. We provide a value to runtime_overrides
  2. We confirm the values in the underlying resource match the overrides for the stack that we're trying to override.

Specifically for administrative: https://github.com/masterpointio/terraform-spacelift-automation/pull/44/files#diff-42b25f743b9dffd1816864939465621064627de79a1654c58e8f124ca64d66d9R267-R270

Were you looking for something else?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, the first test confirms the administrative value, that looks good. I was thinking of a test for how branch is handled - namely, when does it show up in the run_overrides map vs when is it omitted because it is an optional override.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member

@westonplatter westonplatter Jan 30, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That loom video helps a lot - I took from it that the type(any) means we are passing in anything, and the mega merge operation drives the final (defaults + any runtime overrides) outcome. There is no expected intermediate data structure with nulls.

The only remaining question I have (and this comes from my rails/web app developer days) is testing that an override doesn't suddenly alter or negatively impact the expected default values.

So in this example, ensure the default value for administrative comes through unaltered by the non-default space_id value.

# Test that the description is created correctly when non-default template string is used
run "test_description_is_created_correctly_when_non_default_template_string_is_used" {
  command = plan
  variables {
    description = "Space ID: $${stack_settings.space_id}"
  }

  assert {
    condition = spacelift_stack.default["root-module-a-test"].description == "Space ID: 123"
    error_message = "Description was not created correctly: ${jsonencode(local.configs)}"
  }

  # new test - ensure the default `administrative` value comes
  assert {
    condition = spacelift_stack.default["root-module-a-test"].administrative == true
    error_message = "Administrative was not created correctly: ${jsonencode(local.configs)}"
  }
}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, yeah, the suggested code/test above does not match what I was describing. Ok, after reading the code more, this is that I was trying to get at, #48.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The changes I suggested cause a failing. Looks like I don't understand enough of what's going on. Will take another look and follow up tomorrow.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@westonplatter your PR doesn't look wrong and I get the test that you're trying to make now. See comment in your PR.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for catching the misnamed variable. I updated it and move the code around a bit, but it's ready to to add to this branch if you're open to testing for that logic situation.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@westonplatter glad we got that in! Re-review when you can -- Thanks!

@Gowiem Gowiem force-pushed the feature/runtime-overrides branch from 5cae82e to 32b7454 Compare January 30, 2025 21:46
@westonplatter westonplatter mentioned this pull request Jan 31, 2025
@Gowiem Gowiem requested a review from westonplatter January 31, 2025 19:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants