From 515e4e8f7ba42d779f8e801c32ab9aabcf6a2a3c Mon Sep 17 00:00:00 2001 From: Enzo Date: Fri, 29 Nov 2024 18:05:10 +0100 Subject: [PATCH 1/3] add helm charts and rework ci workflows --- .github/workflows/ci.yml | 52 ++++++++++- charts/kaelly-books/.helmignore | 23 +++++ charts/kaelly-books/Chart.yaml | 24 +++++ charts/kaelly-books/templates/_helpers.tpl | 62 +++++++++++++ charts/kaelly-books/templates/configmap.yaml | 12 +++ charts/kaelly-books/templates/deployment.yaml | 60 +++++++++++++ charts/kaelly-books/templates/secrets.yaml | 13 +++ charts/kaelly-books/values.yaml | 89 +++++++++++++++++++ 8 files changed, 334 insertions(+), 1 deletion(-) create mode 100644 charts/kaelly-books/.helmignore create mode 100644 charts/kaelly-books/Chart.yaml create mode 100644 charts/kaelly-books/templates/_helpers.tpl create mode 100644 charts/kaelly-books/templates/configmap.yaml create mode 100644 charts/kaelly-books/templates/deployment.yaml create mode 100644 charts/kaelly-books/templates/secrets.yaml create mode 100644 charts/kaelly-books/values.yaml diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index dee5637..2903ca4 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -54,6 +54,15 @@ jobs: username: ${{ github.actor }} password: ${{ secrets.GITHUB_TOKEN }} + - name: Set Docker image tag + id: set-tag + run: | + if [[ $GITHUB_REF == refs/tags/* ]]; then + echo "tag_name=${GITHUB_REF#refs/tags/}" >> $GITHUB_ENV + else + echo "tag_name=${GITHUB_SHA}" >> $GITHUB_ENV + fi + - name: Build and push Docker images uses: docker/build-push-action@v6 with: @@ -61,6 +70,47 @@ jobs: push: true tags: | ghcr.io/${{ github.repository }}:latest - ghcr.io/${{ github.repository }}:${{ github.sha }} + ghcr.io/${{ github.repository }}:${{ env.tag_name }} cache-from: type=gha cache-to: type=gha,mode=max + + deploy-helm: + runs-on: ubuntu-latest + needs: [docker_build] + if: startsWith(github.ref, 'refs/tags/') + steps: + - name: Checkout Code + uses: actions/checkout@v4 + + - name: Set up Kubernetes context + run: | + mkdir -p ${HOME}/.kube + echo "${{ secrets.KUBECONFIG }}" > ${HOME}/.kube/config + + - name: Install Helm + uses: azure/setup-helm@v4 + + - name: Set image tag + id: set-image-tag + run: | + if [[ $GITHUB_REF == refs/tags/* ]]; then + echo "image_tag_name=${GITHUB_REF#refs/tags/}" >> $GITHUB_ENV + else + echo "image_tag_name=${GITHUB_SHA}" >> $GITHUB_ENV + fi + + - name: Deploy Helm Chart + run: | + helm upgrade --install ${{ github.event.repository.name }} charts/${{ github.event.repository.name }} \ + --namespace kaellybot \ + --set image.tag="${{ env.image_tag_name }}" \ + --set secrets.MYSQL_URL="${{ secrets.MYSQL_URL }}" \ + --set secrets.MYSQL_USER="${{ secrets.MYSQL_USER }}" \ + --set secrets.MYSQL_PASSWORD="${{ secrets.MYSQL_PASSWORD }}" \ + --set secrets.MYSQL_DATABASE="${{ secrets.MYSQL_DATABASE }}" \ + --set secrets.RABBITMQ_ADDRESS="${{ secrets.RABBITMQ_ADDRESS }}" \ + --set configMap.PROBE_PORT="${{ secrets.PROBE_PORT }}" \ + --set configMap.METRIC_PORT="${{ secrets.METRIC_PORT }}" \ + --set configMap.LOG_LEVEL="${{ secrets.LOG_LEVEL }}" \ + --set-string configMap.PRODUCTION="${{ secrets.PRODUCTION }}" + \ No newline at end of file diff --git a/charts/kaelly-books/.helmignore b/charts/kaelly-books/.helmignore new file mode 100644 index 0000000..0e8a0eb --- /dev/null +++ b/charts/kaelly-books/.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/charts/kaelly-books/Chart.yaml b/charts/kaelly-books/Chart.yaml new file mode 100644 index 0000000..af2dd3a --- /dev/null +++ b/charts/kaelly-books/Chart.yaml @@ -0,0 +1,24 @@ +apiVersion: v2 +name: kaelly-books +description: A Helm chart for Kubernetes + +# 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: 1.0.0 + +# This is the version number of the application being deployed. This version number should be +# incremented each time you make changes to the application. Versions are not expected to +# follow Semantic Versioning. They should reflect the version the application is using. +# It is recommended to use it with quotes. +appVersion: "1.0.0" diff --git a/charts/kaelly-books/templates/_helpers.tpl b/charts/kaelly-books/templates/_helpers.tpl new file mode 100644 index 0000000..503c058 --- /dev/null +++ b/charts/kaelly-books/templates/_helpers.tpl @@ -0,0 +1,62 @@ +{{/* +Expand the name of the chart. +*/}} +{{- define "kaelly-books.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 "kaelly-books.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 "kaelly-books.chart" -}} +{{- printf "%s-%s" .Chart.Name .Chart.Version | replace "+" "_" | trunc 63 | trimSuffix "-" }} +{{- end }} + +{{/* +Common labels +*/}} +{{- define "kaelly-books.labels" -}} +helm.sh/chart: {{ include "kaelly-books.chart" . }} +{{ include "kaelly-books.selectorLabels" . }} +{{- if .Chart.AppVersion }} +app.kubernetes.io/version: {{ .Chart.AppVersion | quote }} +{{- end }} +app.kubernetes.io/managed-by: {{ .Release.Service }} +{{- end }} + +{{/* +Selector labels +*/}} +{{- define "kaelly-books.selectorLabels" -}} +app.kubernetes.io/name: {{ include "kaelly-books.name" . }} +app.kubernetes.io/instance: {{ .Release.Name }} +{{- end }} + +{{/* +Create the name of the service account to use +*/}} +{{- define "kaelly-books.serviceAccountName" -}} +{{- if .Values.serviceAccount.create }} +{{- default (include "kaelly-books.fullname" .) .Values.serviceAccount.name }} +{{- else }} +{{- default "default" .Values.serviceAccount.name }} +{{- end }} +{{- end }} diff --git a/charts/kaelly-books/templates/configmap.yaml b/charts/kaelly-books/templates/configmap.yaml new file mode 100644 index 0000000..6c4b926 --- /dev/null +++ b/charts/kaelly-books/templates/configmap.yaml @@ -0,0 +1,12 @@ +apiVersion: v1 +kind: ConfigMap +metadata: + name: {{ .Release.Name }}-configmap + namespace: {{ .Release.Namespace }} +data: +{{- if .Values.configMap }} +{{- range $key, $value := .Values.configMap }} + {{ $key }}: "{{ $value }}" +{{- end }} +{{- else }} +{{- end }} \ No newline at end of file diff --git a/charts/kaelly-books/templates/deployment.yaml b/charts/kaelly-books/templates/deployment.yaml new file mode 100644 index 0000000..8e1ae65 --- /dev/null +++ b/charts/kaelly-books/templates/deployment.yaml @@ -0,0 +1,60 @@ +apiVersion: apps/v1 +kind: Deployment +metadata: + name: {{ include "kaelly-books.fullname" . }} + labels: + {{- include "kaelly-books.labels" . | nindent 4 }} +spec: + {{- if not .Values.autoscaling.enabled }} + replicas: {{ .Values.replicaCount }} + {{- end }} + selector: + matchLabels: + {{- include "kaelly-books.selectorLabels" . | nindent 6 }} + template: + metadata: + {{- with .Values.podAnnotations }} + annotations: + {{- toYaml . | nindent 8 }} + {{- end }} + labels: + {{- include "kaelly-books.labels" . | nindent 8 }} + {{- with .Values.podLabels }} + {{- toYaml . | nindent 8 }} + {{- end }} + spec: + {{- with .Values.imagePullSecrets }} + imagePullSecrets: + {{- toYaml . | nindent 8 }} + {{- end }} + securityContext: + {{- toYaml .Values.podSecurityContext | nindent 8 }} + containers: + - name: {{ .Chart.Name }} + securityContext: + {{- toYaml .Values.securityContext | nindent 12 }} + image: "{{ .Values.image.repository }}:{{ .Values.image.tag | default .Chart.AppVersion }}" + imagePullPolicy: {{ .Values.image.pullPolicy }} + envFrom: + - configMapRef: + name: {{ .Release.Name }}-configmap + - secretRef: + name: {{ .Release.Name }}-secrets + livenessProbe: + {{- toYaml .Values.livenessProbe | nindent 12 }} + readinessProbe: + {{- toYaml .Values.readinessProbe | nindent 12 }} + 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/charts/kaelly-books/templates/secrets.yaml b/charts/kaelly-books/templates/secrets.yaml new file mode 100644 index 0000000..58b749a --- /dev/null +++ b/charts/kaelly-books/templates/secrets.yaml @@ -0,0 +1,13 @@ +apiVersion: v1 +kind: Secret +metadata: + name: {{ .Release.Name }}-secrets + namespace: {{ .Release.Namespace }} +type: Opaque +data: +{{- if .Values.secrets }} +{{- range $key, $value := .Values.secrets }} + {{ $key }}: {{ $value | b64enc }} +{{- end }} +{{- else }} +{{- end }} diff --git a/charts/kaelly-books/values.yaml b/charts/kaelly-books/values.yaml new file mode 100644 index 0000000..d226bd6 --- /dev/null +++ b/charts/kaelly-books/values.yaml @@ -0,0 +1,89 @@ +# Default values for kaelly-books. +# This is a YAML-formatted file. +# Declare variables to be passed into your templates. + +# This will set the replicaset count more information can be found here: https://kubernetes.io/docs/concepts/workloads/controllers/replicaset/ +replicaCount: 1 + +# This sets the container image more information can be found here: https://kubernetes.io/docs/concepts/containers/images/ +image: + repository: ghcr.io/kaellybot/kaelly-books + # This sets the pull policy for images. + pullPolicy: IfNotPresent + # Overrides the image tag whose default is the chart appVersion. + tag: "" + +# This is for the secretes for pulling an image from a private repository more information can be found here: https://kubernetes.io/docs/tasks/configure-pod-container/pull-image-private-registry/ +imagePullSecrets: [] +# This is to override the chart name. +nameOverride: "" +fullnameOverride: "" + +# This is for setting Kubernetes Annotations to a Pod. +# For more information checkout: https://kubernetes.io/docs/concepts/overview/working-with-objects/annotations/ +podAnnotations: {} +# This is for setting Kubernetes Labels to a Pod. +# For more information checkout: https://kubernetes.io/docs/concepts/overview/working-with-objects/labels/ +podLabels: {} + +podSecurityContext: + {} + # fsGroup: 2000 + +securityContext: + {} + # capabilities: + # drop: + # - ALL + # readOnlyRootFilesystem: true + # runAsNonRoot: true + # runAsUser: 1000 + +resources: + # We usually recommend not to specify default resources and to leave this as a conscious + # choice for the user. This also increases chances charts run on environments with little + # resources, such as Minikube. If you do want to specify resources, uncomment the following + # lines, adjust them as necessary, and remove the curly braces after 'resources:'. + limits: + # cpu: 100m + memory: 512Mi + # requests: + # cpu: 100m + # memory: 128Mi + +# This is to setup the liveness and readiness probes more information can be found here: https://kubernetes.io/docs/tasks/configure-pod-container/configure-liveness-readiness-startup-probes/ +livenessProbe: + httpGet: + path: /live + port: 9090 +readinessProbe: + httpGet: + path: /ready + port: 9090 + +#This section is for setting up autoscaling more information can be found here: https://kubernetes.io/docs/concepts/workloads/autoscaling/ +autoscaling: + enabled: false + minReplicas: 1 + maxReplicas: 100 + targetCPUUtilizationPercentage: 80 + # targetMemoryUtilizationPercentage: 80 + +nodeSelector: {} + +tolerations: [] + +affinity: {} + +configMap: + PROBE_PORT: "9090" + METRIC_PORT: "2112" + LOG_LEVEL: "info" + PRODUCTION: "false" + +secrets: + MYSQL_URL: "" + MYSQL_USER: "" + MYSQL_PASSWORD: "" + MYSQL_DATABASE: "" + RABBITMQ_ADDRESS: "" \ No newline at end of file From 0ef1ce91e41af5b76580871a0a89f3fc712eb3f4 Mon Sep 17 00:00:00 2001 From: Enzo Date: Fri, 29 Nov 2024 18:25:47 +0100 Subject: [PATCH 2/3] upd requests --- charts/kaelly-books/values.yaml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/charts/kaelly-books/values.yaml b/charts/kaelly-books/values.yaml index d226bd6..4ac984d 100644 --- a/charts/kaelly-books/values.yaml +++ b/charts/kaelly-books/values.yaml @@ -47,9 +47,9 @@ resources: limits: # cpu: 100m memory: 512Mi - # requests: + requests: # cpu: 100m - # memory: 128Mi + memory: 128Mi # This is to setup the liveness and readiness probes more information can be found here: https://kubernetes.io/docs/tasks/configure-pod-container/configure-liveness-readiness-startup-probes/ livenessProbe: From e94a04806d3e76be5b30002576eb968a5c2dba79 Mon Sep 17 00:00:00 2001 From: Enzo Date: Tue, 3 Dec 2024 23:03:24 +0100 Subject: [PATCH 3/3] fix helm chart deploy --- .github/workflows/ci.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 2903ca4..5dc662f 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -103,6 +103,7 @@ jobs: run: | helm upgrade --install ${{ github.event.repository.name }} charts/${{ github.event.repository.name }} \ --namespace kaellybot \ + --create-namespace \ --set image.tag="${{ env.image_tag_name }}" \ --set secrets.MYSQL_URL="${{ secrets.MYSQL_URL }}" \ --set secrets.MYSQL_USER="${{ secrets.MYSQL_USER }}" \