forked from kubernetes/kubernetes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
update-storage-objects.sh
executable file
·133 lines (119 loc) · 3.98 KB
/
update-storage-objects.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#!/bin/bash
# Copyright 2015 The Kubernetes Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# Script to update etcd objects as per the latest API Version.
# This just reads all objects and then writes them back as is to ensure that
# they are written using the latest API version.
#
# Steps to use this script to upgrade the cluster to a new version:
# https://github.com/kubernetes/kubernetes/blob/master/docs/cluster_management.md#updgrading-to-a-different-api-version
set -o errexit
set -o nounset
set -o pipefail
KUBE_ROOT=$(dirname "${BASH_SOURCE}")/..
source "${KUBE_ROOT}/hack/lib/init.sh"
KUBECTL="${KUBE_OUTPUT_HOSTBIN}/kubectl"
# List of resources to be updated.
# TODO: Get this list of resources from server once
# http://issue.k8s.io/2057 is fixed.
declare -a resources=(
"endpoints"
"events"
"limitranges"
"namespaces"
"nodes"
"pods"
"persistentvolumes"
"persistentvolumeclaims"
"replicationcontrollers"
"resourcequotas"
"secrets"
"services"
"jobs"
"horizontalpodautoscalers"
)
# Find all the namespaces.
namespaces=( $("${KUBECTL}" get namespaces -o go-template="{{range.items}}{{.metadata.name}} {{end}}"))
if [ -z "${namespaces:-}" ]
then
echo "Unexpected: No namespace found. Nothing to do."
exit 1
fi
all_failed=1
for resource in "${resources[@]}"
do
for namespace in "${namespaces[@]}"
do
# If get fails, assume it's because the resource hasn't been installed in the apiserver.
# TODO hopefully we can remove this once we use dynamic discovery of gettable/updateable
# resources.
set +e
instances=( $("${KUBECTL}" get "${resource}" --namespace="${namespace}" -o go-template="{{range.items}}{{.metadata.name}} {{end}}"))
result=$?
set -e
if [[ "${all_failed}" -eq 1 && "${result}" -eq 0 ]]; then
all_failed=0
fi
# Nothing to do if there is no instance of that resource.
if [[ -z "${instances:-}" ]]
then
continue
fi
for instance in "${instances[@]}"
do
# Read and then write it back as is.
# Update can fail if the object was updated after we fetched the
# object, but before we could update it. We, hence, try the update
# operation multiple times. But 5 continuous failures indicate some other
# problem.
success=0
for (( tries=0; tries<5; ++tries ))
do
filename="/tmp/k8s-${namespace}-${resource}-${instance}.json"
( "${KUBECTL}" get "${resource}" "${instance}" --namespace="${namespace}" -o json > "${filename}" ) || true
if [[ ! -s "${filename}" ]]
then
# This happens when the instance has been deleted. We can hence ignore
# this instance.
echo "Looks like ${instance} got deleted. Ignoring it"
continue
fi
output=$("${KUBECTL}" replace -f "${filename}" --namespace="${namespace}") || true
rm "${filename}"
if [ -n "${output:-}" ]
then
success=1
break
fi
done
if [[ "${success}" -eq 0 ]]
then
echo "Error: failed to update ${resource}/${instance} in ${namespace} namespace after 5 tries"
exit 1
fi
done
if [[ "${resource}" == "namespaces" ]] || [[ "${resource}" == "nodes" ]]
then
# These resources are namespace agnostic. No need to update them for every
# namespace.
break
fi
done
done
if [[ "${all_failed}" -eq 1 ]]; then
echo "kubectl get failed for all resources"
exit 1
fi
echo "All objects updated successfully!!"
exit 0