Skip to content

Commit c2c4df9

Browse files
Merge pull request #5 from Scalingo/feat/registry_upstreams
feat(openresty): allow to setup upstreams for load balancing on reads
2 parents 279332a + d1aec2f commit c2c4df9

File tree

3 files changed

+70
-0
lines changed

3 files changed

+70
-0
lines changed

.github/workflows/docker-image.yml

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
name: CI
2+
3+
on:
4+
push:
5+
tags:
6+
- 'v(\.?[0-9]+){3}(-[a-z0-9]+)?'
7+
8+
jobs:
9+
bake:
10+
name: Build and Push the Docker image
11+
runs-on: ubuntu-22.04
12+
env:
13+
image_name: docker-registry-proxy-cache
14+
org_name: scalingo
15+
steps:
16+
# Docker login
17+
- name: Login to Docker Hub
18+
uses: docker/login-action@v3
19+
with:
20+
username: ${{ vars.DOCKERHUB_USERNAME }}
21+
password: ${{ secrets.DOCKER_TOKEN }}
22+
23+
# Set docker buildx environment
24+
- name: Set up Docker Buildx
25+
uses: docker/setup-buildx-action@v3
26+
27+
# Build and push with bake
28+
- name: Build and push
29+
uses: docker/bake-action@v6
30+
with:
31+
push: true
32+
set: |
33+
${{ env.image_name }}.tags=[${{ env.org_name }}/${{ env.image_name }}:${{ github.ref_name }}]

entrypoint.sh

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,9 @@ echo -e "\nManifest caching config: ---\n"
143143
cat /opt/openresty/nginx/conf/nginx.manifest.caching.config.conf
144144
echo "---"
145145

146+
# Upstreams configuration. We generate config based on the environment vars.
147+
echo -n "" >/opt/openresty/nginx/conf/upstreams.conf
148+
146149
if [[ "a${ALLOW_PUSH}" == "atrue" ]]; then
147150
cat <<EOF >/opt/openresty/nginx/conf/allowed.methods.conf
148151
# allow to upload big layers
@@ -164,6 +167,37 @@ else
164167
return 405 "DELETE method is not allowed";
165168
}
166169
EOF
170+
if [ "$UPSTREAM_REGISTRIES" ]; then
171+
UPSTREAM_REGISTRIES_DELIMITER=${UPSTREAM_REGISTRIES_DELIMITER:-" "}
172+
s=$UPSTREAM_REGISTRIES$UPSTREAM_REGISTRIES_DELIMITER
173+
upstream_array=()
174+
while [[ $s ]]; do
175+
upstream_array+=("${s%%"$UPSTREAM_REGISTRIES_DELIMITER"*}")
176+
s=${s#*"$UPSTREAM_REGISTRIES_DELIMITER"}
177+
done
178+
179+
UPSTREAM_REGISTRY_DELIMITER=${UPSTREAM_REGISTRY_DELIMITER:-"|"}
180+
181+
for ONEREGISTRY in "${upstream_array[@]}"; do
182+
s=$ONEREGISTRY$UPSTREAM_REGISTRY_DELIMITER
183+
registry_array=()
184+
while [[ $s ]]; do
185+
registry_array+=("${s%%"$UPSTREAM_REGISTRY_DELIMITER"*}")
186+
s=${s#*"$UPSTREAM_REGISTRY_DELIMITER"}
187+
done
188+
cat <<-EOF >>/opt/openresty/nginx/conf/upstreams.conf
189+
upstream ${registry_array[0]} {
190+
$(
191+
for i in ${!registry_array[@]}; do
192+
if [[ i -gt 0 ]]; then
193+
echo " server ${registry_array[$i]} fail_timeout=5;"
194+
fi
195+
done
196+
)
197+
}
198+
EOF
199+
done
200+
fi
167201
fi
168202

169203
# Only configure htpasswd if the env var exists

nginx.conf

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,9 @@ http {
8181
# Entrypoint generates the proxy_cache_path here, so it is configurable externally.
8282
include /opt/openresty/nginx/conf/cache_max_size.conf;
8383

84+
# Entrypoint generates the upstreams here, so it is configurable externally.
85+
include /opt/openresty/nginx/conf/upstreams.conf;
86+
8487
# Just in case you want to rewrite some hosts. Default maps directly.
8588
map $host $targetHost {
8689
hostnames;

0 commit comments

Comments
 (0)