【问题标题】:k8s/python: How do I read a secret using the Kubernetes Python client?k8s/python:如何使用 Kubernetes Python 客户端读取密钥?
【发布时间】:2019-08-05 04:11:16
【问题描述】:
【问题讨论】:
标签:
python
mongodb
kubernetes
jupyter-notebook
【解决方案1】:
- 为python安装Kubernetes client
- 现在您可以提取秘密了。例如秘密名称 -
mysql-pass,命名空间 - default
from kubernetes import client, config
config.load_kube_config()
v1 = client.CoreV1Api()
secret = v1.read_namespaced_secret("mysql-pass", "default")
print(secret)
- 如果您需要从密钥中提取解码后的密码
from kubernetes import client, config
import base64
import 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)
希望这会有所帮助。
【解决方案2】:
如果你使用 kubernetes 客户端 api,它会给你一个 dict 数据类型的响应,你可能不需要做 spiting 等,你可以这样说,
from kubernetes import client, config
import base64
config.load_kube_config()
v1 = client.CoreV1Api()
sec = v1.read_namespaced_secret("default-token-rsbq7", "default").data
cert = base64.b64decode(sec["ca.crt"])
print(cert)