commit c908d5f1209189ebd2604ebe73a00a5d576f42ea Author: Mathias Beaulieu-Duncan Date: Tue Feb 18 14:39:05 2025 -0500 initial commit diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..fe94993 --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +.idea/ +.DS_Store + +values.override.yaml \ No newline at end of file diff --git a/.helmignore b/.helmignore new file mode 100644 index 0000000..0e8a0eb --- /dev/null +++ b/.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.yaml b/Chart.yaml new file mode 100644 index 0000000..8b24fb5 --- /dev/null +++ b/Chart.yaml @@ -0,0 +1,24 @@ +apiVersion: v2 +name: helm-osrm-backend +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: "v5.27.1" diff --git a/readme.md b/readme.md new file mode 100644 index 0000000..d0e3f43 --- /dev/null +++ b/readme.md @@ -0,0 +1,54 @@ +# Helm Chart + +## Limitation +osrm-extract use allot of memory and if it doesn't have enough the init container will crash and the deployment will fail. Depending on the dataset you are using make sure your pod has enough memory to process the freshly new data it's pulling on deployment. + +## Configuration +Configure your ingress, PVC and settings depending on the dataset you need. +Configuring your PVC with a delete claim policy is recommended if you want to pull the latest dataset when you deploy a new version. +Configuring cors origins to your apps is suggested if you want to avoid the public using your resources. + +> Copy the `values.override.example.yaml` file and rename it to `values.override.yaml` +> It's preferable using the `values.override.yaml` so you can always pull a future version of the chart without overriding your configuration. + +## Debug +```bash +helm template osrm-backend . +``` + +## Install +```bash +helm install osrm-backend . -f values.override.yaml -n osrm +``` + +> Warning! Currently upgrading is not working properly because the new pod tries to attach to the same PVC as the current container. This need to be improved! + +## Update +```bash +helm upgrade osrm-backend . -f values.override.yaml -n osrm +``` + +## Uninstall +```bash +helm uninstall osrm-backend -n osrm +``` + +check init containers status +```bash +kubectl logs -c -n osrm -f +``` + +default init containers +``` +osrm-download +osrm-init +``` + +check logs of the running pod +```bash +kubectl logs -n osrm -f +``` + +## Improvements +- [ ] Allow multiple init container configuration to process multiple `.osm.pbf` files +- [ ] Make PVC Dynamic per release to allow upgrading the helm chart (this also make sure to pull the latest datasets) diff --git a/templates/NOTES.txt b/templates/NOTES.txt new file mode 100644 index 0000000..65bf71f --- /dev/null +++ b/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 "helm-osrm-backend.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 its status by running 'kubectl get --namespace {{ .Release.Namespace }} svc -w {{ include "helm-osrm-backend.fullname" . }}' + export SERVICE_IP=$(kubectl get svc --namespace {{ .Release.Namespace }} {{ include "helm-osrm-backend.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 "helm-osrm-backend.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/templates/_helpers.tpl b/templates/_helpers.tpl new file mode 100644 index 0000000..3a04f0b --- /dev/null +++ b/templates/_helpers.tpl @@ -0,0 +1,62 @@ +{{/* +Expand the name of the chart. +*/}} +{{- define "helm-osrm-backend.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 "helm-osrm-backend.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 "helm-osrm-backend.chart" -}} +{{- printf "%s-%s" .Chart.Name .Chart.Version | replace "+" "_" | trunc 63 | trimSuffix "-" }} +{{- end }} + +{{/* +Common labels +*/}} +{{- define "helm-osrm-backend.labels" -}} +helm.sh/chart: {{ include "helm-osrm-backend.chart" . }} +{{ include "helm-osrm-backend.selectorLabels" . }} +{{- if .Chart.AppVersion }} +app.kubernetes.io/version: {{ .Chart.AppVersion | quote }} +{{- end }} +app.kubernetes.io/managed-by: {{ .Release.Service }} +{{- end }} + +{{/* +Selector labels +*/}} +{{- define "helm-osrm-backend.selectorLabels" -}} +app.kubernetes.io/name: {{ include "helm-osrm-backend.name" . }} +app.kubernetes.io/instance: {{ .Release.Name }} +{{- end }} + +{{/* +Create the name of the service account to use +*/}} +{{- define "helm-osrm-backend.serviceAccountName" -}} +{{- if .Values.serviceAccount.create }} +{{- default (include "helm-osrm-backend.fullname" .) .Values.serviceAccount.name }} +{{- else }} +{{- default "default" .Values.serviceAccount.name }} +{{- end }} +{{- end }} diff --git a/templates/deployment.yaml b/templates/deployment.yaml new file mode 100644 index 0000000..fb13664 --- /dev/null +++ b/templates/deployment.yaml @@ -0,0 +1,85 @@ +apiVersion: apps/v1 +kind: Deployment +metadata: + name: {{ include "helm-osrm-backend.fullname" . }} + labels: + {{- include "helm-osrm-backend.labels" . | nindent 4 }} +spec: + {{- if not .Values.autoscaling.enabled }} + replicas: {{ .Values.replicaCount }} + {{- end }} + selector: + matchLabels: + {{- include "helm-osrm-backend.selectorLabels" . | nindent 6 }} + template: + metadata: + {{- with .Values.podAnnotations }} + annotations: + {{- toYaml . | nindent 8 }} + {{- end }} + labels: + {{- include "helm-osrm-backend.labels" . | nindent 8 }} + {{- with .Values.podLabels }} + {{- toYaml . | nindent 8 }} + {{- end }} + spec: + {{- with .Values.imagePullSecrets }} + imagePullSecrets: + {{- toYaml . | nindent 8 }} + {{- end }} + serviceAccountName: {{ include "helm-osrm-backend.serviceAccountName" . }} + securityContext: + {{- toYaml .Values.podSecurityContext | nindent 8 }} + {{- if .Values.initContainer.enabled }} + initContainers: + - name: osrm-download + image: busybox + command: [ "sh", "-c" ] + args: + - wget -O /data/{{ .Values.initContainer.fileName }} {{ .Values.initContainer.url }}; + volumeMounts: + - name: osrm-data + mountPath: {{ .Values.volume.mountPath }} + - name: osrm-init + image: "{{ .Values.image.repository }}:{{ .Values.image.tag | default .Chart.AppVersion }}" + command: {{ .Values.initContainer.command | toYaml | nindent 10 }} + volumeMounts: + - name: osrm-data + mountPath: {{ .Values.volume.mountPath }} + {{- end }} + 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: {{ .Values.service.port }} + protocol: TCP + livenessProbe: + {{- toYaml .Values.livenessProbe | nindent 12 }} + readinessProbe: + {{- toYaml .Values.readinessProbe | nindent 12 }} + resources: + {{- toYaml .Values.resources | nindent 12 }} + volumeMounts: + - name: osrm-data + mountPath: {{ .Values.volume.mountPath }} + command: {{ .Values.command | toYaml | nindent 10 }} + volumes: + - name: osrm-data + persistentVolumeClaim: + claimName: {{ .Values.volume.existingClaim | default (include "helm-osrm-backend.fullname" .) }} + {{- 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/templates/hpa.yaml b/templates/hpa.yaml new file mode 100644 index 0000000..ed65bfc --- /dev/null +++ b/templates/hpa.yaml @@ -0,0 +1,32 @@ +{{- if .Values.autoscaling.enabled }} +apiVersion: autoscaling/v2 +kind: HorizontalPodAutoscaler +metadata: + name: {{ include "helm-osrm-backend.fullname" . }} + labels: + {{- include "helm-osrm-backend.labels" . | nindent 4 }} +spec: + scaleTargetRef: + apiVersion: apps/v1 + kind: Deployment + name: {{ include "helm-osrm-backend.fullname" . }} + minReplicas: {{ .Values.autoscaling.minReplicas }} + maxReplicas: {{ .Values.autoscaling.maxReplicas }} + metrics: + {{- if .Values.autoscaling.targetCPUUtilizationPercentage }} + - type: Resource + resource: + name: cpu + target: + type: Utilization + averageUtilization: {{ .Values.autoscaling.targetCPUUtilizationPercentage }} + {{- end }} + {{- if .Values.autoscaling.targetMemoryUtilizationPercentage }} + - type: Resource + resource: + name: memory + target: + type: Utilization + averageUtilization: {{ .Values.autoscaling.targetMemoryUtilizationPercentage }} + {{- end }} +{{- end }} diff --git a/templates/ingress.yaml b/templates/ingress.yaml new file mode 100644 index 0000000..792c6b8 --- /dev/null +++ b/templates/ingress.yaml @@ -0,0 +1,43 @@ +{{- if .Values.ingress.enabled -}} +apiVersion: networking.k8s.io/v1 +kind: Ingress +metadata: + name: {{ include "helm-osrm-backend.fullname" . }} + labels: + {{- include "helm-osrm-backend.labels" . | nindent 4 }} + {{- with .Values.ingress.annotations }} + annotations: + {{- toYaml . | nindent 4 }} + {{- end }} +spec: + {{- with .Values.ingress.className }} + ingressClassName: {{ . }} + {{- end }} + {{- if .Values.ingress.tls }} + tls: + {{- range .Values.ingress.extraTls }} + - hosts: + {{- range .hosts }} + - {{ . | quote }} + {{- end }} + secretName: {{ .secretName }} + {{- end }} + {{- end }} + rules: + {{- range .Values.ingress.hosts }} + - host: {{ .host | quote }} + http: + paths: + {{- range .paths }} + - path: {{ .path }} + {{- with .pathType }} + pathType: {{ . }} + {{- end }} + backend: + service: + name: {{ include "helm-osrm-backend.fullname" $ }} + port: + number: {{ $.Values.service.port }} + {{- end }} + {{- end }} +{{- end }} diff --git a/templates/pvc.yaml b/templates/pvc.yaml new file mode 100644 index 0000000..5d73e44 --- /dev/null +++ b/templates/pvc.yaml @@ -0,0 +1,12 @@ +apiVersion: v1 +kind: PersistentVolumeClaim +metadata: + name: {{ include "helm-osrm-backend.fullname" . }} + namespace: {{ .Release.Namespace }} +spec: + accessModes: + - {{ .Values.volume.accessMode }} + resources: + requests: + storage: {{ .Values.volume.size }} + storageClassName: {{ .Values.volume.storageClass }} \ No newline at end of file diff --git a/templates/service.yaml b/templates/service.yaml new file mode 100644 index 0000000..7104906 --- /dev/null +++ b/templates/service.yaml @@ -0,0 +1,15 @@ +apiVersion: v1 +kind: Service +metadata: + name: {{ include "helm-osrm-backend.fullname" . }} + labels: + {{- include "helm-osrm-backend.labels" . | nindent 4 }} +spec: + type: {{ .Values.service.type }} + ports: + - port: {{ .Values.service.port }} + targetPort: http + protocol: TCP + name: http + selector: + {{- include "helm-osrm-backend.selectorLabels" . | nindent 4 }} diff --git a/templates/serviceaccount.yaml b/templates/serviceaccount.yaml new file mode 100644 index 0000000..d7b2ddc --- /dev/null +++ b/templates/serviceaccount.yaml @@ -0,0 +1,13 @@ +{{- if .Values.serviceAccount.create -}} +apiVersion: v1 +kind: ServiceAccount +metadata: + name: {{ include "helm-osrm-backend.serviceAccountName" . }} + labels: + {{- include "helm-osrm-backend.labels" . | nindent 4 }} + {{- with .Values.serviceAccount.annotations }} + annotations: + {{- toYaml . | nindent 4 }} + {{- end }} +automountServiceAccountToken: {{ .Values.serviceAccount.automount }} +{{- end }} diff --git a/templates/tests/test-connection.yaml b/templates/tests/test-connection.yaml new file mode 100644 index 0000000..0f104b2 --- /dev/null +++ b/templates/tests/test-connection.yaml @@ -0,0 +1,15 @@ +apiVersion: v1 +kind: Pod +metadata: + name: "{{ include "helm-osrm-backend.fullname" . }}-test-connection" + labels: + {{- include "helm-osrm-backend.labels" . | nindent 4 }} + annotations: + "helm.sh/hook": test +spec: + containers: + - name: wget + image: busybox + command: ['wget'] + args: ['{{ include "helm-osrm-backend.fullname" . }}:{{ .Values.service.port }}'] + restartPolicy: Never diff --git a/values.override.example.yaml b/values.override.example.yaml new file mode 100644 index 0000000..d9e1dae --- /dev/null +++ b/values.override.example.yaml @@ -0,0 +1,21 @@ +ingress: + enabled: true + className: "nginx" + annotations: + cert-manager.io/cluster-issuer: "letsencrypt-prod" + #nginx.ingress.kubernetes.io/rewrite-target: / + nginx.ingress.kubernetes.io/enable-cors: "true" + nginx.ingress.kubernetes.io/cors-allow-origin: "*" + nginx.ingress.kubernetes.io/cors-allow-methods: "GET, POST, PUT, DELETE, OPTIONS" + nginx.ingress.kubernetes.io/cors-allow-headers: "*" + nginx.ingress.kubernetes.io/cors-allow-credentials: "true" + tls: true + extraTls: + - hosts: + - api-osrm. + secretName: osrm-backend-tls + hosts: + - host: api-osrm. + paths: + - path: / + pathType: Prefix \ No newline at end of file diff --git a/values.yaml b/values.yaml new file mode 100644 index 0000000..48bb8ff --- /dev/null +++ b/values.yaml @@ -0,0 +1,91 @@ +image: + repository: ghcr.io/project-osrm/osrm-backend + pullPolicy: IfNotPresent + +replicaCount: 1 + +autoscaling: + enabled: false + minReplicas: 1 + maxReplicas: 100 + targetCPUUtilizationPercentage: 80 + # targetMemoryUtilizationPercentage: 80 + +serviceAccount: + create: true + automount: true + annotations: {} + name: "" + +service: + type: ClusterIP + port: 5000 + +volume: + mountPath: /data + # this will be probably different for you + storageClass: linode-block-storage + accessMode: ReadWriteOnce + # adjust the size you need depending on the dataset you need + size: 20Gi + existingClaim: "" + +resources: {} + +initContainer: + enabled: true + # url of the dataset you want to + url: "https://download.geofabrik.de/north-america/canada/quebec-latest.osm.pbf" + # don't forget to update the FILE_NAME in the script below + # AND in the command bellow + fileName: "map.osm.pbf" + command: + - "/bin/sh" + - "-c" + - | + set -e; + FILE_NAME=map.osm.pbf; + FILE_BASE=$(echo "$FILE_NAME" | sed 's/.osm.pbf//'); + + echo "Running osrm-extract..."; + osrm-extract -p /opt/car.lua "/data/$FILE_NAME" || { echo "osrm-extract failed"; exit 1; } + + echo "Running osrm-partition..."; + osrm-partition "/data/$FILE_BASE.osrm" || { echo "osrm-partition failed"; exit 1; } + + echo "Running osrm-customize..."; + osrm-customize "/data/$FILE_BASE.osrm" || { echo "osrm-customize failed"; exit 1; } + +command: + - "osrm-routed" + - "--algorithm" + - "mld" + - /data/map.osrm + +# httpGet doesn't support status code 400 and there is no health check call on the osrm api +# I rather avoid making a real call which will use compute everytime the probes triggers +livenessProbe: + exec: + command: + - sh + - -c + - 'cat /proc/net/tcp | grep -i ":1388"' + +readinessProbe: + exec: + command: + - sh + - -c + - 'cat /proc/net/tcp | grep -i ":1388"' + +# important for init containers to give it times to download and extract the data +tolerations: + - key: "node.kubernetes.io/not-ready" + operator: "Exists" + effect: "NoExecute" + tolerationSeconds: 3600 # 60 minutes + + - key: "node.kubernetes.io/unreachable" + operator: "Exists" + effect: "NoExecute" + tolerationSeconds: 3600 # 60 minutes