Pydaconf plugin for Kubernetes Secrets
For more information about Pydaconf see the Docs.
Install using pip install pydaconf-plugins-k8s
- Create config file in toml, yaml or json
secret: K8S_SECRET:///secret_name=my-secret,key=username
- Create Pydantic Model and load the configuration
from pydaconf import PydaConf
from pydantic import BaseModel
class Config(BaseModel):
secret: str
provider = PydaConf[Config]()
provider.from_file("config.yaml")
print(provider.config.secret)
Parameters can be passed in the configuration value in the following format:
K8S_SECRET:///param1=value1,param2=value2
Parameter | Description |
---|---|
secret_name | The name of the kubernetes secret |
key | The key of the specific secret element |
namespace | If not provided, the plugin will try to load it from the POD file system. If not available, the 'default' namespace will be used |
watch | Watch the secret for any updates. |
The plugin can monitor secret update events and notify the on_update_callback. It starts a background thread and subscribes to updates. When an update occurs, it notifies the on_update_callback, ensuring the value in the configuration stays up to date and next time when you access it, it will be the updated version. You can also subscribe to updates using the .on_update
method.
from pydaconf import PydaConf
from pydantic import BaseModel
def on_change_callback(key: str, value: str):
print(f"Configuration updated: {key} -> {value}")
class Config(BaseModel):
api_key: str
provider = PydaConf[Config]()
provider.from_file("config.yaml")
# Subscribe to updates for api_key
provider.on_update(".api_key", on_change_callback)