diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index 89f03b02058ff23e9b26bae89254a37392fa3d5d..2ba86ed04bb3418786b516004a8628c9559f5c92 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -78,11 +78,20 @@ services: --set registry.username=${DEPLOY_USER} --set registry.password=${DEPLOY_TOKEN} --set registry.host=${CI_REGISTRY} - --set galaxy.url=${GALAXY_BASE_URL} - --set galaxy.key=${GALAXY_API_KEY} --set redis.auth.password=${REDIS_PASSWORD} --set django.secretKey=${SECRET_KEY} --set django.hostLabel=${HOSTLABEL} + --set django.imagePullSecrets[0].name="registry-pull-secret-${CI_COMMIT_REF_SLUG}" + --set django.registry.username=${DEPLOY_USER} + --set django.registry.password=${DEPLOY_TOKEN} + --set django.registry.host=${CI_REGISTRY} + --set django.image.repository=${CI_REGISTRY_IMAGE}/server/${CI_COMMIT_REF_SLUG} + --set django.image.tag=${CI_COMMIT_SHORT_SHA} + --set django.image.pullPolicy=Always + --set django.service.port="8000" + --set django.galaxy.enabled=true + --set django.galaxy.url=${GALAXY_BASE_URL} + --set django.galaxy.key=${GALAXY_API_KEY} --set celery.replicaCount=${CELERY_REPLICA_COUNT} ${CI_COMMIT_REF_SLUG} ./chart/ diff --git a/chart/charts/django/.helmignore b/chart/charts/django/.helmignore new file mode 100644 index 0000000000000000000000000000000000000000..0e8a0eb36f4ca2c939201c0d54b5d82a1ea34778 --- /dev/null +++ b/chart/charts/django/.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/charts/django/Chart.yaml b/chart/charts/django/Chart.yaml new file mode 100644 index 0000000000000000000000000000000000000000..a17805d954066f5722ae9cb2f5951a7cc4a89ea1 --- /dev/null +++ b/chart/charts/django/Chart.yaml @@ -0,0 +1,24 @@ +apiVersion: v2 +name: django +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: 0.1.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.16.0" diff --git a/chart/charts/django/templates/NOTES.txt b/chart/charts/django/templates/NOTES.txt new file mode 100644 index 0000000000000000000000000000000000000000..140bcc6c7b60484d5ba768b55c525222683d05ca --- /dev/null +++ b/chart/charts/django/templates/NOTES.txt @@ -0,0 +1,22 @@ +1. Get the application URL by running these commands: +{{- if .Values.ingress.enabled }} +{{- range $host := .Values.ingress.hosts }} + {{- range .paths }} + http{{ if $.Values.ingress.tls }}s{{ end }}://{{ $host.host }}{{ .path }} + {{- end }} +{{- end }} +{{- else if contains "NodePort" .Values.service.type }} + export NODE_PORT=$(kubectl get --namespace {{ .Release.Namespace }} -o jsonpath="{.spec.ports[0].nodePort}" services {{ include "django.fullname" . }}) + export NODE_IP=$(kubectl get nodes --namespace {{ .Release.Namespace }} -o jsonpath="{.items[0].status.addresses[0].address}") + echo http://$NODE_IP:$NODE_PORT +{{- else if contains "LoadBalancer" .Values.service.type }} + NOTE: It may take a few minutes for the LoadBalancer IP to be available. + You can watch the status of by running 'kubectl get --namespace {{ .Release.Namespace }} svc -w {{ include "django.fullname" . }}' + export SERVICE_IP=$(kubectl get svc --namespace {{ .Release.Namespace }} {{ include "django.fullname" . }} --template "{{"{{ range (index .status.loadBalancer.ingress 0) }}{{.}}{{ end }}"}}") + echo http://$SERVICE_IP:{{ .Values.service.port }} +{{- else if contains "ClusterIP" .Values.service.type }} + export POD_NAME=$(kubectl get pods --namespace {{ .Release.Namespace }} -l "app.kubernetes.io/name={{ include "django.name" . }},app.kubernetes.io/instance={{ .Release.Name }}" -o jsonpath="{.items[0].metadata.name}") + export CONTAINER_PORT=$(kubectl get pod --namespace {{ .Release.Namespace }} $POD_NAME -o jsonpath="{.spec.containers[0].ports[0].containerPort}") + echo "Visit http://127.0.0.1:8080 to use your application" + kubectl --namespace {{ .Release.Namespace }} port-forward $POD_NAME 8080:$CONTAINER_PORT +{{- end }} diff --git a/chart/charts/django/templates/_helpers.tpl b/chart/charts/django/templates/_helpers.tpl new file mode 100644 index 0000000000000000000000000000000000000000..bbc3dda2023b2b03539c2b9e220456a379c51832 --- /dev/null +++ b/chart/charts/django/templates/_helpers.tpl @@ -0,0 +1,62 @@ +{{/* +Expand the name of the chart. +*/}} +{{- define "django.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 "django.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 "django.chart" -}} +{{- printf "%s-%s" .Chart.Name .Chart.Version | replace "+" "_" | trunc 63 | trimSuffix "-" }} +{{- end }} + +{{/* +Common labels +*/}} +{{- define "django.labels" -}} +helm.sh/chart: {{ include "django.chart" . }} +{{ include "django.selectorLabels" . }} +{{- if .Chart.AppVersion }} +app.kubernetes.io/version: {{ .Chart.AppVersion | quote }} +{{- end }} +app.kubernetes.io/managed-by: {{ .Release.Service }} +{{- end }} + +{{/* +Selector labels +*/}} +{{- define "django.selectorLabels" -}} +app.kubernetes.io/name: {{ include "django.name" . }} +app.kubernetes.io/instance: {{ .Release.Name }} +{{- end }} + +{{/* +Create the name of the service account to use +*/}} +{{- define "django.serviceAccountName" -}} +{{- if .Values.serviceAccount.create }} +{{- default (include "django.fullname" .) .Values.serviceAccount.name }} +{{- else }} +{{- default "default" .Values.serviceAccount.name }} +{{- end }} +{{- end }} diff --git a/chart/charts/django/templates/_secretHelper.tpl b/chart/charts/django/templates/_secretHelper.tpl new file mode 100644 index 0000000000000000000000000000000000000000..ee073536c3605a197feaed4cb5fc393e8bd8bfe1 --- /dev/null +++ b/chart/charts/django/templates/_secretHelper.tpl @@ -0,0 +1,7 @@ +{{- define "imagePullSecret" }} +{{- if .Values.registry }} +{{- if and .Values.registry.password .Values.registry.username }} +{{- printf "{\"auths\": {\"%s\": {\"auth\": \"%s\"}}}" .Values.registry.host (printf "%s:%s" .Values.registry.username .Values.registry.password | b64enc) | b64enc }} +{{- end }} +{{- end }} +{{- end }} diff --git a/chart/charts/django/templates/deployment.yaml b/chart/charts/django/templates/deployment.yaml new file mode 100644 index 0000000000000000000000000000000000000000..f10bbd54ecfd7b7c02df7dd633c1c8ced627c87e --- /dev/null +++ b/chart/charts/django/templates/deployment.yaml @@ -0,0 +1,120 @@ +{{- $fullname := include "django.fullname" . -}} +apiVersion: apps/v1 +kind: Deployment +metadata: + name: {{ $fullname }} + labels: + {{- include "django.labels" . | nindent 4 }} +spec: + {{- if not .Values.autoscaling.enabled }} + replicas: {{ .Values.replicaCount }} + {{- end }} + selector: + matchLabels: + {{- include "django.selectorLabels" . | nindent 6 }} + strategy: + type: RollingUpdate + rollingUpdate: + maxSurge: 1 + maxUnavailable: 0 + template: + metadata: + {{- with .Values.podAnnotations }} + annotations: + {{- toYaml . | nindent 8 }} + {{- end }} + labels: + {{- include "django.selectorLabels" . | nindent 8 }} + spec: + {{- with .Values.imagePullSecrets }} + imagePullSecrets: + {{- toYaml . | nindent 8 }} + {{- end }} + serviceAccountName: {{ include "django.serviceAccountName" . }} + securityContext: + {{- toYaml .Values.podSecurityContext | nindent 8 }} + initContainers: # check if DB is ready, and only continue when true + - name: check-db-ready + image: postgres:14 + command: + - sh + - -c + - {{ printf "until pg_isready -h %s-postgresql -p 5432; do echo waiting for database; sleep 2; done;" .Release.Name}} + securityContext: + runAsGroup: 999 + runAsUser: 999 + containers: + - name: {{ .Chart.Name }} + securityContext: + {{- toYaml .Values.securityContext | nindent 12 }} + image: "{{ .Values.image.repository }}:{{ .Values.image.tag | default .Chart.AppVersion }}" + imagePullPolicy: {{ .Values.image.pullPolicy }} + env: + - name: DEBUG + value: "true" + - name: DATABASE_HOST + value: {{ printf "%s-postgresql" .Release.Name }} + - name: DATABASE_NAME + value: {{ $dbName }} + - name: DATABASE_PASSWORD + valueFrom: + secretKeyRef: + name: {{ printf "%s.%s-postgresql.credentials.postgresql.acid.zalan.do" $dbUser .Release.Name }} + key: password + - name: DATABASE_USER + valueFrom: + secretKeyRef: + name: {{ printf "%s.%s-postgresql.credentials.postgresql.acid.zalan.do" $dbUser .Release.Name }} + key: username + {{- if .Values.galaxy.enabled }} + - name: GALAXY_BASE_URL + value: {{ .Values.galaxy.url }} + - name: GALAXY_API_KEY + valueFrom: + secretKeyRef: + name: "{{ $fullname }}-credentials" + key: galaxy_api_key + {{- end }} + - name: SECRET_KEY + valueFrom: + secretKeyRef: + name: "{{ $fullname }}-credentials" + key: secret_key + - name: ALLOWED_HOSTS + value: "*" + - name: DJANGO_ENV + value: prod + - name: REDIS_HOST + valueFrom: + secretKeyRef: + name: "{{ $fullname }}-credentials" + key: redis_host + - name: HOSTLABEL + value: {{ .Values.django.hostLabel }} + ports: + - name: http + containerPort: {{ .Values.service.port }} + protocol: TCP + livenessProbe: + httpGet: + path: / + port: http + 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/charts/django/templates/hpa.yaml b/chart/charts/django/templates/hpa.yaml new file mode 100644 index 0000000000000000000000000000000000000000..7be290c4d1c968f0394861740886cdd74b217dca --- /dev/null +++ b/chart/charts/django/templates/hpa.yaml @@ -0,0 +1,28 @@ +{{- if .Values.autoscaling.enabled }} +apiVersion: autoscaling/v2beta1 +kind: HorizontalPodAutoscaler +metadata: + name: {{ include "django.fullname" . }} + labels: + {{- include "django.labels" . | nindent 4 }} +spec: + scaleTargetRef: + apiVersion: apps/v1 + kind: Deployment + name: {{ include "django.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/chart/charts/django/templates/ingress.yaml b/chart/charts/django/templates/ingress.yaml new file mode 100644 index 0000000000000000000000000000000000000000..51153510cb80cdcdb0bc30302a5d37c90cab97d4 --- /dev/null +++ b/chart/charts/django/templates/ingress.yaml @@ -0,0 +1,61 @@ +{{- if .Values.ingress.enabled -}} +{{- $fullName := include "django.fullname" . -}} +{{- $svcPort := .Values.service.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: + {{- include "django.labels" . | nindent 4 }} + {{- 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/chart/charts/django/templates/registry-pull-secret.yaml b/chart/charts/django/templates/registry-pull-secret.yaml new file mode 100644 index 0000000000000000000000000000000000000000..323538d3bd4717fe5e46c0e345128f03eb1dbb18 --- /dev/null +++ b/chart/charts/django/templates/registry-pull-secret.yaml @@ -0,0 +1,17 @@ +{{- if .Values.registry }} +{{- if and .Values.registry.password .Values.registry.username }} +# When creating a pod based on an image that resides in a private Docker registry (a secure registry protected by basic authentication), +# one must specify an imagePullSecrets as part of the pod's spec. +# (see https://kubernetes.io/docs/tasks/configure-pod-container/pull-image-private-registry/#create-a-pod-that-uses-your-secret) + +# This resource creates a docker registry pull secret that's based on values provided in values.yaml (it relies on a template defined in _secretHelper.tpl). The secret must be deployed to +# the same namespace in which the pod will be created. +apiVersion: v1 +kind: Secret +metadata: + name : {{ (index .Values.imagePullSecrets 0).name }} +type: kubernetes.io/dockerconfigjson +data: + .dockerconfigjson: {{ template "imagePullSecret" . }} +{{- end }} +{{- end }} diff --git a/chart/charts/django/templates/secret-credentials.yaml b/chart/charts/django/templates/secret-credentials.yaml new file mode 100644 index 0000000000000000000000000000000000000000..633d8127b9838ffd74f356ff6d41b977a548502a --- /dev/null +++ b/chart/charts/django/templates/secret-credentials.yaml @@ -0,0 +1,12 @@ +apiVersion: v1 +kind: Secret +metadata: + # name: {{ printf "%s-credentials" .Release.Name }} + name: "{{ include 'django.fullname' . }}-credentials" +type: Opaque +data: + secret_key: {{ .Values.secretKey | b64enc | quote }} + {{- if .Values.galaxy.enabled }} + galaxy_api_key : {{ .Values.galaxy.key | b64enc | quote }} + {{- end }} + redis_host: {{ printf "redis://:%s@%s-redis-master" .Values.redis.auth.password .Release.Name | b64enc | quote }} \ No newline at end of file diff --git a/chart/charts/django/templates/service.yaml b/chart/charts/django/templates/service.yaml new file mode 100644 index 0000000000000000000000000000000000000000..4d94ef751e6da52625b455f474999bb9ceab0cf0 --- /dev/null +++ b/chart/charts/django/templates/service.yaml @@ -0,0 +1,15 @@ +apiVersion: v1 +kind: Service +metadata: + name: {{ include "django.fullname" . }} + labels: + {{- include "django.labels" . | nindent 4 }} +spec: + type: {{ .Values.service.type }} + ports: + - port: {{ .Values.service.port }} + targetPort: http + protocol: TCP + name: http + selector: + {{- include "django.selectorLabels" . | nindent 4 }} diff --git a/chart/charts/django/templates/serviceaccount.yaml b/chart/charts/django/templates/serviceaccount.yaml new file mode 100644 index 0000000000000000000000000000000000000000..c686c91825b95874e87640e06fd91709b1ae29e7 --- /dev/null +++ b/chart/charts/django/templates/serviceaccount.yaml @@ -0,0 +1,12 @@ +{{- if .Values.serviceAccount.create -}} +apiVersion: v1 +kind: ServiceAccount +metadata: + name: {{ include "django.serviceAccountName" . }} + labels: + {{- include "django.labels" . | nindent 4 }} + {{- with .Values.serviceAccount.annotations }} + annotations: + {{- toYaml . | nindent 4 }} + {{- end }} +{{- end }} diff --git a/chart/charts/django/templates/tests/test-connection.yaml b/chart/charts/django/templates/tests/test-connection.yaml new file mode 100644 index 0000000000000000000000000000000000000000..d3e8da47776d32687fa5e52d97c148e91f6f31b0 --- /dev/null +++ b/chart/charts/django/templates/tests/test-connection.yaml @@ -0,0 +1,15 @@ +apiVersion: v1 +kind: Pod +metadata: + name: "{{ include "django.fullname" . }}-test-connection" + labels: + {{- include "django.labels" . | nindent 4 }} + annotations: + "helm.sh/hook": test +spec: + containers: + - name: wget + image: busybox + command: ['wget'] + args: ['{{ include "django.fullname" . }}:{{ .Values.service.port }}'] + restartPolicy: Never diff --git a/chart/charts/django/values.yaml b/chart/charts/django/values.yaml new file mode 100644 index 0000000000000000000000000000000000000000..8f00c007ac6cce8e4ac92d94e292217877573d7b --- /dev/null +++ b/chart/charts/django/values.yaml @@ -0,0 +1,104 @@ +# Default values for django. +# This is a YAML-formatted file. +# Declare variables to be passed into your templates. + +replicaCount: 1 + +image: + repository: nginx + pullPolicy: IfNotPresent + # Overrides the image tag whose default is the chart appVersion. + tag: "" + +registry: + image: registry-image + host: my-secure-private-registry.com + username: myUser + password: myPass + +imagePullSecrets: + - name: registry-pull-secret + +nameOverride: "" +fullnameOverride: "" + +serviceAccount: + # Specifies whether a service account should be created + create: true + # Annotations to add to the service account + annotations: {} + # The name of the service account to use. + # If not set and create is true, a name is generated using the fullname template + name: "" + +podAnnotations: {} + +podSecurityContext: {} + # fsGroup: 2000 + +securityContext: + # capabilities: + # drop: + # - ALL + readOnlyRootFilesystem: true + runAsNonRoot: true + runAsUser: 1001 + runAsGroup: 1001 +service: + type: ClusterIP + port: 80 + +ingress: + enabled: false + className: "" + annotations: {} + # kubernetes.io/ingress.class: nginx + # kubernetes.io/tls-acme: "true" + hosts: + - host: chart-example.local + paths: + - path: / + pathType: ImplementationSpecific + tls: [] + # - secretName: chart-example-tls + # hosts: + # - chart-example.local + +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: 128Mi + # requests: + # cpu: 100m + # memory: 128Mi + +autoscaling: + enabled: false + minReplicas: 1 + maxReplicas: 100 + targetCPUUtilizationPercentage: 80 + # targetMemoryUtilizationPercentage: 80 + +nodeSelector: {} + +tolerations: [] + +affinity: {} + + +secretKey: my-secret-key + +database: + user: "" + name: "" + host: "" + + +galaxy: + enable: false + url: "" + key: "my-secret-key" \ No newline at end of file diff --git a/chart/templates/deployment-django.yaml b/chart/templates/_deployment-django.yaml similarity index 100% rename from chart/templates/deployment-django.yaml rename to chart/templates/_deployment-django.yaml