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
Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss
Asked: 2023-06-01 20:41:39 +0000
Seen: 12 times
Last updated: Jun 01 '23
How can popen() be used to direct streaming data to TAR?
In Python, can a string be utilized to retrieve a dataframe that has the same name as the string?
What is the method for merging field value and text into a singular line for display?
What is the method for programmatic access to a time series?