test-update-storage-objects.sh 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. #!/bin/bash
  2. # Copyright 2014 The Kubernetes Authors.
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. # Script to test cluster/update-storage-objects.sh works as expected.
  16. set -o errexit
  17. set -o nounset
  18. set -o pipefail
  19. KUBE_ROOT=$(dirname "${BASH_SOURCE}")/..
  20. source "${KUBE_ROOT}/hack/lib/init.sh"
  21. # The api version in which objects are currently stored in etcd.
  22. KUBE_OLD_API_VERSION=${KUBE_OLD_API_VERSION:-"v1"}
  23. # The api version in which our etcd objects should be converted to.
  24. # The new api version
  25. KUBE_NEW_API_VERSION=${KUBE_NEW_API_VERSION:-"v1"}
  26. KUBE_OLD_STORAGE_VERSIONS=${KUBE_OLD_STORAGE_VERSIONs:-""}
  27. KUBE_NEW_STORAGE_VERSIONS=${KUBE_NEW_STORAGE_VERSIONs:-""}
  28. STORAGE_BACKEND_ETCD2="etcd2"
  29. STORAGE_BACKEND_ETCD3="etcd3"
  30. KUBE_STORAGE_MEDIA_TYPE_JSON="application/json"
  31. KUBE_STORAGE_MEDIA_TYPE_PROTOBUF="application/vnd.kubernetes.protobuf"
  32. ETCD_HOST=${ETCD_HOST:-127.0.0.1}
  33. ETCD_PORT=${ETCD_PORT:-2379}
  34. ETCD_PREFIX=${ETCD_PREFIX:-randomPrefix}
  35. API_PORT=${API_PORT:-8080}
  36. API_HOST=${API_HOST:-127.0.0.1}
  37. KUBE_API_VERSIONS=""
  38. RUNTIME_CONFIG=""
  39. ETCDCTL=$(which etcdctl)
  40. KUBECTL="${KUBE_OUTPUT_HOSTBIN}/kubectl"
  41. UPDATE_ETCD_OBJECTS_SCRIPT="${KUBE_ROOT}/cluster/update-storage-objects.sh"
  42. function startApiServer() {
  43. local storage_backend=${1:-"${STORAGE_BACKEND_ETCD2}"}
  44. local storage_versions=${2:-""}
  45. local storage_media_type=${3:-""}
  46. kube::log::status "Starting kube-apiserver with KUBE_API_VERSIONS: ${KUBE_API_VERSIONS}"
  47. kube::log::status " and storage-backend: ${storage_backend}"
  48. kube::log::status " and storage-media-type: ${storage_media_type}"
  49. kube::log::status " and runtime-config: ${RUNTIME_CONFIG}"
  50. kube::log::status " and storage-version overrides: ${storage_versions}"
  51. KUBE_API_VERSIONS="${KUBE_API_VERSIONS}" \
  52. "${KUBE_OUTPUT_HOSTBIN}/kube-apiserver" \
  53. --insecure-bind-address="${API_HOST}" \
  54. --bind-address="${API_HOST}" \
  55. --insecure-port="${API_PORT}" \
  56. --storage-backend="${storage_backend}" \
  57. --etcd-servers="http://${ETCD_HOST}:${ETCD_PORT}" \
  58. --etcd-prefix="/${ETCD_PREFIX}" \
  59. --runtime-config="${RUNTIME_CONFIG}" \
  60. --cert-dir="${TMPDIR:-/tmp/}" \
  61. --service-cluster-ip-range="10.0.0.0/24" \
  62. --storage-versions="${storage_versions}" \
  63. --storage-media-type=${storage_media_type} 1>&2 &
  64. APISERVER_PID=$!
  65. # url, prefix, wait, times
  66. kube::util::wait_for_url "http://${API_HOST}:${API_PORT}/healthz" "apiserver: " 1 120
  67. }
  68. function killApiServer() {
  69. kube::log::status "Killing api server"
  70. if [[ -n ${APISERVER_PID-} ]]; then
  71. kill ${APISERVER_PID} 1>&2 2>/dev/null
  72. wait ${APISERVER_PID} || true
  73. kube::log::status "api server exited"
  74. fi
  75. unset APISERVER_PID
  76. }
  77. function cleanup() {
  78. killApiServer
  79. kube::etcd::cleanup
  80. kube::log::status "Clean up complete"
  81. }
  82. trap cleanup EXIT SIGINT
  83. make -C "${KUBE_ROOT}" WHAT=cmd/kube-apiserver
  84. make -C "${KUBE_ROOT}" WHAT=cluster/images/etcd/attachlease
  85. kube::etcd::start
  86. ### BEGIN TEST DEFINITION CUSTOMIZATION ###
  87. # source_file,resource,namespace,name,old_version,new_version
  88. tests=(
  89. test/fixtures/doc-yaml/user-guide/job.yaml,jobs,default,pi,extensions/v1beta1,batch/v1
  90. test/fixtures/doc-yaml/user-guide/horizontal-pod-autoscaling/hpa-php-apache.yaml,horizontalpodautoscalers,default,php-apache,extensions/v1beta1,autoscaling/v1
  91. )
  92. # need to include extensions/v1beta1 in new api version because its internal types are used by jobs
  93. # and hpas
  94. KUBE_OLD_API_VERSION="v1,extensions/v1beta1"
  95. KUBE_NEW_API_VERSION="v1,extensions/v1beta1,batch/v1,autoscaling/v1"
  96. KUBE_OLD_STORAGE_VERSIONS="batch=extensions/v1beta1,autoscaling=extensions/v1beta1"
  97. KUBE_NEW_STORAGE_VERSIONS="batch/v1,autoscaling/v1"
  98. ### END TEST DEFINITION CUSTOMIZATION ###
  99. #######################################################
  100. # Step 1: Start a server which supports both the old and new api versions,
  101. # but KUBE_OLD_API_VERSION is the latest (storage) version.
  102. # Additionally use KUBE_STORAGE_MEDIA_TYPE_JSON for storage encoding.
  103. #######################################################
  104. KUBE_API_VERSIONS="${KUBE_OLD_API_VERSION},${KUBE_NEW_API_VERSION}"
  105. RUNTIME_CONFIG="api/all=false,api/${KUBE_OLD_API_VERSION}=true,api/${KUBE_NEW_API_VERSION}=true"
  106. startApiServer ${STORAGE_BACKEND_ETCD2} ${KUBE_OLD_STORAGE_VERSIONS} ${KUBE_STORAGE_MEDIA_TYPE_JSON}
  107. # Create object(s)
  108. for test in ${tests[@]}; do
  109. IFS=',' read -ra test_data <<<"$test"
  110. source_file=${test_data[0]}
  111. kube::log::status "Creating ${source_file}"
  112. ${KUBECTL} create -f "${source_file}"
  113. # Verify that the storage version is the old version
  114. resource=${test_data[1]}
  115. namespace=${test_data[2]}
  116. name=${test_data[3]}
  117. old_storage_version=${test_data[4]}
  118. kube::log::status "Verifying ${resource}/${namespace}/${name} has storage version ${old_storage_version} in etcd"
  119. curl -s http://${ETCD_HOST}:${ETCD_PORT}/v2/keys/${ETCD_PREFIX}/${resource}/${namespace}/${name} | grep ${old_storage_version}
  120. done
  121. killApiServer
  122. #######################################################
  123. # Step 2: Perform etcd2 -> etcd migration.
  124. # We always perform offline migration, so we need to stop etcd.
  125. #######################################################
  126. kube::etcd::stop
  127. TARGET_STORAGE="etcd3" \
  128. DATA_DIRECTORY="${ETCD_DIR}" \
  129. ETCD=$(which etcd) \
  130. ETCDCTL=$(which etcdctl) \
  131. ATTACHLEASE="${KUBE_OUTPUT_HOSTBIN}/attachlease" \
  132. ${KUBE_ROOT}/cluster/images/etcd/migrate-if-needed.sh
  133. kube::etcd::start
  134. #######################################################
  135. # Step 3: Start a server which supports both the old and new api versions,
  136. # but KUBE_NEW_API_VERSION is the latest (storage) version.
  137. # Still use KUBE_STORAGE_MEDIA_TYPE_JSON for storage encoding.
  138. #######################################################
  139. KUBE_API_VERSIONS="${KUBE_NEW_API_VERSION},${KUBE_OLD_API_VERSION}"
  140. RUNTIME_CONFIG="api/all=false,api/${KUBE_OLD_API_VERSION}=true,api/${KUBE_NEW_API_VERSION}=true"
  141. startApiServer ${STORAGE_BACKEND_ETCD3} ${KUBE_NEW_STORAGE_VERSIONS} ${KUBE_STORAGE_MEDIA_TYPE_JSON}
  142. # Update etcd objects, so that will now be stored in the new api version.
  143. kube::log::status "Updating storage versions in etcd"
  144. ${UPDATE_ETCD_OBJECTS_SCRIPT}
  145. # Verify that the storage version was changed in etcd
  146. for test in ${tests[@]}; do
  147. IFS=',' read -ra test_data <<<"$test"
  148. resource=${test_data[1]}
  149. namespace=${test_data[2]}
  150. name=${test_data[3]}
  151. new_storage_version=${test_data[5]}
  152. kube::log::status "Verifying ${resource}/${namespace}/${name} has updated storage version ${new_storage_version} in etcd"
  153. ETCDCTL_API=3 ${ETCDCTL} --endpoints="${ETCD_HOST}:${ETCD_PORT}" get "/${ETCD_PREFIX}/${resource}/${namespace}/${name}" | grep ${new_storage_version}
  154. done
  155. killApiServer
  156. #######################################################
  157. # Step 4 : Start a server which supports only the new api version.
  158. # However, change storage encoding to KUBE_STORAGE_MEDIA_TYPE_PROTOBUF.
  159. #######################################################
  160. KUBE_API_VERSIONS="${KUBE_NEW_API_VERSION}"
  161. RUNTIME_CONFIG="api/all=false,api/${KUBE_NEW_API_VERSION}=true"
  162. # This seems to reduce flakiness.
  163. sleep 1
  164. startApiServer ${STORAGE_BACKEND_ETCD3} ${KUBE_NEW_STORAGE_VERSIONS} ${KUBE_STORAGE_MEDIA_TYPE_PROTOBUF}
  165. for test in ${tests[@]}; do
  166. IFS=',' read -ra test_data <<<"$test"
  167. resource=${test_data[1]}
  168. namespace=${test_data[2]}
  169. name=${test_data[3]}
  170. # Verify that the server is able to read the object.
  171. kube::log::status "Verifying we can retrieve ${resource}/${namespace}/${name} via kubectl"
  172. ${KUBECTL} get --namespace=${namespace} ${resource}/${name}
  173. done
  174. killApiServer