Skip to content
This repository was archived by the owner on May 17, 2023. It is now read-only.

Commit 48ec4ca

Browse files
authored
Merge pull request bowei#14 from geastman3/master
Add wait for iptables if another application (kube-proxy) is using ip…
2 parents ce42ade + 2042ded commit 48ec4ca

File tree

1 file changed

+19
-6
lines changed

1 file changed

+19
-6
lines changed

run.sh

+19-6
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ set -e
1717

1818
IPTABLES=${IPTABLES:-/sbin/iptables}
1919
SLEEP_INTERVAL=${SLEEP_INTERVAL:-10}
20+
WAIT_INTERVAL=${WAIT_INTERVAL:-60}
2021
CONFIG_DIR=${CONFIG_DIR:-/cfg}
2122
if [[ -z ${UUID:-} ]]; then
2223
UUID=$(date "+%s")
@@ -37,7 +38,7 @@ update_nat() {
3738
# Check if the rule already exists if adding.
3839
if [[ ${mode} = 'A' ]]; then
3940
if ${IPTABLES} -t nat -C POSTROUTING -d ${subnet} \
40-
-m comment --comment "${comment}" -j MASQUERADE \
41+
-m comment --comment "${comment}" -j MASQUERADE -w ${WAIT_INTERVAL}\
4142
2>/dev/null; then
4243
log "NAT rule ${comment} is installed"
4344
return
@@ -49,7 +50,7 @@ update_nat() {
4950
-${mode} POSTROUTING \
5051
-d ${subnet} \
5152
-m comment --comment "${comment}" \
52-
-j MASQUERADE
53+
-j MASQUERADE -w ${WAIT_INTERVAL}
5354

5455
case ${mode} in
5556
'A') log "NAT rule ${comment} added";;
@@ -73,16 +74,28 @@ main() {
7374
# Remove the old NAT rules if config file has changed.
7475
if [[ "${old_nat_rules}" != "${nat_rules}" ]]; then
7576
log "Configuration change detected"
76-
for subnet in ${old_nat_rules}; do
77-
update_nat D ${subnet} "${COMMENT_PREFIX}: ${subnet}"
77+
n=0
78+
until [ "$n" -ge 5 ]
79+
do
80+
(for subnet in ${old_nat_rules}; do
81+
update_nat D ${subnet} "${COMMENT_PREFIX}: ${subnet}"
82+
done) && break
83+
n=$((n+1))
84+
sleep 5
7885
done
7986
fi
8087

8188
if [[ -z "${nat_rules}" ]]; then
8289
log "No NAT rules configured"
8390
else
84-
for subnet in ${old_nat_rules}; do
85-
update_nat A ${subnet} "${COMMENT_PREFIX}: ${subnet}"
91+
n=0
92+
until [ "$n" -ge 5 ]
93+
do
94+
(for subnet in ${old_nat_rules}; do
95+
update_nat A ${subnet} "${COMMENT_PREFIX}: ${subnet}"
96+
done) && break
97+
n=$((n+1))
98+
sleep 5
8699
done
87100
fi
88101

0 commit comments

Comments
 (0)