Skip to content

Commit f9e5434

Browse files
[patch] Add function to update pull secret
1 parent 25b9131 commit f9e5434

1 file changed

Lines changed: 63 additions & 0 deletions

File tree

src/mas/devops/ocp.py

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

0 commit comments

Comments
 (0)