functional-test-k8s.sh 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. #!/bin/bash
  2. ARCH="${ARCH:-amd64}"
  3. ETCD_IMG="${ETCD_IMG:-quay.io/coreos/etcd:v3.2.7}"
  4. ETCD_LOCATION="${ETCD_LOCATION:-etcd}"
  5. FLANNEL_NET="${FLANNEL_NET:-10.10.0.0/16}"
  6. TAG=`git describe --tags --dirty`
  7. FLANNEL_DOCKER_IMAGE="${FLANNEL_DOCKER_IMAGE:-quay.io/coreos/flannel:$TAG}"
  8. K8S_VERSION="${K8S_VERSION:-1.13.2}"
  9. HYPERKUBE_IMG="gcr.io/google_containers/hyperkube-${ARCH}"
  10. HYPERKUBE_CMD="${HYPERKUBE_CMD:-/hyperkube}"
  11. HYPERKUBE_APISERVER_CMD="${HYPERKUBE_APISERVER_CMD:-apiserver}"
  12. docker_ip=$(ip -o -f inet addr show docker0 | grep -Po 'inet \K[\d.]+')
  13. etcd_endpt="http://$docker_ip:2379"
  14. k8s_endpt="http://$docker_ip:8080"
  15. # Set the proper imagename according to architecture
  16. if [[ ${ARCH} == "ppc64le" ]]; then
  17. ETCD_IMG+="-ppc64le"
  18. elif [[ ${ARCH} == "arm64" ]]; then
  19. ETCD_IMG+="-arm64"
  20. fi
  21. setup_suite() {
  22. # Run etcd, killing any existing one that was running
  23. # Start etcd
  24. docker rm -f flannel-e2e-test-etcd >/dev/null 2>/dev/null
  25. docker run --name=flannel-e2e-test-etcd -d -p 2379:2379 $ETCD_IMG etcd --listen-client-urls http://0.0.0.0:2379 --advertise-client-urls $etcd_endpt >/dev/null
  26. sleep 1
  27. # Start a kubernetes API server
  28. docker rm -f flannel-e2e-k8s-apiserver >/dev/null 2>/dev/null
  29. docker run -d --net=host --name flannel-e2e-k8s-apiserver ${HYPERKUBE_IMG}:v$K8S_VERSION \
  30. ${HYPERKUBE_CMD} ${HYPERKUBE_APISERVER_CMD} --etcd-servers=$etcd_endpt \
  31. --service-cluster-ip-range=10.101.0.0/16 --insecure-bind-address=0.0.0.0 --allow-privileged >/dev/null
  32. sleep 1
  33. while ! cat <<EOF | docker run -i --rm --net=host ${HYPERKUBE_IMG}:v$K8S_VERSION ${HYPERKUBE_CMD} kubectl create -f - >/dev/null 2>/dev/null
  34. apiVersion: v1
  35. kind: Node
  36. metadata:
  37. name: flannel1
  38. annotations:
  39. dummy: value
  40. spec:
  41. podCIDR: 10.10.1.0/24
  42. EOF
  43. do
  44. sleep 1
  45. done
  46. cat <<EOF | docker run -i --rm --net=host ${HYPERKUBE_IMG}:v$K8S_VERSION ${HYPERKUBE_CMD} kubectl create -f - >/dev/null 2>/dev/null
  47. apiVersion: v1
  48. kind: Node
  49. metadata:
  50. name: flannel2
  51. annotations:
  52. dummy: value
  53. spec:
  54. podCIDR: 10.10.2.0/24
  55. EOF
  56. }
  57. teardown_suite() {
  58. # Teardown the etcd server
  59. docker rm -f flannel-e2e-test-etcd >/dev/null
  60. docker rm -f flannel-e2e-k8s-apiserver >/dev/null
  61. }
  62. teardown() {
  63. docker rm -f flannel-e2e-test-flannel1 >/dev/null 2>/dev/null
  64. docker rm -f flannel-e2e-test-flannel2 >/dev/null 2>/dev/null
  65. }
  66. start_flannel() {
  67. local backend=$1
  68. flannel_conf="{ \"Network\": \"$FLANNEL_NET\", \"Backend\": { \"Type\": \"${backend}\" } }"
  69. for host_num in 1 2; do
  70. docker rm -f flannel-e2e-test-flannel$host_num >/dev/null 2>/dev/null
  71. docker run -e NODE_NAME=flannel$host_num --privileged --name flannel-e2e-test-flannel$host_num -id --entrypoint /bin/sh $FLANNEL_DOCKER_IMAGE >/dev/null
  72. docker exec flannel-e2e-test-flannel$host_num /bin/sh -c 'mkdir -p /etc/kube-flannel'
  73. echo $flannel_conf | docker exec -i flannel-e2e-test-flannel$host_num /bin/sh -c 'cat > /etc/kube-flannel/net-conf.json'
  74. docker exec -d flannel-e2e-test-flannel$host_num /opt/bin/flanneld --kube-subnet-mgr --kube-api-url $k8s_endpt
  75. while ! docker exec flannel-e2e-test-flannel$host_num ls /run/flannel/subnet.env >/dev/null 2>&1; do
  76. sleep 0.1
  77. done
  78. done
  79. }
  80. create_ping_dest() {
  81. # add a dummy interface with $FLANNEL_SUBNET so we have a known working IP to ping
  82. for host_num in 1 2; do
  83. # Use declare to allow the host_num variable to be part of the ping_dest variable name. -g is needed to make it global
  84. declare -g ping_dest$host_num=$(docker "exec" --privileged flannel-e2e-test-flannel$host_num /bin/sh -c '\
  85. source /run/flannel/subnet.env && \
  86. ip link add name dummy0 type dummy && \
  87. ip addr add $FLANNEL_SUBNET dev dummy0 && ip link set dummy0 up && \
  88. echo $FLANNEL_SUBNET | cut -f 1 -d "/" ')
  89. done
  90. }
  91. test_vxlan() {
  92. start_flannel vxlan
  93. create_ping_dest # creates ping_dest1 and ping_dest2 variables
  94. pings
  95. }
  96. if [[ ${ARCH} == "amd64" ]]; then
  97. test_udp() {
  98. start_flannel udp
  99. create_ping_dest # creates ping_dest1 and ping_dest2 variables
  100. pings
  101. }
  102. fi
  103. test_host-gw() {
  104. start_flannel host-gw
  105. create_ping_dest # creates ping_dest1 and ping_dest2 variables
  106. pings
  107. }
  108. test_ipip() {
  109. start_flannel ipip
  110. create_ping_dest # creates ping_dest1 and ping_dest2 variables
  111. pings
  112. }
  113. test_public-ip-overwrite(){
  114. docker exec flannel-e2e-k8s-apiserver kubectl annotate node flannel1 \
  115. flannel.alpha.coreos.com/public-ip-overwrite=172.18.0.2 >/dev/null 2>&1
  116. start_flannel vxlan
  117. assert_equals "172.18.0.2" \
  118. "$(docker exec flannel-e2e-k8s-apiserver kubectl get node/flannel1 -o \
  119. jsonpath='{.metadata.annotations.flannel\.alpha\.coreos\.com/public-ip}' 2>/dev/null)" \
  120. "Overwriting public IP via annotation does not work"
  121. # Remove annotation to not break all other tests
  122. docker exec flannel-e2e-k8s-apiserver kubectl annotate node flannel1 \
  123. flannel.alpha.coreos.com/public-ip-overwrite- >/dev/null 2>&1
  124. }
  125. pings() {
  126. # ping in both directions
  127. assert "docker exec --privileged flannel-e2e-test-flannel1 /bin/ping -c 5 $ping_dest2" "Host 1 cannot ping host 2"
  128. assert "docker exec --privileged flannel-e2e-test-flannel2 /bin/ping -c 5 $ping_dest1" "Host 2 cannot ping host 1"
  129. }
  130. test_manifest() {
  131. # This just tests that the API server accepts the manifest, not that it actually acts on it correctly.
  132. assert "cat ../Documentation/kube-flannel.yml | docker run -i --rm --net=host ${HYPERKUBE_IMG}:v$K8S_VERSION ${HYPERKUBE_CMD} kubectl create -f -"
  133. }