functional-test-k8s.sh 5.3 KB

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