greentic-deployer-dev 1.1.28419928588

Greentic deployer runtime for plan construction and deployment-pack dispatch
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
//! Deterministic Kubernetes manifest rendering for the K8s deployer
//! env-pack (Phase D plan §6 steps 4/5/10/11).
//!
//! Pure functions: structured input ([`Environment`] + [`K8sParams`]) →
//! `serde_json::Value` manifests. No I/O, no clock, no cluster — the same
//! AWS-bootstrap precedent (`render_min_iam_rules_pack`) applied to the
//! K8s desired state. `gtc op env render` (follow-up PR) writes these to
//! disk; the [`Deployer`](super::deployer) verbs hand them to the
//! [`K8sCluster`](super::cluster::K8sCluster) seam.
//!
//! ## Runtime shape (plan §6 step 4/5, Zain v1)
//!
//! - One stable, HA **router** Deployment (≥2 replicas, PDB,
//!   topology-spread) receives all ingress for the env; the Gateway /
//!   Ingress (Zain's choice, Q4) sends 100% of matching traffic to the
//!   stable router Service. Provider-native revision weighting is
//!   deferred to Phase E.
//! - One **worker** Deployment + ClusterIP Service per revision, labeled
//!   `greentic.ai/revision: <ULID>`. The router resolves the deployment,
//!   applies the authoritative `TrafficSplit` from the runtime-config
//!   projection, and dispatches to the selected revision's Service.
//! - The `TrafficSplit` projection rides a ConfigMap
//!   ([`render_runtime_config_map`]) embedding the exact
//!   [`materialize_runtime_config`] output — `apply_traffic_split` is a
//!   ConfigMap upsert, never a `kubectl rollout`.
//!
//! ## Hardening (plan §6 step 11)
//!
//! Every pod spec rendered here passes the Restricted Pod Security
//! profile: non-root (`65532`), no privilege escalation, read-only root
//! filesystem, all capabilities dropped, `RuntimeDefault` seccomp,
//! resource requests/limits (S2 sandbox baseline: `250m/256Mi` requests,
//! `1/1Gi` limits). NetworkPolicies are default-deny with an explicit
//! allow-list (S3). Image digest-pinning is the operator's input
//! ([`K8sParams::runtime_image`]) — the sandbox default is a tag ref;
//! production acceptance requires a digest-pinned ref (the wizard
//! records it; the ship gate verifies it).
//!
//! ## Determinism
//!
//! All objects are built with fixed key order and serialize identically
//! for identical inputs (`serde_json`'s default map is ordered); a unit
//! test pins render-twice equality. Names are RFC 1123 labels: revision
//! ULIDs are lowercased for object NAMES (`gtc-worker-<ulid>`) but kept
//! uppercase in label VALUES (`greentic.ai/revision: <ULID>`) to match
//! the spec's canonical ULID rendering.

use greentic_deploy_spec::{CapabilitySlot, EnvId, Environment, Revision, RevisionLifecycle};
use serde_json::{Value, json};
use sha2::{Digest, Sha256};

use crate::environment::runtime_config::materialize_runtime_config;

/// Sandbox-default runtime image (S1). Tag-pinned for the sandbox only —
/// production requires a digest-pinned ref supplied via the env-pack
/// wizard (`runtime_image`).
///
/// Develop-lane value: `:develop` is the next-dev image (published by
/// greentic-start's `distroless-dev.yml`) that ships the `start --env` serve
/// boot, so sandbox pods actually serve `/healthz`. The stable `main` lane
/// uses `:latest`; keep these in sync when forward-porting — `:develop` must
/// not land on `main`.
pub const DEFAULT_RUNTIME_IMAGE: &str = "ghcr.io/greenticai/greentic-start-distroless:develop";

/// Stable name of the router Deployment / Service / PDB.
pub const ROUTER_NAME: &str = "gtc-router";

/// Name of the runtime-config ConfigMap the router consumes.
pub const RUNTIME_CONFIG_MAP_NAME: &str = "gtc-runtime-config";

/// Port every Greentic pod serves on; Services expose the same port.
const SERVE_PORT: u16 = 8080;

/// Writable in-pod path mounted as `$HOME`. `greentic-start`'s bundle-less
/// boot roots its env store at `$HOME/.greentic/environments/<env_id>/`, so
/// the init container stages `environment.json` there and every runtime write
/// (logs, `.lock`, watcher state) lands on this volume — required because the
/// container runs with `readOnlyRootFilesystem`.
const STAGE_HOME: &str = "/var/greentic";

/// Read-only mount of the env-store ConfigMap the init container copies from.
const ENV_STORE_SRC: &str = "/etc/greentic/env-store";

/// Pod volume names: the writable HOME, a writable `/tmp`, and the read-only
/// env-store source.
const HOME_VOLUME: &str = "greentic-home";
const TMP_VOLUME: &str = "tmp";
const ENV_STORE_SRC_VOLUME: &str = "env-store-src";

/// Name of the ConfigMap carrying the serialized `environment.json` the init
/// container stages into the env store.
pub const ENV_STORE_CONFIG_MAP_NAME: &str = "gtc-env-store";

/// Minimal init image (ships `sh`/`cp`/`mkdir`) used to stage the env store
/// into the writable HOME volume — the runtime image is distroless (no shell).
/// M1 scaffold: M2 replaces this with the distributor-pull init container that
/// also stages the runtime-config and the revision's packs.
const STAGE_INIT_IMAGE: &str = "busybox:1.36.1";

/// Name of the Secret carrying the env's local dev-store (the operator's
/// `.dev.secrets.env`). Rendered only for envs that bind a secrets pack; the
/// worker's `stage-dev-secrets` init container copies it into the writable HOME
/// so `secret://` refs (messaging bot tokens, webhook secrets) resolve in-pod —
/// closing the K8s "no runtime secrets" gap without a cloud secret-store.
pub const DEV_SECRETS_SECRET_NAME: &str = "gtc-dev-secrets";

/// Read-only mount of the dev-store Secret the staging init container copies from.
const DEV_SECRETS_SRC: &str = "/etc/greentic/dev-secrets";

/// Pod volume name for the read-only dev-store Secret source.
const DEV_SECRETS_VOLUME: &str = "dev-secrets-src";

/// Absolute path to the cloudflared binary baked into the runtime image. The
/// worker boots with `--cloudflared on --cloudflared-binary <this>` when
/// [`TunnelMode::Cloudflared`] is selected; greentic-start spawns it via direct
/// exec (no shell, distroless-safe).
const CLOUDFLARED_BINARY: &str = "/usr/local/bin/cloudflared";

/// Operator-tunable knobs for the rendered manifests.
///
/// [`K8sParams::for_env`] derives the sandbox defaults from the env
/// alone. [`K8sParams::from_answers`] overlays the binding's recorded
/// wizard answers (namespace, runtime image, router replicas) on top of
/// those defaults — `op env render` calls this path. The Deployer verbs
/// still use `for_env` until the PR-5.3 orchestration wiring threads
/// answers into them.
/// How the worker is exposed publicly (deployer answer `tunnel`). A public
/// URL is what lets greentic-start auto-register messaging webhooks at boot.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum TunnelMode {
    /// No tunnel — the worker is reachable only in-cluster (default). Messaging
    /// webhooks won't register (no public URL).
    Off,
    /// greentic-start spawns a cloudflared quick tunnel for a public
    /// `*.trycloudflare.com` URL. Single-revision only — each worker pod gets
    /// its own tunnel, so a traffic split would register N competing webhooks.
    Cloudflared,
}

/// Name of the worker pod's ServiceAccount when the env resolves secrets under
/// a workload identity ([`SecretsBackend::Vault`]). The pod's projected token
/// for this SA is what Vault's Kubernetes auth exchanges for a Vault token; the
/// SA→Vault-role binding is provisioned out-of-band by the Vault bootstrap
/// (Phase E.4). One per namespace, env-level.
pub const WORKER_SERVICE_ACCOUNT: &str = "gtc-worker";

// Vault provider defaults (mirror `greentic-secrets-provider-vault-kv`). The
// worker pod emits a `VAULT_*` var only when its value differs from the
// provider's default, keeping the rendered pod spec lean — an absent var and
// the default resolve identically at runtime.
pub(crate) const VAULT_DEFAULT_KV_MOUNT: &str = "secret";
pub(crate) const VAULT_DEFAULT_KV_PREFIX: &str = "greentic";
pub(crate) const VAULT_DEFAULT_AUTH_MOUNT: &str = "kubernetes";
pub(crate) const VAULT_DEFAULT_TRANSIT_MOUNT: &str = "transit";
pub(crate) const VAULT_DEFAULT_TRANSIT_KEY: &str = "greentic";

/// Which backend the worker resolves `secret://` references against at runtime,
/// and the non-secret connection config the deployer renders into the pod so
/// `greentic-start` can construct it. Selected from the env's bound
/// `Secrets`-slot pack descriptor (resolved at the render/reconcile call site).
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum SecretsBackend {
    /// The operator's local dev-store, delivered into the cluster as the
    /// [`DEV_SECRETS_SECRET_NAME`] Secret and staged into the worker's HOME.
    /// Ships secret *values* into the cluster — the default for `local` /
    /// sandbox envs.
    DevStore,
    /// HashiCorp Vault resolved under the pod's ServiceAccount identity
    /// (Kubernetes auth). No secret values enter the cluster — only the pod
    /// identity ([`WORKER_SERVICE_ACCOUNT`]) plus non-secret connection config
    /// rendered as `VAULT_*` pod env (Phase E).
    Vault(VaultBackend),
}

/// Non-secret HashiCorp Vault connection config the deployer renders into the
/// worker pod's environment. The provider reads these `VAULT_*` vars at boot;
/// the pod's identity (and thus the Vault token) comes from its ServiceAccount,
/// never from rendered material. Mirrors the env contract of
/// `greentic-secrets-provider-vault-kv::VaultProviderConfig::from_env`.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct VaultBackend {
    /// `VAULT_ADDR` — the Vault API address (e.g. `http://vault.vault.svc:8200`).
    pub addr: String,
    /// `VAULT_K8S_ROLE` — the Vault Kubernetes-auth role the pod's SA token is
    /// exchanged for. The role's policy governs which paths resolve.
    pub k8s_role: String,
    /// `VAULT_KV_MOUNT` — the KV v2 mount (provider default `secret`).
    pub kv_mount: String,
    /// `VAULT_KV_PREFIX` — path prefix under the mount (provider default
    /// `greentic`).
    pub kv_prefix: String,
    /// `VAULT_K8S_MOUNT` — the Kubernetes auth mount (provider default
    /// `kubernetes`).
    pub auth_mount: String,
    /// `VAULT_TRANSIT_MOUNT` — the transit mount backing envelope decryption
    /// (provider default `transit`).
    pub transit_mount: String,
    /// `VAULT_TRANSIT_KEY` — the transit key name (provider default `greentic`).
    pub transit_key: String,
    /// `VAULT_NAMESPACE` — Vault Enterprise namespace. `None` → omitted.
    pub namespace: Option<String>,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct K8sParams {
    /// Namespace every rendered object lands in. One namespace per
    /// `(tenant, environment)` pair (Q6 preferred pattern).
    pub namespace: String,
    /// Container image for router and worker pods.
    pub runtime_image: String,
    /// Router replica count. Plan step 11 mandates ≥ 2 for HA.
    pub router_replicas: u32,
    /// Worker public-exposure mode. From the `tunnel` deployer answer.
    pub tunnel: TunnelMode,
    /// OCI registry authorities (`host[:port]`) the worker/router may pull
    /// bundles from over plain HTTP. From the `oci_insecure_registries` answer;
    /// rendered as `GREENTIC_OCI_INSECURE_REGISTRIES`. Empty → HTTPS only.
    pub oci_insecure_registries: Vec<String>,
    /// Base64 of the env's local dev-store, set at reconcile time so the
    /// rendered [`DEV_SECRETS_SECRET_NAME`] Secret carries the operator's
    /// secrets. `None` on the pure preview path (`op env render`) and for the
    /// per-revision verbs — they never render the env-level Secret. The staging
    /// init container's copy is guarded on the file existing, so a `None`/empty
    /// Secret is a no-op rather than a boot failure.
    pub dev_secrets_data: Option<String>,
    /// Which secrets backend the worker resolves `secret://` refs against, and
    /// (for [`SecretsBackend::Vault`]) the non-secret connection config rendered
    /// into the pod. Defaults to [`SecretsBackend::DevStore`]; the
    /// render/reconcile call site overlays the env's bound `Secrets`-slot
    /// backend (it owns the binding-answers read, exactly like
    /// [`Self::dev_secrets_data`]).
    pub secrets_backend: SecretsBackend,
}

impl K8sParams {
    /// Sandbox defaults: namespace `gtc-<env-id>`, the S1 default image,
    /// two router replicas.
    pub fn for_env(env: &Environment) -> Self {
        Self {
            namespace: namespace_for_env(&env.environment_id),
            runtime_image: DEFAULT_RUNTIME_IMAGE.to_string(),
            router_replicas: 2,
            tunnel: TunnelMode::Off,
            oci_insecure_registries: Vec::new(),
            dev_secrets_data: None,
            secrets_backend: SecretsBackend::DevStore,
        }
    }

    /// Build params from the deployer binding's recorded wizard answers.
    ///
    /// `answers` is the flat JSON object keyed by wizard question id (the
    /// ecosystem's qa-spec answers convention). `None` (or an object with
    /// all relevant keys unset / blank) falls back to sandbox defaults —
    /// wizard questions are optional.
    ///
    /// Validation:
    /// - `namespace`: valid RFC 1123 label (used verbatim — the wizard
    ///   says it must match the namespace the bootstrap rules pack
    ///   provisioned). `null` / empty-string → default.
    /// - `runtime_image`: non-empty string matching `[a-z0-9.\-_/:@]+`.
    ///   `null` / empty-string → default.
    /// - `router_replicas`: JSON string or number parsed to `u32`; must
    ///   be >= 2 (the router must stay HA). `null` / empty-string →
    ///   default.
    /// - `kubeconfig_context`: silently accepted and ignored (client-
    ///   targeting knob, not a manifest knob — consumed by
    ///   [`kube_client::connect`](super::kube_client::connect)).
    /// - Any other key → `Err` (fail closed on wizard version skew or
    ///   typos).
    pub fn from_answers(
        env: &Environment,
        answers: Option<&serde_json::Value>,
    ) -> Result<Self, String> {
        let Some(answers) = answers else {
            return Ok(Self::for_env(env));
        };
        let obj = answers
            .as_object()
            .ok_or_else(|| "answers must be a JSON object".to_string())?;

        let defaults = Self::for_env(env);

        // Reject unknown keys first (fail closed on version skew).
        const KNOWN_KEYS: &[&str] = &[
            "kubeconfig_context",
            "namespace",
            "runtime_image",
            "router_replicas",
            "tunnel",
            "oci_insecure_registries",
        ];
        for key in obj.keys() {
            if !KNOWN_KEYS.contains(&key.as_str()) {
                return Err(format!("unknown answer key `{key}`"));
            }
        }

        let namespace = match answer_string(obj, "namespace") {
            Some(ns) => {
                if !is_dns1123_label(&ns) {
                    return Err(format!("namespace `{ns}` is not a valid RFC 1123 label"));
                }
                ns
            }
            None => defaults.namespace,
        };

        let runtime_image = match answer_string(obj, "runtime_image") {
            Some(img) => {
                if !img
                    .bytes()
                    .all(|b| b.is_ascii_lowercase() || b.is_ascii_digit() || b".-_/:@".contains(&b))
                {
                    return Err(format!("runtime_image `{img}` contains invalid characters"));
                }
                img
            }
            None => defaults.runtime_image,
        };

        let router_replicas = match obj.get("router_replicas") {
            None | Some(serde_json::Value::Null) => defaults.router_replicas,
            Some(v) => {
                let n: u32 = match v {
                    serde_json::Value::String(s) if s.is_empty() => defaults.router_replicas,
                    serde_json::Value::String(s) => s
                        .parse::<u32>()
                        .map_err(|e| format!("router_replicas `{s}` is not a valid u32: {e}"))?,
                    serde_json::Value::Number(n) => n
                        .as_u64()
                        .and_then(|v| u32::try_from(v).ok())
                        .ok_or_else(|| format!("router_replicas `{n}` is not a valid u32"))?,
                    _ => {
                        return Err(format!(
                            "router_replicas must be a string or number, got {v}"
                        ));
                    }
                };
                if n < 2 {
                    return Err(format!(
                        "router_replicas must be >= 2 (HA requirement), got {n}"
                    ));
                }
                n
            }
        };

        let tunnel = match answer_string(obj, "tunnel") {
            Some(s) => match s.as_str() {
                "cloudflared" => TunnelMode::Cloudflared,
                "off" | "none" => TunnelMode::Off,
                other => {
                    return Err(format!(
                        "tunnel `{other}` is not valid (expected `cloudflared` or `off`)"
                    ));
                }
            },
            None => defaults.tunnel,
        };

        // Accepts a comma-separated string (the wizard form) or a JSON array of
        // strings (declarative env-manifest authors). Blank → no registries.
        let oci_insecure_registries = match obj.get("oci_insecure_registries") {
            None | Some(serde_json::Value::Null) => defaults.oci_insecure_registries,
            Some(serde_json::Value::String(s)) => parse_insecure_registries(s),
            Some(serde_json::Value::Array(items)) => {
                let mut out = Vec::with_capacity(items.len());
                for item in items {
                    match item {
                        serde_json::Value::String(s) if !s.trim().is_empty() => {
                            out.push(s.trim().to_string());
                        }
                        serde_json::Value::String(_) => {}
                        other => {
                            return Err(format!(
                                "oci_insecure_registries entries must be strings, got {other}"
                            ));
                        }
                    }
                }
                out
            }
            Some(other) => {
                return Err(format!(
                    "oci_insecure_registries must be a comma-separated string or array of strings, got {other}"
                ));
            }
        };

        // kubeconfig_context: silently accepted and ignored.

        Ok(Self {
            namespace,
            runtime_image,
            router_replicas,
            tunnel,
            oci_insecure_registries,
            // Reconcile injects the dev-store bytes after this pure parse (it
            // owns the filesystem read); the preview path leaves it unset.
            dev_secrets_data: defaults.dev_secrets_data,
            // The secrets backend is derived from the env's `Secrets`-slot
            // binding, not the deployer wizard answers parsed here; the call
            // site overlays it (like `dev_secrets_data`).
            secrets_backend: defaults.secrets_backend,
        })
    }
}

/// Split the comma-separated `oci_insecure_registries` answer into `host[:port]`
/// authorities, trimming whitespace and dropping blanks. Mirrors greentic-start's
/// `GREENTIC_OCI_INSECURE_REGISTRIES` parser so the round-trip is lossless.
fn parse_insecure_registries(raw: &str) -> Vec<String> {
    raw.split(',')
        .map(str::trim)
        .filter(|e| !e.is_empty())
        .map(str::to_string)
        .collect()
}

/// Extract a non-empty string answer, treating JSON `null` and empty
/// strings as "left blank in the wizard" (unset).
fn answer_string(obj: &serde_json::Map<String, serde_json::Value>, key: &str) -> Option<String> {
    match obj.get(key) {
        None | Some(serde_json::Value::Null) => None,
        Some(serde_json::Value::String(s)) if s.is_empty() => None,
        Some(serde_json::Value::String(s)) => Some(s.clone()),
        Some(other) => Some(other.to_string()),
    }
}

/// The `kubeconfig_context` wizard answer — which kubeconfig context the
/// deployer connects through ([`super::kube_client::connect`]). It is a
/// client-targeting knob, NOT a manifest knob, so [`K8sParams::from_answers`]
/// accepts-and-ignores it; the reconcile path reads it here instead. `null`
/// / empty / absent → `None` (infer current-context / in-cluster).
///
/// Reading it through the same flat-answers convention as the manifest knobs
/// keeps a single home for "how a K8s wizard answer is parsed".
///
/// Gated on `k8s-client`: its only consumer is the reconcile path's
/// `connect`, which is itself feature-gated.
#[cfg(feature = "k8s-client")]
pub(crate) fn kubeconfig_context_from_answers(answers: Option<&Value>) -> Option<String> {
    answer_string(answers?.as_object()?, "kubeconfig_context")
}

/// Check whether `s` is a valid RFC 1123 DNS label: lowercase
/// alphanumeric + `-`, no leading/trailing `-`, length 1..=63.
pub fn is_dns1123_label(s: &str) -> bool {
    let len = s.len();
    if len == 0 || len > 63 {
        return false;
    }
    if s.starts_with('-') || s.ends_with('-') {
        return false;
    }
    s.bytes()
        .all(|b| b.is_ascii_lowercase() || b.is_ascii_digit() || b == b'-')
}

/// Default namespace for an env, derived from the env id.
///
/// Clean ids (already `[a-z0-9-]`, no leading/trailing `-`, and
/// `"gtc-" + id` fits in 63 chars) stay friendly: `gtc-<id>`.
/// Otherwise (any lossy character mapping or overflow) the namespace
/// gets a stable content-hash suffix: `gtc-<sanitized-prefix>-<hash8>`
/// where `hash8` = first 8 hex chars of SHA-256 over the RAW env id
/// string. Two distinct env ids can therefore never collide — the hash
/// discriminates when the sanitized form is ambiguous.
///
/// Env ids are unique per store, so hashing the env id alone suffices.
/// Cross-store collisions on one shared cluster are a production-
/// acceptance concern, out of scaffold scope.
pub fn namespace_for_env(env_id: &EnvId) -> String {
    let raw = env_id.as_str();
    let prefix = "gtc-";

    // Fast path: if the raw id is already clean RFC 1123 and fits, use
    // the friendly form directly — no hash needed.
    let is_clean = !raw.is_empty()
        && raw
            .bytes()
            .all(|b| b.is_ascii_lowercase() || b.is_ascii_digit() || b == b'-')
        && !raw.starts_with('-')
        && !raw.ends_with('-')
        && (prefix.len() + raw.len()) <= 63;

    if is_clean {
        return format!("{prefix}{raw}");
    }

    // Slow path: lossy/truncated derivation — append a content hash so
    // distinct env ids that sanitize identically still get unique
    // namespaces.
    let hash8 = {
        let digest = Sha256::digest(raw.as_bytes());
        format!(
            "{:02x}{:02x}{:02x}{:02x}",
            digest[0], digest[1], digest[2], digest[3]
        )
    };

    let sanitized = sanitize_dns1123_label(raw);
    if sanitized.is_empty() {
        // Degenerate ids (e.g. `...`) — the sanitized form is empty.
        return format!("{prefix}{hash8}");
    }

    // Truncate the sanitized prefix so `gtc-<prefix>-<hash8>` ≤ 63.
    // Layout: "gtc-" (4) + prefix + "-" (1) + hash8 (8) = 13 + prefix.
    let max_prefix_len = 63 - prefix.len() - 1 - hash8.len(); // 63 - 4 - 1 - 8 = 50
    let truncated = if sanitized.len() > max_prefix_len {
        sanitized[..max_prefix_len].trim_end_matches('-')
    } else {
        &sanitized
    };

    if truncated.is_empty() {
        format!("{prefix}{hash8}")
    } else {
        format!("{prefix}{truncated}-{hash8}")
    }
}

/// Name of a revision's worker Deployment AND Service (same name, two
/// kinds). ULIDs are Crockford base32 (uppercase alphanumerics), so the
/// lowercased form is a valid RFC 1123 label and stays unique.
pub fn worker_name(revision: &Revision) -> String {
    format!(
        "gtc-worker-{}",
        revision.revision_id.0.to_string().to_lowercase()
    )
}

/// Coerce a string into an RFC 1123 label fragment: lowercase,
/// `[a-z0-9-]` only (other characters map to `-`), no leading or
/// trailing `-`. Does NOT truncate — callers control length budgets.
/// Returns an empty string for degenerate inputs (all-separator).
fn sanitize_dns1123_label(raw: &str) -> String {
    let s: String = raw
        .to_lowercase()
        .chars()
        .map(|c| {
            if c.is_ascii_lowercase() || c.is_ascii_digit() {
                c
            } else {
                '-'
            }
        })
        .collect();
    s.trim_matches('-').to_string()
}

/// Label key identifying the owning environment, stamped on every
/// rendered object by `common_labels`. The kube client's ownership
/// guard reads it back — they MUST share this constant so a rename can't
/// silently turn the guard into a no-op.
pub const ENV_LABEL: &str = "greentic.ai/env";

/// Shared labels stamped on every object the env-pack renders.
fn common_labels(env: &Environment, component: &str) -> Value {
    let mut labels = json!({
        "app.kubernetes.io/managed-by": "greentic",
        "app.kubernetes.io/component": component,
    });
    labels[ENV_LABEL] = json!(env.environment_id.as_str());
    labels
}

/// Worker pod selector labels: the revision ULID is the identity (plan
/// step 4: `greentic.ai/revision: <ULID>`).
fn worker_selector_labels(env: &Environment, revision: &Revision) -> Value {
    let mut labels = common_labels(env, "worker");
    let map = labels.as_object_mut().expect("labels are an object");
    map.insert(
        "greentic.ai/revision".into(),
        json!(revision.revision_id.0.to_string()),
    );
    map.insert(
        "greentic.ai/deployment".into(),
        json!(revision.deployment_id.0.to_string()),
    );
    map.insert(
        "greentic.ai/bundle".into(),
        json!(revision.bundle_id.as_str()),
    );
    labels
}

/// Restricted-profile pod security context (step 11): non-root distroless
/// uid, RuntimeDefault seccomp.
fn pod_security_context() -> Value {
    json!({
        "runAsNonRoot": true,
        "runAsUser": 65532,
        "runAsGroup": 65532,
        "fsGroup": 65532,
        "seccompProfile": {"type": "RuntimeDefault"},
    })
}

/// Restricted-profile container security context + S2 resource baseline.
fn container_security_context() -> Value {
    json!({
        "allowPrivilegeEscalation": false,
        "readOnlyRootFilesystem": true,
        "capabilities": {"drop": ["ALL"]},
    })
}

fn resource_baseline() -> Value {
    json!({
        "requests": {"cpu": "250m", "memory": "256Mi"},
        "limits": {"cpu": "1", "memory": "1Gi"},
    })
}

/// The env's Namespace object. Rendered for `op env render` and the
/// bootstrap rules pack; whether Greentic or the customer's platform team
/// applies it is the Q6 ownership decision.
pub fn render_namespace(env: &Environment, params: &K8sParams) -> Value {
    json!({
        "apiVersion": "v1",
        "kind": "Namespace",
        "metadata": {
            "name": params.namespace,
            "labels": common_labels(env, "namespace"),
        },
    })
}

/// Runtime entrypoint args shared by router + worker: the new-model
/// bundle-less serve boot (`greentic-start start --env <id>`). The image
/// ENTRYPOINT is `greentic-start`; these become its arguments. The boot reads
/// the env store staged under `$HOME` and binds [`SERVE_PORT`], answering
/// `/healthz` once up.
fn runtime_boot_args(env: &Environment) -> Value {
    json!(["start", "--env", env.environment_id.as_str()])
}

/// Worker boot args: the shared bundle-less serve boot, plus the cloudflared
/// quick-tunnel flags when [`TunnelMode::Cloudflared`] is selected. The tunnel
/// gives the worker a public `*.trycloudflare.com` URL so greentic-start
/// auto-registers messaging webhooks at boot; the cloudflared binary is baked
/// into the runtime image at [`CLOUDFLARED_BINARY`]. The router never tunnels.
fn worker_boot_args(env: &Environment, params: &K8sParams) -> Value {
    let mut args = vec![
        Value::from("start"),
        Value::from("--env"),
        Value::from(env.environment_id.as_str()),
    ];
    if params.tunnel == TunnelMode::Cloudflared {
        args.extend([
            Value::from("--cloudflared"),
            Value::from("on"),
            Value::from("--cloudflared-binary"),
            Value::from(CLOUDFLARED_BINARY),
        ]);
    }
    Value::Array(args)
}

/// True when the env binds a secrets capability pack (the dev-store). Gates the
/// worker's secret staging and the env-level dev-store Secret so only
/// secrets-using envs carry them. Pure on `env`, so the worker pod spec is
/// identical whether rendered by reconcile or `apply-revision` — no flap.
fn env_uses_dev_secrets(env: &Environment) -> bool {
    env.packs.iter().any(|p| p.slot == CapabilitySlot::Secrets)
}

/// Non-secret content hash of the dev-store data. Placed on the worker pod
/// template so K8s triggers a rolling restart when reconcile updates the
/// Secret — the init-container copies at pod startup, so stale pods would run
/// with old credentials indefinitely without this. `None` (preview / no data
/// yet) hashes to a sentinel so the annotation is stable.
fn dev_secrets_content_hash(data: Option<&str>) -> String {
    let mut hasher = Sha256::new();
    match data {
        Some(b64) => hasher.update(b64.as_bytes()),
        None => hasher.update(b"<empty>"),
    }
    let digest = hasher.finalize();
    let mut hex = String::with_capacity(64);
    for byte in digest {
        use std::fmt::Write as _;
        let _ = write!(hex, "{byte:02x}");
    }
    hex
}

/// Init container that copies the dev-store Secret into the worker's writable
/// HOME at the path greentic-start's dev-store backend reads
/// (`$HOME/.greentic/environments/<env_id>/.greentic/dev/.dev.secrets.env`). The
/// copy is guarded on the source existing, so an empty/absent Secret is a no-op
/// rather than a boot failure; the dev-store is opened read-write under a flock
/// at runtime, so it must land on the writable volume, not the read-only mount.
fn stage_dev_secrets_init_container(env: &Environment) -> Value {
    let dev_dir = format!(
        "{STAGE_HOME}/.greentic/environments/{}/.greentic/dev",
        env.environment_id.as_str()
    );
    let src = format!("{DEV_SECRETS_SRC}/.dev.secrets.env");
    json!({
        "name": "stage-dev-secrets",
        "image": STAGE_INIT_IMAGE,
        "securityContext": container_security_context(),
        "command": [
            "sh",
            "-c",
            format!("set -eu; mkdir -p '{dev_dir}'; if [ -f '{src}' ]; then cp '{src}' '{dev_dir}/.dev.secrets.env'; fi"),
        ],
        "volumeMounts": [
            {"name": DEV_SECRETS_VOLUME, "mountPath": DEV_SECRETS_SRC, "readOnly": true},
            {"name": HOME_VOLUME, "mountPath": STAGE_HOME},
        ],
    })
}

/// The backend selector + `VAULT_*` connection env the worker boots with under
/// [`SecretsBackend::Vault`]. `GREENTIC_SECRETS_BACKEND=vault` tells
/// greentic-start's serve boot to construct the Vault manager; the `VAULT_*`
/// vars are the provider's non-secret connection config. Each optional `VAULT_*`
/// is emitted only when it differs from the provider default, so the common case
/// renders just the selector, `VAULT_ADDR`, and `VAULT_K8S_ROLE`. The pod's
/// identity (and thus its Vault token) comes from the projected ServiceAccount
/// token at the standard path — never from a rendered var.
fn secrets_backend_env(vault: &VaultBackend) -> Vec<Value> {
    let mut vars = vec![
        json!({"name": "GREENTIC_SECRETS_BACKEND", "value": "vault"}),
        json!({"name": "VAULT_ADDR", "value": vault.addr}),
        json!({"name": "VAULT_K8S_ROLE", "value": vault.k8s_role}),
    ];
    for (name, value, default) in [
        ("VAULT_KV_MOUNT", &vault.kv_mount, VAULT_DEFAULT_KV_MOUNT),
        ("VAULT_KV_PREFIX", &vault.kv_prefix, VAULT_DEFAULT_KV_PREFIX),
        (
            "VAULT_K8S_MOUNT",
            &vault.auth_mount,
            VAULT_DEFAULT_AUTH_MOUNT,
        ),
        (
            "VAULT_TRANSIT_MOUNT",
            &vault.transit_mount,
            VAULT_DEFAULT_TRANSIT_MOUNT,
        ),
        (
            "VAULT_TRANSIT_KEY",
            &vault.transit_key,
            VAULT_DEFAULT_TRANSIT_KEY,
        ),
    ] {
        if value.as_str() != default {
            vars.push(json!({"name": name, "value": value}));
        }
    }
    if let Some(ns) = &vault.namespace {
        vars.push(json!({"name": "VAULT_NAMESPACE", "value": ns}));
    }
    vars
}

/// The worker's ServiceAccount — the pod identity Vault's Kubernetes auth
/// authenticates ([`SecretsBackend::Vault`]). Env-level (one per namespace),
/// rendered only for Vault envs. `automountServiceAccountToken` stays at the
/// K8s default so the projected token mounts at the path the provider reads;
/// the SA→Vault-role binding is provisioned by the Vault bootstrap (Phase E.4),
/// not here.
fn render_worker_service_account(env: &Environment, params: &K8sParams) -> Value {
    json!({
        "apiVersion": "v1",
        "kind": "ServiceAccount",
        "metadata": {
            "name": WORKER_SERVICE_ACCOUNT,
            "namespace": params.namespace,
            "labels": common_labels(env, "worker"),
        },
    })
}

/// Fixed rayon thread-pool size for the runtime pods. The bundle unpacker
/// (backhand's `parallel` reader, reached on the M2 boot pull) sizes rayon to
/// the HOST core count, ignoring the pod's `cpu` cgroup quota. With the
/// host-sized pool throttled to the pod's limit it starves and silently yields
/// a 0-byte file — a truncated `.gtpack` that fails activation with "Could not
/// find EOCD". A small fixed pool avoids the host-core over-subscription (1
/// thread is its own failure mode for the parallel reader; values 2–8 extract
/// correctly even under a 10% CPU quota — verified empirically). The deeper fix
/// is dropping backhand's `parallel` feature in greentic-bundle; this guards
/// every already-published runtime image regardless.
const RAYON_THREADS: &str = "4";

/// Boot env shared by router + worker. `GREENTIC_GATEWAY_LISTEN_ADDR=0.0.0.0`
/// binds all interfaces (the kubelet probes the pod IP, not loopback, so the
/// runtime's `127.0.0.1` default would make every probe fail); `HOME` roots
/// the env store on the writable staging volume; `RAYON_NUM_THREADS` caps the
/// bundle-unpack thread pool (see [`RAYON_THREADS`]).
fn runtime_boot_env(env: &Environment, oci_insecure_registries: &[String]) -> Vec<Value> {
    let mut vars = vec![
        json!({"name": "GREENTIC_ENV_ID", "value": env.environment_id.as_str()}),
        json!({"name": "HOME", "value": STAGE_HOME}),
        json!({"name": "GREENTIC_GATEWAY_LISTEN_ADDR", "value": "0.0.0.0"}),
        json!({"name": "RAYON_NUM_THREADS", "value": RAYON_THREADS}),
    ];
    // greentic-start honors this only on the digest-gated OCI boot-pull; emitting
    // it when unset would be a harmless no-op, but skip it to keep the pod spec lean.
    if !oci_insecure_registries.is_empty() {
        vars.push(json!({
            "name": "GREENTIC_OCI_INSECURE_REGISTRIES",
            "value": oci_insecure_registries.join(","),
        }));
    }
    vars
}

/// Main-container volume mounts shared by router + worker: the writable HOME
/// (env store + runtime writes) and a writable `/tmp` (the root filesystem is
/// read-only).
fn runtime_volume_mounts() -> Value {
    json!([
        {"name": HOME_VOLUME, "mountPath": STAGE_HOME},
        {"name": TMP_VOLUME, "mountPath": "/tmp"},
    ])
}

/// Pod volumes shared by router + worker: writable HOME + `/tmp` emptyDirs and
/// the read-only env-store ConfigMap the init container copies from. Returned as
/// a `Vec` so the worker can append the optional dev-store Secret volume.
fn runtime_pod_volumes() -> Vec<Value> {
    vec![
        json!({"name": HOME_VOLUME, "emptyDir": {}}),
        json!({"name": TMP_VOLUME, "emptyDir": {}}),
        json!({"name": ENV_STORE_SRC_VOLUME, "configMap": {"name": ENV_STORE_CONFIG_MAP_NAME}}),
    ]
}

/// Init container that stages the env store into the writable HOME volume at
/// the path `greentic-start` reads (`$HOME/.greentic/environments/<env_id>/`).
/// M1 stages only `environment.json` (→ an empty runtime-config → the
/// "serving probes only" boot). M2 replaces this with the distributor-pull
/// container that also stages the runtime-config and the revision's packs.
///
/// Assumes a simple (RFC 1123-ish) env id so the path segment matches the
/// store's; the sandbox env ids the wizard accepts satisfy this.
fn env_store_init_container(env: &Environment) -> Value {
    let dst = format!(
        "{STAGE_HOME}/.greentic/environments/{}",
        env.environment_id.as_str()
    );
    json!({
        "name": "stage-env-store",
        "image": STAGE_INIT_IMAGE,
        "securityContext": container_security_context(),
        "command": [
            "sh",
            "-c",
            format!("set -eu; mkdir -p '{dst}'; cp {ENV_STORE_SRC}/environment.json '{dst}/environment.json'"),
        ],
        "volumeMounts": [
            {"name": ENV_STORE_SRC_VOLUME, "mountPath": ENV_STORE_SRC, "readOnly": true},
            {"name": HOME_VOLUME, "mountPath": STAGE_HOME},
        ],
    })
}

/// One revision's worker Deployment (plan step 4).
///
/// The pod carries its full revision identity as environment variables
/// (`GREENTIC_ENV_ID` / `GREENTIC_REVISION_ID` / `GREENTIC_DEPLOYMENT_ID`
/// / `GREENTIC_BUNDLE_ID` / `GREENTIC_BUNDLE_DIGEST`) — the serve process
/// boots from the staged env store, while the revision-identity vars are the
/// contract the M2 distributor-pull init container reads to fetch the
/// revision's packs.
///
/// Readiness probes `/healthz` today; the per-revision
/// `/healthz/<revision_id>` route is the acceptance-gate target once
/// `greentic-start` serves it.
pub fn render_worker_deployment(
    env: &Environment,
    revision: &Revision,
    params: &K8sParams,
) -> Value {
    let labels = worker_selector_labels(env, revision);
    let mut env_vars = runtime_boot_env(env, &params.oci_insecure_registries);
    env_vars.extend([
        json!({"name": "GREENTIC_REVISION_ID", "value": revision.revision_id.0.to_string()}),
        json!({"name": "GREENTIC_DEPLOYMENT_ID", "value": revision.deployment_id.0.to_string()}),
        json!({"name": "GREENTIC_BUNDLE_ID", "value": revision.bundle_id.as_str()}),
        json!({"name": "GREENTIC_BUNDLE_DIGEST", "value": revision.bundle_digest}),
    ]);

    // How the worker resolves `secret://` refs at runtime. Worker-only either
    // way — the router never resolves secrets, mirroring the historical
    // dev-store staging.
    //
    // - `DevStore`: stage the operator's dev-store into the worker's writable
    //   HOME (messaging bot tokens, webhook secrets resolve there). The Secret
    //   volume is `optional` and the init copy is guarded on the file existing,
    //   so an env with no secrets yet boots cleanly.
    // - `Vault`: no values cross into the cluster — the pod gets the Vault
    //   ServiceAccount identity ([`WORKER_SERVICE_ACCOUNT`]) plus `VAULT_*`
    //   connection env, and greentic-start resolves refs from Vault in-pod.
    //
    // Pure on `env` + `params` so reconcile and `apply-revision` agree.
    let mut init_containers = vec![env_store_init_container(env)];
    let mut volumes = runtime_pod_volumes();
    let mut service_account: Option<&str> = None;
    let uses_dev_secrets =
        matches!(params.secrets_backend, SecretsBackend::DevStore) && env_uses_dev_secrets(env);
    match &params.secrets_backend {
        SecretsBackend::DevStore => {
            if uses_dev_secrets {
                init_containers.push(stage_dev_secrets_init_container(env));
                volumes.push(json!({
                    "name": DEV_SECRETS_VOLUME,
                    "secret": {"secretName": DEV_SECRETS_SECRET_NAME, "optional": true},
                }));
            }
        }
        SecretsBackend::Vault(vault) => {
            env_vars.extend(secrets_backend_env(vault));
            service_account = Some(WORKER_SERVICE_ACCOUNT);
        }
    }

    // Pod-template annotations: when the env stages dev-store material, a
    // content hash triggers a rolling restart on `reconcile` whenever the
    // operator rotates a credential. The preview path (`None`) omits the
    // annotation so `op env render` stays pure. `apply-revision` also
    // renders `None`, so the annotation is stable across verb paths.
    let mut pod_annotations = serde_json::Map::new();
    if uses_dev_secrets {
        let hash = dev_secrets_content_hash(params.dev_secrets_data.as_deref());
        pod_annotations.insert(
            "greentic.ai/dev-store-hash".to_string(),
            Value::String(hash),
        );
    }
    let pod_metadata = if pod_annotations.is_empty() {
        json!({"labels": labels})
    } else {
        json!({"labels": labels, "annotations": Value::Object(pod_annotations)})
    };

    let mut deployment = json!({
        "apiVersion": "apps/v1",
        "kind": "Deployment",
        "metadata": {
            "name": worker_name(revision),
            "namespace": params.namespace,
            "labels": labels,
        },
        "spec": {
            "replicas": 1,
            "selector": {"matchLabels": {
                "greentic.ai/revision": revision.revision_id.0.to_string(),
            }},
            "template": {
                "metadata": pod_metadata,
                "spec": {
                    "securityContext": pod_security_context(),
                    "initContainers": Value::Array(init_containers),
                    "containers": [{
                        "name": "worker",
                        "image": params.runtime_image,
                        "args": worker_boot_args(env, params),
                        "securityContext": container_security_context(),
                        "resources": resource_baseline(),
                        "ports": [{"name": "http", "containerPort": SERVE_PORT}],
                        "env": Value::Array(env_vars),
                        "volumeMounts": runtime_volume_mounts(),
                        "readinessProbe": {
                            "httpGet": {"path": "/healthz", "port": SERVE_PORT},
                            "initialDelaySeconds": 2,
                            "periodSeconds": 5,
                        },
                    }],
                    "volumes": Value::Array(volumes),
                },
            },
        },
    });
    // The pod's identity under Vault: its projected SA token is what Vault's
    // Kubernetes auth exchanges for a token. Injected here (rather than inline)
    // so the DevStore path renders an identical pod spec to before.
    if let Some(sa) = service_account {
        deployment["spec"]["template"]["spec"]["serviceAccountName"] = Value::from(sa);
    }
    deployment
}

/// One revision's ClusterIP Service — the stable address the router
/// dispatches that revision's traffic to.
pub fn render_worker_service(env: &Environment, revision: &Revision, params: &K8sParams) -> Value {
    json!({
        "apiVersion": "v1",
        "kind": "Service",
        "metadata": {
            "name": worker_name(revision),
            "namespace": params.namespace,
            "labels": worker_selector_labels(env, revision),
        },
        "spec": {
            "type": "ClusterIP",
            "selector": {
                "greentic.ai/revision": revision.revision_id.0.to_string(),
            },
            "ports": [{"name": "http", "port": SERVE_PORT, "targetPort": SERVE_PORT}],
        },
    })
}

/// Both worker objects for a revision, in apply order (Deployment first,
/// then Service). What [`Deployer::warm_revision`](super::deployer)
/// applies and [`Deployer::archive_revision`](super::deployer) deletes.
pub fn render_worker_manifests(
    env: &Environment,
    revision: &Revision,
    params: &K8sParams,
) -> Vec<Value> {
    vec![
        render_worker_deployment(env, revision, params),
        render_worker_service(env, revision, params),
    ]
}

/// Whether a revision's persisted lifecycle puts its worker objects in the
/// cluster's desired state.
///
/// `warm_revision` applies the worker pair (`Staged → Warming → Ready`) and
/// the objects stay up through `Draining` — drain is routing-side, teardown
/// happens at `archive_revision` (the B7 two-state model). So:
///
/// - `Warming` / `Ready` / `Draining` → present.
/// - `Inactive` → absent. A post-drain revision's objects may still exist
///   transiently until the operator archives it, but it is pending teardown,
///   not desired.
/// - `Staged` / `Failed` / `Archived` → absent (never applied, or torn down).
///
/// The single home for this policy: the renderer ([`super::render`]) emits a
/// revision's workers iff present, and the reconcile path ([`super::deployer`])
/// prunes a revision's workers iff NOT present — applying the same predicate
/// from both sides guarantees the two never disagree.
pub(crate) fn has_cluster_presence(lifecycle: RevisionLifecycle) -> bool {
    matches!(
        lifecycle,
        RevisionLifecycle::Warming | RevisionLifecycle::Ready | RevisionLifecycle::Draining
    )
}

/// The stable router Deployment (plan step 5): ≥2 replicas, topology
/// spread, runtime-config ConfigMap mounted read-only. The router is
/// authoritative for `TrafficSplit` enforcement in the Zain v1 pilot.
pub fn render_router_deployment(env: &Environment, params: &K8sParams) -> Value {
    let labels = common_labels(env, "router");
    json!({
        "apiVersion": "apps/v1",
        "kind": "Deployment",
        "metadata": {
            "name": ROUTER_NAME,
            "namespace": params.namespace,
            "labels": labels,
        },
        "spec": {
            "replicas": params.router_replicas,
            "selector": {"matchLabels": labels},
            "template": {
                "metadata": {"labels": labels},
                "spec": {
                    "securityContext": pod_security_context(),
                    "topologySpreadConstraints": [{
                        "maxSkew": 1,
                        "topologyKey": "kubernetes.io/hostname",
                        "whenUnsatisfiable": "ScheduleAnyway",
                        "labelSelector": {"matchLabels": labels},
                    }],
                    "initContainers": [env_store_init_container(env)],
                    "containers": [{
                        "name": "router",
                        "image": params.runtime_image,
                        "args": runtime_boot_args(env),
                        "securityContext": container_security_context(),
                        "resources": resource_baseline(),
                        "ports": [{"name": "http", "containerPort": SERVE_PORT}],
                        "env": Value::Array(runtime_boot_env(env, &params.oci_insecure_registries)),
                        "volumeMounts": runtime_volume_mounts(),
                        "readinessProbe": {
                            "httpGet": {"path": "/healthz", "port": SERVE_PORT},
                            "initialDelaySeconds": 2,
                            "periodSeconds": 5,
                        },
                    }],
                    "volumes": Value::Array(runtime_pod_volumes()),
                },
            },
        },
    })
}

/// The stable router Service — the single target the Gateway / Ingress
/// routes 100% of the env's traffic to.
pub fn render_router_service(env: &Environment, params: &K8sParams) -> Value {
    let labels = common_labels(env, "router");
    json!({
        "apiVersion": "v1",
        "kind": "Service",
        "metadata": {
            "name": ROUTER_NAME,
            "namespace": params.namespace,
            "labels": labels,
        },
        "spec": {
            "type": "ClusterIP",
            "selector": labels,
            "ports": [{"name": "http", "port": SERVE_PORT, "targetPort": SERVE_PORT}],
        },
    })
}

/// Router PodDisruptionBudget (step 11): voluntary disruptions keep at
/// least one router serving.
pub fn render_router_pdb(env: &Environment, params: &K8sParams) -> Value {
    let labels = common_labels(env, "router");
    json!({
        "apiVersion": "policy/v1",
        "kind": "PodDisruptionBudget",
        "metadata": {
            "name": ROUTER_NAME,
            "namespace": params.namespace,
            "labels": labels,
        },
        "spec": {
            "minAvailable": 1,
            "selector": {"matchLabels": labels},
        },
    })
}

/// The runtime-config ConfigMap: the env's `TrafficSplit` projection
/// (exactly [`materialize_runtime_config`]) as `runtime-config.json`.
/// `apply_traffic_split` upserts this object; the router reloads it and
/// enforces the split in-process.
pub fn render_runtime_config_map(env: &Environment, params: &K8sParams) -> Value {
    let runtime_config = materialize_runtime_config(env);
    let payload = serde_json::to_string(&runtime_config)
        .expect("runtime-config projection serializes (pure spec types)");
    json!({
        "apiVersion": "v1",
        "kind": "ConfigMap",
        "metadata": {
            "name": RUNTIME_CONFIG_MAP_NAME,
            "namespace": params.namespace,
            "labels": common_labels(env, "router"),
        },
        "data": {"runtime-config.json": payload},
    })
}

/// The env-store ConfigMap: the serialized `environment.json` the init
/// container stages into `$HOME/.greentic/environments/<env_id>/` so
/// `greentic-start`'s bundle-less boot can `EnvironmentStore::load` it. On
/// disk the store reads `environment.json` back as a plain [`Environment`]
/// (`read_json::<Environment>` + `validate`), so re-serializing the same
/// value round-trips. M1 stages only this document (no runtime-config → the
/// "serving probes only" boot); M2 extends staging to the runtime-config and
/// the revision's packs.
pub fn render_env_store_config_map(env: &Environment, params: &K8sParams) -> Value {
    let environment_json =
        serde_json::to_string(env).expect("Environment serializes (pure spec types)");
    json!({
        "apiVersion": "v1",
        "kind": "ConfigMap",
        "metadata": {
            "name": ENV_STORE_CONFIG_MAP_NAME,
            "namespace": params.namespace,
            "labels": common_labels(env, "env-store"),
        },
        "data": {"environment.json": environment_json},
    })
}

/// The dev-store Secret: the operator's local `.dev.secrets.env` delivered into
/// the cluster so the worker resolves `secret://` refs in-pod (the K8s "no
/// runtime secrets" gap, bridged without a cloud secret-store). `data` carries
/// the base64 dev-store when reconcile read it ([`K8sParams::dev_secrets_data`]);
/// an empty `data` (the pure preview path, or an env with no secrets put yet)
/// renders a structurally-complete Secret with no material — the staging init
/// container's copy is guarded on the file existing. Only emitted for envs that
/// bind a secrets pack ([`env_uses_dev_secrets`]).
fn render_dev_secrets_secret(env: &Environment, params: &K8sParams) -> Value {
    let mut data = serde_json::Map::new();
    if let Some(b64) = &params.dev_secrets_data {
        data.insert(".dev.secrets.env".to_string(), Value::String(b64.clone()));
    }
    json!({
        "apiVersion": "v1",
        "kind": "Secret",
        "type": "Opaque",
        "metadata": {
            "name": DEV_SECRETS_SECRET_NAME,
            "namespace": params.namespace,
            "labels": common_labels(env, "dev-secrets"),
        },
        "data": Value::Object(data),
    })
}

/// Whether the environment routes a revision that the worker pulls at boot —
/// a revision referenced by a traffic split that carries a `bundle_source_uri`.
/// This mirrors greentic-start's bundle-less boot, which materializes only
/// routed revisions, so it gates the M2 worker-egress allowance below: with no
/// pullable routed revision the worker keeps the tighter default-deny egress.
fn env_has_pullable_routed_revision(env: &Environment) -> bool {
    env.traffic_splits.iter().any(|split| {
        split.entries.iter().any(|entry| {
            env.revisions.iter().any(|revision| {
                revision.revision_id == entry.revision_id && revision.bundle_source_uri.is_some()
            })
        })
    })
}

/// Default-deny + allow-list NetworkPolicies (S3): deny everything, then
/// allow DNS egress for all pods, ingress→router on the serve port (the
/// Gateway/Ingress data-plane peer is refined per Q4), router→worker on
/// the serve port, and worker ingress from the router only.
///
/// Two more policies, `gtc-allow-worker-egress` and `gtc-allow-router-egress`,
/// govern egress for the M2 boot bundle pull. BOTH the worker AND the router
/// boot `start --env` and materialize routed bundle-sourced revisions, so BOTH
/// need egress to the bundle source — one policy per pulling role. Each is
/// ALWAYS rendered — so reconcile's plain upsert converges it both ways without
/// env-level pruning — and its egress rule toggles by pullability: allow-all
/// while a routed revision carries a `bundle_source_uri` (the bundle-less pod
/// must reach its registry at boot, or the default-deny leaves it DNS-only and
/// the pull fails closed under a NetworkPolicy-enforcing CNI), and an empty deny
/// rule otherwise (so an env that stops pulling closes the opening on the next
/// reconcile rather than leaving a stale allow-all hole). Each selector is
/// scoped to THIS env's pods of that role, so one env's pullable revision never
/// grants egress to a sibling env sharing the namespace. (NetworkPolicy-enforcing
/// CNIs — including modern kindnet — gate this; it is also covered by the render
/// unit tests.)
pub fn render_network_policies(env: &Environment, params: &K8sParams) -> Vec<Value> {
    let router_labels = common_labels(env, "router");
    let worker_component = json!({"app.kubernetes.io/component": "worker"});
    let dns_ports = json!([
        {"protocol": "UDP", "port": 53},
        {"protocol": "TCP", "port": 53},
    ]);
    let mut policies = vec![
        json!({
            "apiVersion": "networking.k8s.io/v1",
            "kind": "NetworkPolicy",
            "metadata": {
                "name": "gtc-default-deny",
                "namespace": params.namespace,
                "labels": common_labels(env, "network-policy"),
            },
            "spec": {
                "podSelector": {},
                "policyTypes": ["Ingress", "Egress"],
            },
        }),
        json!({
            "apiVersion": "networking.k8s.io/v1",
            "kind": "NetworkPolicy",
            "metadata": {
                "name": "gtc-allow-dns",
                "namespace": params.namespace,
                "labels": common_labels(env, "network-policy"),
            },
            "spec": {
                "podSelector": {},
                "policyTypes": ["Egress"],
                "egress": [{"ports": dns_ports}],
            },
        }),
        json!({
            "apiVersion": "networking.k8s.io/v1",
            "kind": "NetworkPolicy",
            "metadata": {
                "name": "gtc-allow-router",
                "namespace": params.namespace,
                "labels": common_labels(env, "network-policy"),
            },
            "spec": {
                "podSelector": {"matchLabels": router_labels},
                "policyTypes": ["Ingress", "Egress"],
                "ingress": [{"ports": [{"protocol": "TCP", "port": SERVE_PORT}]}],
                "egress": [{
                    "to": [{"podSelector": {"matchLabels": worker_component}}],
                    "ports": [{"protocol": "TCP", "port": SERVE_PORT}],
                }],
            },
        }),
        json!({
            "apiVersion": "networking.k8s.io/v1",
            "kind": "NetworkPolicy",
            "metadata": {
                "name": "gtc-allow-workers",
                "namespace": params.namespace,
                "labels": common_labels(env, "network-policy"),
            },
            "spec": {
                "podSelector": {"matchLabels": worker_component},
                "policyTypes": ["Ingress"],
                "ingress": [{
                    "from": [{"podSelector": {"matchLabels": json!({
                        "app.kubernetes.io/component": "router",
                    })}}],
                    "ports": [{"protocol": "TCP", "port": SERVE_PORT}],
                }],
            },
        }),
    ];
    // M2 boot bundle pull AND the Vault secrets backend both need egress out of
    // the default-deny namespace. BOTH the worker AND the router boot
    // `start --env` and materialize routed bundle-sourced revisions, so BOTH
    // need egress to the bundle source while a routed revision is pullable;
    // additionally the WORKER needs egress to Vault when it resolves secrets
    // there (`SecretsBackend::Vault`) — the router never resolves secrets.
    // Render one stable, env-scoped policy per role: allow-all egress when that
    // role has an opening (the pod fetches its own packs integrity-gated against
    // the revision's `bundle_digest`, and the Vault token exchange is the
    // worker's own outbound call, so breadth is not a pack-injection vector — a
    // per-destination allow-list is a tracked hardening follow-up); an empty
    // deny rule otherwise. Always rendered so reconcile converges allow→deny
    // without env-level pruning, closing the opening once the env stops pulling
    // or leaves Vault. DNS egress stays granted by `gtc-allow-dns` regardless.
    let pullable = env_has_pullable_routed_revision(env);
    let worker_uses_vault = matches!(params.secrets_backend, SecretsBackend::Vault(_));
    for role in ["worker", "router"] {
        let allow_all = pullable || (role == "worker" && worker_uses_vault);
        let egress = if allow_all { json!([{}]) } else { json!([]) };
        policies.push(json!({
            "apiVersion": "networking.k8s.io/v1",
            "kind": "NetworkPolicy",
            "metadata": {
                "name": format!("gtc-allow-{role}-egress"),
                "namespace": params.namespace,
                "labels": common_labels(env, "network-policy"),
            },
            "spec": {
                "podSelector": {"matchLabels": common_labels(env, role)},
                "policyTypes": ["Egress"],
                "egress": egress,
            },
        }));
    }
    policies
}

/// Every environment-level object, in apply order: Namespace, env-store
/// ConfigMap + runtime ConfigMap (the pods stage / mount them — must exist
/// first), router Deployment + Service + PDB, NetworkPolicies. Per-revision
/// worker objects are NOT included — they ride the revision lifecycle verbs.
/// `gtc op env render` emits this set (plus present-revision workers)
/// through the [`ManifestRenderer`](crate::env_packs::render::ManifestRenderer)
/// impl in [`super::render`].
pub fn render_environment_manifests(env: &Environment, params: &K8sParams) -> Vec<Value> {
    let mut manifests = vec![
        render_namespace(env, params),
        render_env_store_config_map(env, params),
        render_runtime_config_map(env, params),
        render_router_deployment(env, params),
        render_router_service(env, params),
        render_router_pdb(env, params),
    ];
    manifests.extend(render_network_policies(env, params));
    // The env-level secrets object, last (never pruned; appended after the
    // NetworkPolicies so it never shifts the index-pinned env-level objects;
    // reconcile applies it before the workers — the trait impl extends with
    // workers after this set — so the worker's volume/identity resolves
    // cleanly):
    // - `DevStore`: the dev-store Secret carrying the operator's values, only
    //   when the env binds a secrets pack.
    // - `Vault`: the worker ServiceAccount the pod authenticates to Vault as.
    //   No secret material is rendered.
    match &params.secrets_backend {
        SecretsBackend::DevStore => {
            if env_uses_dev_secrets(env) {
                manifests.push(render_dev_secrets_secret(env, params));
            }
        }
        SecretsBackend::Vault(_) => {
            manifests.push(render_worker_service_account(env, params));
        }
    }
    manifests
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::env_packs::deployer::conformance::build_fixture_env;

    fn fixture() -> (Environment, K8sParams) {
        let env = build_fixture_env();
        let params = K8sParams::for_env(&env);
        (env, params)
    }

    #[test]
    fn presence_policy_matches_the_b7_two_state_model() {
        use RevisionLifecycle::*;
        for (lifecycle, present) in [
            (Inactive, false),
            (Staged, false),
            (Warming, true),
            (Ready, true),
            (Draining, true),
            (Failed, false),
            (Archived, false),
        ] {
            assert_eq!(
                has_cluster_presence(lifecycle),
                present,
                "{lifecycle:?} presence policy drifted"
            );
        }
    }

    #[test]
    fn params_for_env_derive_sandbox_defaults() {
        let (env, params) = fixture();
        assert_eq!(params.namespace, format!("gtc-{}", env.environment_id));
        assert_eq!(params.runtime_image, DEFAULT_RUNTIME_IMAGE);
        assert_eq!(params.router_replicas, 2, "plan step 11: router HA ≥ 2");
    }

    #[test]
    fn sanitize_dns1123_label_handles_env_id_charset() {
        // EnvId permits uppercase, `.` and `_` — namespaces do not.
        assert_eq!(sanitize_dns1123_label("Prod.EU_west"), "prod-eu-west");
        // Trailing separator junk is trimmed.
        assert_eq!(sanitize_dns1123_label("x-"), "x");
        // Degenerate input yields empty (caller handles fallback).
        assert_eq!(sanitize_dns1123_label("..."), "");
    }

    #[test]
    fn namespace_collision_proof_distinct_ids_never_share_namespace() {
        // `prod-eu-west` is clean → friendly `gtc-prod-eu-west`.
        let clean = EnvId::try_from("prod-eu-west").unwrap();
        let ns_clean = namespace_for_env(&clean);
        assert_eq!(ns_clean, "gtc-prod-eu-west");

        // `Prod.EU_west` sanitizes to the same fragment but is lossy →
        // gets a hash suffix → DIFFERENT namespace.
        let lossy = EnvId::try_from("Prod.EU_west").unwrap();
        let ns_lossy = namespace_for_env(&lossy);
        assert_ne!(
            ns_lossy, ns_clean,
            "lossy derivation must not collide with the clean id"
        );
        assert!(
            ns_lossy.starts_with("gtc-prod-eu-west-"),
            "lossy namespace must start with the sanitized prefix: {ns_lossy}"
        );
        // Hash suffix is 8 hex chars.
        let suffix = ns_lossy.strip_prefix("gtc-prod-eu-west-").unwrap();
        assert_eq!(suffix.len(), 8);
        assert!(
            suffix.chars().all(|c| c.is_ascii_hexdigit()),
            "suffix must be hex: {suffix}"
        );
    }

    #[test]
    fn namespace_long_ids_truncated_but_distinct() {
        // Two long ids that share a 50-char sanitized prefix but differ
        // after that — they must produce different namespaces, both ≤ 63.
        let base = "a".repeat(55);
        let id_a = EnvId::try_from(format!("{base}xxxxx")).unwrap();
        let id_b = EnvId::try_from(format!("{base}yyyyy")).unwrap();
        let ns_a = namespace_for_env(&id_a);
        let ns_b = namespace_for_env(&id_b);
        assert_ne!(ns_a, ns_b, "truncated long ids must not collide");
        assert!(ns_a.len() <= 63, "namespace must be ≤ 63 chars: {ns_a}");
        assert!(ns_b.len() <= 63, "namespace must be ≤ 63 chars: {ns_b}");
        // Both are valid RFC 1123.
        for ns in [&ns_a, &ns_b] {
            assert!(
                ns.chars()
                    .all(|c| c.is_ascii_lowercase() || c.is_ascii_digit() || c == '-'),
                "invalid RFC 1123: {ns}"
            );
            assert!(
                !ns.starts_with('-') && !ns.ends_with('-'),
                "leading/trailing dash: {ns}"
            );
        }
    }

    #[test]
    fn namespace_derivation_is_deterministic() {
        let id = EnvId::try_from("Prod.EU_west").unwrap();
        let a = namespace_for_env(&id);
        let b = namespace_for_env(&id);
        assert_eq!(a, b, "same id must always produce the same namespace");
    }

    #[test]
    fn namespace_degenerate_id_gets_hash_only() {
        // An env id made entirely of dots: sanitizes to empty.
        let id = EnvId::try_from("...").unwrap();
        let ns = namespace_for_env(&id);
        assert!(ns.starts_with("gtc-"), "must have gtc- prefix: {ns}");
        // "gtc-" + 8 hex = 12 chars
        assert_eq!(ns.len(), 12);
        assert!(
            ns[4..].chars().all(|c| c.is_ascii_hexdigit()),
            "must be gtc-<hex8>: {ns}"
        );
    }

    #[test]
    fn worker_name_is_a_lowercase_rfc1123_label() {
        let (env, _) = fixture();
        let name = worker_name(&env.revisions[0]);
        assert!(name.starts_with("gtc-worker-"));
        assert!(name.len() <= 63);
        assert!(
            name.chars()
                .all(|c| c.is_ascii_lowercase() || c.is_ascii_digit() || c == '-'),
            "name must be RFC 1123: {name}"
        );
        // Two revisions never collide.
        assert_ne!(name, worker_name(&env.revisions[1]));
    }

    #[test]
    fn rendering_is_deterministic() {
        let (env, params) = fixture();
        let a = render_environment_manifests(&env, &params);
        let b = render_environment_manifests(&env, &params);
        assert_eq!(
            serde_json::to_string(&a).unwrap(),
            serde_json::to_string(&b).unwrap(),
            "identical inputs must serialize identically"
        );
        let wa = render_worker_manifests(&env, &env.revisions[0], &params);
        let wb = render_worker_manifests(&env, &env.revisions[0], &params);
        assert_eq!(
            serde_json::to_string(&wa).unwrap(),
            serde_json::to_string(&wb).unwrap()
        );
    }

    #[test]
    fn worker_deployment_carries_revision_label_and_identity_env() {
        let (env, params) = fixture();
        let rev = &env.revisions[0];
        let d = render_worker_deployment(&env, rev, &params);
        let ulid = rev.revision_id.0.to_string();
        // Plan step 4: the revision label is the worker identity.
        assert_eq!(
            d["metadata"]["labels"]["greentic.ai/revision"],
            serde_json::json!(ulid)
        );
        assert_eq!(
            d["spec"]["selector"]["matchLabels"]["greentic.ai/revision"],
            serde_json::json!(ulid)
        );
        // Identity env vars for the runtime entrypoint.
        let envs = d["spec"]["template"]["spec"]["containers"][0]["env"]
            .as_array()
            .unwrap();
        let names: Vec<&str> = envs.iter().map(|e| e["name"].as_str().unwrap()).collect();
        for required in [
            "GREENTIC_ENV_ID",
            "GREENTIC_REVISION_ID",
            "GREENTIC_DEPLOYMENT_ID",
            "GREENTIC_BUNDLE_ID",
            "GREENTIC_BUNDLE_DIGEST",
        ] {
            assert!(names.contains(&required), "missing env var {required}");
        }
    }

    #[test]
    fn insecure_oci_registries_render_into_worker_and_router_pods() {
        let (env, mut params) = fixture();
        params.oci_insecure_registries = vec![
            "localhost:5000".to_string(),
            "reg.internal:5000".to_string(),
        ];

        for d in [
            render_worker_deployment(&env, &env.revisions[0], &params),
            render_router_deployment(&env, &params),
        ] {
            let envs = d["spec"]["template"]["spec"]["containers"][0]["env"]
                .as_array()
                .unwrap();
            let var = envs
                .iter()
                .find(|e| e["name"] == "GREENTIC_OCI_INSECURE_REGISTRIES")
                .expect("the insecure-registries env var is rendered on both pods");
            assert_eq!(var["value"], "localhost:5000,reg.internal:5000");
        }
    }

    #[test]
    fn no_insecure_oci_registries_env_var_by_default() {
        let (env, params) = fixture();
        for d in [
            render_worker_deployment(&env, &env.revisions[0], &params),
            render_router_deployment(&env, &params),
        ] {
            let envs = d["spec"]["template"]["spec"]["containers"][0]["env"]
                .as_array()
                .unwrap();
            assert!(
                !envs
                    .iter()
                    .any(|e| e["name"] == "GREENTIC_OCI_INSECURE_REGISTRIES"),
                "the HTTPS-only default must not emit the insecure-registries var"
            );
        }
    }

    #[test]
    fn every_pod_spec_passes_the_restricted_hardening_gate() {
        let (env, params) = fixture();
        let pods = [
            render_worker_deployment(&env, &env.revisions[0], &params),
            render_router_deployment(&env, &params),
        ];
        for d in &pods {
            let pod = &d["spec"]["template"]["spec"];
            assert_eq!(pod["securityContext"]["runAsNonRoot"], true);
            assert_eq!(pod["securityContext"]["runAsUser"], 65532);
            assert_eq!(pod["securityContext"]["fsGroup"], 65532);
            assert_eq!(
                pod["securityContext"]["seccompProfile"]["type"],
                "RuntimeDefault"
            );
            let c = &pod["containers"][0];
            assert_eq!(c["securityContext"]["allowPrivilegeEscalation"], false);
            assert_eq!(c["securityContext"]["readOnlyRootFilesystem"], true);
            assert_eq!(c["securityContext"]["capabilities"]["drop"][0], "ALL");
            assert!(c["resources"]["requests"]["cpu"].is_string());
            assert!(c["resources"]["limits"]["memory"].is_string());
            assert!(c["readinessProbe"]["httpGet"]["path"].is_string());
            // The staging init container rides the same restricted profile.
            let ic = &pod["initContainers"][0];
            assert_eq!(ic["securityContext"]["allowPrivilegeEscalation"], false);
            assert_eq!(ic["securityContext"]["readOnlyRootFilesystem"], true);
            assert_eq!(ic["securityContext"]["capabilities"]["drop"][0], "ALL");
        }
    }

    #[test]
    fn router_is_ha_with_pdb_and_spread() {
        let (env, params) = fixture();
        let d = render_router_deployment(&env, &params);
        assert_eq!(d["spec"]["replicas"], 2);
        assert!(
            d["spec"]["template"]["spec"]["topologySpreadConstraints"][0]["topologyKey"]
                .is_string()
        );
        let pdb = render_router_pdb(&env, &params);
        assert_eq!(pdb["spec"]["minAvailable"], 1);
        // The router boots the bundle-less serve path and stages the env store
        // via the init container before the main container starts.
        let pod = &d["spec"]["template"]["spec"];
        assert_eq!(
            pod["containers"][0]["args"],
            serde_json::json!(["start", "--env", env.environment_id.as_str()])
        );
        assert_eq!(pod["initContainers"][0]["name"], "stage-env-store");
        let cm_volume = pod["volumes"]
            .as_array()
            .unwrap()
            .iter()
            .find(|v| v["configMap"]["name"] == ENV_STORE_CONFIG_MAP_NAME);
        assert!(
            cm_volume.is_some(),
            "router mounts the env-store ConfigMap as the init source"
        );
    }

    #[test]
    fn runtime_config_map_embeds_the_exact_projection() {
        let (env, params) = fixture();
        let cm = render_runtime_config_map(&env, &params);
        let payload = cm["data"]["runtime-config.json"].as_str().unwrap();
        let expected = serde_json::to_string(&materialize_runtime_config(&env)).unwrap();
        assert_eq!(payload, expected, "the ConfigMap IS the projection");
    }

    #[test]
    fn env_store_config_map_round_trips_to_a_loadable_environment() {
        let (env, params) = fixture();
        let cm = render_env_store_config_map(&env, &params);
        assert_eq!(cm["metadata"]["name"], ENV_STORE_CONFIG_MAP_NAME);
        let json = cm["data"]["environment.json"]
            .as_str()
            .expect("environment.json key");
        // The init container drops this verbatim where greentic-start's
        // bundle-less boot reads it; the store loads it back as a plain
        // Environment, so it must deserialize, validate, and keep its id.
        let parsed: Environment = serde_json::from_str(json).expect("environment.json parses");
        parsed.validate().expect("staged environment validates");
        assert_eq!(parsed.environment_id, env.environment_id);
    }

    #[test]
    fn both_pods_boot_the_bundle_less_serve_path() {
        let (env, params) = fixture();
        let pods = [
            render_worker_deployment(&env, &env.revisions[0], &params),
            render_router_deployment(&env, &params),
        ];
        for d in &pods {
            let pod = &d["spec"]["template"]["spec"];
            // `greentic-start start --env <id>` — the new-model serve boot.
            assert_eq!(
                pod["containers"][0]["args"],
                serde_json::json!(["start", "--env", env.environment_id.as_str()])
            );
            let envs = pod["containers"][0]["env"].as_array().unwrap();
            let find = |name: &str| envs.iter().find(|e| e["name"] == name).map(|e| &e["value"]);
            // Bind the pod IP, not the runtime's 127.0.0.1 default, or the
            // kubelet readiness probe never reaches /healthz.
            assert_eq!(find("GREENTIC_GATEWAY_LISTEN_ADDR").unwrap(), "0.0.0.0");
            assert_eq!(find("HOME").unwrap(), STAGE_HOME);
            // Cap the bundle-unpack rayon pool so backhand's parallel reader
            // doesn't starve under the pod's cpu quota (→ truncated `.gtpack`).
            assert_eq!(find("RAYON_NUM_THREADS").unwrap(), RAYON_THREADS);
            // The init container stages environment.json into the env store.
            let script = pod["initContainers"][0]["command"][2].as_str().unwrap();
            assert!(
                script.contains("environment.json"),
                "init stages environment.json"
            );
            assert!(
                script.contains(env.environment_id.as_str()),
                "into the env's store dir"
            );
        }
    }

    #[test]
    fn network_policies_default_deny_then_allowlist() {
        let (env, params) = fixture();
        let policies = render_network_policies(&env, &params);
        assert_eq!(policies[0]["metadata"]["name"], "gtc-default-deny");
        // Default-deny selects all pods, both directions, no allow rules.
        assert_eq!(policies[0]["spec"]["podSelector"], serde_json::json!({}));
        assert!(policies[0]["spec"].get("ingress").is_none());
        assert!(policies[0]["spec"].get("egress").is_none());
        let names: Vec<&str> = policies
            .iter()
            .map(|p| p["metadata"]["name"].as_str().unwrap())
            .collect();
        assert_eq!(
            names,
            [
                "gtc-default-deny",
                "gtc-allow-dns",
                "gtc-allow-router",
                "gtc-allow-workers",
                "gtc-allow-worker-egress",
                "gtc-allow-router-egress",
            ]
        );
        // Both pull-egress policies are always rendered; with no pullable routed
        // revision (the plain fixture) each egress is an empty deny rule and its
        // selector is scoped to this env.
        for name in ["gtc-allow-worker-egress", "gtc-allow-router-egress"] {
            let egress = policies
                .iter()
                .find(|p| p["metadata"]["name"] == name)
                .unwrap_or_else(|| panic!("{name} is always rendered"));
            assert_eq!(egress["spec"]["egress"], serde_json::json!([]));
            assert_eq!(
                egress["spec"]["podSelector"]["matchLabels"][ENV_LABEL],
                env.environment_id.as_str()
            );
        }
    }

    /// The fixture with a `bundle_source_uri` set on its first routed revision
    /// (r_warm, routed by the first traffic split) — i.e. a worker that pulls
    /// its bundle at boot.
    fn pullable_fixture() -> (Environment, K8sParams) {
        let (mut env, params) = fixture();
        env.revisions[0].bundle_source_uri =
            Some("oci://registry.example/bundles/demo@sha256:abc123".to_string());
        (env, params)
    }

    #[test]
    fn pull_egress_policies_render_for_a_pullable_routed_revision() {
        let (env, params) = pullable_fixture();
        let policies = render_network_policies(&env, &params);
        // BOTH the worker and the router boot `start --env` and pull, so each
        // gets an allow-all egress opening scoped to this env's pods of that
        // role. (The full name list is asserted by the plain-fixture test.)
        for role in ["worker", "router"] {
            let name = format!("gtc-allow-{role}-egress");
            let egress = policies
                .iter()
                .find(|p| p["metadata"]["name"] == name.as_str())
                .unwrap_or_else(|| panic!("{name} is always rendered"));
            let selector = &egress["spec"]["podSelector"]["matchLabels"];
            assert_eq!(
                selector["app.kubernetes.io/component"], role,
                "the allowance targets {role} pods"
            );
            assert_eq!(
                selector[ENV_LABEL],
                env.environment_id.as_str(),
                "scoped to this env's {role}s — not a sibling env sharing the namespace"
            );
            assert_eq!(egress["spec"]["policyTypes"], serde_json::json!(["Egress"]));
            // One egress rule with no `to`/`ports` == allow-all egress, so the
            // pod can reach a public or in-cluster registry on any port.
            assert_eq!(egress["spec"]["egress"], serde_json::json!([{}]));
        }
    }

    #[test]
    fn pull_egress_denies_when_the_pullable_revision_is_not_routed() {
        let (mut env, params) = fixture();
        // A revision carries a source uri but no traffic split routes it — the
        // pods never pull it (mirrors greentic-start's routed-only boot pull),
        // so both always-rendered policies stay in their deny shape.
        env.traffic_splits.clear();
        env.revisions[0].bundle_source_uri =
            Some("oci://registry.example/bundles/demo@sha256:abc123".to_string());
        let policies = render_network_policies(&env, &params);
        for role in ["worker", "router"] {
            let name = format!("gtc-allow-{role}-egress");
            let egress = policies
                .iter()
                .find(|p| p["metadata"]["name"] == name.as_str())
                .unwrap_or_else(|| panic!("{name} is always rendered"));
            assert_eq!(
                egress["spec"]["egress"],
                serde_json::json!([]),
                "an unrouted pullable revision leaves {role} egress denied"
            );
        }
    }

    #[test]
    fn pull_egress_policies_are_stable_env_level_objects() {
        // Always rendered regardless of pullability, so the env-level object
        // count does not change when a revision becomes pullable — reconcile
        // converges each egress rule in place rather than adding/removing the
        // object (it cannot prune env-level objects).
        let (pullable_env, params) = pullable_fixture();
        let (plain_env, _) = fixture();
        let pullable = render_environment_manifests(&pullable_env, &params);
        let plain = render_environment_manifests(&plain_env, &params);
        assert_eq!(
            pullable.len(),
            plain.len(),
            "object count is stable across pullability"
        );
        for set in [&pullable, &plain] {
            for name in ["gtc-allow-worker-egress", "gtc-allow-router-egress"] {
                assert!(
                    set.iter().any(|m| m["metadata"]["name"] == name),
                    "{name} is always present"
                );
            }
        }
    }

    #[test]
    fn env_store_config_map_preserves_bundle_source_uri() {
        let (env, params) = pullable_fixture();
        let cm = render_env_store_config_map(&env, &params);
        let json = cm["data"]["environment.json"]
            .as_str()
            .expect("environment.json key");
        let parsed: Environment = serde_json::from_str(json).expect("environment.json parses");
        // PR2b's boot seam reads `bundle_source_uri` off the staged revision to
        // decide what to pull — it must survive the ConfigMap round-trip.
        let routed = parsed
            .revisions
            .iter()
            .find(|r| r.revision_id == env.revisions[0].revision_id)
            .expect("routed revision present after round-trip");
        assert_eq!(
            routed.bundle_source_uri.as_deref(),
            Some("oci://registry.example/bundles/demo@sha256:abc123")
        );
    }

    #[test]
    fn environment_manifests_land_in_the_env_namespace_in_apply_order() {
        let (env, params) = fixture();
        let manifests = render_environment_manifests(&env, &params);
        // Namespace first, then both ConfigMaps (env-store + runtime-config)
        // before the router Deployment that stages / mounts them.
        assert_eq!(manifests[0]["kind"], "Namespace");
        assert_eq!(manifests[1]["kind"], "ConfigMap");
        assert_eq!(manifests[1]["metadata"]["name"], ENV_STORE_CONFIG_MAP_NAME);
        assert_eq!(manifests[2]["kind"], "ConfigMap");
        assert_eq!(manifests[2]["metadata"]["name"], RUNTIME_CONFIG_MAP_NAME);
        assert_eq!(manifests[3]["kind"], "Deployment");
        for m in &manifests[1..] {
            assert_eq!(
                m["metadata"]["namespace"],
                serde_json::json!(params.namespace),
                "{} must be namespaced",
                m["kind"]
            );
        }
    }

    // ---- from_answers + is_dns1123_label -----------------------------------

    #[test]
    fn from_answers_none_equals_for_env() {
        let env = build_fixture_env();
        assert_eq!(
            K8sParams::from_answers(&env, None).unwrap(),
            K8sParams::for_env(&env),
        );
    }

    #[test]
    fn from_answers_empty_object_equals_for_env() {
        let env = build_fixture_env();
        let empty = serde_json::json!({});
        assert_eq!(
            K8sParams::from_answers(&env, Some(&empty)).unwrap(),
            K8sParams::for_env(&env),
        );
    }

    #[test]
    fn from_answers_custom_namespace_propagates() {
        let env = build_fixture_env();
        let answers = serde_json::json!({"namespace": "my-ns"});
        let params = K8sParams::from_answers(&env, Some(&answers)).unwrap();
        assert_eq!(params.namespace, "my-ns");
        // Custom namespace propagates into every rendered object.
        let manifests = render_environment_manifests(&env, &params);
        assert_eq!(manifests[0]["metadata"]["name"], "my-ns");
        for m in &manifests[1..] {
            assert_eq!(
                m["metadata"]["namespace"].as_str(),
                Some("my-ns"),
                "{} namespace mismatch",
                m["kind"]
            );
        }
        // Worker objects too.
        for rev in &env.revisions {
            let workers = render_worker_manifests(&env, rev, &params);
            for w in &workers {
                assert_eq!(
                    w["metadata"]["namespace"].as_str(),
                    Some("my-ns"),
                    "worker {} namespace mismatch",
                    w["kind"]
                );
            }
        }
    }

    #[test]
    fn from_answers_custom_runtime_image() {
        let env = build_fixture_env();
        let answers = serde_json::json!({"runtime_image": "ghcr.io/acme/rt@sha256:abc123"});
        let params = K8sParams::from_answers(&env, Some(&answers)).unwrap();
        assert_eq!(params.runtime_image, "ghcr.io/acme/rt@sha256:abc123");
        // Router and worker containers use the image.
        let router = render_router_deployment(&env, &params);
        assert_eq!(
            router["spec"]["template"]["spec"]["containers"][0]["image"].as_str(),
            Some("ghcr.io/acme/rt@sha256:abc123")
        );
        let worker = render_worker_deployment(&env, &env.revisions[0], &params);
        assert_eq!(
            worker["spec"]["template"]["spec"]["containers"][0]["image"].as_str(),
            Some("ghcr.io/acme/rt@sha256:abc123")
        );
    }

    #[test]
    fn from_answers_router_replicas_override() {
        let env = build_fixture_env();
        let answers = serde_json::json!({"router_replicas": "4"});
        let params = K8sParams::from_answers(&env, Some(&answers)).unwrap();
        assert_eq!(params.router_replicas, 4);
    }

    #[test]
    fn from_answers_router_replicas_as_number() {
        let env = build_fixture_env();
        let answers = serde_json::json!({"router_replicas": 3});
        let params = K8sParams::from_answers(&env, Some(&answers)).unwrap();
        assert_eq!(params.router_replicas, 3);
    }

    #[test]
    fn from_answers_router_replicas_one_rejected() {
        let env = build_fixture_env();
        let answers = serde_json::json!({"router_replicas": "1"});
        let err = K8sParams::from_answers(&env, Some(&answers)).unwrap_err();
        assert!(err.contains("must be >= 2"), "got: {err}");
    }

    #[test]
    fn from_answers_oci_insecure_registries_comma_string() {
        let env = build_fixture_env();
        let answers =
            serde_json::json!({"oci_insecure_registries": " localhost:5000 , reg.internal:5000 ,"});
        let params = K8sParams::from_answers(&env, Some(&answers)).unwrap();
        assert_eq!(
            params.oci_insecure_registries,
            vec![
                "localhost:5000".to_string(),
                "reg.internal:5000".to_string()
            ]
        );
    }

    #[test]
    fn from_answers_oci_insecure_registries_array() {
        let env = build_fixture_env();
        let answers = serde_json::json!({
            "oci_insecure_registries": ["localhost:5000", "  ", "reg.internal:5000"]
        });
        let params = K8sParams::from_answers(&env, Some(&answers)).unwrap();
        assert_eq!(
            params.oci_insecure_registries,
            vec![
                "localhost:5000".to_string(),
                "reg.internal:5000".to_string()
            ]
        );
    }

    #[test]
    fn from_answers_oci_insecure_registries_non_string_entry_rejected() {
        let env = build_fixture_env();
        let answers = serde_json::json!({"oci_insecure_registries": ["localhost:5000", 5000]});
        let err = K8sParams::from_answers(&env, Some(&answers)).unwrap_err();
        assert!(err.contains("must be strings"), "got: {err}");
    }

    #[test]
    fn from_answers_unknown_key_rejected() {
        let env = build_fixture_env();
        let answers = serde_json::json!({"bogus_key": "value"});
        let err = K8sParams::from_answers(&env, Some(&answers)).unwrap_err();
        assert!(err.contains("bogus_key"), "got: {err}");
    }

    #[test]
    fn from_answers_invalid_namespace_rejected() {
        let env = build_fixture_env();
        // Uppercase is not valid RFC 1123.
        let answers = serde_json::json!({"namespace": "MyNS"});
        let err = K8sParams::from_answers(&env, Some(&answers)).unwrap_err();
        assert!(err.contains("not a valid RFC 1123"), "got: {err}");
    }

    #[test]
    fn from_answers_namespace_too_long_rejected() {
        let env = build_fixture_env();
        let long = "a".repeat(64);
        let answers = serde_json::json!({"namespace": long});
        let err = K8sParams::from_answers(&env, Some(&answers)).unwrap_err();
        assert!(err.contains("not a valid RFC 1123"), "got: {err}");
    }

    #[test]
    fn from_answers_non_object_rejected() {
        let env = build_fixture_env();
        let bad = serde_json::json!("not an object");
        let err = K8sParams::from_answers(&env, Some(&bad)).unwrap_err();
        assert!(err.contains("JSON object"), "got: {err}");
    }

    #[test]
    fn from_answers_empty_string_falls_back_to_default() {
        let env = build_fixture_env();
        let answers = serde_json::json!({
            "namespace": "",
            "runtime_image": "",
            "router_replicas": "",
        });
        assert_eq!(
            K8sParams::from_answers(&env, Some(&answers)).unwrap(),
            K8sParams::for_env(&env),
        );
    }

    #[test]
    fn from_answers_null_values_fall_back_to_default() {
        let env = build_fixture_env();
        let answers = serde_json::json!({
            "namespace": null,
            "runtime_image": null,
            "router_replicas": null,
            "kubeconfig_context": null,
        });
        assert_eq!(
            K8sParams::from_answers(&env, Some(&answers)).unwrap(),
            K8sParams::for_env(&env),
        );
    }

    #[test]
    fn from_answers_kubeconfig_context_ignored() {
        let env = build_fixture_env();
        let answers = serde_json::json!({"kubeconfig_context": "my-ctx"});
        let params = K8sParams::from_answers(&env, Some(&answers)).unwrap();
        // kubeconfig_context is a client-targeting knob, not a manifest
        // knob — it must not affect any rendered param.
        assert_eq!(params, K8sParams::for_env(&env));
    }

    #[test]
    fn is_dns1123_label_accepts_valid() {
        assert!(is_dns1123_label("my-ns"));
        assert!(is_dns1123_label("a"));
        assert!(is_dns1123_label("abc-123"));
        assert!(is_dns1123_label(&"a".repeat(63)));
    }

    #[test]
    fn is_dns1123_label_rejects_invalid() {
        assert!(!is_dns1123_label(""));
        assert!(!is_dns1123_label("-abc"));
        assert!(!is_dns1123_label("abc-"));
        assert!(!is_dns1123_label("ABC"));
        assert!(!is_dns1123_label("a.b"));
        assert!(!is_dns1123_label(&"a".repeat(64)));
    }

    /// Fixture env that binds a dev-store secrets pack (the predicate that
    /// turns on worker secret-staging + the env-level dev-store Secret).
    fn secrets_env() -> Environment {
        use greentic_deploy_spec::{EnvPackBinding, PackDescriptor, PackId};
        let mut env = build_fixture_env();
        env.packs.push(EnvPackBinding {
            slot: CapabilitySlot::Secrets,
            kind: PackDescriptor::try_new("greentic.secrets.dev-store@1.0.0").unwrap(),
            pack_ref: PackId::new("greentic.secrets.dev-store"),
            answers_ref: None,
            generation: 0,
            previous_binding_ref: None,
        });
        env
    }

    #[test]
    fn non_secrets_env_renders_no_dev_secret_or_staging() {
        let (env, params) = fixture();
        // No secrets pack → no dev-store Secret in the env-level set.
        let env_level = render_environment_manifests(&env, &params);
        assert!(
            env_level.iter().all(|o| o["kind"] != "Secret"),
            "a non-secrets env must not render a dev-store Secret"
        );
        // Worker keeps the single env-store init container, no dev-secrets volume.
        let d = render_worker_deployment(&env, &env.revisions[0], &params);
        let init = d["spec"]["template"]["spec"]["initContainers"]
            .as_array()
            .unwrap();
        assert_eq!(init.len(), 1);
        assert_eq!(init[0]["name"], "stage-env-store");
        let vols = d["spec"]["template"]["spec"]["volumes"].as_array().unwrap();
        assert!(vols.iter().all(|v| v["name"] != DEV_SECRETS_VOLUME));
    }

    #[test]
    fn secrets_env_renders_dev_secrets_secret_and_staging() {
        let env = secrets_env();
        let params = K8sParams::for_env(&env);

        // Env-level set gains exactly one Secret, appended after the policies.
        let env_level = render_environment_manifests(&env, &params);
        let secrets: Vec<&Value> = env_level.iter().filter(|o| o["kind"] == "Secret").collect();
        assert_eq!(secrets.len(), 1, "exactly one dev-store Secret");
        assert_eq!(secrets[0]["metadata"]["name"], DEV_SECRETS_SECRET_NAME);
        assert_eq!(
            env_level.last().unwrap()["kind"],
            "Secret",
            "Secret is appended last so it never shifts the index-pinned objects"
        );

        // Worker stages it: a second init container + the optional secret volume.
        let d = render_worker_deployment(&env, &env.revisions[0], &params);
        let init = d["spec"]["template"]["spec"]["initContainers"]
            .as_array()
            .unwrap();
        let names: Vec<&str> = init.iter().map(|c| c["name"].as_str().unwrap()).collect();
        assert_eq!(names, ["stage-env-store", "stage-dev-secrets"]);
        // The staging copy lands in the dev-store path greentic-start reads.
        let cmd = init[1]["command"][2].as_str().unwrap();
        assert!(
            cmd.contains(".greentic/dev/.dev.secrets.env"),
            "stages into the dev-store path: {cmd}"
        );

        let vols = d["spec"]["template"]["spec"]["volumes"].as_array().unwrap();
        let sv = vols
            .iter()
            .find(|v| v["name"] == DEV_SECRETS_VOLUME)
            .expect("dev-secrets volume present");
        assert_eq!(sv["secret"]["secretName"], DEV_SECRETS_SECRET_NAME);
        assert_eq!(
            sv["secret"]["optional"], true,
            "an absent Secret must not block worker boot"
        );

        // The worker pod template carries a content-hash annotation so K8s
        // rolls pods when the dev-store changes on a subsequent reconcile.
        let ann = &d["spec"]["template"]["metadata"]["annotations"];
        assert!(
            ann["greentic.ai/dev-store-hash"].is_string(),
            "secrets env worker must carry dev-store hash annotation"
        );
    }

    #[test]
    fn dev_store_hash_changes_trigger_rolling_restart() {
        let env = secrets_env();
        let mut params = K8sParams::for_env(&env);

        // No data → sentinel hash.
        let d1 = render_worker_deployment(&env, &env.revisions[0], &params);
        let h1 = d1["spec"]["template"]["metadata"]["annotations"]["greentic.ai/dev-store-hash"]
            .as_str()
            .unwrap()
            .to_string();

        // With data → different hash.
        params.dev_secrets_data = Some("Zm9vYmFy".to_string());
        let d2 = render_worker_deployment(&env, &env.revisions[0], &params);
        let h2 = d2["spec"]["template"]["metadata"]["annotations"]["greentic.ai/dev-store-hash"]
            .as_str()
            .unwrap()
            .to_string();
        assert_ne!(h1, h2, "changing dev-store data must change the hash");

        // Same data → same hash (idempotent).
        let d3 = render_worker_deployment(&env, &env.revisions[0], &params);
        let h3 = d3["spec"]["template"]["metadata"]["annotations"]["greentic.ai/dev-store-hash"]
            .as_str()
            .unwrap()
            .to_string();
        assert_eq!(h2, h3, "same data must produce the same hash");
    }

    #[test]
    fn non_secrets_env_has_no_dev_store_hash_annotation() {
        let (env, params) = fixture();
        let d = render_worker_deployment(&env, &env.revisions[0], &params);
        let ann = &d["spec"]["template"]["metadata"]["annotations"];
        assert!(
            ann.is_null(),
            "non-secrets env worker must not carry annotations"
        );
    }

    #[test]
    fn dev_secrets_secret_carries_data_only_when_provided() {
        let env = secrets_env();
        let mut params = K8sParams::for_env(&env);

        // Preview path (no bytes): structurally-complete Secret, empty data.
        let empty = render_dev_secrets_secret(&env, &params);
        assert_eq!(empty["data"].as_object().unwrap().len(), 0);
        assert_eq!(empty["type"], "Opaque");

        // Reconcile path: the base64 dev-store is carried under `.dev.secrets.env`.
        params.dev_secrets_data = Some("Zm9vYmFy".to_string());
        let filled = render_dev_secrets_secret(&env, &params);
        assert_eq!(filled["data"][".dev.secrets.env"], "Zm9vYmFy");
    }

    #[test]
    fn worker_tunnel_flags_only_when_cloudflared() {
        let (env, mut params) = fixture();
        let id = env.environment_id.as_str();

        // Default: no tunnel flags — args match the shared bundle-less boot.
        assert_eq!(params.tunnel, TunnelMode::Off);
        let d = render_worker_deployment(&env, &env.revisions[0], &params);
        assert_eq!(
            d["spec"]["template"]["spec"]["containers"][0]["args"],
            json!(["start", "--env", id])
        );

        // Cloudflared: the worker spawns the in-image quick tunnel.
        params.tunnel = TunnelMode::Cloudflared;
        let d = render_worker_deployment(&env, &env.revisions[0], &params);
        assert_eq!(
            d["spec"]["template"]["spec"]["containers"][0]["args"],
            json!([
                "start",
                "--env",
                id,
                "--cloudflared",
                "on",
                "--cloudflared-binary",
                CLOUDFLARED_BINARY
            ])
        );

        // The router never tunnels, regardless of the answer.
        let r = render_router_deployment(&env, &params);
        assert_eq!(
            r["spec"]["template"]["spec"]["containers"][0]["args"],
            json!(["start", "--env", id])
        );
    }

    #[test]
    fn from_answers_parses_tunnel() {
        let env = build_fixture_env();
        let cf = K8sParams::from_answers(&env, Some(&json!({"tunnel": "cloudflared"}))).unwrap();
        assert_eq!(cf.tunnel, TunnelMode::Cloudflared);
        let off = K8sParams::from_answers(&env, Some(&json!({"tunnel": "off"}))).unwrap();
        assert_eq!(off.tunnel, TunnelMode::Off);
        // Absent → default off.
        let none = K8sParams::from_answers(&env, Some(&json!({}))).unwrap();
        assert_eq!(none.tunnel, TunnelMode::Off);
        // Invalid → fail closed.
        assert!(K8sParams::from_answers(&env, Some(&json!({"tunnel": "ngrok"}))).is_err());
    }

    // ---- Vault workload-identity backend (Phase E.3) -----------------------

    /// A Vault backend whose mounts/prefix/transit all match the provider
    /// defaults — so only the selector, addr, and role render as pod env.
    fn vault_backend() -> VaultBackend {
        VaultBackend {
            addr: "http://vault.vault.svc:8200".to_string(),
            k8s_role: "greentic-worker".to_string(),
            kv_mount: VAULT_DEFAULT_KV_MOUNT.to_string(),
            kv_prefix: VAULT_DEFAULT_KV_PREFIX.to_string(),
            auth_mount: VAULT_DEFAULT_AUTH_MOUNT.to_string(),
            transit_mount: VAULT_DEFAULT_TRANSIT_MOUNT.to_string(),
            transit_key: VAULT_DEFAULT_TRANSIT_KEY.to_string(),
            namespace: None,
        }
    }

    fn vault_params(env: &Environment) -> K8sParams {
        K8sParams {
            secrets_backend: SecretsBackend::Vault(vault_backend()),
            ..K8sParams::for_env(env)
        }
    }

    /// The worker pod's env var value for `name`, if present.
    fn worker_env_value<'a>(d: &'a Value, name: &str) -> Option<&'a str> {
        d["spec"]["template"]["spec"]["containers"][0]["env"]
            .as_array()
            .unwrap()
            .iter()
            .find(|e| e["name"] == name)
            .and_then(|e| e["value"].as_str())
    }

    #[test]
    fn vault_worker_carries_identity_and_connection_env() {
        let env = build_fixture_env();
        let params = vault_params(&env);
        let d = render_worker_deployment(&env, &env.revisions[0], &params);

        // The pod authenticates to Vault as its ServiceAccount (no rendered
        // material — the projected SA token is its credential).
        assert_eq!(
            d["spec"]["template"]["spec"]["serviceAccountName"],
            WORKER_SERVICE_ACCOUNT
        );
        // Backend selector + the required connection vars greentic-start reads.
        assert_eq!(
            worker_env_value(&d, "GREENTIC_SECRETS_BACKEND"),
            Some("vault")
        );
        assert_eq!(
            worker_env_value(&d, "VAULT_ADDR"),
            Some("http://vault.vault.svc:8200")
        );
        assert_eq!(
            worker_env_value(&d, "VAULT_K8S_ROLE"),
            Some("greentic-worker")
        );

        // No dev-store material crosses into the cluster: only the env-store
        // init container, no dev-secrets volume, no dev-store-hash annotation.
        let init = d["spec"]["template"]["spec"]["initContainers"]
            .as_array()
            .unwrap();
        assert_eq!(init.len(), 1);
        assert_eq!(init[0]["name"], "stage-env-store");
        let vols = d["spec"]["template"]["spec"]["volumes"].as_array().unwrap();
        assert!(vols.iter().all(|v| v["name"] != DEV_SECRETS_VOLUME));
        assert!(
            d["spec"]["template"]["metadata"]["annotations"].is_null(),
            "vault worker carries no dev-store hash annotation"
        );
    }

    #[test]
    fn vault_default_connection_vars_are_omitted() {
        let env = build_fixture_env();
        let params = vault_params(&env); // all-default mounts/prefix/transit
        let d = render_worker_deployment(&env, &env.revisions[0], &params);
        // Defaults match the provider, so the worker omits them — an absent var
        // and the provider default resolve identically at runtime.
        for absent in [
            "VAULT_KV_MOUNT",
            "VAULT_KV_PREFIX",
            "VAULT_K8S_MOUNT",
            "VAULT_TRANSIT_MOUNT",
            "VAULT_TRANSIT_KEY",
            "VAULT_NAMESPACE",
        ] {
            assert_eq!(
                worker_env_value(&d, absent),
                None,
                "{absent} default must be omitted"
            );
        }
    }

    #[test]
    fn vault_non_default_connection_vars_are_emitted() {
        let env = build_fixture_env();
        let mut backend = vault_backend();
        backend.kv_mount = "kv".to_string();
        backend.kv_prefix = "tenant-a".to_string();
        backend.auth_mount = "k8s-eu".to_string();
        backend.transit_mount = "tr".to_string();
        backend.transit_key = "rk".to_string();
        backend.namespace = Some("admin/team".to_string());
        let params = K8sParams {
            secrets_backend: SecretsBackend::Vault(backend),
            ..K8sParams::for_env(&env)
        };
        let d = render_worker_deployment(&env, &env.revisions[0], &params);
        assert_eq!(worker_env_value(&d, "VAULT_KV_MOUNT"), Some("kv"));
        assert_eq!(worker_env_value(&d, "VAULT_KV_PREFIX"), Some("tenant-a"));
        assert_eq!(worker_env_value(&d, "VAULT_K8S_MOUNT"), Some("k8s-eu"));
        assert_eq!(worker_env_value(&d, "VAULT_TRANSIT_MOUNT"), Some("tr"));
        assert_eq!(worker_env_value(&d, "VAULT_TRANSIT_KEY"), Some("rk"));
        assert_eq!(worker_env_value(&d, "VAULT_NAMESPACE"), Some("admin/team"));
    }

    #[test]
    fn vault_router_has_no_secrets_identity() {
        let env = build_fixture_env();
        let params = vault_params(&env);
        let r = render_router_deployment(&env, &params);
        // The router routes traffic; it never resolves `secret://`, so it gets
        // neither the Vault identity nor the connection env.
        assert!(
            r["spec"]["template"]["spec"]
                .get("serviceAccountName")
                .is_none(),
            "router must not carry the Vault ServiceAccount"
        );
        let envs = r["spec"]["template"]["spec"]["containers"][0]["env"]
            .as_array()
            .unwrap();
        assert!(
            envs.iter()
                .all(|e| e["name"] != "GREENTIC_SECRETS_BACKEND" && e["name"] != "VAULT_ADDR"),
            "router carries no Vault connection env"
        );
    }

    #[test]
    fn vault_env_renders_service_account_and_no_secret() {
        let env = build_fixture_env();
        let params = vault_params(&env);
        let manifests = render_environment_manifests(&env, &params);
        // The worker ServiceAccount is the env-level secrets object under Vault.
        let sa: Vec<&Value> = manifests
            .iter()
            .filter(|o| o["kind"] == "ServiceAccount")
            .collect();
        assert_eq!(sa.len(), 1, "exactly one worker ServiceAccount");
        assert_eq!(sa[0]["metadata"]["name"], WORKER_SERVICE_ACCOUNT);
        assert_eq!(sa[0]["metadata"]["namespace"], json!(params.namespace));
        assert!(
            manifests.iter().all(|o| o["kind"] != "Secret"),
            "no secret values are rendered into the cluster under Vault"
        );
    }

    #[test]
    fn vault_opens_worker_egress_not_router() {
        let env = build_fixture_env();
        let params = vault_params(&env); // no pullable routed revision
        let policies = render_network_policies(&env, &params);
        let egress = |name: &str| {
            policies
                .iter()
                .find(|p| p["metadata"]["name"] == name)
                .map(|p| p["spec"]["egress"].clone())
                .unwrap()
        };
        // The worker needs egress to reach Vault; the router does not resolve
        // secrets, so its egress stays denied (no pullable revision either).
        assert_eq!(egress("gtc-allow-worker-egress"), json!([{}]));
        assert_eq!(egress("gtc-allow-router-egress"), json!([]));
    }
}