Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BC-7083 can return static versions based on env vars #20

Merged
merged 3 commits into from
Apr 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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:
Loki-Afro marked this conversation as resolved.
Show resolved Hide resolved
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()