Skip to content

Commit

Permalink
BC-7083 can return static versions based on env vars (#20)
Browse files Browse the repository at this point in the history
  • Loading branch information
Loki-Afro authored Apr 8, 2024
1 parent 3d31855 commit 63549f3
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 13 deletions.
2 changes: 2 additions & 0 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ Goal of this project is to aggregate responses from different services while not
## configuration

configuration happens using env vars prefixed with `version.`.
you may not use static and url together, static will take priority.

## usage

Expand All @@ -18,5 +19,6 @@ docker run --rm \
-eversion.client.url='http://esel:3100/version' \
-eversion.nuxt-client.url='http://esel:4000/nuxtversion' \
-eversion.server.url='http://esel:3030/serverversion' \
-eversion.dof_app_deploy.static='696'
version-aggregator
```
4 changes: 4 additions & 0 deletions ansible/roles/version-aggregator/templates/deployment.yml.j2
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@ spec:
envFrom:
- configMapRef:
name: version-aggregator-configmap
env:
# we need that env here to ensure to get restarted when it changes!
- name: version.dof_app_deploy.static
value: {{ DOF_APP_DEPLOY_BRANCH_NAME }}
resources:
limits:
cpu: {{ VERSION_AGGREGATOR__CPU_LIMITS|default("1000m", true) }}
Expand Down
36 changes: 23 additions & 13 deletions app.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,21 @@ def do_GET(self):
result = {}
encountered_issues = False
for service in services:
service_url = services[service]
logging.info('calling for client ' + service + ': ' + service_url)
request = urllib.request.Request(service_url)
try:
with urllib.request.urlopen(request) as response:
data = json.loads(response.read().decode('UTF-8'))
result[service] = data
except URLError:
logging.error('service not available ' + service_url, exc_info=True)
encountered_issues = True
result[service] = 'unavailable'
static_version, _value = services[service]
if static_version:
result[service] = _value
else:
service_url = _value
logging.info('calling for client ' + service + ': ' + service_url)
request = urllib.request.Request(service_url)
try:
with urllib.request.urlopen(request) as response:
data = json.loads(response.read().decode('UTF-8'))
result[service] = data
except URLError:
logging.error('service not available ' + service_url, exc_info=True)
encountered_issues = True
result[service] = 'unavailable'

result['services-unavailable'] = encountered_issues
self.send_response(200)
Expand All @@ -41,8 +45,14 @@ def do_GET(self):

for key, value in os.environ.items():
if key.startswith('version.'):
re_result = re.search(r"(\bversion\.\b)(\b.+\b)\.url", key)
services[re_result.group(2)] = value
re_match = re.search(r"^(version\.(.+))\.(static|url)$", key)
if re_match:
service_name = re_match.group(2)
is_static = re_match.group(3) == 'static'
services[service_name] = (is_static, value)
else:
logging.error("config is neither static nor an url: " + key)


httpd = HTTPServer(('0.0.0.0', 8080), WebServer)
httpd.serve_forever()

0 comments on commit 63549f3

Please sign in to comment.