From eff1b2c37368ee04a65cf7eb2c0c3acfe69f32b5 Mon Sep 17 00:00:00 2001 From: Thor77 Date: Mon, 6 Jun 2022 14:09:32 +0200 Subject: [PATCH 1/2] Add helm chart --- chart/.helmignore | 23 ++++++++++++ chart/Chart.yaml | 6 ++++ chart/templates/_helpers.tpl | 62 +++++++++++++++++++++++++++++++++ chart/templates/deployment.yaml | 51 +++++++++++++++++++++++++++ chart/templates/ingress.yaml | 19 ++++++++++ chart/templates/service.yaml | 14 ++++++++ chart/values.yaml | 23 ++++++++++++ kubernetes/deployment.yml.erb | 25 ------------- kubernetes/rolebinding.yml | 13 ------- kubernetes/service.yml | 46 ------------------------ 10 files changed, 198 insertions(+), 84 deletions(-) create mode 100644 chart/.helmignore create mode 100644 chart/Chart.yaml create mode 100644 chart/templates/_helpers.tpl create mode 100644 chart/templates/deployment.yaml create mode 100644 chart/templates/ingress.yaml create mode 100644 chart/templates/service.yaml create mode 100644 chart/values.yaml delete mode 100644 kubernetes/deployment.yml.erb delete mode 100644 kubernetes/rolebinding.yml delete mode 100644 kubernetes/service.yml diff --git a/chart/.helmignore b/chart/.helmignore new file mode 100644 index 0000000..0e8a0eb --- /dev/null +++ b/chart/.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/chart/Chart.yaml b/chart/Chart.yaml new file mode 100644 index 0000000..e80651a --- /dev/null +++ b/chart/Chart.yaml @@ -0,0 +1,6 @@ +apiVersion: v2 +name: turniere-frontend +description: turnie.re frontend +type: application +version: 0.1.0 +appVersion: "latest" diff --git a/chart/templates/_helpers.tpl b/chart/templates/_helpers.tpl new file mode 100644 index 0000000..7ba5edc --- /dev/null +++ b/chart/templates/_helpers.tpl @@ -0,0 +1,62 @@ +{{/* +Expand the name of the chart. +*/}} +{{- define "chart.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 "chart.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 "chart.chart" -}} +{{- printf "%s-%s" .Chart.Name .Chart.Version | replace "+" "_" | trunc 63 | trimSuffix "-" }} +{{- end }} + +{{/* +Common labels +*/}} +{{- define "chart.labels" -}} +helm.sh/chart: {{ include "chart.chart" . }} +{{ include "chart.selectorLabels" . }} +{{- if .Chart.AppVersion }} +app.kubernetes.io/version: {{ .Chart.AppVersion | quote }} +{{- end }} +app.kubernetes.io/managed-by: {{ .Release.Service }} +{{- end }} + +{{/* +Selector labels +*/}} +{{- define "chart.selectorLabels" -}} +app.kubernetes.io/name: {{ include "chart.name" . }} +app.kubernetes.io/instance: {{ .Release.Name }} +{{- end }} + +{{/* +Create the name of the service account to use +*/}} +{{- define "chart.serviceAccountName" -}} +{{- if .Values.serviceAccount.create }} +{{- default (include "chart.fullname" .) .Values.serviceAccount.name }} +{{- else }} +{{- default "default" .Values.serviceAccount.name }} +{{- end }} +{{- end }} diff --git a/chart/templates/deployment.yaml b/chart/templates/deployment.yaml new file mode 100644 index 0000000..e736841 --- /dev/null +++ b/chart/templates/deployment.yaml @@ -0,0 +1,51 @@ +apiVersion: apps/v1 +kind: Deployment +metadata: + name: {{ include "chart.fullname" . }} + labels: + {{- include "chart.labels" . | nindent 4 }} +spec: + replicas: {{ .Values.replicaCount }} + selector: + matchLabels: + {{- include "chart.selectorLabels" . | nindent 6 }} + template: + metadata: + {{- with .Values.podAnnotations }} + annotations: + {{- toYaml . | nindent 8 }} + {{- end }} + labels: + {{- include "chart.selectorLabels" . | nindent 8 }} + spec: + containers: + - name: {{ .Chart.Name }} + securityContext: + {{- toYaml .Values.securityContext | nindent 12 }} + image: "{{ .Values.image.repository }}:{{ .Values.image.tag | default .Chart.AppVersion }}" + imagePullPolicy: {{ .Values.image.pullPolicy }} + ports: + - name: http + containerPort: 80 + protocol: TCP + env: + - name: TURNIERE_API_URL + value: "{{ .Values.turniere.backend }}" + readinessProbe: + httpGet: + path: / + port: http + resources: + {{- toYaml .Values.resources | nindent 12 }} + {{- with .Values.nodeSelector }} + nodeSelector: + {{- toYaml . | nindent 8 }} + {{- end }} + {{- with .Values.affinity }} + affinity: + {{- toYaml . | nindent 8 }} + {{- end }} + {{- with .Values.tolerations }} + tolerations: + {{- toYaml . | nindent 8 }} + {{- end }} diff --git a/chart/templates/ingress.yaml b/chart/templates/ingress.yaml new file mode 100644 index 0000000..b707a95 --- /dev/null +++ b/chart/templates/ingress.yaml @@ -0,0 +1,19 @@ +{{- $fullName := include "chart.fullname" . -}} +--- +apiVersion: traefik.containo.us/v1alpha1 +kind: IngressRoute +metadata: + name: {{ $fullName }} +spec: + entryPoints: + - websecure + routes: +{{ range $host := .Values.ingress.hosts }} + - match: Host(`{{ $host }}`) + kind: Rule + services: + - name: {{ $fullName }} + port: {{ $.Values.service.port }} +{{ end }} + tls: + certResolver: leresolver diff --git a/chart/templates/service.yaml b/chart/templates/service.yaml new file mode 100644 index 0000000..76d3b37 --- /dev/null +++ b/chart/templates/service.yaml @@ -0,0 +1,14 @@ +apiVersion: v1 +kind: Service +metadata: + name: {{ include "chart.fullname" . }} + labels: + {{- include "chart.labels" . | nindent 4 }} +spec: + ports: + - port: {{ .Values.service.port }} + targetPort: http + protocol: TCP + name: http + selector: + {{- include "chart.selectorLabels" . | nindent 4 }} diff --git a/chart/values.yaml b/chart/values.yaml new file mode 100644 index 0000000..121eeeb --- /dev/null +++ b/chart/values.yaml @@ -0,0 +1,23 @@ +replicaCount: 1 + +image: + repository: registry.gitlab.com/turniere/turniere-frontend/production/commits + pullPolicy: IfNotPresent + tag: "" + +podAnnotations: {} + +securityContext: {} +# runAsNonRoot: true +# runAsUser: 1000 + +service: + port: 8080 + +ingress: + hosts: [] + +resources: {} + +turniere: + backend: "" diff --git a/kubernetes/deployment.yml.erb b/kubernetes/deployment.yml.erb deleted file mode 100644 index bbf825e..0000000 --- a/kubernetes/deployment.yml.erb +++ /dev/null @@ -1,25 +0,0 @@ -apiVersion: apps/v1 -kind: Deployment -metadata: - name: frontend-deployment - namespace: turniere-frontend - labels: - app: turniere-frontend -spec: - replicas: 1 - selector: - matchLabels: - app: turniere-frontend - template: - metadata: - labels: - app: turniere-frontend - spec: - containers: - - name: frontend-container - image: registry.gitlab.com/turniere/turniere-frontend:<%= current_sha %> - ports: - - containerPort: 80 - env: - - name: TURNIERE_API_URL - value: 'https://api01.turnie.re' diff --git a/kubernetes/rolebinding.yml b/kubernetes/rolebinding.yml deleted file mode 100644 index 6f5a596..0000000 --- a/kubernetes/rolebinding.yml +++ /dev/null @@ -1,13 +0,0 @@ -kind: RoleBinding -apiVersion: rbac.authorization.k8s.io/v1 -metadata: - name: gitlab-deploy-rb - namespace: turniere-frontend -subjects: - - kind: ServiceAccount - name: gitlab-deploy - namespace: gitlab -roleRef: - kind: ClusterRole - name: gitlab-deploy-role - apiGroup: rbac.authorization.k8s.io diff --git a/kubernetes/service.yml b/kubernetes/service.yml deleted file mode 100644 index 48a85c2..0000000 --- a/kubernetes/service.yml +++ /dev/null @@ -1,46 +0,0 @@ -apiVersion: v1 -kind: Service -metadata: - name: frontend-service - namespace: turniere-frontend -spec: - selector: - app: turniere-frontend - ports: - - name: http - port: 80 - targetPort: 80 ---- -apiVersion: traefik.containo.us/v1alpha1 -kind: IngressRoute -metadata: - name: frontend-ingress-http - namespace: turniere-frontend -spec: - entryPoints: - - http - routes: - - match: Host(`frontend01.turnie.re`) || Host(`turnie.re`) - kind: Rule - services: - - name: frontend-service - port: 80 - middlewares: - - name: redirect ---- -apiVersion: traefik.containo.us/v1alpha1 -kind: IngressRoute -metadata: - name: frontend-ingress-https - namespace: turniere-frontend -spec: - entryPoints: - - https - routes: - - match: Host(`frontend01.turnie.re`) || Host(`turnie.re`) - kind: Rule - services: - - name: frontend-service - port: 80 - tls: - certResolver: default From 02b0ffcfa332d90b811820914a5e8a561abfbab6 Mon Sep 17 00:00:00 2001 From: Thor77 Date: Thu, 9 Jun 2022 17:42:28 +0200 Subject: [PATCH 2/2] Switch to ci template and move Dockerfile --- .gitlab-ci.yml | 55 +++------------------- Dockerfile => docker/production/Dockerfile | 0 2 files changed, 6 insertions(+), 49 deletions(-) rename Dockerfile => docker/production/Dockerfile (100%) diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index dab906b..3d19a63 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -1,54 +1,11 @@ stages: - build + - test - deploy -kaniko: - tags: - - docker - stage: build - image: - name: gcr.io/kaniko-project/executor:debug - entrypoint: [""] - script: - - echo "{\"auths\":{\"$CI_REGISTRY\":{\"username\":\"$CI_REGISTRY_USER\",\"password\":\"$CI_REGISTRY_PASSWORD\"}}}" > /kaniko/.docker/config.json - - /kaniko/executor --context $CI_PROJECT_DIR --dockerfile $CI_PROJECT_DIR/Dockerfile --destination $CI_REGISTRY_IMAGE:latest --destination $CI_REGISTRY_IMAGE:$CI_COMMIT_SHA --destination $CI_REGISTRY_IMAGE:$CI_COMMIT_REF_SLUG +variables: + DOCKER_IMAGE_ENVS: "production" -kubernetes: - tags: - - turniere-deploy - stage: deploy - only: - refs: - - master - variables: - - $FORCE_DEPLOY - image: ruby - variables: - KUBERNETES_SERVICE_ACCOUNT_OVERWRITE: gitlab-deploy - script: - - curl -LO https://storage.googleapis.com/kubernetes-release/release/$(curl -s https://storage.googleapis.com/kubernetes-release/release/stable.txt)/bin/linux/amd64/kubectl - - chmod +x kubectl - - mv kubectl /usr/bin/kubectl - - kubectl config set-cluster k8s --server="https://kubernetes.default.svc" --certificate-authority=/var/run/secrets/kubernetes.io/serviceaccount/ca.crt - - kubectl config set-credentials sa --token="$(cat /var/run/secrets/kubernetes.io/serviceaccount/token)" - - kubectl config set-context local --cluster=k8s --user=sa --namespace=turniere-frontend - - gem install krane - - krane render -f kubernetes --current-sha "$CI_COMMIT_SHA" | krane deploy turniere-frontend local -f - - -deploy_watchtower: - tags: - - docker - stage: deploy - only: - refs: - - master - variables: - - $FORCE_DEPLOY - image: docker - services: - - docker:dind - script: - - docker login -u $CI_REGISTRY_USER -p $CI_REGISTRY_PASSWORD $CI_REGISTRY - - docker pull $CI_REGISTRY_IMAGE:$CI_COMMIT_SHA - - docker tag $CI_REGISTRY_IMAGE:$CI_COMMIT_SHA $CI_REGISTRY_IMAGE/production:deploy - - docker push $CI_REGISTRY_IMAGE/production:deploy +include: + - project: 'turniere/turniere-infra' + file: '/ci/pipeline.yaml' diff --git a/Dockerfile b/docker/production/Dockerfile similarity index 100% rename from Dockerfile rename to docker/production/Dockerfile