Skip to content

Commit c1921fe

Browse files
Merge pull request #197 from Pionerd/wait-for-redis
Improve wait-for-redis logic
2 parents e4d9dd6 + 3a5ffa2 commit c1921fe

File tree

5 files changed

+94
-11
lines changed

5 files changed

+94
-11
lines changed

helm/oauth2-proxy/Chart.yaml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name: oauth2-proxy
2-
version: 7.4.1
2+
version: 7.5.0
33
apiVersion: v2
44
appVersion: 7.6.0
55
home: https://oauth2-proxy.github.io/oauth2-proxy/
@@ -35,7 +35,7 @@ kubeVersion: ">=1.9.0-0"
3535
annotations:
3636
artifacthub.io/changes: |
3737
- kind: changed
38-
description: Fix link in readme to existingSecret needed fields
38+
description: Improved the readiness check for Redis to support all types
3939
links:
4040
- name: Github PR
41-
url: https://github.com/oauth2-proxy/manifests/pull/193
41+
url: https://github.com/oauth2-proxy/manifests/pull/197
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
#!/bin/sh
2+
3+
RETRY_INTERVAL=5 # Interval between retries in seconds
4+
elapsed=0 # Elapsed time
5+
6+
check_redis() {
7+
host=$1
8+
port=$2
9+
while [ $elapsed -lt $TOTAL_RETRY_TIME ]; do
10+
echo "Checking Redis at $host:$port... Elapsed time: ${elapsed}s"
11+
if nc -z -w1 $TIMEOUT $host $port > /dev/null 2>&1; then
12+
echo "Redis is up at $host:$port!"
13+
return 0
14+
else
15+
echo "Redis is down at $host:$port. Retrying in $RETRY_INTERVAL seconds."
16+
sleep $RETRY_INTERVAL
17+
elapsed=$((elapsed + RETRY_INTERVAL))
18+
fi
19+
done
20+
echo "Failed to connect to Redis at $host:$port after $TOTAL_RETRY_TIME seconds."
21+
return 1
22+
}
23+
24+
# For parsing and checking connections
25+
parse_and_check() {
26+
url=$1
27+
clean_url=${url#redis://}
28+
host=$(echo $clean_url | cut -d':' -f1)
29+
port=$(echo $clean_url | cut -d':' -f2)
30+
check_redis $host $port
31+
}
32+
33+
# Main
34+
if [ "$OAUTH2_PROXY_REDIS_USE_CLUSTER" = "true" ]; then
35+
echo "Checking Redis in cluster mode..."
36+
echo "$OAUTH2_PROXY_REDIS_CLUSTER_CONNECTION_URLS" | tr ',' '\n' | while read -r addr; do
37+
parse_and_check $addr || exit 1
38+
done
39+
elif [ "$OAUTH2_PROXY_REDIS_USE_SENTINEL" = "true" ]; then
40+
echo "Checking Redis in sentinel mode..."
41+
echo "$OAUTH2_PROXY_REDIS_SENTINEL_CONNECTION_URLS" | tr ',' '\n' | while read -r addr; do
42+
parse_and_check $addr || exit 1
43+
done
44+
elif [ -n "$OAUTH2_PROXY_REDIS_CONNECTION_URL" ]; then
45+
echo "Checking standalone Redis..."
46+
parse_and_check "$OAUTH2_PROXY_REDIS_CONNECTION_URL" || exit 1
47+
else
48+
echo "Redis configuration not specified."
49+
exit 1
50+
fi
51+
52+
echo "Redis check completed."
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{{- if and .Values.redis.enabled .Values.initContainers.waitForRedis.enabled }}
2+
apiVersion: v1
3+
kind: ConfigMap
4+
metadata:
5+
labels:
6+
app: {{ template "oauth2-proxy.name" . }}
7+
{{- include "oauth2-proxy.labels" . | indent 4 }}
8+
name: {{ template "oauth2-proxy.fullname" . }}-wait-for-redis
9+
namespace: {{ template "oauth2-proxy.namespace" $ }}
10+
data:
11+
check-redis.sh: |
12+
{{ .Files.Get "scripts/check-redis.sh" | indent 4 }}
13+
{{- end }}

helm/oauth2-proxy/templates/deployment.yaml

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -62,20 +62,32 @@ spec:
6262
{{- if and .Values.redis.enabled .Values.initContainers.waitForRedis.enabled }}
6363
initContainers:
6464
- name: wait-for-redis
65-
image: "{{ .Values.initContainers.waitForRedis.image.repository }}:{{ include "kubectl.version" . }}"
65+
image: "{{ .Values.initContainers.waitForRedis.image.repository }}:{{ .Values.initContainers.waitForRedis.image.tag }}"
6666
imagePullPolicy: {{ .Values.initContainers.waitForRedis.image.pullPolicy }}
67-
args:
68-
- wait
69-
- pod/{{ include "oauth2-proxy.redis.fullname" . }}-master-0
70-
- --for=condition=ready
71-
- --timeout={{ .Values.initContainers.waitForRedis.timeout }}s
67+
command: ["/bin/sh", "-c", "/scripts/check-redis.sh"]
68+
env:
69+
- name: TOTAL_RETRY_TIME
70+
value: "{{ .Values.initContainers.waitForRedis.timeout }}"
71+
{{- if eq (default "" .Values.sessionStorage.redis.clientType) "standalone" }}
72+
- name: OAUTH2_PROXY_REDIS_CONNECTION_URL
73+
value: {{ include "oauth2-proxy.redis.StandaloneUrl" . }}
74+
{{- else if eq (default "" .Values.sessionStorage.redis.clientType) "cluster" }}
75+
- name: OAUTH2_PROXY_REDIS_CLUSTER_CONNECTION_URLS
76+
value: {{ .Values.sessionStorage.redis.cluster.connectionUrls }}
77+
{{- else if eq (default "" .Values.sessionStorage.redis.clientType) "sentinel" }}
78+
- name: OAUTH2_PROXY_REDIS_SENTINEL_CONNECTION_URLS
79+
value: {{ .Values.sessionStorage.redis.sentinel.connectionUrls }}
80+
{{- end }}
7281
{{- if .Values.initContainers.waitForRedis.securityContext.enabled }}
7382
{{- $securityContext := unset .Values.initContainers.waitForRedis.securityContext "enabled" }}
7483
securityContext:
7584
{{- toYaml $securityContext | nindent 10 }}
7685
{{- end }}
7786
resources:
7887
{{- toYaml .Values.initContainers.waitForRedis.resources | nindent 10 }}
88+
volumeMounts:
89+
- name: redis-script
90+
mountPath: /scripts
7991
{{- end }}
8092
{{- if .Values.terminationGracePeriodSeconds }}
8193
terminationGracePeriodSeconds: {{ .Values.terminationGracePeriodSeconds }}
@@ -319,7 +331,12 @@ spec:
319331
secretName: {{ template "oauth2-proxy.fullname" . }}-accesslist
320332
{{- end }}
321333
{{- end }}
322-
334+
{{- if and .Values.redis.enabled .Values.initContainers.waitForRedis.enabled }}
335+
- name: redis-script
336+
configMap:
337+
name: {{ template "oauth2-proxy.fullname" . }}-wait-for-redis
338+
defaultMode: 0775
339+
{{- end }}
323340
{{- if or .Values.config.existingConfig .Values.config.configFile }}
324341
- configMap:
325342
defaultMode: 420

helm/oauth2-proxy/values.yaml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,8 @@ initContainers:
279279
waitForRedis:
280280
enabled: true
281281
image:
282-
repository: "docker.io/bitnami/kubectl"
282+
repository: "alpine"
283+
tag: "latest"
283284
pullPolicy: "IfNotPresent"
284285
# uses the kubernetes version of the cluster
285286
# the chart is deployed on, if not set

0 commit comments

Comments
 (0)