kind.sh 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. #!/usr/bin/env bash
  2. # shellcheck disable=SC2034
  3. export KUBECONFIG="kind.yaml"
  4. KIND_CLUSTER="kind-cluster-kilo"
  5. KIND_BINARY="${KIND_BINARY:-kind}"
  6. KUBECTL_BINARY="${KUBECTL_BINARY:-kubectl}"
  7. KGCTL_BINARY="${KGCTL_BINARY:-kgctl}"
  8. KILO_IMAGE="${KILO_IMAGE:-squat/kilo}"
  9. retry() {
  10. local COUNT="${1:-10}"
  11. local SLEEP="${2:-5}"
  12. local ERROR=$3
  13. [ -n "$ERROR" ] && ERROR="$ERROR "
  14. shift 3
  15. for c in $(seq 1 "$COUNT"); do
  16. if "$@"; then
  17. return 0
  18. else
  19. printf "%s(attempt %d/%d)\n" "$ERROR" "$c" "$COUNT" | color "$YELLOW"
  20. if [ "$c" != "$COUNT" ]; then
  21. printf "retrying in %d seconds...\n" "$SLEEP" | color "$YELLOW"
  22. sleep "$SLEEP"
  23. fi
  24. fi
  25. done
  26. return 1
  27. }
  28. create_interface() {
  29. docker run -d --name="$1" --rm --network=host --cap-add=NET_ADMIN --device=/dev/net/tun -v /var/run/wireguard:/var/run/wireguard -e WG_LOG_LEVEL=debug leonnicolas/boringtun --foreground --disable-drop-privileges true "$1"
  30. }
  31. delete_interface() {
  32. docker rm --force "$1"
  33. }
  34. create_peer() {
  35. cat <<EOF | $KUBECTL_BINARY apply -f -
  36. apiVersion: kilo.squat.ai/v1alpha1
  37. kind: Peer
  38. metadata:
  39. name: $1
  40. spec:
  41. allowedIPs:
  42. - $2
  43. persistentKeepalive: $3
  44. publicKey: $4
  45. EOF
  46. }
  47. delete_peer() {
  48. $KUBECTL_BINARY delete peer "$1"
  49. }
  50. is_ready() {
  51. for pod in $($KUBECTL_BINARY -n "$1" get pods -o name -l "$2"); do
  52. if ! $KUBECTL_BINARY -n "$1" get "$pod" | tail -n 1 | grep -q Running; then
  53. return 1;
  54. fi
  55. done
  56. return 0
  57. }
  58. # Returns non zero if one pod of the given name in the given namespace is not ready.
  59. block_until_ready_by_name() {
  60. block_until_ready "$1" "app.kubernetes.io/name=$2"
  61. }
  62. # Blocks until all pods of a deployment are ready.
  63. block_until_ready() {
  64. retry 30 5 "some $2 pods are not ready yet" is_ready "$1" "$2"
  65. }
  66. # Set up the kind cluster and deploy Kilo, Adjacency and a helper with curl.
  67. setup_suite() {
  68. $KIND_BINARY delete clusters $KIND_CLUSTER > /dev/null
  69. # Create the kind cluster.
  70. $KIND_BINARY create cluster --name $KIND_CLUSTER --config ./kind-config.yaml
  71. # Load the Kilo image into kind.
  72. docker tag "$KILO_IMAGE" squat/kilo:test
  73. $KIND_BINARY load docker-image squat/kilo:test --name $KIND_CLUSTER
  74. # Create the kubeconfig secret.
  75. $KUBECTL_BINARY create secret generic kubeconfig --from-file=kubeconfig="$KUBECONFIG" -n kube-system
  76. # Apply Kilo the the cluster.
  77. $KUBECTL_BINARY apply -f ../manifests/crds.yaml
  78. $KUBECTL_BINARY apply -f kilo-kind-userspace.yaml
  79. block_until_ready_by_name kube-system kilo-userspace
  80. $KUBECTL_BINARY wait nodes --all --for=condition=Ready
  81. # wait for coredns
  82. block_until_ready kube_system k8s-app=kube-dns
  83. $KUBECTL_BINARY taint node $KIND_CLUSTER-control-plane node-role.kubernetes.io/master:NoSchedule-
  84. $KUBECTL_BINARY apply -f https://raw.githubusercontent.com/heptoprint/adjacency/master/example.yaml
  85. $KUBECTL_BINARY apply -f helper-curl.yaml
  86. block_until_ready_by_name adjacency adjacency
  87. block_until_ready_by_name default curl
  88. }
  89. check_ping() {
  90. local LOCAL
  91. while [ $# -gt 0 ]; do
  92. case $1 in
  93. --local)
  94. LOCAL=true
  95. ;;
  96. esac
  97. shift
  98. done
  99. for ip in $($KUBECTL_BINARY get pods -l app.kubernetes.io/name=adjacency -o jsonpath='{.items[*].status.podIP}'); do
  100. if [ -n "$LOCAL" ]; then
  101. ping=$(curl -m 1 -s http://"$ip":8080/ping)
  102. else
  103. ping=$($KUBECTL_BINARY get pods -l app.kubernetes.io/name=curl -o name | xargs -I{} "$KUBECTL_BINARY" exec {} -- /bin/sh -c "curl -m 1 -s http://$ip:8080/ping")
  104. fi
  105. if [ "$ping" = "pong" ]; then
  106. echo "successfully pinged $ip"
  107. else
  108. printf 'failed to ping %s; expected "pong" but got "%s"\n' "$ip" "$ping"
  109. return 1
  110. fi
  111. done
  112. return 0
  113. }
  114. check_adjacent() {
  115. $KUBECTL_BINARY get pods -l app.kubernetes.io/name=curl -o name | xargs -I{} "$KUBECTL_BINARY" exec {} -- /bin/sh -c 'curl -m 1 -s adjacency:8080/?format=fancy'
  116. assert_equals "12" \
  117. "$($KUBECTL_BINARY get pods -l app.kubernetes.io/name=curl -o name | xargs -I{} "$KUBECTL_BINARY" exec {} -- /bin/sh -c 'curl -m 1 -s adjacency:8080/?format=json' | jq | grep -c true)" \
  118. "Adjacency returned the wrong number of successful pings"
  119. echo "sleep for 30s (one reconciliation period) and try again..."
  120. sleep 30
  121. $KUBECTL_BINARY get pods -l app.kubernetes.io/name=curl -o name | xargs -I{} "$KUBECTL_BINARY" exec {} -- /bin/sh -c 'curl -m 1 -s adjacency:8080/?format=fancy'
  122. assert_equals "12" \
  123. "$($KUBECTL_BINARY get pods -l app.kubernetes.io/name=curl -o name | xargs -I{} "$KUBECTL_BINARY" exec {} -- /bin/sh -c 'curl -m 1 -s adjacency:8080/?format=json' | jq | grep -c true)" \
  124. "Adjacency returned the wrong number of successful pings"
  125. }
  126. check_peer() {
  127. local INTERFACE=$1
  128. local PEER=$2
  129. local ALLOWED_IP=$3
  130. local GRANULARITY=$4
  131. create_interface "$INTERFACE"
  132. docker run --rm --entrypoint=/usr/bin/wg "$KILO_IMAGE" genkey > "$INTERFACE"
  133. create_peer "$PEER" "$ALLOWED_IP" 10 "$(docker run --rm --entrypoint=/bin/sh -v "$PWD/$INTERFACE":/key "$KILO_IMAGE" -c 'cat /key | wg pubkey')"
  134. $KGCTL_BINARY showconf peer "$PEER" --mesh-granularity="$GRANULARITY" > "$PEER".ini
  135. docker run --rm --network=host --cap-add=NET_ADMIN --entrypoint=/usr/bin/wg -v /var/run/wireguard:/var/run/wireguard -v "$PWD/$PEER.ini":/peer.ini "$KILO_IMAGE" setconf "$INTERFACE" /peer.ini
  136. docker run --rm --network=host --cap-add=NET_ADMIN --entrypoint=/usr/bin/wg -v /var/run/wireguard:/var/run/wireguard -v "$PWD/$INTERFACE":/key "$KILO_IMAGE" set "$INTERFACE" private-key /key
  137. docker run --rm --network=host --cap-add=NET_ADMIN --entrypoint=/sbin/ip "$KILO_IMAGE" address add "$ALLOWED_IP" dev "$INTERFACE"
  138. docker run --rm --network=host --cap-add=NET_ADMIN --entrypoint=/sbin/ip "$KILO_IMAGE" link set "$INTERFACE" up
  139. docker run --rm --network=host --cap-add=NET_ADMIN --entrypoint=/sbin/ip "$KILO_IMAGE" route add 10.42/16 dev "$INTERFACE"
  140. retry 10 5 "" check_ping --local
  141. rm "$INTERFACE" "$PEER".ini
  142. delete_peer "$PEER"
  143. delete_interface "$INTERFACE"
  144. }
  145. test_locationmesh() {
  146. # shellcheck disable=SC2016
  147. $KUBECTL_BINARY patch ds -n kube-system kilo -p '{"spec": {"template":{"spec":{"containers":[{"name":"kilo","args":["--hostname=$(NODE_NAME)","--create-interface=false","--kubeconfig=/etc/kubernetes/kubeconfig","--mesh-granularity=location"]}]}}}}'
  148. sleep 5
  149. block_until_ready_by_name kube-system kilo-userspace
  150. $KUBECTL_BINARY wait pod -l app.kubernetes.io/name=adjacency --for=condition=Ready --timeout 3m
  151. retry 30 5 "" check_ping
  152. sleep 5
  153. retry 10 5 "the adjacency matrix is not complete yet" check_adjacent
  154. }
  155. test_locationmesh_peer() {
  156. check_peer wg1 e2e 10.5.0.1/32 location
  157. }
  158. test_fullmesh() {
  159. # shellcheck disable=SC2016
  160. $KUBECTL_BINARY patch ds -n kube-system kilo -p '{"spec": {"template":{"spec":{"containers":[{"name":"kilo","args":["--hostname=$(NODE_NAME)","--create-interface=false","--kubeconfig=/etc/kubernetes/kubeconfig","--mesh-granularity=full"]}]}}}}'
  161. sleep 5
  162. block_until_ready_by_name kube-system kilo-userspace
  163. $KUBECTL_BINARY wait pod -l app.kubernetes.io/name=adjacency --for=condition=Ready --timeout 3m
  164. retry 30 5 "" check_ping
  165. sleep 5
  166. retry 10 5 "the adjacency matrix is not complete yet" check_adjacent
  167. }
  168. test_fullmesh_peer() {
  169. check_peer wg1 e2e 10.5.0.1/32 full
  170. }
  171. teardown_suite () {
  172. $KIND_BINARY delete clusters $KIND_CLUSTER
  173. }