From b0f25336e0306d956fa95b3003232a80aa7b1265 Mon Sep 17 00:00:00 2001 From: Gabriel Cerioni Date: Fri, 8 Dec 2023 14:31:32 -0300 Subject: [PATCH] Override configuration with environment variables if they exist Override configuration with environment variables if they exist. Hey, team. This is your friend Gabs from Harness, and I have a quick suggestion to allow us to provide some important configuration that overrides the config file app.config when necessary. For example, during my CD Pipeline, I will retrieve the Redis details from a Vault Server, using Harness expressions (<+secrets.getValue("redis_host")>) and use it in the K8s Deployment -> Pod -> Container environment variables. For example, I create a Secret or a Configmap to hold these, but then I serve the variables for the python app at runtime, with K8s's envFrom. Cheers! --- redisolar/__init__.py | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/redisolar/__init__.py b/redisolar/__init__.py index 07584313..57881c27 100644 --- a/redisolar/__init__.py +++ b/redisolar/__init__.py @@ -13,6 +13,15 @@ def create_app(config_file=DEV_CONFIG): app = Flask(__name__, instance_relative_config=True) app.config.from_pyfile(config_file) + # Override configuration with environment variables if they exist + redis_host = os.environ.get('REDIS_HOST') + if redis_host: + app.config['REDIS_HOST'] = redis_host + + redis_port = os.environ.get('REDIS_PORT') + if redis_port: + app.config['REDIS_PORT'] = redis_port + app.register_blueprint(api.blueprint) api.configure(app)