Removing one depot restarts three CHG-5188

Open2 versionsInfrastructure as code · Medium · Fix · about 30 min ·Linux + Docker

Lab machine

A private Linux machine with Docker Engine. Starting takes about 30 seconds. Sessions last up to 60 minutes.
Change review opened CHG-5188 at 10:20task

depot-2 closed on Friday. The change that removes it from the depot list would also replace the scanner APIs of depot-3 and depot-4, mid-shift.

The scanner APIs are created from a list of depots. How each one is keyed decides what happens when an entry leaves the list.

"Their scanners have live sessions. Restarting those APIs drops 400 handhelds mid-shift." (change reviewer)

The plan is honest about this. Read it before you apply anything.

Your task

Change the stack so instances are keyed by depot name instead of position, without replacing any existing container, then apply the decommission so only depot-2 goes away.

On the machine

  • infra/ (a Git repository and its local state)
  • bin/tofu -chdir=infra plan
  • docker ps shows the real scanner API containers

Timeline

Fridepot-2 closes.
09:50CHG-5188: remove depot-2 from infra/terraform.tfvars.
10:20Reviewer: "why does removing ONE depot touch depot-3 and depot-4?"

Done when

  1. depot-2's scanner API is gone.
  2. depot-1, depot-3, and depot-4 keep their running containers.
  3. Instances are keyed by depot name and the stack plans clean.

Hints

Hint 1

Compare each instance's key before and after the list change.

Hint 2

Replacement happens because the name at a key changes. A key that is a list position shifts, whether it is written as count or as for_each.

Hint 3

Key instances by a stable value: the depot name.

Hint 4

Key the instances by depot name and add moved blocks from the old keys for the depots you keep.

Show the solution

`bin/tofu -chdir=infra plan` replaces the scanner APIs of depot-3 and depot-4. Their instance keys are list positions: either `count`, or a `for_each` over a map keyed by index, which is the same thing written differently. Switch to `for_each = toset(var.depots)` and use `each.key` for the name and env. Add `moved` blocks from the old keys (`[0]`, `[2]`, `[3]`, or `["0"]`, `["2"]`, `["3"]`) to `["depot-1"]`, `["depot-3"]`, and `["depot-4"]`. The plan then destroys only depot-2. Apply it.