lib.sh 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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. # create_cluster launches a kind cluster and deploys Kilo, Adjacency, and a helper with curl.
  72. create_cluster() {
  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. delete_cluster () {
  96. $KIND_BINARY delete clusters $KIND_CLUSTER
  97. }
  98. curl_pod() {
  99. $KUBECTL_BINARY get pods -l app.kubernetes.io/name=curl -o name | xargs -I{} "$KUBECTL_BINARY" exec {} -- /bin/sh -c "curl $*"
  100. }
  101. check_ping() {
  102. local LOCAL
  103. while [ $# -gt 0 ]; do
  104. case $1 in
  105. --local)
  106. LOCAL=true
  107. ;;
  108. esac
  109. shift
  110. done
  111. for ip in $($KUBECTL_BINARY get pods -l app.kubernetes.io/name=adjacency -o jsonpath='{.items[*].status.podIP}'); do
  112. if [ -n "$LOCAL" ]; then
  113. ping=$(curl -m 1 -s http://"$ip":8080/ping)
  114. else
  115. ping=$(curl_pod -m 1 -s http://"$ip":8080/ping)
  116. fi
  117. if [ "$ping" = "pong" ]; then
  118. echo "successfully pinged $ip"
  119. else
  120. printf 'failed to ping %s; expected "pong" but got "%s"\n' "$ip" "$ping"
  121. return 1
  122. fi
  123. done
  124. return 0
  125. }
  126. check_adjacent() {
  127. $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'
  128. [ "$(curl_pod -m 1 -s adjacency:8080/?format=json | jq | grep -c true)" -eq "$1" ]
  129. }
  130. check_peer() {
  131. local INTERFACE=$1
  132. local PEER=$2
  133. local ALLOWED_IP=$3
  134. local GRANULARITY=$4
  135. create_interface "$INTERFACE"
  136. docker run --rm --entrypoint=/usr/bin/wg "$KILO_IMAGE" genkey > "$INTERFACE"
  137. 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"
  138. assert "$KGCTL_BINARY showconf peer $PEER --mesh-granularity=$GRANULARITY > $PEER.ini" "should be able to get Peer configuration"
  139. 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"
  140. 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
  141. docker run --rm --network=host --cap-add=NET_ADMIN --entrypoint=/sbin/ip "$KILO_IMAGE" address add "$ALLOWED_IP" dev "$INTERFACE"
  142. docker run --rm --network=host --cap-add=NET_ADMIN --entrypoint=/sbin/ip "$KILO_IMAGE" link set "$INTERFACE" up
  143. docker run --rm --network=host --cap-add=NET_ADMIN --entrypoint=/sbin/ip "$KILO_IMAGE" route add 10.42/16 dev "$INTERFACE"
  144. assert "retry 10 5 '' check_ping --local" "should be able to ping Pods from host"
  145. assert_equals "$($KGCTL_BINARY showconf peer "$PEER")" "$($KGCTL_BINARY showconf peer "$PEER" --mesh-granularity="$GRANULARITY")" "kgctl should be able to auto detect the mesh granularity"
  146. rm "$INTERFACE" "$PEER".ini
  147. delete_peer "$PEER"
  148. delete_interface "$INTERFACE"
  149. }