You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
fix: emit Networks and Addresses in deterministic order (#431, #169)
The per-container Networks and Addresses slices passed to templates were
built by ranging over Go maps (NetworkSettings.Networks, NetworkSettings.Ports
and Config.ExposedPorts), whose iteration order is random. This caused
generated files to churn on every regeneration and triggered spurious reloads
/ container restarts even when nothing meaningful changed (#431), and made
`index .Networks 0` pick an arbitrary network (#169).
- internal/context/address.go: sort addresses in GetContainerAddresses via a
new sortAddresses helper (Port numeric -> Proto -> HostPort -> HostIP -> IP).
- internal/generator/generator.go: sort runtimeContainer.Networks by Name via
a new sortNetworks helper, right after the network map is collected.
- Tests: add order-sensitive TestSortAddresses / TestGetContainerAddressesSorted
and TestSortNetworks. Existing order-insensitive address tests still pass.
- README.md: document the deterministic ordering, the behavior change to
`index .Networks 0`, and how to select a network by name with `where`.
#169 needs no further code change: a specific network is selectable via
`index (where $value.Networks "Name" $name) 0` once the order is stable.
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Copy file name to clipboardExpand all lines: README.md
+14Lines changed: 14 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -394,6 +394,20 @@ For example, this is a JSON version of an emitted RuntimeContainer struct:
394
394
}
395
395
```
396
396
397
+
#### Ordering of `Addresses` and `Networks`
398
+
399
+
The `Addresses` and `Networks` slices are emitted in a deterministic order, so generated output is stable across regenerations and does not trigger spurious reloads/restarts when nothing meaningful changed:
400
+
401
+
-`Addresses` are sorted by port (compared numerically), then by protocol, host port, host IP and IP.
402
+
-`Networks` are sorted alphabetically by `Name`.
403
+
404
+
As a consequence, `index .Networks 0` returns the alphabetically-first network (for example `bridge`) rather than an arbitrary one. To select a specific network by name, filter with `where`:
405
+
406
+
```
407
+
{{ $net := index (where $value.Networks "Name" "my-network") 0 }}
408
+
server {{ $net.IP }}:{{ (index $value.Addresses 0).Port }};
409
+
```
410
+
397
411
#### Functions
398
412
399
413
-[Functions from Go](https://pkg.go.dev/text/template#hdr-Functions)
0 commit comments