kind.sh 8.3 KB

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