To utilize Python FastAPI for retrieving secret values from a K8 cluster, you can use the kubernetes module in Python to interact with the Kubernetes API and retrieve the secrets.
pip install kubernetes
pip install fastapi
from kubernetes import client, config
from fastapi import FastAPI
config.load_incluster_config()
v1 = client.CoreV1Api()
secrets = v1.list_secret_for_all_namespaces().items
app = FastAPI()
@app.get("/secrets")
def get_secrets():
secrets = []
v1 = client.CoreV1Api()
secrets = v1.list_secret_for_all_namespaces().items
return secrets
# Get secrets for the 'default' namespace
secrets = v1.list_namespaced_secret(namespace='default').items
# Get secrets of type 'Opaque'
secrets = [s for s in secrets if s.type == 'Opaque']
if __name__ == "__main__":
uvicorn.run(app, host="0.0.0.0", port=8000)
You can then query the endpoint to retrieve the secrets:
curl http://localhost:8000/secrets
Asked: 2023-06-01 20:41:39 +0000
Seen: 12 times
Last updated: Jun 01 '23