Skip to content

Commit

Permalink
Support dual-stack when IPv6 is main family
Browse files Browse the repository at this point in the history
Works only when $IPV6_PREFIX is used
  • Loading branch information
Lars Ekman committed Sep 16, 2020
1 parent 28a8612 commit 3320913
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 4 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
image/bin/list-nodes
image/bin/ipv4
image/opt/cni/bin/*
image/build-date
!image/opt/cni/bin/node-local
1 change: 1 addition & 0 deletions build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ cmd_image() {
go build -ldflags "-extldflags '-static' -X main.version=$__version" \
-o image/bin/list-nodes ./cmd/...
strip image/bin/list-nodes
gcc -o image/bin/ipv4 -static ./src/ipv4.c
date > image/build-date
docker build -t $__image:$__version .
}
Expand Down
9 changes: 7 additions & 2 deletions image/bin/xcluster-cni-router.sh
Original file line number Diff line number Diff line change
Expand Up @@ -220,8 +220,13 @@ get_addresses() {
fi
done

test -n "$a6" && return 0
test -n "$a4" || return 0
test -n "$a6" -a -n "$a4" && return 0
if test -z "$a4"; then
# We have an ipv6 address, but no ipv4.
# We can only handle this case if $IPV6_PREFIX is used.
test -n "$IPV6_PREFIX" && a4=$(ipv4 $a6)
return 0
fi

# We have an ipv4 address, but no ipv6.

Expand Down
37 changes: 35 additions & 2 deletions image/xcluster-cni.sh
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,44 @@ cmd_env() {
cmd_pod_cidrs() {
cmd_env
list-nodes | jq "select(.metadata.name == \"$K8S_NODE\")" > $my_node_info
if test "$(jq .spec.podCIDRs < $my_node_info)" != "null"; then
if test "$(jq .spec.podCIDRs < $my_node_info)" = "null"; then
# Pre v1.16 cluster
if test "$(jq .spec.podCIDR < $my_node_info)" != "null"; then
jq -r '.spec.podCIDR' < $my_node_info
fi
return 0
fi

if test "$(jq .spec.podCIDRs[1] < $my_node_info)" = "null"; then
# Single-stack cluster
jq -r '.spec.podCIDRs[]' < $my_node_info
return 0
fi

# For dual-stack the CNI-plugin must make sure that the first
# address is of the "main" family in the cluster ?!?!?!
if echo "$KUBERNETES_SERVICE_HOST" | grep -q : ; then
# Main family ipv6
if jq -r '.spec.podCIDRs[0]' < $my_node_info grep -q : ; then
# Ok, correct family
jq -r '.spec.podCIDRs[]' < $my_node_info
else
# Reverse the order
jq -r '.spec.podCIDRs[1]' < $my_node_info
jq -r '.spec.podCIDRs[0]' < $my_node_info
fi
else
jq -r '.spec.podCIDR' < $my_node_info
# Main family ipv4
if jq -r '.spec.podCIDRs[0]' < $my_node_info grep -q : ; then
# Reverse the order
jq -r '.spec.podCIDRs[1]' < $my_node_info
jq -r '.spec.podCIDRs[0]' < $my_node_info
else
# Ok, correct family
jq -r '.spec.podCIDRs[]' < $my_node_info
fi
fi
return 0
}

# Print the interface that holds the passed address
Expand Down
24 changes: 24 additions & 0 deletions src/ipv4.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#include <stdio.h>
#include <arpa/inet.h>


int
main(int argc, char* argv[])
{
if (argc < 2) {
printf("0.0.0.0\n");
return 0;
}
unsigned char buf[sizeof(struct in6_addr)];
if (inet_pton(AF_INET6, argv[1], buf) == 1) {
struct in6_addr* ap = (struct in6_addr*)buf;
struct in_addr a4 = {ap->s6_addr32[3]};
printf("%s\n", inet_ntoa(a4));
return 0;
}
if (inet_pton(AF_INET, argv[1], buf) == 1) {
printf("%s\n", argv[1]);
return 0;
}
return -1;
}

0 comments on commit 3320913

Please sign in to comment.