diff --git a/.github/workflows/build_helm_charts.yml b/.github/workflows/build_helm_charts.yml index f165e5e..a08e29b 100644 --- a/.github/workflows/build_helm_charts.yml +++ b/.github/workflows/build_helm_charts.yml @@ -103,7 +103,10 @@ jobs: - name: Run chart-testing (install) if: steps.list-changed.outputs.changed == 'true' working-directory: . - run: ct install --config .github/linters/ct.yaml + # hapi-fhir is linted but not installed: it has no templating, so it cannot be + # scaled to zero for CI, and its pods need the hapi-fhir-db Secret, which is + # created outside the chart. + run: ct install --config .github/linters/ct.yaml --excluded-charts hapi-fhir helm-publish: runs-on: ubuntu-latest diff --git a/.release-please-manifest.json b/.release-please-manifest.json index e78cbc8..0778da5 100644 --- a/.release-please-manifest.json +++ b/.release-please-manifest.json @@ -8,6 +8,7 @@ "deployment/charts/ocr-service": "0.1.0", "deployment/charts/nifi": "0.0.0", "deployment/charts/nginx": "0.0.0", + "deployment/charts/hapi-fhir": "0.0.0", "deployment/terraform/openstack-kubernetes-k3s": "0.1.4", "apps/cohorter": "0.0.1", "apps/nifi": "2.0.6", diff --git a/deployment/charts/README.md b/deployment/charts/README.md index fe1bc74..3d13185 100644 --- a/deployment/charts/README.md +++ b/deployment/charts/README.md @@ -43,6 +43,13 @@ Existing deployments must reload `nifi-auth` for NGINX before upgrading. OpenSearch upgrades also require the bootstrap Secret and preparation of administrative access when using an admin certificate. [OpenSearch upgrade details](opensearch/README.md#install) +## HAPI FHIR + +`hapi-fhir/` runs a HAPI FHIR JPA server (R4) with PostgreSQL and its own single-node +OpenSearch for full-text search. Its manifests are plain Kubernetes YAML with +no Helm templating and no values. It needs the `hapi-fhir-db` Secret before +installation. [HAPI FHIR chart](hapi-fhir/README.md) + ## Chart development ```bash @@ -71,6 +78,9 @@ can validate Kubernetes resource installation without deployment-owned ConfigMaps and Secrets. Application startup and TLS checks require a separate runtime test with those inputs supplied. +The HAPI FHIR chart has no values, so it cannot be scaled to zero. CI lints it +and skips the install test, because its pods need the `hapi-fhir-db` Secret. + To package locally, keep generated archives outside the source tree: ```bash diff --git a/deployment/charts/hapi-fhir/Chart.yaml b/deployment/charts/hapi-fhir/Chart.yaml new file mode 100644 index 0000000..152a6f2 --- /dev/null +++ b/deployment/charts/hapi-fhir/Chart.yaml @@ -0,0 +1,14 @@ +apiVersion: v2 +name: hapi-fhir +description: HAPI FHIR JPA server (R4) with PostgreSQL and OpenSearch full-text search +type: application +version: 0.1.0 +appVersion: "v8.12.0-2" + +home: https://docs.cogstack.org/ +sources: + - https://github.com/CogStack/cogstack-platform + - https://github.com/hapifhir/hapi-fhir-jpaserver-starter +maintainers: + - name: mooperd +icon: "https://avatars.githubusercontent.com/u/28688163" diff --git a/deployment/charts/hapi-fhir/README.md b/deployment/charts/hapi-fhir/README.md new file mode 100644 index 0000000..e7bc729 --- /dev/null +++ b/deployment/charts/hapi-fhir/README.md @@ -0,0 +1,105 @@ +# CogStack HAPI FHIR Helm chart + +A [HAPI FHIR](https://hapifhir.io/) JPA server (FHIR R4) with PostgreSQL as the +resource store and a single-node OpenSearch as its full-text index, which is +what answers `_text` and `_content` searches. Every other search stays in +PostgreSQL, so a search straight after a write still sees it. + +| Workload | Image | Service | Storage | +| --- | --- | --- | --- | +| `hapi-fhir` | `hapiproject/hapi:v8.12.0-2` | `hapi-fhir:8080` | none | +| `hapi-fhir-db` | `postgres:16-alpine` | `hapi-fhir-db:5432` | 10Gi PVC | +| `hapi-fhir-opensearch` | `opensearchproject/opensearch:3.9.0` | `hapi-fhir-opensearch:9200` | 10Gi PVC | + +## No templating + +The files in `templates/` are plain Kubernetes YAML. They contain no Helm +template expressions, and the chart has no values: `helm template` prints them +as they are written. Helm packages, versions, installs and removes the chart, +and nothing else. + +This has three consequences. + +- **Fixed names.** Resource names do not include the release name, so install + one release per namespace. +- **No `--set`.** To change an image, a size or a setting, edit the manifests + (or post-process them with `helm template | kustomize`). +- **No automatic restart on a configuration change.** HAPI reads the + `hapi-fhir-config` ConfigMap only at startup. After an upgrade that changes + it, run `kubectl rollout restart deployment/hapi-fhir`. + +## Install + +The chart does not contain the database password. Create the `hapi-fhir-db` +Secret first, in the namespace you install into. Keep it across upgrades: +PostgreSQL sets the password only when it first initialises its volume. + +```bash +kubectl create namespace cogstack +kubectl -n cogstack create secret generic hapi-fhir-db \ + --from-literal=POSTGRES_PASSWORD="$(openssl rand -hex 24)" +helm upgrade --install hapi-fhir deployment/charts/hapi-fhir -n cogstack \ + --wait --timeout 15m +``` + +HAPI waits for PostgreSQL and OpenSearch, then migrates its schema. The +first start takes a few minutes. + +## Access + +The chart creates only ClusterIP Services. It has no Ingress or HTTPRoute, +because a hostname cannot be set without templating. PostgreSQL and +OpenSearch are meant to stay inside the cluster; the OpenSearch container +uninstalls its security plugin at startup, so it runs without authentication +and needs no certificates. + +```bash +kubectl -n cogstack port-forward svc/hapi-fhir 8080:8080 +curl http://localhost:8080/fhir/metadata +curl 'http://localhost:8080/fhir/Patient?_text=x&_summary=count' +``` + +The second request answers `400` (`HAPI-1192`) if HAPI is not using +OpenSearch. + +To publish the server, route your own Ingress or Gateway API HTTPRoute to the +`hapi-fhir` Service on port `8080`. HAPI builds its links from the +`X-Forwarded-*` headers the proxy sends. + +If resources were stored before OpenSearch was attached, index them once: + +```bash +curl -X POST 'http://localhost:8080/fhir/$reindex' \ + -H 'Content-Type: application/fhir+json' \ + -d '{"resourceType":"Parameters"}' +``` + +## OpenSearch and HAPI + +HAPI reaches OpenSearch through Hibernate Search, whose backend type is named +`elasticsearch` in the configuration. Two things follow from using OpenSearch +there. + +- **No `spring.elasticsearch` settings.** They make HAPI start its Elasticsearch + Java client, which OpenSearch rejects with `406 Not Acceptable`, and HAPI then + fails to start. That client serves only the `$lastn` operation, so `$lastn` + is not available with this chart. +- **A version warning at startup.** Hibernate Search 7.2 in HAPI v8.12.0 logs + `HSEARCH400085: Unknown Elasticsearch version running on the cluster: + 'opensearch:3.9.0'`. Indexing, `_text`, `_content` and `$reindex` work. + +## Sizing + +OpenSearch runs with a 512 MB heap and a 1200Mi memory limit, and uses +`node.store.allow_mmap=false` so the nodes need no `vm.max_map_count` change. +PostgreSQL is tuned for about 2 GB of memory. These suit a small shared +cluster; edit the manifests for anything larger. + +## Remove + +```bash +helm uninstall hapi-fhir -n cogstack +``` + +This deletes the PersistentVolumeClaims, and with them the data. The +`hapi-fhir-db` Secret is yours to delete. diff --git a/deployment/charts/hapi-fhir/templates/hapi-fhir.yaml b/deployment/charts/hapi-fhir/templates/hapi-fhir.yaml new file mode 100644 index 0000000..1ba8957 --- /dev/null +++ b/deployment/charts/hapi-fhir/templates/hapi-fhir.yaml @@ -0,0 +1,181 @@ +# HAPI FHIR JPA server (R4). +# The ConfigMap is merged on top of the application.yaml in the image. +apiVersion: v1 +kind: ConfigMap +metadata: + name: hapi-fhir-config +data: + application.yaml: | + spring: + # No spring.elasticsearch block: it would make HAPI start its Elasticsearch Java + # client (used only for $lastn), which OpenSearch rejects with 406. Hibernate + # Search has its own client, configured under backend below, and that one works + # with OpenSearch. Its backend type is still called elasticsearch. + datasource: + hikari: + maximum-pool-size: 30 + jpa: + properties: + hibernate: + search: + enabled: true + schema_management: + strategy: CREATE + backend: + type: elasticsearch + hosts: hapi-fhir-opensearch:9200 + protocol: http + layout: + strategy: ca.uhn.fhir.jpa.search.elastic.IndexNamePrefixLayoutStrategy + analysis: + configurer: ca.uhn.fhir.jpa.search.HapiHSearchAnalysisConfigurers$HapiElasticsearchAnalysisConfigurer + scroll_timeout: 60 + dynamic_mapping: true + schema_management: + settings_file: ca/uhn/fhir/jpa/elastic/index-settings.json + minimal_required_status: YELLOW + minimal_required_status_wait_timeout: 10000 + hapi: + fhir: + # _text / _content indexing (off by default since HAPI FHIR 8.0) + search_index_full_text_enabled: true + # false: OpenSearch answers only _text / _content, and every other search stays + # in PostgreSQL, so a search straight after a write still sees it. + # true also indexes and answers ordinary search parameters in + # OpenSearch, which lags a write by its refresh interval. + advanced_lucene_indexing: false + store_resource_in_lucene_index_enabled: false + # Never reuse a cached search: timings measure the search, not the cache. + reuse_cached_search_results_millis: 0 + allow_contains_searches: true +--- +apiVersion: apps/v1 +kind: Deployment +metadata: + name: hapi-fhir +spec: + replicas: 1 + strategy: + type: Recreate # a new version migrates the schema — never two versions at once + selector: + matchLabels: + app: hapi-fhir + template: + metadata: + labels: + app: hapi-fhir + spec: + securityContext: + runAsNonRoot: true + runAsUser: 65532 + runAsGroup: 65532 + fsGroup: 65532 + seccompProfile: + type: RuntimeDefault + initContainers: + - name: wait-for-db + image: postgres:16-alpine + command: ["/bin/sh", "-c"] + args: + - | + until pg_isready -h hapi-fhir-db -p 5432 -U fhir; do + echo "waiting for postgres..."; sleep 5; + done + - name: wait-for-opensearch + image: postgres:16-alpine # for its busybox wget; the HAPI image has no shell + command: ["/bin/sh", "-c"] + args: + - | + until wget -q -O /dev/null "http://hapi-fhir-opensearch:9200/_cluster/health?wait_for_status=yellow&timeout=5s"; do + echo "waiting for opensearch..."; sleep 5; + done + containers: + - name: hapi-fhir + # distroless flavour + # (Docker Hub tags carry a -N release suffix; bare v8.12.0 does not exist) + image: hapiproject/hapi:v8.12.0-2 + securityContext: + allowPrivilegeEscalation: false + readOnlyRootFilesystem: true + capabilities: + drop: ["ALL"] + ports: + - name: http + containerPort: 8080 + - name: http-metrics + containerPort: 8081 + env: + # --- FHIR version: R4 (hapi-fhir-structures-r4) --- + - name: HAPI_FHIR_FHIR_VERSION + value: R4 + # --- database wiring (mirrors the hapi-fhir-jpaserver chart) --- + - name: SPRING_DATASOURCE_URL + value: jdbc:postgresql://hapi-fhir-db:5432/fhir + - name: SPRING_DATASOURCE_USERNAME + value: fhir + - name: SPRING_DATASOURCE_PASSWORD + valueFrom: + secretKeyRef: + name: hapi-fhir-db + key: POSTGRES_PASSWORD + - name: SPRING_DATASOURCE_DRIVERCLASSNAME + value: org.postgresql.Driver + - name: spring.jpa.properties.hibernate.dialect + value: ca.uhn.fhir.jpa.model.dialect.HapiFhirPostgresDialect + # --- OpenSearch and search settings: the hapi-fhir-config ConfigMap --- + - name: SPRING_CONFIG_ADDITIONAL_LOCATION + value: file:/config/ + - name: HAPI_FHIR_USE_APACHE_ADDRESS_STRATEGY + value: "true" + - name: MANAGEMENT_SERVER_PORT + value: "8081" + - name: MANAGEMENT_ENDPOINT_HEALTH_PROBES_ADD_ADDITIONAL_PATHS + value: "true" + - name: MANAGEMENT_ENDPOINTS_WEB_EXPOSURE_INCLUDE + value: "health,prometheus" + startupProbe: + httpGet: + path: /readyz + port: http + failureThreshold: 30 + initialDelaySeconds: 30 + periodSeconds: 10 + readinessProbe: + httpGet: + path: /readyz + port: http + periodSeconds: 20 + timeoutSeconds: 10 + livenessProbe: + httpGet: + path: /livez + port: http + periodSeconds: 20 + timeoutSeconds: 10 + volumeMounts: + - name: tmp + mountPath: /tmp + - name: lucene + mountPath: /app/target + - name: config + mountPath: /config + readOnly: true + volumes: + - name: config + configMap: + name: hapi-fhir-config + - name: tmp + emptyDir: {} + - name: lucene + emptyDir: {} +--- +apiVersion: v1 +kind: Service +metadata: + name: hapi-fhir +spec: + selector: + app: hapi-fhir + ports: + - port: 8080 + targetPort: http diff --git a/deployment/charts/hapi-fhir/templates/opensearch.yaml b/deployment/charts/hapi-fhir/templates/opensearch.yaml new file mode 100644 index 0000000..53f4633 --- /dev/null +++ b/deployment/charts/hapi-fhir/templates/opensearch.yaml @@ -0,0 +1,101 @@ +# OpenSearch (single node): the Hibernate Search index next to PostgreSQL, which is +# what answers _text and _content. Hibernate Search reaches it through its +# Elasticsearch-compatible backend. +apiVersion: v1 +kind: PersistentVolumeClaim +metadata: + name: hapi-fhir-opensearch +spec: + accessModes: ["ReadWriteOnce"] + resources: + requests: + storage: 10Gi +--- +apiVersion: apps/v1 +kind: Deployment +metadata: + name: hapi-fhir-opensearch +spec: + replicas: 1 + strategy: + type: Recreate # single RWO volume — never two pods at once + selector: + matchLabels: + app: hapi-fhir-opensearch + template: + metadata: + labels: + app: hapi-fhir-opensearch + spec: + securityContext: + runAsNonRoot: true + runAsUser: 1000 + runAsGroup: 1000 + fsGroup: 1000 + seccompProfile: + type: RuntimeDefault + containers: + - name: opensearch + image: opensearchproject/opensearch:3.9.0 + securityContext: + allowPrivilegeEscalation: false + capabilities: + drop: ["ALL"] + # No authentication: reachable only inside the cluster (ClusterIP Service, + # no Ingress or HTTPRoute). The security plugin is uninstalled before + # OpenSearch starts, so there are no certificates, users or demo + # configuration. The image's entrypoint then starts OpenSearch as usual. + command: + - bash + - -ec + - | + bin/opensearch-plugin remove --purge opensearch-security + exec ./opensearch-docker-entrypoint.sh + ports: + - name: http + containerPort: 9200 + env: + - name: discovery.type + value: single-node + - name: cluster.name + value: hapi-fhir + # The nodes keep the default vm.max_map_count, too low for mmap'd indices. + - name: node.store.allow_mmap + value: "false" + # Deliberately small, with the limit below, for a cluster with little spare + # memory. + - name: OPENSEARCH_JAVA_OPTS + value: "-Xms512m -Xmx512m" + resources: + limits: + memory: 1200Mi + startupProbe: + httpGet: + path: /_cluster/health?wait_for_status=yellow&timeout=1s + port: http + failureThreshold: 60 + periodSeconds: 5 + readinessProbe: + httpGet: + path: /_cluster/health?wait_for_status=yellow&timeout=1s + port: http + periodSeconds: 20 + timeoutSeconds: 10 + volumeMounts: + - name: data + mountPath: /usr/share/opensearch/data + volumes: + - name: data + persistentVolumeClaim: + claimName: hapi-fhir-opensearch +--- +apiVersion: v1 +kind: Service +metadata: + name: hapi-fhir-opensearch +spec: + selector: + app: hapi-fhir-opensearch + ports: + - port: 9200 + targetPort: http diff --git a/deployment/charts/hapi-fhir/templates/postgres.yaml b/deployment/charts/hapi-fhir/templates/postgres.yaml new file mode 100644 index 0000000..bbfcd24 --- /dev/null +++ b/deployment/charts/hapi-fhir/templates/postgres.yaml @@ -0,0 +1,90 @@ +# PostgreSQL: the FHIR resource store. +# The hapi-fhir-db Secret (key POSTGRES_PASSWORD) is not part of the chart; create it +# before installing (see the chart README). +apiVersion: v1 +kind: PersistentVolumeClaim +metadata: + name: hapi-fhir-db +spec: + accessModes: ["ReadWriteOnce"] + resources: + requests: + storage: 10Gi +--- +apiVersion: apps/v1 +kind: Deployment +metadata: + name: hapi-fhir-db +spec: + replicas: 1 + strategy: + type: Recreate # single RWO volume — never two pods at once + selector: + matchLabels: + app: hapi-fhir-db + template: + metadata: + labels: + app: hapi-fhir-db + spec: + containers: + - name: postgres + image: postgres:16-alpine + # Modest tuning for a single small node. + args: + - -c + - shared_buffers=512MB + - -c + - effective_cache_size=1500MB + - -c + - work_mem=32MB + - -c + - maintenance_work_mem=256MB + - -c + - max_connections=100 + - -c + - shared_preload_libraries=pg_stat_statements + ports: + - containerPort: 5432 + env: + - name: POSTGRES_DB + value: fhir + - name: POSTGRES_USER + value: fhir + - name: POSTGRES_PASSWORD + valueFrom: + secretKeyRef: + name: hapi-fhir-db + key: POSTGRES_PASSWORD + - name: PGDATA + value: /var/lib/postgresql/data/pgdata + readinessProbe: + exec: + command: ["pg_isready", "-U", "fhir", "-d", "fhir"] + initialDelaySeconds: 10 + periodSeconds: 10 + volumeMounts: + - name: data + mountPath: /var/lib/postgresql/data + - name: shm + mountPath: /dev/shm + volumes: + - name: data + persistentVolumeClaim: + claimName: hapi-fhir-db + # The container default of 64Mi is too small for parallel queries. + - name: shm + emptyDir: + medium: Memory + sizeLimit: 1Gi +--- +apiVersion: v1 +kind: Service +metadata: + name: hapi-fhir-db +spec: + selector: + app: hapi-fhir-db + ports: + - port: 5432 + targetPort: 5432 diff --git a/deployment/charts/hapi-fhir/values.yaml b/deployment/charts/hapi-fhir/values.yaml new file mode 100644 index 0000000..17ebdde --- /dev/null +++ b/deployment/charts/hapi-fhir/values.yaml @@ -0,0 +1,3 @@ +# This chart has no values. Everything under templates/ is plain Kubernetes YAML with +# no Helm templating: what is in the file is what gets applied. Helm only packages, +# versions, installs and removes it. To change something, edit the manifests. diff --git a/deployment/scripts/minimum_deployment.sh b/deployment/scripts/minimum_deployment.sh new file mode 100755 index 0000000..7441d4f --- /dev/null +++ b/deployment/scripts/minimum_deployment.sh @@ -0,0 +1,88 @@ +#!/usr/bin/env bash +# Deploy the minimum CogStack stack to the current kubectl context: +# HAPI FHIR, OpenSearch and NiFi. +# +# bash deployment/scripts/minimum_deployment.sh +# +# Each service is rendered with `helm template` to deployment/rendered/.yaml +# and applied with `kubectl apply`. The namespace is KUBE_NAMESPACE in +# deployment/env/general.env. Safe to run again: existing Secrets are kept. +# +# The charts read four Secrets that cannot be committed, so step 1 creates any +# that are missing. OpenSearch users and their passwords come from +# deployment/env/authentication.env. +# +# Requirements: kubectl, helm, openssl, htpasswd. +set -euo pipefail + +cd -- "$(dirname -- "${BASH_SOURCE[0]}")/.." +source ./env/general.env +ns=$KUBE_NAMESPACE +mkdir -p rendered + +kubectl create namespace "$ns" --dry-run=client -o yaml | kubectl apply -f - + +# --- 1. Secrets ---------------------------------------------------------------- +secret_missing() { ! kubectl -n "$ns" get secret "$1" >/dev/null 2>&1; } + +# HAPI FHIR: PostgreSQL password. +if secret_missing hapi-fhir-db; then + kubectl -n "$ns" create secret generic hapi-fhir-db \ + --from-literal=POSTGRES_PASSWORD="$(openssl rand -hex 24)" +fi + +# NiFi: key that encrypts sensitive values in the flow. +if secret_missing nifi-auth; then + kubectl -n "$ns" create secret generic nifi-auth \ + --from-literal=NIFI_SENSITIVE_PROPS_KEY="$(openssl rand -hex 16)" +fi + +# OpenSearch: the security plugin requires TLS between nodes. One self-signed +# certificate, which every node presents and trusts, is enough. Its subject must +# match plugins.security.nodes_dn in charts/opensearch/config/opensearch.yml. +if secret_missing opensearch-node-tls; then + tls=$(mktemp -d) + openssl req -x509 -newkey rsa:4096 -nodes -days 1460 \ + -subj "/C=UK/ST=London/L=UK/O=cogstack/OU=cogstack/CN=opensearch-shared" \ + -keyout "$tls/node.key" -out "$tls/node.crt" + kubectl -n "$ns" create secret generic opensearch-node-tls \ + --from-file=node.crt="$tls/node.crt" \ + --from-file=node.key="$tls/node.key" \ + --from-file=ca.crt="$tls/node.crt" + rm -rf -- "$tls" +fi + +# OpenSearch: users, roles and role mappings (opensearch-security-bootstrap). +# Does nothing when the Secret already exists. +bash scripts/security/load_opensearch_auth.sh opensearch + +# --- 2. Render and apply ------------------------------------------------------- +helm template hapi-fhir charts/hapi-fhir > rendered/hapi-fhir.yaml +kubectl -n "$ns" apply -f rendered/hapi-fhir.yaml + +# The upstream OpenSearch chart is a dependency, downloaded into charts/opensearch/charts. +helm repo add opensearch https://opensearch-project.github.io/helm-charts/ --force-update +helm dependency build charts/opensearch +helm template opensearch charts/opensearch --skip-tests \ + --set-file 'opensearch.config.opensearch\.yml=charts/opensearch/config/opensearch.yml' \ + > rendered/opensearch.yaml +kubectl -n "$ns" apply -f rendered/opensearch.yaml + +helm template cogstack-nifi charts/nifi > rendered/nifi.yaml +kubectl -n "$ns" apply -f rendered/nifi.yaml + +# --- 3. Status ----------------------------------------------------------------- +kubectl -n "$ns" get pods +cat <