Skip to content

Commit 42ade28

Browse files
committed
Added FQDN support to experiment with resolution for db cluster
1 parent 3a7c86d commit 42ade28

File tree

10 files changed

+186
-317
lines changed

10 files changed

+186
-317
lines changed

helm-chart/stac-fastapi/README.md

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,11 +62,23 @@ helm install my-stac-api ./helm-chart/stac-fastapi \
6262

6363
## Configuration
6464

65+
### Global Options
66+
67+
```yaml
68+
global:
69+
imageRegistry: ""
70+
storageClass: ""
71+
clusterDomain: "cluster.local"
72+
```
73+
74+
The chart builds fully qualified service endpoints for bundled databases using the Kubernetes cluster domain. Adjust `clusterDomain` if your cluster doesn't use the default `cluster.local` suffix.
75+
6576
## Backend Selection
6677

6778
The chart supports both Elasticsearch and OpenSearch backends, but only deploys **one backend at a time** based on the `backend` configuration:
6879

6980
### Elasticsearch Backend
81+
7082
```yaml
7183
backend: elasticsearch
7284
elasticsearch:
@@ -76,6 +88,7 @@ opensearch:
7688
```
7789

7890
### OpenSearch Backend
91+
7992
```yaml
8093
backend: opensearch
8194
elasticsearch:
@@ -113,6 +126,11 @@ app:
113126
tag: "latest"
114127
pullPolicy: IfNotPresent
115128
129+
waitForDatabase:
130+
enabled: true
131+
intervalSeconds: 2
132+
maxAttempts: 120
133+
116134
env:
117135
STAC_FASTAPI_TITLE: "STAC API"
118136
STAC_FASTAPI_DESCRIPTION: "A STAC FastAPI implementation"
@@ -124,8 +142,24 @@ app:
124142
STAC_FASTAPI_RATE_LIMIT: "200/minute"
125143
```
126144

145+
The optional `waitForDatabase` block adds a lightweight init container that blocks STAC FastAPI startup until the backing Elasticsearch/OpenSearch service is reachable—mirroring the docker-compose `wait-for-it` helper. Disable it by setting `app.waitForDatabase.enabled=false` if you prefer the application to start immediately and rely on internal retries instead.
146+
127147
### Database Configuration
128148

149+
#### Application Credentials
150+
151+
If your Elasticsearch or OpenSearch cluster requires authentication, provide credentials to the application with the `app.databaseAuth` block. You can reference an existing secret or supply literal values:
152+
153+
```yaml
154+
app:
155+
databaseAuth:
156+
existingSecret: "stac-opensearch-admin" # Optional. When set, keys are read from this secret.
157+
usernameKey: "username" # Secret key that stores the username (defaults to "username").
158+
passwordKey: "password" # Secret key that stores the password (defaults to "password").
159+
# username: "admin" # Optional literal username when not using a secret.
160+
# password: "changeme" # Optional literal password when not using a secret.
161+
```
162+
129163
#### Bundled Elasticsearch
130164

131165
```yaml
@@ -394,7 +428,7 @@ kubectl exec -it deployment/my-stac-api -- curl http://elasticsearch:9200/_healt
394428
kubectl port-forward service/my-stac-api 8080:80
395429
```
396430

397-
Then visit http://localhost:8080
431+
Then visit <http://localhost:8080>
398432

399433
## Configuration Reference
400434

@@ -423,4 +457,4 @@ Contributions are welcome! Please ensure any changes maintain compatibility with
423457

424458
## License
425459

426-
This chart is released under the same license as the STAC FastAPI project.
460+
This chart is released under the same license as the STAC FastAPI project.

helm-chart/stac-fastapi/templates/_helpers.tpl

Lines changed: 73 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -62,41 +62,53 @@ Create the name of the service account to use
6262
{{- end }}
6363

6464
{{/*
65-
Create the database host based on backend selection
65+
Create the bundled database service name based on backend selection
6666
*/}}
67-
{{- define "stac-fastapi.databaseHost" -}}
68-
{{- if .Values.externalDatabase.enabled }}
69-
{{- .Values.externalDatabase.host }}
70-
{{- else if eq .Values.backend "elasticsearch" }}
71-
{{- if .Values.elasticsearch.enabled }}
72-
{{- if .Values.elasticsearch.masterService }}
73-
{{- .Values.elasticsearch.masterService }}
74-
{{- else if .Values.elasticsearch.fullnameOverride }}
75-
{{- printf "%s-master" .Values.elasticsearch.fullnameOverride }}
76-
{{- else if .Values.elasticsearch.clusterName }}
77-
{{- printf "%s-master" .Values.elasticsearch.clusterName }}
78-
{{- else }}
79-
{{- printf "%s-%s" .Release.Name "elasticsearch-master" }}
80-
{{- end }}
81-
{{- else }}
82-
{{- fail "Elasticsearch is not enabled but backend is set to elasticsearch" }}
83-
{{- end }}
67+
{{- define "stac-fastapi.databaseServiceName" -}}
68+
{{- if eq .Values.backend "elasticsearch" }}
69+
{{- if .Values.elasticsearch.enabled }}
70+
{{- if .Values.elasticsearch.masterService }}
71+
{{- .Values.elasticsearch.masterService }}
72+
{{- else if .Values.elasticsearch.fullnameOverride }}
73+
{{- printf "%s-master" .Values.elasticsearch.fullnameOverride }}
74+
{{- else if .Values.elasticsearch.clusterName }}
75+
{{- printf "%s-master" .Values.elasticsearch.clusterName }}
76+
{{- else }}
77+
{{- printf "%s-%s" .Release.Name "elasticsearch-master" }}
78+
{{- end }}
79+
{{- else }}
80+
{{- fail "Elasticsearch is not enabled but backend is set to elasticsearch" }}
81+
{{- end }}
8482
{{- else if eq .Values.backend "opensearch" }}
85-
{{- if .Values.opensearch.enabled }}
86-
{{- if .Values.opensearch.masterService }}
87-
{{- .Values.opensearch.masterService }}
88-
{{- else if .Values.opensearch.fullnameOverride }}
89-
{{- printf "%s-master" .Values.opensearch.fullnameOverride }}
90-
{{- else if .Values.opensearch.clusterName }}
91-
{{- printf "%s-master" .Values.opensearch.clusterName }}
83+
{{- if .Values.opensearch.enabled }}
84+
{{- if .Values.opensearch.masterService }}
85+
{{- .Values.opensearch.masterService }}
86+
{{- else if .Values.opensearch.fullnameOverride }}
87+
{{- printf "%s-master" .Values.opensearch.fullnameOverride }}
88+
{{- else if .Values.opensearch.clusterName }}
89+
{{- printf "%s-master" .Values.opensearch.clusterName }}
90+
{{- else }}
91+
{{- printf "%s-%s" .Release.Name "opensearch-cluster-master" }}
92+
{{- end }}
93+
{{- else }}
94+
{{- fail "OpenSearch is not enabled but backend is set to opensearch" }}
95+
{{- end }}
9296
{{- else }}
93-
{{- printf "%s-%s" .Release.Name "opensearch-cluster-master" }}
97+
{{- fail "Invalid backend specified. Must be 'elasticsearch' or 'opensearch'" }}
9498
{{- end }}
95-
{{- else }}
96-
{{- fail "OpenSearch is not enabled but backend is set to opensearch" }}
9799
{{- end }}
100+
101+
{{/*
102+
Create the database host based on backend selection
103+
*/}}
104+
{{- define "stac-fastapi.databaseHost" -}}
105+
{{- if .Values.externalDatabase.enabled }}
106+
{{- .Values.externalDatabase.host }}
98107
{{- else }}
99-
{{- fail "Invalid backend specified. Must be 'elasticsearch' or 'opensearch'" }}
108+
{{- $service := include "stac-fastapi.databaseServiceName" . | trim }}
109+
{{- $namespace := .Release.Namespace | default "default" }}
110+
{{- $clusterDomain := default "cluster.local" .Values.global.clusterDomain }}
111+
{{- printf "%s.%s.svc.%s" $service $namespace $clusterDomain }}
100112
{{- end }}
101113
{{- end }}
102114

@@ -130,12 +142,42 @@ Create the image repository with tag
130142
Create environment variables for the application
131143
*/}}
132144
{{- define "stac-fastapi.environment" -}}
145+
{{- $env := default (dict) .Values.app.env -}}
146+
{{- $envFromSecret := default (dict) .Values.app.envFromSecret -}}
147+
{{- $dbAuth := default (dict) .Values.app.databaseAuth -}}
133148
- name: BACKEND
134149
value: {{ .Values.backend | quote }}
135150
- name: ES_HOST
136151
value: {{ include "stac-fastapi.databaseHost" . | quote }}
137152
- name: ES_PORT
138153
value: {{ include "stac-fastapi.databasePort" . | quote }}
154+
{{- if $dbAuth.existingSecret }}
155+
{{- $usernameKey := default "username" $dbAuth.usernameKey }}
156+
{{- $passwordKey := default "password" $dbAuth.passwordKey }}
157+
{{- if not (hasKey $env "ES_USER") }}
158+
- name: ES_USER
159+
valueFrom:
160+
secretKeyRef:
161+
name: {{ $dbAuth.existingSecret }}
162+
key: {{ $usernameKey }}
163+
{{- end }}
164+
{{- if not (hasKey $env "ES_PASS") }}
165+
- name: ES_PASS
166+
valueFrom:
167+
secretKeyRef:
168+
name: {{ $dbAuth.existingSecret }}
169+
key: {{ $passwordKey }}
170+
{{- end }}
171+
{{- else }}
172+
{{- if and (not (hasKey $env "ES_USER")) $dbAuth.username }}
173+
- name: ES_USER
174+
value: {{ $dbAuth.username | quote }}
175+
{{- end }}
176+
{{- if and (not (hasKey $env "ES_PASS")) $dbAuth.password }}
177+
- name: ES_PASS
178+
value: {{ $dbAuth.password | quote }}
179+
{{- end }}
180+
{{- end }}
139181
{{- if .Values.externalDatabase.enabled }}
140182
- name: ES_USE_SSL
141183
value: {{ .Values.externalDatabase.ssl | quote }}
@@ -151,11 +193,11 @@ Create environment variables for the application
151193
key: {{ .Values.externalDatabase.apiKeySecretKey }}
152194
{{- end }}
153195
{{- end }}
154-
{{- range $key, $value := .Values.app.env }}
196+
{{- range $key, $value := $env }}
155197
- name: {{ $key }}
156198
value: {{ $value | quote }}
157199
{{- end }}
158-
{{- range $key, $secretName := .Values.app.envFromSecret }}
200+
{{- range $key, $secretName := $envFromSecret }}
159201
- name: {{ $key }}
160202
valueFrom:
161203
secretKeyRef:

helm-chart/stac-fastapi/templates/deployment.yaml

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,39 @@ spec:
2020
labels:
2121
{{- include "stac-fastapi.selectorLabels" . | nindent 8 }}
2222
spec:
23+
{{- if .Values.app.waitForDatabase.enabled }}
24+
initContainers:
25+
- name: wait-for-database
26+
image: {{ .Values.app.waitForDatabase.image | default "busybox:1.36.1" }}
27+
imagePullPolicy: {{ .Values.app.waitForDatabase.pullPolicy | default "IfNotPresent" }}
28+
command:
29+
- sh
30+
- -c
31+
- |
32+
set -e
33+
HOST="{{ include "stac-fastapi.databaseHost" . | trim }}"
34+
PORT="{{ include "stac-fastapi.databasePort" . | trim }}"
35+
ATTEMPTS={{ .Values.app.waitForDatabase.maxAttempts | default 120 }}
36+
INTERVAL={{ .Values.app.waitForDatabase.intervalSeconds | default 2 }}
37+
COUNT=0
38+
echo "Waiting for ${HOST}:${PORT} before starting STAC FastAPI..."
39+
while true; do
40+
if nc -z "$HOST" "$PORT"; then
41+
echo "${HOST}:${PORT} is reachable."
42+
exit 0
43+
fi
44+
COUNT=$((COUNT + 1))
45+
if [ "$COUNT" -ge "$ATTEMPTS" ]; then
46+
echo "Timed out waiting for ${HOST}:${PORT} after ${COUNT} attempts." >&2
47+
exit 1
48+
fi
49+
sleep "$INTERVAL"
50+
done
51+
{{- with .Values.app.waitForDatabase.resources }}
52+
resources:
53+
{{- toYaml . | nindent 12 }}
54+
{{- end }}
55+
{{- end }}
2356
{{- with .Values.app.imagePullSecrets }}
2457
imagePullSecrets:
2558
{{- toYaml . | nindent 8 }}

helm-chart/stac-fastapi/values-elasticsearch.yaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,11 @@ app:
1919
port: 80
2020
targetPort: 8080
2121

22+
waitForDatabase:
23+
enabled: true
24+
intervalSeconds: 2
25+
maxAttempts: 180
26+
2227
# Ingress configuration
2328
ingress:
2429
enabled: true

helm-chart/stac-fastapi/values-external.yaml

Lines changed: 0 additions & 52 deletions
This file was deleted.

0 commit comments

Comments
 (0)