k8s/python: How do I read a secret using the Kubernetes Python client?
- Install Kubernetes client for python
- Now you can pull the secret. For example secret name -
mysql-pass
, namespace -default
from kubernetes import client, configconfig.load_kube_config()v1 = client.CoreV1Api()secret = v1.read_namespaced_secret("mysql-pass", "default")print(secret)
- If you need to extract decoded password from the secret
from kubernetes import client, configimport base64import sys config.load_kube_config()v1 = client.CoreV1Api()sec = str(v1.read_namespaced_secret("mysql-pass", "default").data)pas = base64.b64decode(sec.strip().split()[1].translate(None, '}\''))print(pas)
Hope this will help.
If you use kubernetes client api it will give you response as a dict datatype and you might not need to do spiting etc, You can say something like this,
from kubernetes import client, configimport base64config.load_kube_config()v1 = client.CoreV1Api()sec = v1.read_namespaced_secret("default-token-rsbq7", "default").datacert = base64.b64decode(sec["ca.crt"])print(cert)