@@ -78,12 +78,14 @@ def update_mounts(name, spec, namespace, logger, **kwargs):
7878 """This handler will idempotently update the volume mounts."""
7979 del kwargs
8080 logger .info ("Will idempotently update volume mounts." )
81+ # import ipdb;ipdb.set_trace()
8182 for (
8283 manifest ,
8384 mounted ,
8485 mount_path ,
8586 sub_path ,
8687 entrypoints ,
88+ images ,
8789 ) in iter_mounts_and_manifests (namespace , spec ["mounts" ]):
8890 m_kind , m_name = manifest ["kind" ], manifest ["metadata" ]["name" ]
8991
@@ -120,11 +122,13 @@ def update_mounts(name, spec, namespace, logger, **kwargs):
120122 sub_path = sub_path ,
121123 )
122124 update_entrypoints (manifest = manifest , entrypoints = entrypoints )
125+ update_images (manifest = manifest , images = images )
123126 kubectl_apply (namespace = namespace , manifest = manifest , logger = logger )
124127 else :
125128 if spec .get ("mode" ) == "modify" :
126129 remove_mount (manifest = manifest , volume_name = name )
127130 restore_entrypoints (manifest = manifest , entrypoints = entrypoints )
131+ restore_images (manifest = manifest )
128132 logger .info ("Idempotently unmounting volume to %s:%s" , m_kind , m_name )
129133 kubectl_apply (namespace = namespace , manifest = manifest , logger = logger )
130134 elif kubectl_get (namespace = namespace , kind = m_kind , labels = {"devenv" : name }):
@@ -222,7 +226,7 @@ def kubectl_get(namespace: str, kind: str, labels: dict[str, str]) -> list[dict]
222226def iter_mounts_and_manifests (namespace , mounts ):
223227 for mount in mounts :
224228 assert isinstance (mount , dict ), repr (mount )
225- for attr in ("kind" , "labels" , "mountPath" , "mounted" , "entrypoints" ):
229+ for attr in ("kind" , "labels" , "mountPath" , "mounted" , "entrypoints" , "images" ):
226230 assert attr in mount , mount
227231 if mount ["kind" ].lower () != "deployment" :
228232 raise NotImplementedError ("Only deployments are supported." )
@@ -235,6 +239,7 @@ def iter_mounts_and_manifests(namespace, mounts):
235239 mount ["mountPath" ],
236240 mount .get ("subPath" , "" ),
237241 mount ["entrypoints" ],
242+ mount ["images" ],
238243 )
239244
240245
@@ -258,6 +263,7 @@ def add_mount(
258263 volumes .append (
259264 {"name" : volume_name , "persistentVolumeClaim" : {"claimName" : pvc_name }}
260265 )
266+ manifest ["spec" ]["template" ]["spec" ]["volumes" ] = volumes
261267 # Configure volume mount.
262268 for container in manifest ["spec" ]["template" ]["spec" ]["containers" ]:
263269 mounts = container .get ("volumeMounts" , [])
@@ -281,7 +287,7 @@ def add_mount(
281287
282288def remove_mount (* , manifest : dict , volume_name : str ) -> None :
283289 # Remove volume.
284- volumes = manifest ["spec" ]["template" ]["spec" ][ "volumes" ]
290+ volumes = manifest ["spec" ]["template" ]["spec" ]. get ( "volumes" , [])
285291 for i , volume in reversed (list (enumerate (volumes ))):
286292 if volume ["name" ] == volume_name :
287293 volumes .pop (i )
@@ -311,3 +317,26 @@ def restore_entrypoints(*, manifest: dict, entrypoints: dict) -> None:
311317 if container .get ("command" ):
312318 del container ["command" ]
313319 del container ["args" ]
320+
321+
322+ def update_images (* , manifest : dict , images : dict ) -> None :
323+ for container in manifest ["spec" ]["template" ]["spec" ]["containers" ]:
324+ image = images .get (container ["name" ])
325+ if image is None :
326+ continue
327+ manifest ["metadata" ]["annotations" ][
328+ f"devenv.dell.com/{ container ['name' ]} "
329+ ] = container ["image" ]
330+ container ["image" ] = image
331+
332+
333+ def restore_images (* , manifest : dict ) -> None :
334+ for container in manifest ["spec" ]["template" ]["spec" ]["containers" ]:
335+ image = manifest ["metadata" ]["annotations" ].get (
336+ f"devenv.dell.com/{ container ['name' ]} "
337+ )
338+ if image :
339+ container ["image" ] = image
340+ del manifest ["metadata" ]["annotations" ][
341+ f"devenv.dell.com/{ container ['name' ]} "
342+ ]
0 commit comments