Ask Your Question
3

What is the way to utilize Python FastAPI for retrieving secret values from a K8 cluster?

asked 2023-06-01 20:41:39 +0000

huitzilopochtli gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
2

answered 2023-06-01 20:52:02 +0000

scrum gravatar image

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.

  1. Install the dependencies:
pip install kubernetes
pip install fastapi
  1. Import the necessary modules:
from kubernetes import client, config
from fastapi import FastAPI
  1. Load the Kubernetes configuration:
config.load_incluster_config()
  1. Use a Kubernetes client to retrieve the secrets:
v1 = client.CoreV1Api()
secrets = v1.list_secret_for_all_namespaces().items
  1. Create a FastAPI endpoint to retrieve the secrets:
app = FastAPI()

@app.get("/secrets")
def get_secrets():
    secrets = []
    v1 = client.CoreV1Api()
    secrets = v1.list_secret_for_all_namespaces().items
    return secrets
  1. Optionally, you can filter the secrets based on specific criteria such as the namespace or the secret type:
# 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']
  1. Run the FastAPI application:
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
edit flag offensive delete link more

Your Answer

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

Add Answer


Question Tools

Stats

Asked: 2023-06-01 20:41:39 +0000

Seen: 12 times

Last updated: Jun 01 '23