From 72311d9b61c9a08a868fade6c8abdf99e1919759 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Arsham=20Teymour=C4=B1?= Date: Tue, 10 Aug 2021 20:07:07 +0300 Subject: [PATCH] adding front to the CI adding frontend configmaps and envVars optimizing docker-compose and DockerfIle --- .gitlab-ci.yml | 60 +++++++++++++------- docker-compose.yml | 3 + frontend/Dockerfile | 11 ++-- helm/templates/core-deployment.yaml | 7 +++ helm/templates/core-hpa.yaml | 29 ++++++++++ helm/templates/frontend-deployment.yaml | 21 +++++++ helm/templates/frontend-env-config.yaml | 10 ++++ helm/templates/frontend-hpa.yaml | 29 ++++++++++ helm/templates/frontend-nginx-configmap.yaml | 37 ++++++++++++ helm/templates/shard-deployment.yaml | 7 +++ helm/templates/shard-hpa.yaml | 14 ++--- helm/values.yaml | 7 +++ 12 files changed, 203 insertions(+), 32 deletions(-) create mode 100644 helm/templates/core-hpa.yaml create mode 100644 helm/templates/frontend-env-config.yaml create mode 100644 helm/templates/frontend-hpa.yaml create mode 100644 helm/templates/frontend-nginx-configmap.yaml diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index 9cefa99..ed86641 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -1,8 +1,10 @@ variables: - CONTAINER_REGISTRY: "docker.io/parity" - CONTAINER_REPO: "substrate-telemetry-backend" - KUBE_NAMESPACE: "substrate-telemetry" - IMAGE_FULL_NAME: "${CONTAINER_REGISTRY}/${CONTAINER_REPO}:${CI_COMMIT_SHORT_SHA}" + CONTAINER_REGISTRY: "docker.io/parity" + BACKEND_CONTAINER_REPO: "substrate-telemetry-backend" + FRONTEND_CONTAINER_REPO: "substrate-telemetry-frontend" + KUBE_NAMESPACE: "substrate-telemetry" + BACKEND_IMAGE_FULL_NAME: "${CONTAINER_REGISTRY}/${BACKEND_CONTAINER_REPO}:${CI_COMMIT_SHORT_SHA}" + FRONTEND_IMAGE_FULL_NAME: "${CONTAINER_REGISTRY}/${FRONTEND_CONTAINER_REPO}:${CI_COMMIT_SHORT_SHA}" stages: - dockerize @@ -11,10 +13,19 @@ stages: #.delete_deployment: &delete_deployment # - helm uninstall -n "$KUBE_NAMESPACE" "$KUBE_NAMESPACE" +.dockerize: &dockerize + stage: dockerize + image: quay.io/buildah/stable + rules: + - if: '$CI_COMMIT_BRANCH == "jsdw-sharding"' + when: manual + tags: + - kubernetes-parity-build + .deploy-k8s: &deploy-k8s image: paritytech/kubetools:3.5.3 script: - - echo "Deploying using image $IMAGE_FULL_NAME" + - echo "Deploying using image $BACKEND_IMAGE_FULL_NAME" - echo "Using Helm `helm version --short`" - export KUBERNETES_VERSION_TAG="$CI_PIPELINE_ID" - |- @@ -25,8 +36,10 @@ stages: - helm --debug template --create-namespace --namespace $KUBE_NAMESPACE - --set image.repository="${CONTAINER_REGISTRY}/${CONTAINER_REPO}" - --set image.tag="${CI_COMMIT_SHORT_SHA}" + --set image.backend.repository="${CONTAINER_REGISTRY}/${BACKEND_CONTAINER_REPO}" + --set image.backend.tag="${CI_COMMIT_SHORT_SHA}" + --set image.frontend.repository="${CONTAINER_REGISTRY}/${FRONTEND_CONTAINER_REPO}" + --set image.frontend.tag="${CI_COMMIT_SHORT_SHA}" $KUBE_NAMESPACE ./helm/ # install the chart into the relevant cluster - helm --debug upgrade @@ -35,8 +48,10 @@ stages: --timeout 120s --create-namespace --namespace $KUBE_NAMESPACE - --set image.repository="${CONTAINER_REGISTRY}/${CONTAINER_REPO}" - --set image.tag="${CI_COMMIT_SHORT_SHA}" + --set image.backend.repository="${CONTAINER_REGISTRY}/${BACKEND_CONTAINER_REPO}" + --set image.backend.tag="${CI_COMMIT_SHORT_SHA}" + --set image.frontend.repository="${CONTAINER_REGISTRY}/${FRONTEND_CONTAINER_REPO}" + --set image.frontend.tag="${CI_COMMIT_SHORT_SHA}" $KUBE_NAMESPACE ./helm/ rules: - if: '$CI_COMMIT_BRANCH == "jsdw-sharding"' @@ -44,22 +59,27 @@ stages: tags: - kubernetes-parity-build -dockerize: - stage: dockerize - image: quay.io/buildah/stable +dockerize-backend: + <<: &dockerize script: - - echo "Building image $IMAGE_FULL_NAME" + - echo "Building image $BACKEND_IMAGE_FULL_NAME" - buildah bud --format=docker - --tag "$IMAGE_FULL_NAME" ./backend/ + --tag "$BACKEND_IMAGE_FULL_NAME" ./backend/ - echo ${Docker_Hub_Pass_Parity} | buildah login --username ${Docker_Hub_User_Parity} --password-stdin docker.io - - buildah push --format=v2s2 "$IMAGE_FULL_NAME" - rules: - - if: '$CI_COMMIT_BRANCH == "jsdw-sharding"' - when: manual - tags: - - kubernetes-parity-build + - buildah push --format=v2s2 "$BACKEND_IMAGE_FULL_NAME" + +dockerize-frontend: + <<: &dockerize + script: + - echo "Building image $FRONTEND_IMAGE_FULL_NAME" + - buildah bud + --format=docker + --tag "$FRONTEND_IMAGE_FULL_NAME" ./frontend/ + - echo ${Docker_Hub_Pass_Parity} | + buildah login --username ${Docker_Hub_User_Parity} --password-stdin docker.io + - buildah push --format=v2s2 "$FRONTEND_IMAGE_FULL_NAME" deploy-parity-stg: stage: staging diff --git a/docker-compose.yml b/docker-compose.yml index c6d5ebc..1f0db07 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -9,6 +9,9 @@ services: volumes: - /app/node_modules - ./packages:/app/packages + - ./nginx/nginx.conf:/etc/nginx/nginx.conf + - ./nginx/default:/etc/nginx/sites-available/default + - ./env-config.js:/usr/share/nginx/html/env-config.js ports: - 3000:80 expose: diff --git a/frontend/Dockerfile b/frontend/Dockerfile index 5206edf..5f927ea 100644 --- a/frontend/Dockerfile +++ b/frontend/Dockerfile @@ -19,13 +19,14 @@ ENV SUBSTRATE_TELEMETRY_URL= WORKDIR /usr/share/nginx/html -COPY --from=builder /opt/builder/env.sh /usr/bin/ -RUN apk add --no-cache bash; chmod +x /usr/bin/env.sh +#COPY --from=builder /opt/builder/env.sh /usr/bin/ +#RUN apk add --no-cache bash; chmod +x /usr/bin/env.sh -COPY --from=builder /opt/builder/nginx/nginx.conf /etc/nginx/nginx.conf -COPY --from=builder /opt/builder/nginx/default /etc/nginx/sites-available/default +#COPY --from=builder /opt/builder/nginx/nginx.conf /etc/nginx/nginx.conf +#COPY --from=builder /opt/builder/nginx/default /etc/nginx/sites-available/default COPY --from=builder /opt/builder/build /usr/share/nginx/html EXPOSE 80 -CMD ["/bin/bash", "-c", "/usr/bin/env.sh && nginx -g \"daemon off;\""] +#CMD ["/bin/bash", "-c", "/usr/bin/env.sh && nginx -g \"daemon off;\""] +CMD [ "nginx", "-g" ,"\"daemon off;\""] diff --git a/helm/templates/core-deployment.yaml b/helm/templates/core-deployment.yaml index 393cf03..f283856 100644 --- a/helm/templates/core-deployment.yaml +++ b/helm/templates/core-deployment.yaml @@ -35,6 +35,13 @@ spec: {{- toYaml .Values.securityContext | nindent 12 }} image: "{{ .Values.image.backend.repository }}:{{ .Values.image.backend.tag | default .Chart.AppVersion }}" imagePullPolicy: {{ .Values.image.backend.pullPolicy }} + {{- if .Values.envVars.core }} + env: + {{- range $key, $val := .Values.envVars.core }} + - name: {{ $key }} + value: {{ $val | quote }} + {{- end }} + {{- end }} command: - "telemetry_core" args: diff --git a/helm/templates/core-hpa.yaml b/helm/templates/core-hpa.yaml new file mode 100644 index 0000000..5e39026 --- /dev/null +++ b/helm/templates/core-hpa.yaml @@ -0,0 +1,29 @@ +{{- if .Values.autoscaling.core.enabled }} +apiVersion: autoscaling/v2beta1 +kind: HorizontalPodAutoscaler +metadata: + name: telemetry-core + labels: + {{- include "substrate-telemetry.labels" . | nindent 4 }} + telemetry-component: core +spec: + scaleTargetRef: + apiVersion: apps/v1 + kind: Deployment + name: telemetry-core + minReplicas: {{ .Values.autoscaling.core.minReplicas }} + maxReplicas: {{ .Values.autoscaling.core.maxReplicas }} + metrics: + {{- if .Values.autoscaling.core.targetCPUUtilizationPercentage }} + - type: Resource + resource: + name: cpu + targetAverageUtilization: {{ .Values.autoscaling.core.targetCPUUtilizationPercentage }} + {{- end }} + {{- if .Values.autoscaling.core.targetMemoryUtilizationPercentage }} + - type: Resource + resource: + name: memory + targetAverageUtilization: {{ .Values.autoscaling.core.targetMemoryUtilizationPercentage }} + {{- end }} +{{- end }} diff --git a/helm/templates/frontend-deployment.yaml b/helm/templates/frontend-deployment.yaml index 0b0a49a..55d5933 100644 --- a/helm/templates/frontend-deployment.yaml +++ b/helm/templates/frontend-deployment.yaml @@ -35,6 +35,13 @@ spec: {{- toYaml .Values.securityContext | nindent 12 }} image: "{{ .Values.image.repository }}:{{ .Values.image.tag | default .Chart.AppVersion }}" imagePullPolicy: {{ .Values.image.pullPolicy }} + {{- if .Values.envVars.frontend }} + env: + {{- range $key, $val := .Values.envVars.frontend }} + - name: {{ $key }} + value: {{ $val | quote }} + {{- end }} + {{- end }} command: - "telemetry_frontend" args: @@ -52,8 +59,22 @@ spec: httpGet: path: /health port: http + volumeMounts: + - name: nginx + mountPath: "/etc/nginx/nginx.conf" + readOnly: true + - name: env-config + mountPath: "/usr/share/nginx/html/env-config.js" + readOnly: true resources: {{- toYaml .Values.resources | nindent 12 }} + volumes: + - name: nginx + configMap: + name: frontend-nginx + - name: env-config + configMap: + name: frontend-env-config {{- with .Values.nodeSelector }} nodeSelector: {{- toYaml . | nindent 8 }} diff --git a/helm/templates/frontend-env-config.yaml b/helm/templates/frontend-env-config.yaml new file mode 100644 index 0000000..f2856d2 --- /dev/null +++ b/helm/templates/frontend-env-config.yaml @@ -0,0 +1,10 @@ +apiVersion: v1 +kind: ConfigMap +metadata: + name: frontend-env-config +data: + env-config.js: | + window.process_env = { + SUBSTRATE_TELEMETRY_URL: "ws://feed.telemetry.parity-stg.parity.io/feed", + SUBSTRATE_TELEMETRY_SAMPLE: "", + } diff --git a/helm/templates/frontend-hpa.yaml b/helm/templates/frontend-hpa.yaml new file mode 100644 index 0000000..6546c3f --- /dev/null +++ b/helm/templates/frontend-hpa.yaml @@ -0,0 +1,29 @@ +{{- if .Values.autoscaling.frontend.enabled }} +apiVersion: autoscaling/v2beta1 +kind: HorizontalPodAutoscaler +metadata: + name: telemetry-frontend + labels: + {{- include "substrate-telemetry.labels" . | nindent 4 }} + telemetry-component: frontend +spec: + scaleTargetRef: + apiVersion: apps/v1 + kind: Deployment + name: telemetry-frontend + minReplicas: {{ .Values.autoscaling.frontend.minReplicas }} + maxReplicas: {{ .Values.autoscaling.frontend.maxReplicas }} + metrics: + {{- if .Values.autoscaling.frontend.targetCPUUtilizationPercentage }} + - type: Resource + resource: + name: cpu + targetAverageUtilization: {{ .Values.autoscaling.frontend.targetCPUUtilizationPercentage }} + {{- end }} + {{- if .Values.autoscaling.frontend.targetMemoryUtilizationPercentage }} + - type: Resource + resource: + name: memory + targetAverageUtilization: {{ .Values.autoscaling.frontend.targetMemoryUtilizationPercentage }} + {{- end }} +{{- end }} diff --git a/helm/templates/frontend-nginx-configmap.yaml b/helm/templates/frontend-nginx-configmap.yaml new file mode 100644 index 0000000..52a34d2 --- /dev/null +++ b/helm/templates/frontend-nginx-configmap.yaml @@ -0,0 +1,37 @@ +apiVersion: v1 +kind: ConfigMap +metadata: + name: frontend-nginx +data: + nginx.conf: | + user nginx; + worker_processes auto; + worker_rlimit_nofile 30000; + error_log /var/log/nginx/error.log warn; + pid /var/run/nginx.pid; + + server { + root /usr/share/nginx/html; + index index.html; + listen 80; + listen [::]:80; + server_name _; + } + + events { + worker_connections 8000; + } + + http { + include /etc/nginx/mime.types; + default_type application/octet-stream; + log_format main '$remote_addr - $remote_user [$time_local] "$request" ' + '$status $body_bytes_sent "$http_referer" ' + '"$http_user_agent" "$http_x_forwarded_for"'; + access_log /var/log/nginx/access.log main; + sendfile on; + #tcp_nopush on; + keepalive_timeout 65; + gzip on; + include /etc/nginx/conf.d/*.conf; + } diff --git a/helm/templates/shard-deployment.yaml b/helm/templates/shard-deployment.yaml index d5ecda5..c5a7e66 100644 --- a/helm/templates/shard-deployment.yaml +++ b/helm/templates/shard-deployment.yaml @@ -35,6 +35,13 @@ spec: {{- toYaml .Values.securityContext | nindent 12 }} image: "{{ .Values.image.backend.repository }}:{{ .Values.image.backend.tag | default .Chart.AppVersion }}" imagePullPolicy: {{ .Values.image.backend.pullPolicy }} + {{- if .Values.envVars.shard }} + env: + {{- range $key, $val := .Values.envVars.shard }} + - name: {{ $key }} + value: {{ $val | quote }} + {{- end }} + {{- end }} command: - "telemetry_shard" args: diff --git a/helm/templates/shard-hpa.yaml b/helm/templates/shard-hpa.yaml index eabbced..ff674c4 100644 --- a/helm/templates/shard-hpa.yaml +++ b/helm/templates/shard-hpa.yaml @@ -1,4 +1,4 @@ -{{- if .Values.autoscaling.enabled }} +{{- if .Values.autoscaling.shard.enabled }} apiVersion: autoscaling/v2beta1 kind: HorizontalPodAutoscaler metadata: @@ -11,19 +11,19 @@ spec: apiVersion: apps/v1 kind: Deployment name: telemetry-shard - minReplicas: {{ .Values.autoscaling.minReplicas }} - maxReplicas: {{ .Values.autoscaling.maxReplicas }} + minReplicas: {{ .Values.autoscaling.shard.minReplicas }} + maxReplicas: {{ .Values.autoscaling.shard.maxReplicas }} metrics: - {{- if .Values.autoscaling.targetCPUUtilizationPercentage }} + {{- if .Values.autoscaling.shard.targetCPUUtilizationPercentage }} - type: Resource resource: name: cpu - targetAverageUtilization: {{ .Values.autoscaling.targetCPUUtilizationPercentage }} + targetAverageUtilization: {{ .Values.autoscaling.shard.targetCPUUtilizationPercentage }} {{- end }} - {{- if .Values.autoscaling.targetMemoryUtilizationPercentage }} + {{- if .Values.autoscaling.shard.targetMemoryUtilizationPercentage }} - type: Resource resource: name: memory - targetAverageUtilization: {{ .Values.autoscaling.targetMemoryUtilizationPercentage }} + targetAverageUtilization: {{ .Values.autoscaling.shard.targetMemoryUtilizationPercentage }} {{- end }} {{- end }} diff --git a/helm/values.yaml b/helm/values.yaml index f098e77..86922a0 100644 --- a/helm/values.yaml +++ b/helm/values.yaml @@ -23,6 +23,13 @@ imagePullSecrets: [] nameOverride: "" fullnameOverride: "" +envVars: + shard: {} + core: {} + frontend: + SUBSTRATE_TELEMETRY_URL: telemetry-core + + serviceAccount: # Specifies whether a service account should be created create: true