@@ -332,3 +332,68 @@ def execInPod(core_v1_api: client.CoreV1Api, pod_name: str, namespace, command:
332332 logger .debug (f"stdout: \n ----------------------------------------------------------------\n { stdout } \n ----------------------------------------------------------------\n " )
333333
334334 return stdout
335+
336+
337+ def updateGlobalPullSecret (dynClient : DynamicClient , registryUrl : str , username : str , password : str ) -> dict :
338+ """
339+ Update the global pull secret in openshift-config namespace with new registry credentials.
340+
341+ Args:
342+ dynClient: OpenShift Dynamic Client
343+ registryUrl: Registry URL (e.g., "myregistry.com:5000")
344+ username: Registry username
345+ password: Registry password
346+
347+ Returns:
348+ dict: Updated secret information
349+ """
350+ import json
351+ import base64
352+
353+ logger .info (f"Updating global pull secret with credentials for { registryUrl } " )
354+
355+ # Get the existing pull secret
356+ secretsAPI = dynClient .resources .get (api_version = "v1" , kind = "Secret" )
357+ try :
358+ pullSecret = secretsAPI .get (name = "pull-secret" , namespace = "openshift-config" )
359+ except NotFoundError :
360+ raise Exception ("Global pull-secret not found in openshift-config namespace" )
361+
362+ # Convert to dict to allow modifications
363+ secretDict = pullSecret .to_dict ()
364+
365+ # Decode the existing dockerconfigjson
366+ dockerConfigJson = secretDict ['data' ].get (".dockerconfigjson" , "" )
367+ dockerConfig = json .loads (base64 .b64decode (dockerConfigJson ).decode ('utf-8' ))
368+
369+ # Create auth string (username:password base64 encoded)
370+ authString = base64 .b64encode (f"{ username } :{ password } " .encode ('utf-8' )).decode ('utf-8' )
371+
372+ # Add or update the registry credentials
373+ if "auths" not in dockerConfig :
374+ dockerConfig ["auths" ] = {}
375+
376+ dockerConfig ["auths" ][registryUrl ] = {
377+ "username" : username ,
378+ "password" : password ,
379+ "email" : username ,
380+ "auth" : authString
381+ }
382+
383+ # Encode back to base64
384+ updatedDockerConfig = base64 .b64encode (json .dumps (dockerConfig ).encode ('utf-8' )).decode ('utf-8' )
385+
386+ # Update the secret dict
387+ secretDict ['data' ][".dockerconfigjson" ] = updatedDockerConfig
388+
389+ # Apply the updated secret
390+ updatedSecret = secretsAPI .apply (body = secretDict , namespace = "openshift-config" )
391+
392+ logger .info (f"Successfully updated global pull secret with credentials for { registryUrl } " )
393+
394+ return {
395+ "name" : updatedSecret .metadata .name ,
396+ "namespace" : updatedSecret .metadata .namespace ,
397+ "registry" : registryUrl ,
398+ "changed" : True
399+ }
0 commit comments