-
Notifications
You must be signed in to change notification settings - Fork 9
/
migration-check.sh
executable file
·207 lines (185 loc) · 8.36 KB
/
migration-check.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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
#!/usr/bin/env bash
# Copyright 2017 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.
set -o errexit
EXIT_CODE=0
function setExitCode() {
echo
EXIT_CODE=1
}
function checkIfClassExist(){
local classesNames=$@
for class in ${classesNames}
do
name=$(echo "${class}" | cut -d'/' -f2)
status=$(echo "${class}" | cut -d'/' -f3)
namespace=$(echo "${class}" | cut -d'/' -f4)
instanceClassName=$(echo "${className}" | cut -d'/' -f2)
instanceNamespace=$(echo "${className}" | cut -d'/' -f3)
if [[ "${name}" = "${instanceClassName}" ]]; then
if [[ -n "${namespace}" ]]; then
if [[ "${namespace}" = "${instanceNamespace}" ]]; then
if [[ "${status}" != "true" ]]; then
return 0
fi
fi
else
if [[ "${status}" = "false" ]]; then
return 0
fi
fi
fi
done
return 1
}
function checkIfClusterClassesExistForInstances(){
clusterServiceClassesNames=$(kubectl get clusterserviceclasses -o custom-columns=NAME:.spec.externalName,STATUS:.status.removedFromBrokerCatalog --no-headers)
serviceInstancesClassesNames=$(kubectl get serviceinstances --all-namespaces -ojsonpath="{.items[*].spec.clusterServiceClassExternalName}")
set +o errexit
for className in ${serviceInstancesClassesNames}
do
name=$(echo "${className}" | cut -d'/' -f2)
if [[ "${name}" = "<none>" ]]; then
continue
fi
checkIfClassExist $(mergeCustomColumns 2 "${clusterServiceClassesNames[@]}")
if [[ $? -eq 1 ]]; then
echo "ClusterServiceClass/${className} not exist for the ServiceInstances:"
kubectl get serviceinstances --all-namespaces -o custom-columns=NAME:.metadata.name,NAMESPACE:.metadata.namespace,STATUS:.status.provisionStatus,CLASS\ NAME:.spec.clusterServiceClassExternalName | grep "${name}" | grep -v "<none>"
setExitCode
fi
done
set -o errexit
}
function checkIfClassesExistForInstances(){
serviceClassesNames=$(kubectl get serviceclasses --all-namespaces -o custom-columns=NAME:.spec.externalName,STATUS:.status.removedFromBrokerCatalog,NAMESPACE:.metadata.namespace --no-headers)
serviceInstancesClassesNames=$(kubectl get serviceinstances --all-namespaces -o custom-columns=NAME:.spec.serviceClassExternalName,NAMESPACE:.metadata.namespace --no-headers)
set +o errexit
for className in $(mergeCustomColumns 2 "${serviceInstancesClassesNames[@]}")
do
name=$(echo "${className}" | cut -d'/' -f2)
if [[ "${name}" = "<none>" ]]; then
continue
fi
checkIfClassExist $(mergeCustomColumns 3 "${serviceClassesNames[@]}")
if [[ $? -eq 1 ]]; then
echo "ServiceClass${className} not exist for the ServiceInstances:"
kubectl get serviceinstances --all-namespaces -o custom-columns=NAME:.metadata.name,NAMESPACE:.metadata.namespace,STATUS:.status.provisionStatus,CLASS\ NAME:.spec.serviceClassExternalName | grep "${name}" | grep -v "<none>"
setExitCode
fi
done
set -o errexit
}
function mergeCustomColumns(){
local num=$1
local list=$2
local result=()
local column=0
local i=0
for item in ${list}
do
result[i]="${result[i]}/${item}"
(( column++ ))
if [[ ${num} -eq ${column} ]]; then
column=0
(( i++ ))
fi
done
echo "${result[@]}"
}
function checkIfResourcesAreInProgress(){
#
# Check if any class/plans were removed from broker's catalog
#
CSC=$(kubectl get clusterserviceclasses -ojsonpath="{.items[*].metadata.deletionTimestamp}")
if [[ -n "${CSC}" ]]; then
echo "There are being deleted ClusterServiceClasses:"
kubectl get clusterserviceclasses -o custom-columns=NAME:.metadata.name,DELETION\ TIME:.metadata.deletionTimestamp
setExitCode
fi
SC=$(kubectl get serviceclasses --all-namespaces -ojsonpath="{.items[*].metadata.deletionTimestamp}")
if [[ -n "${SC}" ]]; then
echo "There are being deleted ServiceClasses:"
kubectl get serviceclasses --all-namespaces -o custom-columns=NAME:.metadata.name,NAMESPACE:.metadata.namespace,DELETION\ TIME:.metadata.deletionTimestamp
setExitCode
fi
CSP=$(kubectl get clusterserviceplans -ojsonpath="{.items[*].metadata.deletionTimestamp}")
if [[ -n "${CSP}" ]]; then
echo "There are being deleted ClusterServicePlans:"
kubectl get clusterserviceplans -o custom-columns=NAME:.metadata.name,DELETION\ TIME:.metadata.deletionTimestamp
setExitCode
fi
SP=$(kubectl get serviceplans --all-namespaces -ojsonpath="{.items[*].metadata.deletionTimestamp}")
if [[ -n "${SP}" ]]; then
echo "There are being deleted ServicePlans:"
kubectl get serviceplans --all-namespaces -o custom-columns=NAME:.metadata.name,NAMESPACE:.metadata.namespace,DELETION\ TIME:.metadata.deletionTimestamp
setExitCode
fi
#
# Check if any instance/binding is in progress or is being deleted
#
SI=$(kubectl get serviceinstances --all-namespaces -ojsonpath="{.items[*].metadata.deletionTimestamp}")
if [[ -n "${SI}" ]]; then
echo "There are being deleted ServiceInstances:"
kubectl get serviceinstances --all-namespaces -o custom-columns=NAME:.metadata.name,NAMESPACE:.metadata.namespace,DELETION\ TIME:.metadata.deletionTimestamp
setExitCode
fi
for status in $(kubectl get serviceinstances --all-namespaces -ojsonpath="{.items[*].status.asyncOpInProgress}")
do
if [[ -n "${status}" ]] && ${status}; then
echo "There are ServiceInstance in progress:"
kubectl get serviceinstances --all-namespaces -o custom-columns=NAME:.metadata.name,NAMESPACE:.metadata.namespace,IN\ PROGRESS:.status.asyncOpInProgress
setExitCode
fi
done
SBI=$(kubectl get servicebindings --all-namespaces -ojsonpath="{.items[*].metadata.deletionTimestamp}")
if [[ -n ${SBI} ]]; then
echo "There are being deleted ServiceBindings:"
kubectl get servicebindings --all-namespaces -o custom-columns=NAME:.metadata.name,NAMESPACE:.metadata.namespace,DELETION\ TIME:.metadata.deletionTimestamp
setExitCode
fi
for status in $(kubectl get servicebindings --all-namespaces -ojsonpath="{.items[*].status.asyncOpInProgress}")
do
if [[ -n "${status}" ]] && ${status}; then
echo "There are ServiceBinding in progress:"
kubectl get servicebindings --all-namespaces -o custom-columns=NAME:.metadata.name,NAMESPACE:.metadata.namespace,IN\ PROGRESS:.status.asyncOpInProgress
setExitCode
fi
done
#
# Check if any broker is being deleted
#
SB=$(kubectl get servicebrokers --all-namespaces -ojsonpath="{.items[*].metadata.deletionTimestamp}")
if [[ -n ${SB} ]]; then
echo "There are being deleted ServiceBrokers:"
kubectl get servicebrokers --all-namespaces -o custom-columns=NAME:.metadata.name,NAMESPACE:.metadata.namespace,DELETION\ TIME:.metadata.deletionTimestamp
setExitCode
fi
CSB=$(kubectl get clusterservicebrokers -ojsonpath="{.items[*].metadata.deletionTimestamp}")
if [[ -n ${CSB} ]]; then
echo "There are being deleted ClusterServiceBrokers:"
kubectl get clusterservicebrokers -o custom-columns=NAME:.metadata.name,DELETION\ TIME:.metadata.deletionTimestamp
setExitCode
fi
}
# Check if there are some instances with not existing classes
checkIfClusterClassesExistForInstances
checkIfClassesExistForInstances
checkIfResourcesAreInProgress
if [[ ${EXIT_CODE} -eq 0 ]]; then
echo "Your Service Catalog resources are ready to migrate!"
else
echo "Please prepare your Service Catalog resources before migration. Check docs/migration-apiserver-to-crds.md#preparation"
fi
exit ${EXIT_CODE}