@@ -1307,12 +1307,45 @@ fn add_git_sync_resources(
13071307
13081308/// Convert user-supplied `envOverrides` into a list of [`EnvVar`]s.
13091309fn env_vars_from_overrides ( env_overrides : & HashMap < String , String > ) -> Vec < EnvVar > {
1310+ // Collect into a `BTreeMap` first so the env vars come out in a deterministic (sorted) order;
1311+ // `HashMap` iteration order is randomised per instance and would otherwise churn the containers
1312+ // this feeds between reconciles. Mirrors the override handling in `env_vars.rs`.
13101313 env_overrides
13111314 . iter ( )
1315+ . collect :: < BTreeMap < _ , _ > > ( )
1316+ . into_iter ( )
13121317 . map ( |( k, v) | EnvVar {
13131318 name : k. clone ( ) ,
13141319 value : Some ( v. clone ( ) ) ,
13151320 ..EnvVar :: default ( )
13161321 } )
13171322 . collect ( )
13181323}
1324+
1325+ #[ cfg( test) ]
1326+ mod tests {
1327+ use std:: collections:: HashMap ;
1328+
1329+ use super :: env_vars_from_overrides;
1330+
1331+ /// The env vars must come out in a deterministic (sorted-by-name) order. `env_overrides` is a
1332+ /// `HashMap`, whose iteration order is randomised per instance, so iterating it directly would
1333+ /// vary the rendered env array between reconciles and churn the git-sync containers it feeds.
1334+ #[ test]
1335+ fn env_vars_from_overrides_are_sorted_by_name ( ) {
1336+ let overrides = HashMap :: from ( [
1337+ ( "CHARLIE" . to_string ( ) , "3" . to_string ( ) ) ,
1338+ ( "ALPHA" . to_string ( ) , "1" . to_string ( ) ) ,
1339+ ( "ECHO" . to_string ( ) , "5" . to_string ( ) ) ,
1340+ ( "BRAVO" . to_string ( ) , "2" . to_string ( ) ) ,
1341+ ( "DELTA" . to_string ( ) , "4" . to_string ( ) ) ,
1342+ ] ) ;
1343+
1344+ let names: Vec < String > = env_vars_from_overrides ( & overrides)
1345+ . into_iter ( )
1346+ . map ( |env_var| env_var. name )
1347+ . collect ( ) ;
1348+
1349+ assert_eq ! ( names, [ "ALPHA" , "BRAVO" , "CHARLIE" , "DELTA" , "ECHO" ] ) ;
1350+ }
1351+ }
0 commit comments