Skip to content

fix: allow six levels of management groups in an architecture - #257

Merged
Qi Zhang (Zhangqi910) merged 1 commit into
mainfrom
user/yingjie/fix-issue-4349-architecture-depth
Nov 27, 2026
Merged

Qi Zhang (Zhangqi910) merged 1 commit into
mainfrom
user/yingjie/fix-issue-4349-architecture-depth

Conversation

@Y1ngJ1eChen

Copy link
Copy Markdown
Contributor

Summary

An architecture with six levels of management groups fails to load with architectureRecursion: recursion depth exceeded, although Azure supports six levels below the tenant root group (Azure/Azure-Landing-Zones#4349). This PR fixes an off-by-one in architectureRecursion: an architecture can now contain six levels, and the error for a deeper one names the management group that is too deep.

Refs Azure/Azure-Landing-Zones#4349. Not "Fixes": Terraform users get the fix with the terraform-provider-alz release that upgrades to this version, and the issue will be closed then.

Root cause

architectureRecursion adds one level of management groups per call:

  1. It returns the error if depth > MaxRecursionDepth (5), before anything else.
  2. It adds the management groups whose parent the previous call added.
  3. If it added any, it calls itself with depth+1 to look for their children.

Step 3 always runs once more after the deepest level has been added. For six levels (depths 0-5), that call is at depth 6 and fails in step 1 before it finds that there are no children, so the effective limit was five levels.

Six levels was the intent:

  • The existing "management group hierarchy too deep" test uses seven levels (level0-level6).
  • avm-ptn-alz has resources up to management_groups_level_6; management_groups_level_5 could never be used.
  • Azure supports six levels below the tenant root group.

Not a regression: the check dates from #116 (v0.14.0); #210 only moved the 5 into MaxRecursionDepth.

Changes

File Change
alzlib.go architectureRecursion checks the depth where a management group would be added (once its parent is found in the previous level), instead of on entry. The error names the management group, its level and the limit. The comment of MaxRecursionDepth now says that it is the maximum zero-based depth, i.e. six levels; its value (5) is unchanged.
alzlib_test.go TestGenerateArchitecturesTbt: a new "management group hierarchy at maximum depth" case with six levels; the "too deep" case expects the new error; successful cases also check that every management group was added.

The error for seven levels, before and after:

architectureRecursion: recursion depth exceeded
architectureRecursion: management group level6 is at level 7, but an architecture can contain at most 6 levels of management groups

Behaviour notes

  • No API change; MaxRecursionDepth keeps its value.
  • Architectures with six levels now load. Nothing that loaded before changes (see the differential check below).
  • The recursion still ends by depth 6 at the latest: a call at depth 6 either finds a management group and returns the error, or finds none and does not recurse.
  • The error text for an architecture that is too deep changes, as shown above. terraform-provider-alz and alzlibtool pass it on without parsing it, and a GitHub code search finds the old text only in alzlib itself.
  • The level in the error counts from 1, like the Azure limit ("six levels"). Level() in deployment.Hierarchy and the level attribute of the provider's alz_architecture stay zero-based (0-5).

Validation

Check Result
go test ./... (incl. the read-only Azure integration tests), go vet ./... pass
golangci-lint v2.11.4 with Go 1.25.2 (--new-from-rev) 0 issues
TestGenerateArchitecturesTbt and TestInitSimple, -count=20 pass
The new test cases on main "maximum depth" fails with recursion depth exceeded; "too deep" fails on the message
A version of the fix that silently drops the deepest level still passes require.NoError, but fails the new "every management group was added" assertion: should have 6 item(s), but has 5
Separate verification suite against main (5461147) all 12 bug-detecting tests fail on main, all 9 guards pass on both; all 8 mutants of the fix are caught (limit one lower or higher, no limit, deepest level skipped silently, zero-based level or wrong limit in the message, MaxRecursionDepth = 6, the original entry check)
Differential: every architecture of 128 libraries loaded with main and with this branch, comparing management groups, parents, levels, archetypes and the deployment.Hierarchy up to five levels (69 generated libraries) and the real platform/alz, platform/amba and testdata/simple: byte-identical. Exactly six levels (16): fail on main; with this PR every management group is built, the deepest at level 5. Seven to nine levels (40): rejected by both, and the new error always names a management group at level 7
alzlibtool check library --offline six levels: fails on main, passes with this PR; seven levels: fails with the new error
terraform-provider-alz v0.22.1 built against this branch builds, unit tests pass, no code change needed

Live Azure, read-only (terraform plan only, nothing applied), avm-ptn-alz 0.22.0. "Before" is terraform-provider-alz v0.22.1 as released or built against main; "This PR" is v0.22.1 built against this branch.

Configuration Before This PR
The hierarchy from the issue: 25 management groups, six levels fails: Failed to initialize AlzLib ... recursion depth exceeded plans all 25 with the defined parents; the four deepest in management_groups_level_5, none in management_groups_level_6
Seven levels fails: recursion depth exceeded fails: management group ...-level7 is at level 7, but an architecture can contain at most 6 levels of management groups
The five-level variant from the issue (23 management groups) plans identical plan (29 resources)
The workaround: top management group created outside the architecture plans identical plan (31 resources)
alz_architecture for platform/alz@2026.11.0, platform/slz@2026.11.0, platform/amba@2026.10.0 12 / 15 / 6 management groups, 238 / 306 / 44 policy role assignments identical

Open question

Should an architecture whose root is the tenant root group itself (exists: true) be allowed seven levels? This PR keeps six: an architecture is usually placed below the tenant root group, where a seventh level always fails when it is applied, and rejecting it when the library loads is better than a partial apply. Allowing seven would only change the constant and the tests.

Follow-ups (not in this PR)

  • terraform-provider-alz: bump alzlib once this is released, with acceptance tests for six and seven levels.
  • Azure Landing Zones docs: how levels are counted (from parent_resource_id; management groups with exists: true count). alzlib does not know how deep the parent management group is, so an architecture below a non-root parent can still exceed the Azure limit when it is applied.
  • Management groups whose parents form a cycle, or that are their own parent, are never reached from a root and are dropped without an error. Unchanged by this PR; I'll open a separate issue.
  • deployment.Hierarchy.ManagementGroupsAtLevel returns every management group (return h.mgs instead of return res). Nothing in alzlib or terraform-provider-alz calls it; I'll open a separate issue.

@codecov-commenter

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 55.92%. Comparing base (5461147) to head (076c11b).

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #257      +/-   ##
==========================================
+ Coverage   55.87%   55.92%   +0.05%     
==========================================
  Files          56       56              
  Lines        5264     5270       +6     
==========================================
+ Hits         2941     2947       +6     
+ Misses       2003     2002       -1     
- Partials      320      321       +1     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@Zhangqi910
Qi Zhang (Zhangqi910) added this pull request to the merge queue Nov 27, 2026
Merged via the queue into main with commit ab33565 Nov 27, 2026
8 checks passed
@Zhangqi910
Qi Zhang (Zhangqi910) deleted the user/yingjie/fix-issue-4349-architecture-depth branch November 27, 2026 15:11

This branch was successfully deployed

1 active deployment
test — 076c11b6 Deployed Nov 26, 2026 by Y1ngJ1eChen via test (windows-latest) #701
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.

4 participants