-
Notifications
You must be signed in to change notification settings - Fork 4
/
020-clusterOperations.sh
218 lines (177 loc) · 7.58 KB
/
020-clusterOperations.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
208
209
210
211
212
213
214
215
216
217
218
#! /bin/bash
# Expected variables to be exported by calling script(s):
# $UPDATE_TO_KUBERNETES_VERSION
# $CLUSTERS_FILE_NAME
## Load nodePool Level Operation functions
source "./030-nodePoolOperations.sh"
### Cluster functions
function createClusterUpgradeCandidatesJSON(){
echo "Generating list of AKS CLusters to upgrade..."
local __candidatesSummaryDetails=$(az aks list --query "[].{name: name, id: id, kubernetesVersion: kubernetesVersion, resourceGroup: resourceGroup, agentPoolProfiles: agentPoolProfiles[?orchestratorVersion < '$UPDATE_TO_KUBERNETES_VERSION' && osType == 'Linux' && mode == 'User'].{count:count, name: name, mode: mode, vmSize: vmSize, orchestratorVersion:orchestratorVersion}}" -o json)
echo $__candidatesSummaryDetails > "$TEMP_FOLDER/$CLUSTERS_FILE_NAME"
if [ $? -eq 0 ]
then
echo "Succeeded to create list of cluster upgrade candidates"
removeClustersFromUpgradeCandidatesJSON
if [ $? -eq 0 ]
then
return 0
else
return 1
fi
else
echo "Failed to create list of cluster upgrade candidates" >> $TEMP_FOLDER$ERR_LOG_FILE_NAME
return 1
fi
}
function removeClustersFromUpgradeCandidatesJSON() {
local __excludedClusterArray=(${EXCLUDED_CLUSTERS_LIST:-$1})
local __clustersFileJSON="$TEMP_FOLDER$CLUSTERS_FILE_NAME"
if [ -z "$__excludedClusterArray" ]
then
echo "No clusters remove from list of cluster upgrade candidates"
return 0
else
echo "Removing clusters: $__excludedClusterArray"
echo "fails here"
for __excludedCluster in "${__excludedClusterArray[@]}"
do
echo "fails here2"
local __arr=($(echo $__excludedCluster | tr ":" " "))
# Using messey array syntax to make array indexes consistent between bash & ZSH
local __RG="${__arr[@]:0:1}"
local __clusterName="${__arr[@]:1:1}"
removeClusterFromClusterUpgradeCandidatesJSON $__RG $__clusterName
done
fi
}
function removeClusterFromClusterUpgradeCandidatesJSON() {
local __RG=$1
local __clusterName=$1
cp "$TEMP_FOLDER$CLUSTERS_FILE_NAME" "$TEMP_FOLDER$CLUSTERS_FILE_NAME.original.backup"
echo "fails here3"
cat "$TEMP_FOLDER$CLUSTERS_FILE_NAME.original.backup" \
| jq -r --arg RG "$__RG" --arg clusterName "$__clusterName" '[.[] | select((.name!=$RG) and (.resourceGroup!=$clusterName))]' \
> "$TEMP_FOLDER$CLUSTERS_FILE_NAME.new"
}
function checkClusterExists() {
local __RG=$1
local __clusterName=$2
if [ "$(az aks show -g $__RG -n $__clusterName -o tsv --query 'name')" = "$__clusterName" ]
then
echo "Cluster Found"
cluster_version=$(az aks show -g $RG -n $NAME -o tsv --query 'kubernetesVersion')
return 0
else
echo "Cluster Not Found"
return 1
fi
}
function checkAndUpgradeAllClusterControlPlanes() {
checkAllClusterControlPlanes 0
}
function checkAllClusterControlPlanes() {
local __upgradeControlPlane="${1:-1}"
for __clusterName in $(cat "$TEMP_FOLDER$CLUSTERS_FILE_NAME" | jq -r '.[] | .name')
do
local __RG=$(cat "$TEMP_FOLDER$CLUSTERS_FILE_NAME" | jq -r --arg clusterName "$__clusterName" '.[] | select(.name==$clusterName).resourceGroup')
local __clusterK8sVersion=$(cat "$TEMP_FOLDER$CLUSTERS_FILE_NAME" | jq -r --arg clusterName "$__clusterName" '.[] | select(.name==$clusterName).kubernetesVersion')
local __targetK8sVersion=$UPDATE_TO_KUBERNETES_VERSION
checkClusterControlPlane $__RG $__clusterName $__clusterK8sVersion $__targetK8sVersion $__upgradeControlPlane
done
}
function checkAndUpgradeClusterControlPlane() {
local __RG=$1
local __clusterName=$2
local __clusterK8sVersion=$3
local __targetK8sVersion=$4
checkClusterControlPlane $__RG $__clusterName $__clusterK8sVersion $__targetK8sVersion 0
}
function checkClusterControlPlane() {
local __RG=$1
local __clusterName=$2
local __clusterK8sVersion=$3
local __targetK8sVersion=$4
local __upgradeControlPlane="${5:-1}"
checkClusterControlPlaneNeedsUpgrade $__RG $__clusterName $__clusterK8sVersion $__targetK8sVersion $__upgradeControlPlane
}
function checkClusterControlPlaneNeedsUpgrade() {
local __RG=$1
local __clusterName=$2
local __clusterK8sVersion=$3
local __targetK8sVersion=$4
local __upgradeControlPlane="${5:-1}"
if [ $(helperCheckSemVer $__clusterK8sVersion) -lt $(helperCheckSemVer $__targetK8sVersion) ]
then
echo "Control Plane Upgrade needed for cluster $__clusterName."
echo "Current Cluster version equal to: $__clusterK8sVersion"
echo "Target Cluster version K8s $__targetK8sVersion"
if [ "$__upgradeControlPlane" -eq 0 ]
then
upgradeClusterControlPlane $__RG $__clusterName $__targetK8sVersion
if [ $? -eq 0 ]
then
return 0
else
return 1
fi
fi
else
echo "Control Plane Upgrade not needed."
echo "Control Plane version equal to: $__clusterK8sVersion."
echo "Target K8s $__targetK8sVersion"
return 1
fi
}
function upgradeClusterControlPlane() {
local __RG=$1
local __clusterName=$2
local __K8SVersion=$3
echo "Upgrading Cluster $__clusterName Control Plane to K8s v.$__K8SVersion"
echo "Started at: $(date)"
## Problem: Bug in CLI where -y/--yes flag is ignored and user input is still required.
# az aks upgrade -g $__RG -n $__clusterName -k $__K8SVersion --control-plane-only --yes
## Work around for above Problem
local __resourceID=$(cat "$TEMP_FOLDER$CLUSTERS_FILE_NAME" | jq -r --arg RG "$__RG" --arg clusterName "$__clusterName" '.[] | select((.name==$clusterName) and (.resourceGroup==$RG)).id')
az resource update --ids $__resourceID --set "properties.kubernetesVersion=$__K8SVersion"
if [ $? -eq 0 ]
then
echo "Succeeded: Upgraded Cluster $__clusterName Control Plane to K8s v.$__K8SVersion"
echo "Finished at: $(date)"
return 0
else
echo "Failed: Control Plane Upgrade to v.$__K8SVersion"
return 1
fi
}
function checkAndRollingUpgradeAllClustersAndNodePools() {
for __clusterName in $(cat "$TEMP_FOLDER$CLUSTERS_FILE_NAME" | jq -r '.[] | .name')
do
local __RG=$(cat "$TEMP_FOLDER$CLUSTERS_FILE_NAME"| jq -r --arg clusterName "$__clusterName" '.[] | select(.name==$clusterName).resourceGroup')
local __clusterK8sVersion=$(cat "$TEMP_FOLDER$CLUSTERS_FILE_NAME"| jq -r --arg clusterName "$__clusterName" '.[] | select(.name==$clusterName).kubernetesVersion')
local __targetK8sVersion=$UPDATE_TO_KUBERNETES_VERSION
getClusterCredentials $__RG $__clusterName
setClusterConfigContext $__clusterName
checkAndUpgradeClusterControlPlane $__RG $__clusterName $__clusterK8sVersion $__targetK8sVersion
if [ $? -eq 0 ]
then
upgradeNodePoolsInCluster $__clusterName
if [ $? -eq 0 ]
then
echo "Successfully upgraded Cluster: $__clusterName"
else
echo "Failed to upgrade Cluster: $__clusterName"
fi
fi
done
}
function getClusterCredentials() {
local __RG=$1
local __clusterName=$2
# Get Kube Config Creds and overwrite if already exists (No User Prompts)
az aks get-credentials -g $__RG -n $__clusterName --overwrite-existing
}
function setClusterConfigContext() {
local __clusterName=$1
kubectl config set-context $__clusterName
}