Skip to content

Commit

Permalink
Local api infra (#107)
Browse files Browse the repository at this point in the history
* Add basic api local k8s deployment

* Update readme

* action test to see if worth doing

* Authenticate to ghcr with github.actor?

* Increase timeout?

* Add more timeout

* more timeout

* Move helm chart out of local

* Fix helm chart path

* Remove ghcr secret because api image is public

* Larger minikube cluster

* Move deploy to a single helm install instead of using helmsman. WIP failing tests

* Replace docker compose local setup with minikube

* Update readme

* Cleanup docker compose-related files

* Fix chat lint

* Increase application start timeout...

* Add debug

* Add api logs to debug action

* Add missing environment :)

* Re-add docker compose

* Update image to the one that gets auto-built

* Fix tag

* Fix helm test api test runner image
  • Loading branch information
liviuba authored Jun 12, 2024
1 parent 1e9a3a4 commit 702d908
Show file tree
Hide file tree
Showing 19 changed files with 538 additions and 2 deletions.
81 changes: 81 additions & 0 deletions .github/workflows/api_kube.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
name: Lint and Test Charts

on:
push:
branches:
- main
pull_request:
branches:
- main

jobs:
helm-lint:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v3
with:
fetch-depth: 0

- name: Install chart-testing
uses: helm/[email protected]

- run: helm repo add bitnami https://charts.bitnami.com/bitnami

- name: Run chart-testing (lint)
run: ct lint --charts ./api/helm-chart --debug

api-test:
runs-on: ubuntu-latest
environment:
name: test
steps:
- name: Checkout
uses: actions/checkout@v3
with:
fetch-depth: 0

- uses: azure/[email protected]
- uses: azure/setup-kubectl@v3

- name: Start minikube - if this breaks on local do a 'minikube delete'
id: minikube
uses: medyagh/setup-minikube@master
with:
cpus: 4
memory: 4G # 'wait' below times out on Actions with 2G
start-args: '--force'

- run: helm repo add bitnami https://charts.bitnami.com/bitnami

- name: Get helm dependencies
run: helm dependency build ./api/helm-chart

- name: Start everything
run: |
helm install bia-api-local ./api/helm-chart \
--set "api_env.mongoRootUser=${{ vars.MONGO_INITDB_ROOT_USERNAME }}" \
--set "api_env.mongoRootPassword=${{ secrets.MONGO_INITDB_ROOT_PASSWORD }}" \
--set "api_env.jwtSecretKey=${{ secrets.JWT_SECRET_KEY }}" \
--set "api_env.userCreateSecretToken=${{ secrets.USER_CREATE_SECRET_TOKEN }}" \
--set "mongodb.enabled=true" \
--set "mongodb.auth.rootUser=${{ vars.MONGO_INITDB_ROOT_USERNAME }}" \
--set "mongodb.auth.rootPassword=${{ secrets.MONGO_INITDB_ROOT_PASSWORD }}"
- name: Wait for api to start and health-check
run: kubectl wait --for=condition=ready pod -l app.kubernetes.io/name=api --timeout=180s

- name: Run tests
run: helm test bia-api-local --logs

- name: List pods for CI minikube debug
run: |
kubectl get pods
kubectl describe pods -l app.kubernetes.io/name=api
kubectl logs -l app.kubernetes.io/name=api
if: ${{ failure() }}

- name: Cleanup
run: |
minikube delete
if: ${{ always() }}
4 changes: 3 additions & 1 deletion api/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -62,4 +62,6 @@ env
# Project-specific
####
.env
.venv
.venv

helm-chart/charts
23 changes: 23 additions & 0 deletions api/helm-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/
6 changes: 6 additions & 0 deletions api/helm-chart/Chart.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
dependencies:
- name: mongodb
repository: https://charts.bitnami.com/bitnami
version: 15.4.3
digest: sha256:59add6e9bcbda9f7e6d374adf69b03c7d28f0f1ca6660d734a86b2bb3b5b7f41
generated: "2024-05-29T16:21:49.346169347+01:00"
18 changes: 18 additions & 0 deletions api/helm-chart/Chart.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
apiVersion: v2
name: api
description: A Helm chart for the BioImage Archive api
maintainers:
- name: LA # Liviu Anita
email: [email protected]

type: application

version: 0.1.0

appVersion: "0.1.0"

dependencies:
- name: mongodb
version: 15.4.3
repository: https://charts.bitnami.com/bitnami
condition: mongodb.enabled
22 changes: 22 additions & 0 deletions api/helm-chart/templates/NOTES.txt
Original file line number Diff line number Diff line change
@@ -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 "api.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 "api.fullname" . }}'
export SERVICE_IP=$(kubectl get svc --namespace {{ .Release.Namespace }} {{ include "api.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 "api.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 api/helm-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 "api.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 "api.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 "api.chart" -}}
{{- printf "%s-%s" .Chart.Name .Chart.Version | replace "+" "_" | trunc 63 | trimSuffix "-" }}
{{- end }}

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

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

{{/*
Create the name of the service account to use
*/}}
{{- define "api.serviceAccountName" -}}
{{- if .Values.serviceAccount.create }}
{{- default (include "api.fullname" .) .Values.serviceAccount.name }}
{{- else }}
{{- default "default" .Values.serviceAccount.name }}
{{- end }}
{{- end }}
59 changes: 59 additions & 0 deletions api/helm-chart/templates/deployment.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
apiVersion: apps/v1
kind: Deployment
metadata:
name: {{ include "api.fullname" . }}
spec:
selector:
matchLabels:
{{- include "api.selectorLabels" . | nindent 6 }}
template:
metadata:
labels:
{{- include "api.labels" . | nindent 8 }}
spec:
{{- with .Values.imagePullSecrets }}
imagePullSecrets:
{{- toYaml . | nindent 8 }}
{{- end }}
containers:
- name: {{ .Chart.Name }}
image: "ghcr.io/bioimage-archive/bia-integrator-api:{{ .Values.image.tag }}"
imagePullPolicy: {{ .Values.image.pullPolicy }}
resources:
{{- toYaml .Values.resources | nindent 12 }}
ports:
- name: http
containerPort: 8080
protocol: TCP
livenessProbe:
httpGet:
path: /v1/search/studies?limit=1
port: http
initialDelaySeconds: 10
failureThreshold: 10
periodSeconds: 60
readinessProbe:
httpGet:
path: /v1/openapi.json
port: http
initialDelaySeconds: 10
failureThreshold: 10
periodSeconds: 60
env:
- name: DB_NAME
value: {{ .Values.api_env.dbName }}
- name: JWT_SECRET_KEY
valueFrom:
secretKeyRef:
name: {{ include "api.fullname" . }}
key: jwtSecretKey
- name: USER_CREATE_SECRET_TOKEN
valueFrom:
secretKeyRef:
name: {{ include "api.fullname" . }}
key: userCreateSecretToken
- name: MONGO_CONNSTRING
valueFrom:
secretKeyRef:
name: {{ include "api.fullname" . }}
key: mongoConnstring
61 changes: 61 additions & 0 deletions api/helm-chart/templates/ingress.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
{{- if .Values.ingress.enabled -}}
{{- $fullName := include "api.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 "api.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 }}
9 changes: 9 additions & 0 deletions api/helm-chart/templates/secrets.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
apiVersion: v1
kind: Secret
metadata:
name: {{ include "api.fullname" . }}
type: Opaque
stringData:
mongoConnstring: "mongodb://{{ .Values.api_env.mongoRootUser }}:{{ .Values.api_env.mongoRootPassword }}@{{ .Values.api_env.mongoHost }}:27017"
jwtSecretKey: {{ .Values.api_env.jwtSecretKey }}
userCreateSecretToken: {{ .Values.api_env.userCreateSecretToken }}
11 changes: 11 additions & 0 deletions api/helm-chart/templates/service.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
apiVersion: v1
kind: Service
metadata:
name: {{ include "api.fullname" . }}
labels:
spec:
type: {{ .Values.service.type }}
ports:
- port: {{ .Values.service.port }}
selector:
{{- include "api.selectorLabels" . | nindent 4 }}
Loading

0 comments on commit 702d908

Please sign in to comment.