1212import yaml
1313
1414from azext_confcom import config
15- from azext_confcom .lib .platform import PRIVILEDGED_CAPABILITIES , VN2_MOUNTS , VN2_PRIVILEGED_MOUNTS , VN2_WORKLOAD_IDENTITY_ENV_RULES , VN2_WORKLOAD_IDENTITY_MOUNTS
15+ from azext_confcom .lib .platform import (
16+ PRIVILEDGED_CAPABILITIES ,
17+ VN2_PRIVILEGED_MOUNTS ,
18+ VN2_WORKLOAD_IDENTITY_ENV_RULES ,
19+ VN2_WORKLOAD_IDENTITY_MOUNTS ,
20+ )
1621from azext_confcom .lib .policy import ContainerUser
17- from azext_confcom .lib .containers import from_image as container_from_image , merge_containers , merge_containers
22+ from azext_confcom .lib .containers import (
23+ from_image as container_from_image ,
24+ merge_containers ,
25+ )
1826
1927
2028def find_vn2_containers (vn2_template ):
@@ -41,7 +49,7 @@ def vn2_container_env_rules(template: dict, container: dict, template_variables:
4149 is_special = re .match ('^===VIRTUALNODE2.CC.THIM.(.+)===$' , env_var .get ('value' ))
4250 yield {
4351 "pattern" : f"{ env_var .get ('name' )} ={ '.*' if is_special else env_var .get ('value' )} " ,
44- "strategy" : "re2" if is_special else "string" ,
52+ "strategy" : "re2" if is_special else "string" ,
4553 "required" : False ,
4654 }
4755
@@ -76,8 +84,17 @@ def vn2_container_env_rules(template: dict, container: dict, template_variables:
7684
7785 elif "resourceFieldRef" in env_var .get ('valueFrom' ):
7886 ref = env_var .get ('valueFrom' ).get ("resourceFieldRef" , {})
79- container = next (c for c in template ["spec" ]["containers" ] if c .get ("name" ) == ref .get ("containerName" ))
80- value = container .get ("resources" , {})
87+ ref_container_name = ref .get ("containerName" ) or container .get ("name" )
88+ ref_container = next (
89+ (
90+ c for c in template ["spec" ]["containers" ]
91+ if c .get ("name" ) == ref_container_name
92+ ),
93+ None ,
94+ )
95+ if ref_container is None :
96+ continue
97+ value = ref_container .get ("resources" , {})
8198 for part in ref ["resource" ].split ("." ):
8299 value = value .get (part , {})
83100 yield {
@@ -116,6 +133,7 @@ def vn2_container_mounts(template: dict, container: dict) -> list[dict]:
116133 for m in container .get ("volumeMounts" , [])
117134 ]
118135
136+
119137def containers_from_vn2 (
120138 template : str ,
121139 container_name : str
@@ -143,10 +161,14 @@ def containers_from_vn2(
143161 elif kind in ["Pod" , "Deployment" , "StatefulSet" , "DaemonSet" , "Job" , "CronJob" , "ReplicaSet" ]:
144162 for container in find_vn2_containers (doc ):
145163 if container .get ("name" ) == container_name :
146- assert template_container is None and template_doc is None , f"Multiple containers with name { container_name } found."
164+ if template_container is not None or template_doc is not None :
165+ raise AssertionError (
166+ f"Multiple containers with name { container_name } found."
167+ )
147168 template_container = container
148169 template_doc = doc
149- assert template_container is not None , f"No containers with name { container_name } found."
170+ if template_container is None :
171+ raise AssertionError (f"No containers with name { container_name } found." )
150172
151173 image_container_def = container_from_image (template_container .get ("image" ), platform = "vn2" )
152174
@@ -159,20 +181,25 @@ def containers_from_vn2(
159181 "pattern" : rule .get ("pattern" ) or f"{ rule .get ('name' )} ={ rule .get ('value' )} " ,
160182 "strategy" : rule .get ("strategy" , "string" ),
161183 "required" : rule .get ("required" , False ),
162- } for rule in (
163- config .OPENGCS_ENV_RULES
164- + config .FABRIC_ENV_RULES
165- + config .MANAGED_IDENTITY_ENV_RULES
166- + config .ENABLE_RESTART_ENV_RULE
167- + config .VIRTUAL_NODE_ENV_RULES
168- )]
184+ }
185+ for rule in (
186+ config .OPENGCS_ENV_RULES
187+ + config .FABRIC_ENV_RULES
188+ + config .MANAGED_IDENTITY_ENV_RULES
189+ + config .ENABLE_RESTART_ENV_RULE
190+ + config .VIRTUAL_NODE_ENV_RULES
191+ )
192+ ]
169193 + list (vn2_container_env_rules (template_doc , template_container , variables ))
170194 ),
171195 "mounts" : vn2_container_mounts (template_doc , template_container ),
172196 }
173197
174198 # Parse security context
175- security_context = template_doc .get ("spec" , {}).get ("securityContext" , {}) | template_container .get ("securityContext" , {})
199+ security_context = (
200+ template_doc .get ("spec" , {}).get ("securityContext" , {})
201+ | template_container .get ("securityContext" , {})
202+ )
176203 if security_context .get ("privileged" , False ):
177204 template_container_def ["allow_elevated" ] = True
178205 template_container_def ["mounts" ] += VN2_PRIVILEGED_MOUNTS
@@ -192,7 +219,9 @@ def containers_from_vn2(
192219 }]
193220
194221 if security_context .get ("seccompProfile" ):
195- template_container_def ["seccomp_profile_sha256" ] = sha256 (base64 .b64decode (security_context .get ("seccompProfile" ))).hexdigest ()
222+ template_container_def ["seccomp_profile_sha256" ] = sha256 (
223+ base64 .b64decode (security_context .get ("seccompProfile" ))
224+ ).hexdigest ()
196225
197226 if security_context .get ("allowPrivilegeEscalation" ) is False :
198227 template_container_def ["no_new_privileges" ] = True
0 commit comments