update-storage-objects.sh 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. #!/bin/bash
  2. # Copyright 2015 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 update etcd objects as per the latest API Version.
  16. # This just reads all objects and then writes them back as is to ensure that
  17. # they are written using the latest API version.
  18. #
  19. # Steps to use this script to upgrade the cluster to a new version:
  20. # https://github.com/kubernetes/kubernetes/blob/master/docs/cluster_management.md#updgrading-to-a-different-api-version
  21. set -o errexit
  22. set -o nounset
  23. set -o pipefail
  24. KUBE_ROOT=$(dirname "${BASH_SOURCE}")/..
  25. source "${KUBE_ROOT}/hack/lib/init.sh"
  26. KUBECTL="${KUBE_OUTPUT_HOSTBIN}/kubectl"
  27. # List of resources to be updated.
  28. # TODO: Get this list of resources from server once
  29. # http://issue.k8s.io/2057 is fixed.
  30. declare -a resources=(
  31. "endpoints"
  32. "events"
  33. "limitranges"
  34. "namespaces"
  35. "nodes"
  36. "pods"
  37. "persistentvolumes"
  38. "persistentvolumeclaims"
  39. "replicationcontrollers"
  40. "resourcequotas"
  41. "secrets"
  42. "services"
  43. "jobs"
  44. "horizontalpodautoscalers"
  45. )
  46. # Find all the namespaces.
  47. namespaces=( $("${KUBECTL}" get namespaces -o go-template="{{range.items}}{{.metadata.name}} {{end}}"))
  48. if [ -z "${namespaces:-}" ]
  49. then
  50. echo "Unexpected: No namespace found. Nothing to do."
  51. exit 1
  52. fi
  53. all_failed=1
  54. for resource in "${resources[@]}"
  55. do
  56. for namespace in "${namespaces[@]}"
  57. do
  58. # If get fails, assume it's because the resource hasn't been installed in the apiserver.
  59. # TODO hopefully we can remove this once we use dynamic discovery of gettable/updateable
  60. # resources.
  61. set +e
  62. instances=( $("${KUBECTL}" get "${resource}" --namespace="${namespace}" -o go-template="{{range.items}}{{.metadata.name}} {{end}}"))
  63. result=$?
  64. set -e
  65. if [[ "${all_failed}" -eq 1 && "${result}" -eq 0 ]]; then
  66. all_failed=0
  67. fi
  68. # Nothing to do if there is no instance of that resource.
  69. if [[ -z "${instances:-}" ]]
  70. then
  71. continue
  72. fi
  73. for instance in "${instances[@]}"
  74. do
  75. # Read and then write it back as is.
  76. # Update can fail if the object was updated after we fetched the
  77. # object, but before we could update it. We, hence, try the update
  78. # operation multiple times. But 5 continuous failures indicate some other
  79. # problem.
  80. success=0
  81. for (( tries=0; tries<5; ++tries ))
  82. do
  83. filename="/tmp/k8s-${namespace}-${resource}-${instance}.json"
  84. ( "${KUBECTL}" get "${resource}" "${instance}" --namespace="${namespace}" -o json > "${filename}" ) || true
  85. if [[ ! -s "${filename}" ]]
  86. then
  87. # This happens when the instance has been deleted. We can hence ignore
  88. # this instance.
  89. echo "Looks like ${instance} got deleted. Ignoring it"
  90. continue
  91. fi
  92. output=$("${KUBECTL}" replace -f "${filename}" --namespace="${namespace}") || true
  93. rm "${filename}"
  94. if [ -n "${output:-}" ]
  95. then
  96. success=1
  97. break
  98. fi
  99. done
  100. if [[ "${success}" -eq 0 ]]
  101. then
  102. echo "Error: failed to update ${resource}/${instance} in ${namespace} namespace after 5 tries"
  103. exit 1
  104. fi
  105. done
  106. if [[ "${resource}" == "namespaces" ]] || [[ "${resource}" == "nodes" ]]
  107. then
  108. # These resources are namespace agnostic. No need to update them for every
  109. # namespace.
  110. break
  111. fi
  112. done
  113. done
  114. if [[ "${all_failed}" -eq 1 ]]; then
  115. echo "kubectl get failed for all resources"
  116. exit 1
  117. fi
  118. echo "All objects updated successfully!!"
  119. exit 0