From 1664a51a4e92bf1048347ecd48b9a9a417af4bed Mon Sep 17 00:00:00 2001 From: Anne Matthes <Anne.Matthes@open-xchange.com> Date: Fri, 16 Apr 2021 11:28:05 +0200 Subject: [PATCH] Refactoring --- .gitignore | 2 + .../manifest-service/templates/configMap.yaml | 6 +-- .../templates/deployment.yaml | 15 ++++---- helm/values/develop.yaml | 5 ++- index.js | 37 ++++++++----------- 5 files changed, 31 insertions(+), 34 deletions(-) diff --git a/.gitignore b/.gitignore index d7e2d1a..039d21a 100644 --- a/.gitignore +++ b/.gitignore @@ -115,3 +115,5 @@ dist .yarn/build-state.yml .yarn/install-state.gz .pnp.* + +config diff --git a/helm/manifest-service/templates/configMap.yaml b/helm/manifest-service/templates/configMap.yaml index a727d17..5724af4 100644 --- a/helm/manifest-service/templates/configMap.yaml +++ b/helm/manifest-service/templates/configMap.yaml @@ -1,8 +1,8 @@ -{{- if .Values.environment.manifestUrls }} apiVersion: v1 kind: ConfigMap metadata: name: {{ include "manifest-service.fullname" . }} data: - manifest_urls: {{ join "," .Values.environment.manifestUrls }} -{{- end }} + urls.yaml: | + manifests: + {{ .Values.manifests | toYaml }} diff --git a/helm/manifest-service/templates/deployment.yaml b/helm/manifest-service/templates/deployment.yaml index a9cac00..e50bec7 100644 --- a/helm/manifest-service/templates/deployment.yaml +++ b/helm/manifest-service/templates/deployment.yaml @@ -33,14 +33,6 @@ spec: {{- toYaml .Values.securityContext | nindent 12 }} image: "{{ .Values.image.repository }}:{{ .Values.image.tag | default .Chart.AppVersion }}" imagePullPolicy: {{ .Values.image.pullPolicy }} - {{- if .Values.environment.manifestUrls }} - env: - - name: MANIFEST_URLS - valueFrom: - configMapKeyRef: - name: {{ include "manifest-service.fullname" . }} - key: manifest_urls - {{- end }} ports: - name: http containerPort: {{ .Values.containerPort | default 8080 }} @@ -55,7 +47,14 @@ spec: port: http resources: {{- toYaml .Values.resources | nindent 12 }} + volumeMounts: + - name: manifest-config + mountPath: /app/config/manifests {{- with .Values.nodeSelector }} + volumes: + - name: manifest-config + configMap: + name: {{ include "manifest-service.fullname" . }} nodeSelector: {{- toYaml . | nindent 8 }} {{- end }} diff --git a/helm/values/develop.yaml b/helm/values/develop.yaml index df3116b..6d9809e 100644 --- a/helm/values/develop.yaml +++ b/helm/values/develop.yaml @@ -1,7 +1,8 @@ replicaCount: 1 containerPort: 8080 -environment: - manifestUrls: +manifests: + - https://manifest-service-dummy.k3s.os.oxui.de/manifest.json + - https://manifest-service-dummy.k3s.os.oxui.de/manifest.json ingress: enabled: true hosts: diff --git a/index.js b/index.js index cfee10a..9bc7551 100644 --- a/index.js +++ b/index.js @@ -44,30 +44,25 @@ app.use(apiMetrics({ excludeRoutes: ignorePaths })) app.use('/api-docs', swaggerUi.serve, swaggerUi.setup(swaggerDocument)) app.use('/swagger.json', (req, res) => res.json(swaggerDocument)) -const urls = (process.env.MANIFEST_URLS || '').split(',').map(url => url + '/manifest.json') +const urls = yaml.load(fs.readFileSync('./config/manifests/url.yaml', 'utf8')).manifests -// Specific routes -app.get(['/', '/api/manifest.json'], async (req, res) => { - const content = await getManifest(urls) - res.json(content || []) +app.get('/api/manifest.json', async (req, res, next) => { + try { + const results = urls.map(url => fetch(url).then(res => { + if (!res.ok) throw new Error(`Failed to load manifest for url ${res.url} (Status: ${res.status}: ${res.statusText})`) + return res.json().catch(err => { throw new Error(`Failed to load manifest for url ${res.url}: ${err}`) }) + })) + const content = await Promise.all(results) + res.json(content.flat() || []) + } catch (err) { + next(err) + } }) -// collect manifest files from different services and merge them into a single file -async function getManifest (urls) { - return await Promise.all( - urls.map(url => fetch(url).then(res => { - if (!res.ok) { - throw new Error(`Failed to load manifest for url ${res.url} (Status: ${res.status}: ${res.statusText})`) - } - return res.json().catch(err => { - throw new Error(`Failed to load manifest for url ${res.url}: ${err}`) - }) - }).catch((err) => { - logger.error(err) - return [] - })) - ).then(urls => urls.flat()) -} +app.use(function (err, req, res, next) { + logger.error(err) + res.status(500).end() +}) // Binds and listens for connections on the specified host and port app.listen(process.env.PORT, () => { -- GitLab