Skip to content

Commit

Permalink
build: add helm chart for deployment in Kubernetes (#1427)
Browse files Browse the repository at this point in the history
* fix: add favicon.ico to public frontend dir

* fix: make ENCRYPTION_KEY variable mandatory for settings

* docs: update project description

* build: add basic helm chart for deployment
  • Loading branch information
spwoodcock authored Apr 8, 2024
1 parent 853304f commit 84c9448
Show file tree
Hide file tree
Showing 18 changed files with 595 additions and 3 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -89,3 +89,6 @@ envsubst

# Scripts
**/openapi.json

# Chart dependencies
chart/charts
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

![HOT Logo](https://github.com/hotosm/fmtm/blob/main/images/hot_logo.png?raw=true)

A project to provide tools for Open Mapping campaigns.
Coordinated field mapping for Open Mapping campaigns.

<!-- markdownlint-disable -->

Expand Down
23 changes: 23 additions & 0 deletions chart/.helmignore
Original file line number Diff line number Diff line change
@@ -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/
9 changes: 9 additions & 0 deletions chart/Chart.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
dependencies:
- name: postgresql
repository: oci://registry-1.docker.io/bitnamicharts
version: 15.2.2
- name: minio
repository: https://charts.min.io
version: 5.1.0
digest: sha256:3801e669631e6bd2f742dd745bfea2d61b06e9b6ac4ea38196340703c2c1fa08
generated: "2024-04-05T14:57:56.774720071Z"
22 changes: 22 additions & 0 deletions chart/Chart.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
apiVersion: v2
type: application
name: fmtm
description: Field Mapping Tasking Manager - coordinated field mapping.
version: "0.1.0"
appVersion: "2024.2.0"
maintainers:
- email: [email protected]
name: Sam Woodcock
home: https://github.com/hotosm/fmtm/tree/main/chart
icon: https://fmtm.hotosm.org/favicon.ico
dependencies:
- name: postgresql
version: 15.2.2
repository: oci://registry-1.docker.io/bitnamicharts
condition: db.enabled
alias: db
- name: minio
version: 5.1.0
repository: https://charts.min.io
condition: s3.enabled
alias: s3
70 changes: 70 additions & 0 deletions chart/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
# HOTOSM FMTM Chart

Chart for HOTOSM Field Mapping Tasking Manager.

## Secrets

Requires secrets to be pre-populated.

- **db-fmtm-vars** for postgres database

- key: FMTM_DB_HOST
- key: FMTM_DB_USER
- key: FMTM_DB_PASSWORD
- key: FMTM_DB_NAME

```bash
kubectl create secret generic db-fmtm-vars \
--from-literal=FMTM_DB_HOST=fmtm-db.fmtm.svc.cluster.local \
--from-literal=FMTM_DB_USER=xxxxxxx \
--from-literal=FMTM_DB_PASSWORD=xxxxxxx \
--from-literal=FMTM_DB_NAME=xxxxxxx
```

- **s3-fmtm-vars** for Minio S3

- key: S3_ENDPOINT
- key: S3_ACCESS_KEY
- key: S3_SECRET_KEY
- key: S3_BUCKET_NAME

```bash
kubectl create secret generic s3-fmtm-vars \
--from-literal=S3_ENDPOINT=fmtm-s3.fmtm.svc.cluster.local \
--from-literal=S3_ACCESS_KEY=fmtm \
--from-literal=S3_SECRET_KEY=xxxxxxx \
--from-literal=S3_BUCKET_NAME=fmtm-data
```

- **api-fmtm-vars** for FastAPI

- key: ENCRYPTION_KEY
- key: FMTM_DOMAIN
- key: OSM_CLIENT_ID
- key: OSM_CLIENT_SECRET
- key: OSM_SECRET_KEY

```bash
kubectl create secret generic api-fmtm-vars \
--from-literal=ENCRYPTION_KEY=xxxxxxx \
--from-literal=FMTM_DOMAIN=some.domain.com \
--from-literal=OSM_CLIENT_ID=xxxxxxx \
--from-literal=OSM_CLIENT_SECRET=xxxxxxx \
--from-literal=OSM_SECRET_KEY=xxxxxxx
```

## Deployment

```bash
helm upgrade --install fmtm oci://ghcr.io/hotosm/fmtm \
--namespace fmtm --create-namespace
```

Values can be overriden using `values.yaml` or the `--set` flag.

```bash
helm upgrade --install fmtm . \
--set image.tag=development \
--set image.pullPolicy="Always" \
--set domain="some.new.domain"
```
18 changes: 18 additions & 0 deletions chart/templates/NOTES.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
1. Get the application URL by running these commands:
{{- if .Values.backend.ingress.enabled }}
http{{ if $.Values.backend.ingress.tls }}s{{ end }}://{{ .Values.domain }}
{{- else if contains "NodePort" .Values.backend.service.type }}
export NODE_PORT=$(kubectl get --namespace {{ .Release.Namespace }} -o jsonpath="{.spec.ports[0].nodePort}" services {{ include "fmtm.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.backend.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 "fmtm.fullname" . }}'
export SERVICE_IP=$(kubectl get svc --namespace {{ .Release.Namespace }} {{ include "fmtm.fullname" . }} --template "{{"{{ range (index .status.loadBalancer.ingress 0) }}{{.}}{{ end }}"}}")
echo http://$SERVICE_IP:{{ .Values.backend.service.port }}
{{- else if contains "ClusterIP" .Values.backend.service.type }}
export POD_NAME=$(kubectl get pods --namespace {{ .Release.Namespace }} -l "app.kubernetes.io/name={{ include "fmtm.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 }}
62 changes: 62 additions & 0 deletions chart/templates/_helpers.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
{{/*
Expand the name of the chart.
*/}}
{{- define "fmtm.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 "fmtm.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 "fmtm.chart" -}}
{{- printf "%s-%s" .Chart.Name .Chart.Version | replace "+" "_" | trunc 63 | trimSuffix "-" }}
{{- end }}

{{/*
Common labels
*/}}
{{- define "fmtm.labels" -}}
helm.sh/chart: {{ include "fmtm.chart" . }}
{{ include "fmtm.selectorLabels" . }}
{{- if .Chart.AppVersion }}
app.kubernetes.io/version: {{ .Chart.AppVersion | quote }}
{{- end }}
app.kubernetes.io/managed-by: {{ .Release.Service }}
{{- end }}

{{/*
Selector labels
*/}}
{{- define "fmtm.selectorLabels" -}}
app.kubernetes.io/name: {{ include "fmtm.name" . }}
app.kubernetes.io/instance: {{ .Release.Name }}
{{- end }}

{{/*
Create the name of the service account to use
*/}}
{{- define "fmtm.serviceAccountName" -}}
{{- if .Values.serviceAccount.create }}
{{- default (include "fmtm.fullname" .) .Values.serviceAccount.name }}
{{- else }}
{{- default "default" .Values.serviceAccount.name }}
{{- end }}
{{- end }}
92 changes: 92 additions & 0 deletions chart/templates/deployment.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
apiVersion: apps/v1
kind: Deployment
metadata:
name: {{ include "fmtm.fullname" . }}
labels:
{{- include "fmtm.labels" . | nindent 4 }}
spec:
{{- if not .Values.backend.autoscaling.enabled }}
replicas: {{ .Values.replicaCount }}
{{- end }}
selector:
matchLabels:
{{- include "fmtm.selectorLabels" . | nindent 6 }}
template:
metadata:
{{- with .Values.podAnnotations }}
annotations:
{{- toYaml . | nindent 8 }}
{{- end }}
labels:
{{- include "fmtm.labels" . | nindent 8 }}
{{- with .Values.podLabels }}
{{- toYaml . | nindent 8 }}
{{- end }}
spec:
{{- with .Values.imagePullSecrets }}
imagePullSecrets:
{{- toYaml . | nindent 8 }}
{{- end }}
serviceAccountName: {{ include "fmtm.serviceAccountName" . }}
securityContext:
{{- toYaml .Values.podSecurityContext | nindent 8 }}
initContainers:
{{- if .Values.backend.ingress.disabled -}}
- name: db-check
image: {{ .Values.db.image.registry }}/{{ .Values.db.image.repository }}:{{ .Values.db.image.tag }}
imagePullPolicy: IfNotPresent
envFrom:
{{- toYaml .Values.backend.extraEnvFrom | nindent 12 }}
command:
- /bin/bash
- -c
- |
until pg_isready -h "$FMTM_DB_HOST"; do
echo "waiting for database to be ready";
sleep 2;
done;
{{- end }}
- name: migrations
image: "{{ .Values.image.repository }}/backend:{{ .Values.image.tag | default .Chart.AppVersion }}"
imagePullPolicy: {{ .Values.image.pullPolicy }}
envFrom:
{{- toYaml .Values.backend.extraEnvFrom | nindent 12 }}
command: ["/migrate-entrypoint.sh"]
containers:
- name: {{ .Chart.Name }}
securityContext:
{{- toYaml .Values.securityContext | nindent 12 }}
image: "{{ .Values.image.repository }}/backend:{{ .Values.image.tag | default .Chart.AppVersion }}"
imagePullPolicy: {{ .Values.image.pullPolicy }}
ports:
- name: http
containerPort: {{ .Values.backend.service.port }}
protocol: TCP
livenessProbe:
{{- toYaml .Values.backend.livenessProbe | nindent 12 }}
readinessProbe:
{{- toYaml .Values.backend.readinessProbe | nindent 12 }}
resources:
{{- toYaml .Values.backend.resources | nindent 12 }}
envFrom:
{{- toYaml .Values.backend.extraEnvFrom | nindent 12 }}
volumeMounts:
{{- with .Values.backend.volumeMounts }}
{{- toYaml . | nindent 12 }}
{{- end }}
{{- with .Values.backend.volumes }}
volumes:
{{- toYaml . | nindent 8 }}
{{- end }}
{{- with .Values.nodeSelector }}
nodeSelector:
{{- toYaml . | nindent 8 }}
{{- end }}
{{- with .Values.affinity }}
affinity:
{{- toYaml . | nindent 8 }}
{{- end }}
{{- with .Values.tolerations }}
tolerations:
{{- toYaml . | nindent 8 }}
{{- end }}
32 changes: 32 additions & 0 deletions chart/templates/hpa.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
{{- if .Values.backend.autoscaling.enabled }}
apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
metadata:
name: {{ include "fmtm.fullname" . }}
labels:
{{- include "fmtm.labels" . | nindent 4 }}
spec:
scaleTargetRef:
apiVersion: apps/v1
kind: Deployment
name: {{ include "fmtm.fullname" . }}
minReplicas: {{ .Values.backend.autoscaling.minReplicas }}
maxReplicas: {{ .Values.backend.autoscaling.maxReplicas }}
metrics:
{{- if .Values.backend.autoscaling.targetCPUUtilizationPercentage }}
- type: Resource
resource:
name: cpu
target:
type: Utilization
averageUtilization: {{ .Values.backend.autoscaling.targetCPUUtilizationPercentage }}
{{- end }}
{{- if .Values.backend.autoscaling.targetMemoryUtilizationPercentage }}
- type: Resource
resource:
name: memory
target:
type: Utilization
averageUtilization: {{ .Values.backend.autoscaling.targetMemoryUtilizationPercentage }}
{{- end }}
{{- end }}
40 changes: 40 additions & 0 deletions chart/templates/ingress.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
{{- if .Values.backend.ingress.enabled -}}
{{- $fullName := include "fmtm.fullname" . -}}
{{- $svcPort := .Values.backend.service.port -}}
{{- if not (hasKey .Values.backend.ingress.annotations "kubernetes.io/ingress.class") }}
{{- $_ := set .Values.backend.ingress.annotations "kubernetes.io/ingress.class" .Values.backend.ingress.className}}
{{- end }}
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: {{ $fullName }}
labels:
{{- include "fmtm.labels" . | nindent 4 }}
{{- with .Values.backend.ingress.annotations }}
annotations:
{{- toYaml . | nindent 4 }}
{{- end }}
spec:
ingressClassName: {{ .Values.backend.ingress.className }}
{{- if .Values.backend.ingress.tls }}
tls:
{{- range .Values.backend.ingress.tls }}
- hosts:
{{- range .hosts }}
- {{ . | quote }}
{{- end }}
secretName: {{ .secretName }}
{{- end }}
{{- end }}
rules:
- host: "api.{{ .Values.domain }}"
http:
paths:
- path: /
pathType: ImplementationSpecific
backend:
service:
name: {{ $fullName }}
port:
number: {{ $svcPort }}
{{- end }}
Loading

0 comments on commit 84c9448

Please sign in to comment.