diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index 14625c7..25571c3 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -1,57 +1,4 @@ -image: docker:20.10.16 - -variables: - # When using dind service, you must instruct Docker to talk with - # the daemon started inside of the service. The daemon is available - # with a network connection instead of the default - # /var/run/docker.sock socket. - DOCKER_HOST: tcp://docker:2376 - # - # The 'docker' hostname is the alias of the service container as described at - # https://docs.gitlab.com/ee/ci/services/#accessing-the-services. - # If you're using GitLab Runner 12.7 or earlier with the Kubernetes executor and Kubernetes 1.6 or earlier, - # the variable must be set to tcp://localhost:2376 because of how the - # Kubernetes executor connects services to the job container - # DOCKER_HOST: tcp://localhost:2376 - # - # Specify to Docker where to create the certificates. Docker - # creates them automatically on boot, and creates - # `/certs/client` to share between the service and job - # container, thanks to volume mount from config.toml - DOCKER_TLS_CERTDIR: "/certs" - # These are usually specified by the entrypoint, however the - # Kubernetes executor doesn't run entrypoints - # https://gitlab.com/gitlab-org/gitlab-runner/-/issues/4125 - DOCKER_TLS_VERIFY: 1 - DOCKER_CERT_PATH: "$DOCKER_TLS_CERTDIR/client" - -services: - - docker:20.10.16-dind - -stages: - - build - - deploy - -build-job: - stage: build - tags: - - docker - only: - - master - script: - - docker info - - docker build -t docker-registry.dopcore.com/andrusyakka/urban-couscous . - - echo "$DOCKER_PASSWORD" | docker login docker-registry.dopcore.com -u "$DOCKER_USER" --password-stdin - - docker push docker-registry.dopcore.com/andrusyakka/urban-couscous - -deploy-job: - stage: deploy - image: bitnami/kubectl:latest - tags: - - docker - only: - - master - script: - - kubectl version - - kubectl rollout restart deploy/astra-frontend - +include: + - project: 'astra/microservice-ci' + ref: main + file: '/templates/microservice.yaml' diff --git a/Dockerfile b/Dockerfile index d5f1f09..83c6b28 100644 --- a/Dockerfile +++ b/Dockerfile @@ -13,4 +13,4 @@ COPY ./deploy/nginx.conf /etc/nginx/nginx.conf COPY --from=builder /front/dist /var/www/html -EXPOSE 8888 \ No newline at end of file +EXPOSE 8080 \ No newline at end of file diff --git a/deploy/.env b/deploy/.env new file mode 100644 index 0000000..a4c934b --- /dev/null +++ b/deploy/.env @@ -0,0 +1,2 @@ +SERVICE_REPLICAS=1 +SERVICE_PORT=8080 \ No newline at end of file diff --git a/deploy/.helmignore b/deploy/.helmignore new file mode 100644 index 0000000..0e8a0eb --- /dev/null +++ b/deploy/.helmignore @@ -0,0 +1,23 @@ +# Patterns to ignore when building packages. +# This supports shell glob matching, relative path matching, and +# negation (prefixed with !). Only one pattern per line. +.DS_Store +# Common VCS dirs +.git/ +.gitignore +.bzr/ +.bzrignore +.hg/ +.hgignore +.svn/ +# Common backup files +*.swp +*.bak +*.tmp +*.orig +*~ +# Various IDEs +.project +.idea/ +*.tmproj +.vscode/ diff --git a/deploy/Chart.yaml b/deploy/Chart.yaml new file mode 100644 index 0000000..21e3f88 --- /dev/null +++ b/deploy/Chart.yaml @@ -0,0 +1,24 @@ +apiVersion: v2 +name: astra-file-storage +appVersion: latest +description: A Helm chart to deploy app - astra + +# A chart can be either an 'application' or a 'library' chart. +# +# Application charts are a collection of templates that can be packaged into versioned archives +# to be deployed. +# +# Library charts provide useful utilities or functions for the chart developer. They're included as +# a dependency of application charts to inject those utilities and functions into the rendering +# pipeline. Library charts do not define any templates and therefore cannot be deployed. +type: application + +# This is the chart version. This version number should be incremented each time you make changes +# to the chart and its templates, including the app version. +# Versions are expected to follow Semantic Versioning (https://semver.org/) +version: 0.1.1 + +engine: gotpl + +maintainers: +- name: DOP diff --git a/deploy/nginx.conf b/deploy/nginx.conf index 483b04e..10da03f 100644 --- a/deploy/nginx.conf +++ b/deploy/nginx.conf @@ -15,11 +15,11 @@ http { keepalive_timeout 65; gzip_static on; server { - listen 8888; - + listen 8080; + charset utf-8; absolute_redirect off; - + location / { root /var/www/html; try_files $uri /index.html; @@ -31,7 +31,7 @@ http { add_header Cache-Control "no-cache, public, must-revalidate, proxy-revalidate"; } } - + location /health { return 200 '{"status": "UP"}'; } diff --git a/deploy/templates/NOTES.txt b/deploy/templates/NOTES.txt new file mode 100644 index 0000000..483dc61 --- /dev/null +++ b/deploy/templates/NOTES.txt @@ -0,0 +1 @@ +192.168.1.112:8888 diff --git a/deploy/templates/_helpers.tpl b/deploy/templates/_helpers.tpl new file mode 100644 index 0000000..3d14643 --- /dev/null +++ b/deploy/templates/_helpers.tpl @@ -0,0 +1,62 @@ +{{/* +Expand the name of the chart. +*/}} +{{- define "astra-frontend.name" -}} +{{- default .Chart.Name .Values.nameOverride | trunc 63 | trimSuffix "-" }} +{{- end }} + +{{/* +Create a default fully qualified app name. +We truncate at 63 chars because some Kubernetes name fields are limited to this (by the DNS naming spec). +If release name contains chart name it will be used as a full name. +*/}} +{{- define "astra-frontend.fullname" -}} +{{- if .Values.fullnameOverride }} +{{- .Values.fullnameOverride | trunc 63 | trimSuffix "-" }} +{{- else }} +{{- $name := default .Chart.Name .Values.nameOverride }} +{{- if contains $name .Release.Name }} +{{- .Release.Name | trunc 63 | trimSuffix "-" }} +{{- else }} +{{- printf "%s-%s" .Release.Name $name | trunc 63 | trimSuffix "-" }} +{{- end }} +{{- end }} +{{- end }} + +{{/* +Create chart name and version as used by the chart label. +*/}} +{{- define "astra-frontend.chart" -}} +{{- printf "%s-%s" .Chart.Name .Chart.Version | replace "+" "_" | trunc 63 | trimSuffix "-" }} +{{- end }} + +{{/* +Common labels +*/}} +{{- define "astra-frontend.labels" -}} +helm.sh/chart: {{ include "astra-frontend.chart" . }} +{{ include "astra-frontend.selectorLabels" . }} +{{- if .Chart.AppVersion }} +app.kubernetes.io/version: {{ .Chart.AppVersion | quote }} +{{- end }} +app.kubernetes.io/managed-by: {{ .Release.Service }} +{{- end }} + +{{/* +Selector labels +*/}} +{{- define "astra-frontend.selectorLabels" -}} +app.kubernetes.io/name: {{ include "astra-frontend.name" . }} +app.kubernetes.io/instance: {{ .Release.Name }} +{{- end }} + +{{/* +Create the name of the service account to use +*/}} +{{- define "astra-frontend.serviceAccountName" -}} +{{- if .Values.serviceAccount.create }} +{{- default (include "astra-frontend.fullname" .) .Values.serviceAccount.name }} +{{- else }} +{{- default "default" .Values.serviceAccount.name }} +{{- end }} +{{- end }} diff --git a/deploy/templates/deployment.yaml b/deploy/templates/deployment.yaml new file mode 100644 index 0000000..704f4e1 --- /dev/null +++ b/deploy/templates/deployment.yaml @@ -0,0 +1,46 @@ +apiVersion: apps/v1 +kind: Deployment +metadata: + name: {{ .Values.app.fullName }} +spec: + replicas: {{ .Values.replicas }} + revisionHistoryLimit: 10 + selector: + matchLabels: + app: {{ .Values.app.fullName }} + strategy: + rollingUpdate: + maxSurge: 25% + maxUnavailable: 25% + type: RollingUpdate + template: + metadata: + labels: + app: {{ .Values.app.fullName }} + spec: + imagePullSecrets: + - name: {{ .Values.images.pullSecret }} + containers: + - name: {{ .Values.app.fullName }} + env: + - name: TZ + value: Europe/Moscow + - name: PORT + value: {{ .Values.app.server.port | quote }} + - name: SHARE_PATH + value: /app/public + {{- .Values.envs | toYaml | nindent 12 }} + + image: {{ .Values.app.image }}:{{ .Chart.AppVersion }} #"docker-registry.dopcore.com/andrusyakka/urban-couscous:latest" + imagePullPolicy: Always + ports: + - name: http + containerPort: {{ .Values.app.server.port }} + protocol: TCP + volumeMounts: + - name: data + mountPath: /app/public + volumes: + - name: data + persistentVolumeClaim: + claimName: fileshare \ No newline at end of file diff --git a/deploy/templates/hpa.yaml b/deploy/templates/hpa.yaml new file mode 100644 index 0000000..cc40f06 --- /dev/null +++ b/deploy/templates/hpa.yaml @@ -0,0 +1,28 @@ +{{- if .Values.autoscaling.enabled }} +apiVersion: autoscaling/v2beta1 +kind: HorizontalPodAutoscaler +metadata: + name: {{ include ".Values.app.fullName" . }} + labels: + app: {{ include ".Values.app.fullName" . }} +spec: + scaleTargetRef: + apiVersion: apps/v1 + kind: Deployment + name: {{ include ".Values.app.fullName" . }} + minReplicas: {{ .Values.autoscaling.minReplicas }} + maxReplicas: {{ .Values.autoscaling.maxReplicas }} + metrics: + {{- if .Values.autoscaling.targetCPUUtilizationPercentage }} + - type: Resource + resource: + name: cpu + targetAverageUtilization: {{ .Values.autoscaling.targetCPUUtilizationPercentage }} + {{- end }} + {{- if .Values.autoscaling.targetMemoryUtilizationPercentage }} + - type: Resource + resource: + name: memory + targetAverageUtilization: {{ .Values.autoscaling.targetMemoryUtilizationPercentage }} + {{- end }} +{{- end }} diff --git a/deploy/templates/ingress.yaml b/deploy/templates/ingress.yaml new file mode 100644 index 0000000..516515e --- /dev/null +++ b/deploy/templates/ingress.yaml @@ -0,0 +1,61 @@ +{{- if .Values.ingress.enabled -}} +{{- $fullName := include ".Values.app.fullName" . -}} +{{- $svcPort := .Values.app.server.port -}} +{{- if and .Values.ingress.className (not (semverCompare ">=1.18-0" .Capabilities.KubeVersion.GitVersion)) }} + {{- if not (hasKey .Values.ingress.annotations "kubernetes.io/ingress.class") }} + {{- $_ := set .Values.ingress.annotations "kubernetes.io/ingress.class" .Values.ingress.className}} + {{- end }} +{{- end }} +{{- if semverCompare ">=1.19-0" .Capabilities.KubeVersion.GitVersion -}} +apiVersion: networking.k8s.io/v1 +{{- else if semverCompare ">=1.14-0" .Capabilities.KubeVersion.GitVersion -}} +apiVersion: networking.k8s.io/v1beta1 +{{- else -}} +apiVersion: extensions/v1beta1 +{{- end }} +kind: Ingress +metadata: + name: {{ $fullName }} + labels: + app: {{ $fullName }} + {{- with .Values.ingress.annotations }} + annotations: + {{- toYaml . | nindent 4 }} + {{- end }} +spec: + {{- if and .Values.ingress.className (semverCompare ">=1.18-0" .Capabilities.KubeVersion.GitVersion) }} + ingressClassName: {{ .Values.ingress.className }} + {{- end }} + {{- if .Values.ingress.tls }} + tls: + {{- range .Values.ingress.tls }} + - hosts: + {{- range .hosts }} + - {{ . | quote }} + {{- end }} + secretName: {{ .secretName }} + {{- end }} + {{- end }} + rules: + {{- range .Values.ingress.hosts }} + - host: {{ .host | quote }} + http: + paths: + {{- range .paths }} + - path: {{ .path }} + {{- if and .pathType (semverCompare ">=1.18-0" $.Capabilities.KubeVersion.GitVersion) }} + pathType: {{ .pathType }} + {{- end }} + backend: + {{- if semverCompare ">=1.19-0" $.Capabilities.KubeVersion.GitVersion }} + service: + name: {{ $fullName }} + port: + number: {{ $svcPort }} + {{- else }} + serviceName: {{ $fullName }} + servicePort: {{ $svcPort }} + {{- end }} + {{- end }} + {{- end }} +{{- end }} diff --git a/deploy/templates/service.yaml b/deploy/templates/service.yaml new file mode 100644 index 0000000..eff85e3 --- /dev/null +++ b/deploy/templates/service.yaml @@ -0,0 +1,16 @@ +apiVersion: v1 +kind: Service +metadata: + name: {{ .Values.app.fullName }} + namespace: default +spec: + ports: + - port: {{ .Values.app.server.port }} + protocol: TCP + targetPort: {{ .Values.app.server.port }} + selector: + app: {{ .Values.app.fullName }} + sessionAffinity: None + type: ClusterIP +status: + loadBalancer: {} \ No newline at end of file diff --git a/deploy/values.template.yaml b/deploy/values.template.yaml new file mode 100644 index 0000000..0adf226 --- /dev/null +++ b/deploy/values.template.yaml @@ -0,0 +1,16 @@ +app: + fullName: ${CI_PROJECT_TITLE} + image: docker-registry.dopcore.com/${CI_PROJECT_PATH} + server: + port: ${SERVICE_PORT} + +replicas: ${SERVICE_REPLICAS} + +images: + pullSecret: regcred + +ingress: + enabled: false + +autoscaling: + enabled: false