diff --git a/site/weavedns.md b/site/weavedns.md
index 9a697f2c..e3da2fcc 100644
--- a/site/weavedns.md
+++ b/site/weavedns.md
@@ -164,6 +164,18 @@ command:
$ weave dns-remove $C
```
+By omitting the container name it is possible to add/remove DNS
+records that associate names in the weaveDNS domain with IP addresses
+that do not belong to containers, e.g. non-weave addresses of external
+services:
+
+```bash
+$ weave dns-add 192.128.16.45 -h db.weave.local
+```
+
+Note that such records get removed when stopping the weave peer on
+which they were added.
+
## Resolve weaveDNS entries from host
You can resolve entries from any host running weaveDNS with `weave
diff --git a/test/240_dns_add_name_test.sh b/test/240_dns_add_name_test.sh
index 10efa621..54cb0469 100755
--- a/test/240_dns_add_name_test.sh
+++ b/test/240_dns_add_name_test.sh
@@ -4,6 +4,7 @@
C1=10.2.0.78
C2=10.2.0.34
+C3=10.2.0.12
NAME1=seeone.weave.local
NAME2=seetwo.weave.local
NAME3=seethree.weave.local
@@ -34,4 +35,10 @@ weave_on $HOST1 dns-remove c1 -h $NAME3
assert_no_dns_record $HOST1 c1 $NAME3
+weave_on $HOST1 dns-add $C3 -h $NAME1
+assert_dns_record $HOST1 c1 $NAME1 $C3
+
+weave_on $HOST1 dns-remove $C3 -h $NAME1
+assert_no_dns_record $HOST1 c1 $NAME1
+
end_suite
diff --git a/weave b/weave
index 3ad4571e..52c46e61 100755
--- a/weave
+++ b/weave
@@ -69,8 +69,10 @@ weave run [--with-dns | --without-dns] [--no-rewrite-hosts]
weave start [ ...]
weave attach [ ...]
weave detach [ ...]
-weave dns-add [ ...] [-h ]
-weave dns-remove [ ...] [-h ]
+weave dns-add [ ...] [-h ] |
+ ... -h
+weave dns-remove [ ...] [-h ] |
+ ... -h
weave dns-lookup
weave expose [ ...] [-h ]
weave hide [ ...]
@@ -1064,10 +1066,15 @@ with_container_fqdn() {
# Register FQDN in $2 as names for addresses $3.. under full container ID $1
put_dns_fqdn() {
CHECK_ALIVE="-d check-alive=true"
- if [ "$1" = "--no-check-alive" ] ; then
- CHECK_ALIVE=
- shift 1
- fi
+ put_dns_fqdn_helper "$@"
+}
+
+put_dns_fqdn_no_check_alive() {
+ CHECK_ALIVE=
+ put_dns_fqdn_helper "$@"
+}
+
+put_dns_fqdn_helper() {
CONTAINER_ID="$1"
FQDN="$2"
shift 2
@@ -1098,6 +1105,27 @@ delete_dns_fqdn() {
done
}
+collect_dns_add_remove_args() {
+ collect_ip_args "$@"
+ shift $IP_COUNT
+ if [ $# -gt 0 -a "$1" != "-h" ] ; then
+ C="$1"
+ shift
+ fi
+ if [ $# -eq 2 -a "$1" = "-h" ] ; then
+ FQDN="$2"
+ shift 2
+ fi
+ [ $# -eq 0 -a \( -n "$C" -o \( $IP_COUNT -gt 0 -a -n "$FQDN" \) \) ] || usage
+ check_running $CONTAINER_NAME
+ if [ -n "$C" ] ; then
+ CONTAINER=$(container_id $C)
+ if [ $IP_COUNT -eq 0 ] ; then
+ IP_ARGS=$(with_container_addresses echo_ips $CONTAINER)
+ fi
+ fi
+}
+
######################################################################
# IP Allocation Management helpers
######################################################################
@@ -1785,33 +1813,25 @@ case "$COMMAND" in
show_addrs $ALL_CIDRS
;;
dns-add)
- collect_ip_args "$@"
- shift $IP_COUNT
- [ $# -eq 1 -o \( $# -eq 3 -a "$2" = "-h" \) ] || usage
- check_running $CONTAINER_NAME
- CONTAINER=$(container_id $1)
- if [ $IP_COUNT -eq 0 ] ; then
- IP_ARGS=$(with_container_addresses echo_ips $CONTAINER)
+ collect_dns_add_remove_args "$@"
+ FN=put_dns_fqdn
+ if [ -z "$CONTAINER" ] ; then
+ FN=put_dns_fqdn_no_check_alive
+ CONTAINER=weave:extern
fi
- if [ $# -eq 1 ] ; then
- with_container_fqdn $CONTAINER put_dns_fqdn $IP_ARGS
+ if [ -n "$FQDN" ] ; then
+ $FN $CONTAINER $FQDN $IP_ARGS
else
- put_dns_fqdn $CONTAINER "$3" $IP_ARGS
+ with_container_fqdn $CONTAINER $FN $IP_ARGS
fi
;;
dns-remove)
- collect_ip_args "$@"
- shift $IP_COUNT
- [ $# -eq 1 -o \( $# -eq 3 -a "$2" = "-h" \) ] || usage
- check_running $CONTAINER_NAME
- CONTAINER=$(container_id $1)
- if [ $IP_COUNT -eq 0 ] ; then
- IP_ARGS=$(with_container_addresses echo_ips $CONTAINER)
- fi
- if [ $# -eq 1 ] ; then
- delete_dns $CONTAINER $IP_ARGS
+ collect_dns_add_remove_args "$@"
+ [ -n "$CONTAINER" ] || CONTAINER=weave:extern
+ if [ -n "$FQDN" ] ; then
+ delete_dns_fqdn $CONTAINER $FQDN $IP_ARGS
else
- delete_dns_fqdn $CONTAINER "$3" $IP_ARGS
+ delete_dns $CONTAINER $IP_ARGS
fi
;;
dns-lookup)
@@ -1837,7 +1857,7 @@ case "$COMMAND" in
add_iptables_rule nat WEAVE -d $CIDR ! -s $CIDR -j MASQUERADE
add_iptables_rule nat WEAVE -s $CIDR ! -d $CIDR -j MASQUERADE
if [ -n "$FQDN" ] ; then
- when_weave_running put_dns_fqdn --no-check-alive weave:expose $FQDN $CIDR
+ when_weave_running put_dns_fqdn_no_check_alive weave:expose $FQDN $CIDR
fi
fi
done