Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add IPv6 support for local-resolvers substitution script #855

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .test/config.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ imageTests+=(
static
templates
templates-resolver
templates-resolver-ipv6
workers
modules
'
Expand Down
2 changes: 2 additions & 0 deletions .test/tests/templates-resolver-ipv6/expected-std-out.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
example.com - OK
ipv6 nameserver(s) present
81 changes: 81 additions & 0 deletions .test/tests/templates-resolver-ipv6/run.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
#!/bin/bash

[ "$DEBUG" ] && set -x

set -eo pipefail

# check if we have ipv6 available
if [ ! -f "/proc/net/if_inet6" ]; then
exit 0
fi

dir="$(dirname "$(readlink -f "$BASH_SOURCE")")"

image="$1"

clientImage='buildpack-deps:buster-curl'
# ensure the clientImage is ready and available
if ! docker image inspect "$clientImage" &> /dev/null; then
docker pull "$clientImage" > /dev/null
fi

# Create a new Docker network
nid="$(docker network create --ipv6 --subnet fd0c:7e57::/64 nginx-test-ipv6-network)"

_network_exit_handler() {
docker network rm -f $nid > /dev/null
}

# Create an instance of the container-under-test
serverImage="$("$HOME/oi/test/tests/image-name.sh" librarytest/nginx-template "$image")"
"$HOME/oi/test/tests/docker-build.sh" "$dir" "$serverImage" <<EOD
FROM $image
COPY dir/server.conf.template /etc/nginx/templates/server.conf.template
EOD
cid="$(docker run -d --network $nid -e NGINX_ENTRYPOINT_LOCAL_RESOLVERS=true -e NGINX_MY_SERVER_NAME=example.com "$serverImage")"

_container_exit_handler() {
docker rm -vf $cid > /dev/null
}
_exit_handler() { _container_exit_handler; _network_exit_handler; }
trap "_exit_handler" EXIT

ipv6cid="$(docker inspect -f '{{range.NetworkSettings.Networks}}{{.GlobalIPv6Address}}{{end}}' $cid)"

_request() {
local method="$1"
shift

local proto="$1"
shift

local url="${1#/}"
shift

if [ "$(docker inspect -f '{{.State.Running}}' "$cid" 2>/dev/null)" != 'true' ]; then
echo >&2 "$image stopped unexpectedly!"
( set -x && docker logs "$cid" ) >&2 || true
false
fi

docker run --rm \
--network "$nid" \
"$clientImage" \
curl -fsSL -X"$method" --connect-to "::[$ipv6cid]:" "$@" "$proto://example.com/$url"
}

. "$HOME/oi/test/retry.sh" '[ "$(_request GET / --output /dev/null || echo $?)" != 7 ]'

# Check that we can request /
_request GET http '/resolver-templates' | grep 'example.com - OK'

result="$(docker exec $cid grep resolver /etc/nginx/conf.d/server.conf)"

case "$result" in
resolver*\[*\]*)
echo "ipv6 nameserver(s) present"
;;
*)
echo "no ipv6 nameserver(s) present"
;;
esac
10 changes: 10 additions & 0 deletions .test/tests/templates-resolver-ipv6/server.conf.template
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
resolver ${NGINX_LOCAL_RESOLVERS};

server {
listen 80;
listen [::]:80;
server_name ${NGINX_MY_SERVER_NAME};
default_type text/plain;
location = / { return 200 'OK\n'; }
location / { return 200 "${NGINX_MY_SERVER_NAME} - OK\n"; }
}
2 changes: 1 addition & 1 deletion entrypoint/15-local-resolvers.envsh
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@ PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin

[ "${NGINX_ENTRYPOINT_LOCAL_RESOLVERS:-}" ] || return 0

NGINX_LOCAL_RESOLVERS=$(awk 'BEGIN{ORS=" "} $1=="nameserver" {print $2}' /etc/resolv.conf)
NGINX_LOCAL_RESOLVERS=$(awk 'BEGIN{ORS=" "} $1=="nameserver" {if ($2 ~ ":") {print "["$2"]"} else {print $2}}' /etc/resolv.conf)
export NGINX_LOCAL_RESOLVERS
2 changes: 1 addition & 1 deletion mainline/alpine-slim/15-local-resolvers.envsh
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@ PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin

[ "${NGINX_ENTRYPOINT_LOCAL_RESOLVERS:-}" ] || return 0

NGINX_LOCAL_RESOLVERS=$(awk 'BEGIN{ORS=" "} $1=="nameserver" {print $2}' /etc/resolv.conf)
NGINX_LOCAL_RESOLVERS=$(awk 'BEGIN{ORS=" "} $1=="nameserver" {if ($2 ~ ":") {print "["$2"]"} else {print $2}}' /etc/resolv.conf)
export NGINX_LOCAL_RESOLVERS
2 changes: 1 addition & 1 deletion mainline/debian/15-local-resolvers.envsh
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@ PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin

[ "${NGINX_ENTRYPOINT_LOCAL_RESOLVERS:-}" ] || return 0

NGINX_LOCAL_RESOLVERS=$(awk 'BEGIN{ORS=" "} $1=="nameserver" {print $2}' /etc/resolv.conf)
NGINX_LOCAL_RESOLVERS=$(awk 'BEGIN{ORS=" "} $1=="nameserver" {if ($2 ~ ":") {print "["$2"]"} else {print $2}}' /etc/resolv.conf)
export NGINX_LOCAL_RESOLVERS
2 changes: 1 addition & 1 deletion stable/alpine-slim/15-local-resolvers.envsh
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@ PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin

[ "${NGINX_ENTRYPOINT_LOCAL_RESOLVERS:-}" ] || return 0

NGINX_LOCAL_RESOLVERS=$(awk 'BEGIN{ORS=" "} $1=="nameserver" {print $2}' /etc/resolv.conf)
NGINX_LOCAL_RESOLVERS=$(awk 'BEGIN{ORS=" "} $1=="nameserver" {if ($2 ~ ":") {print "["$2"]"} else {print $2}}' /etc/resolv.conf)
export NGINX_LOCAL_RESOLVERS
2 changes: 1 addition & 1 deletion stable/debian/15-local-resolvers.envsh
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@ PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin

[ "${NGINX_ENTRYPOINT_LOCAL_RESOLVERS:-}" ] || return 0

NGINX_LOCAL_RESOLVERS=$(awk 'BEGIN{ORS=" "} $1=="nameserver" {print $2}' /etc/resolv.conf)
NGINX_LOCAL_RESOLVERS=$(awk 'BEGIN{ORS=" "} $1=="nameserver" {if ($2 ~ ":") {print "["$2"]"} else {print $2}}' /etc/resolv.conf)
export NGINX_LOCAL_RESOLVERS
Loading