kind.sh 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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. [ "$($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)" -eq "$1" ]
  117. }
  118. check_peer() {
  119. local INTERFACE=$1
  120. local PEER=$2
  121. local ALLOWED_IP=$3
  122. local GRANULARITY=$4
  123. create_interface "$INTERFACE"
  124. docker run --rm --entrypoint=/usr/bin/wg "$KILO_IMAGE" genkey > "$INTERFACE"
  125. 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"
  126. assert "$KGCTL_BINARY showconf peer $PEER --mesh-granularity=$GRANULARITY > $PEER.ini" "should be able to get Peer configuration"
  127. 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"
  128. 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
  129. docker run --rm --network=host --cap-add=NET_ADMIN --entrypoint=/sbin/ip "$KILO_IMAGE" address add "$ALLOWED_IP" dev "$INTERFACE"
  130. docker run --rm --network=host --cap-add=NET_ADMIN --entrypoint=/sbin/ip "$KILO_IMAGE" link set "$INTERFACE" up
  131. docker run --rm --network=host --cap-add=NET_ADMIN --entrypoint=/sbin/ip "$KILO_IMAGE" route add 10.42/16 dev "$INTERFACE"
  132. assert "retry 10 5 '' check_ping --local" "should be able to ping Pods from host"
  133. rm "$INTERFACE" "$PEER".ini
  134. delete_peer "$PEER"
  135. delete_interface "$INTERFACE"
  136. }
  137. test_locationmesh() {
  138. # shellcheck disable=SC2016
  139. $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"]}]}}}}'
  140. block_until_ready_by_name kube-system kilo-userspace
  141. $KUBECTL_BINARY wait pod -l app.kubernetes.io/name=adjacency --for=condition=Ready --timeout 3m
  142. assert "retry 30 5 '' check_ping" "should be able to ping all Pods"
  143. assert "retry 10 5 'the adjacency matrix is not complete yet' check_adjacent 12" "adjacency should return the right number of successful pings"
  144. echo "sleep for 30s (one reconciliation period) and try again..."
  145. sleep 30
  146. 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"
  147. }
  148. test_locationmesh_peer() {
  149. check_peer wg1 e2e 10.5.0.1/32 location
  150. }
  151. test_fullmesh() {
  152. # shellcheck disable=SC2016
  153. $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"]}]}}}}'
  154. block_until_ready_by_name kube-system kilo-userspace
  155. $KUBECTL_BINARY wait pod -l app.kubernetes.io/name=adjacency --for=condition=Ready --timeout 3m
  156. assert "retry 30 5 '' check_ping" "should be able to ping all Pods"
  157. assert "retry 10 5 'the adjacency matrix is not complete yet' check_adjacent 12" "adjacency should return the right number of successful pings"
  158. echo "sleep for 30s (one reconciliation period) and try again..."
  159. sleep 30
  160. 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"
  161. }
  162. test_fullmesh_peer() {
  163. check_peer wg1 e2e 10.5.0.1/32 full
  164. }
  165. teardown_suite () {
  166. $KIND_BINARY delete clusters $KIND_CLUSTER
  167. }