Skip to content

Commit

Permalink
Merge pull request #5 from qpoint-io/marc-barry/support-new-qtap
Browse files Browse the repository at this point in the history
Add support for port mappings and TO_DOMAIN.
  • Loading branch information
marc-barry authored Nov 9, 2023
2 parents 98ef6cd + d3395eb commit a533bf3
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 15 deletions.
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
FROM alpine:3.18

RUN apk add --no-cache iptables bash && rm -rf /var/cache/apk/*
RUN apk add --no-cache bash bind-tools iptables && rm -rf /var/cache/apk/*

COPY docker-entrypoint.sh /usr/local/bin/

Expand Down
35 changes: 21 additions & 14 deletions docker-entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,29 @@

set -e

if [[ -n "$TO_DOMAIN" ]]; then
TO_ADDR=$(dig +short "$TO_DOMAIN" | head -n 1)
fi

# Default values for ACCEPT_UIDS and ACCEPT_GIDS
DEFAULT_ACCEPT_UIDS="1010" # Default UID of Qtap
DEFAULT_ACCEPT_GIDS="1010" # Default GID of Qtap

DEFAULT_TO_PORT="10000" # Default listen port of Qtap
DEFAULT_PORT_MAPPING="10080:80,10443:443,10000:"

# Set default values for ACCEPT_UIDS and ACCEPT_GIDS if they are not provided
ACCEPT_UIDS="${ACCEPT_UIDS:-$DEFAULT_ACCEPT_UIDS}"
ACCEPT_GIDS="${ACCEPT_GIDS:-$DEFAULT_ACCEPT_GIDS}"

TO_PORT="${TO_PORT:-$DEFAULT_TO_PORT}"
PORT_MAPPING="${PORT_MAPPING:-$DEFAULT_PORT_MAPPING}"

apply_rules() {
local PORT_SPECIFIER="$1"
local TO_PORT="$1"
local DEST_PORT="$2"

local PORT_SPECIFIER=""
if [[ -n "$DEST_PORT" ]]; then
PORT_SPECIFIER="--dport $DEST_PORT"
fi

# Apply rules for UIDs
IFS=',' read -ra UIDS <<< "$ACCEPT_UIDS"
Expand All @@ -37,16 +46,14 @@ apply_rules() {
fi
}

# If DESTINATION_PORTS is set, split it into an array and apply rules for each port
if [[ -n "$DESTINATION_PORTS" ]]; then
IFS=',' read -ra DEST_PORTS <<< "$DESTINATION_PORTS"
for PORT in "${DEST_PORTS[@]}"; do
apply_rules "--dport $PORT"
done
else
# Apply rules without specifying dport
apply_rules ""
fi
IFS=',' read -ra MAPPINGS <<< "$PORT_MAPPING"
for MAPPING in "${MAPPINGS[@]}"; do
IFS=':' read -ra PORTS <<< "$MAPPING"
TO_PORT="${PORTS[0]}"
DEST_PORT="${PORTS[1]}"

apply_rules "$TO_PORT" "$DEST_PORT"
done

# Ensure the rules are set
iptables -t nat -L -n -v
2 changes: 2 additions & 0 deletions example2.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ spec:
image: us-docker.pkg.dev/qpoint-edge/public/qtap:<SHA>
ports:
- containerPort: 10000
- containerPort: 10080
- containerPort: 10443
command: ["qtap"]
args: ["gateway", "--no-hot-restart"]
env:
Expand Down

0 comments on commit a533bf3

Please sign in to comment.