tringuyen | eb9eb59 | 2020-04-01 17:51:06 +0000 | [diff] [blame] | 1 | #!/bin/bash |
| 2 | |
| 3 | TEMP_POD=$(kubectl get pod -n $NAMESPACE_ENV --selector \ |
| 4 | app='{{ include "common.fullname" . }}' -o \ |
| 5 | jsonpath='{.items[?(@.metadata.ownerReferences[].kind=="ReplicaSet")].metadata.name}') |
| 6 | |
| 7 | tmp_MYSQL_PASSWORD=$(echo -n $(kubectl exec -n $NAMESPACE_ENV $TEMP_POD -- printenv \ |
| 8 | MYSQL_PASSWORD) | base64) |
| 9 | |
| 10 | tmp_ROOT_PASSWORD=$(echo -n $(kubectl exec -n $NAMESPACE_ENV $TEMP_POD -- printenv \ |
| 11 | MYSQL_ROOT_PASSWORD) | base64) |
| 12 | |
| 13 | FLAG_EX_ROOT_SEC='{{ include "common.secret.getSecretNameFast" (dict "global" . "uid" (include "common.mariadb.secret.rootPassUID" .)) }}' |
| 14 | |
| 15 | FLAG_EX_SEC='{{ include "common.secret.getSecretNameFast" (dict "global" . "uid" (include "common.mariadb.secret.userCredentialsUID" .)) }}' |
| 16 | |
| 17 | kubectl patch secret $FLAG_EX_ROOT_SEC -p \ |
| 18 | '{"data":{"password":"'"$tmp_ROOT_PASSWORD"'"}}' |
| 19 | |
| 20 | kubectl patch secret $FLAG_EX_SEC -p \ |
| 21 | '{"data":{"password":"'"$tmp_MYSQL_PASSWORD"'"}}' |
| 22 | |
Marat Salakhutdinov | a72170b | 2020-06-27 23:18:17 -0400 | [diff] [blame^] | 23 | MYSQL_USER=$(kubectl exec -n $NAMESPACE_ENV $TEMP_POD -- printenv MYSQL_USER) |
| 24 | |
| 25 | MYSQL_PASSWORD=$(echo -n $(kubectl exec -n $NAMESPACE_ENV $TEMP_POD -- printenv MYSQL_PASSWORD)) |
| 26 | |
| 27 | MYSQL_ROOT_PASSWORD=$(kubectl exec -n $NAMESPACE_ENV $TEMP_POD -- printenv MYSQL_ROOT_PASSWORD) |
| 28 | |
| 29 | CURRENT_STS_REPLICA=$(kubectl get statefulsets -n $NAMESPACE_ENV \ |
| 30 | {{ include "common.fullname" . }} -o jsonpath='{.status.replicas}') |
| 31 | |
| 32 | DEPLOYMENT_REPLICA=$(kubectl get deployment -n $NAMESPACE_ENV \ |
| 33 | {{ include "common.fullname" . }}-upgrade-deployment -o \ |
| 34 | jsonpath='{.status.replicas}') |
| 35 | |
| 36 | if [[ $CURRENT_STS_REPLICA == "0" ]] |
| 37 | then |
| 38 | echo "Seems there was no upgrade of cluster and we will scale up cluster replicas back to $REPLICA_COUNT now" |
| 39 | kubectl scale statefulsets {{ include "common.fullname" . }} --replicas=$REPLICA_COUNT |
| 40 | fi |
| 41 | |
| 42 | MY_REPLICA_NUMBER=$(kubectl get statefulsets -n $NAMESPACE_ENV \ |
| 43 | {{ include "common.fullname" . }} -o jsonpath='{.status.replicas}') |
| 44 | |
| 45 | while [[ ! $MY_REPLICA_NUMBER == $REPLICA_COUNT ]] |
| 46 | do |
| 47 | echo "The cluster is not scaled up to $REPLICA_COUNT yet. Please wait ..." |
| 48 | MY_REPLICA_NUMBER=$(kubectl get statefulsets -n $NAMESPACE_ENV \ |
| 49 | {{ include "common.fullname" . }} -o jsonpath='{.status.replicas}') |
| 50 | echo "The current status of the cluster is $MY_REPLICA_NUMBER" |
| 51 | sleep 2 |
| 52 | if [[ $MY_REPLICA_NUMBER == $REPLICA_COUNT ]] |
| 53 | then |
| 54 | break |
| 55 | fi |
| 56 | done |
| 57 | |
| 58 | CLUSTER_NO=$(kubectl exec -n $NAMESPACE_ENV $TEMP_POD -- \ |
| 59 | mysql --skip-column-names -h{{ $.Values.service.name }} -u$MYSQL_USER \ |
| 60 | -p$MYSQL_PASSWORD -e "SHOW GLOBAL STATUS LIKE 'wsrep_cluster_size';" | \ |
| 61 | awk '{print $2}') |
| 62 | |
| 63 | CLUSTER_STATE=$(kubectl exec -n $NAMESPACE_ENV $TEMP_POD -- \ |
| 64 | mysql --skip-column-names -h{{ $.Values.service.name }} -u$MYSQL_USER \ |
| 65 | -p$MYSQL_PASSWORD -e "SHOW GLOBAL STATUS LIKE 'wsrep_local_state_comment';" \ |
| 66 | | awk '{print $2}') |
| 67 | |
| 68 | while [[ ! $CLUSTER_NO == $((REPLICA_COUNT+DEPLOYMENT_REPLICA)) ]] \ |
| 69 | || [[ ! $CLUSTER_STATE == "Synced" ]] |
| 70 | do |
| 71 | echo "$CLUSTER_NO and $CLUSTER_STATE" |
| 72 | CLUSTER_NO=$(kubectl exec -n $NAMESPACE_ENV $TEMP_POD -- mysql \ |
| 73 | --skip-column-names -h{{ $.Values.service.name }} -u$MYSQL_USER \ |
| 74 | -p$MYSQL_PASSWORD -e "SHOW GLOBAL STATUS LIKE 'wsrep_cluster_size';" \ |
| 75 | | awk '{print $2}') |
| 76 | CLUSTER_STATE=$(kubectl exec -n $NAMESPACE_ENV $TEMP_POD -- mysql \ |
| 77 | --skip-column-names -h{{ $.Values.service.name }} -u$MYSQL_USER \ |
| 78 | -p$MYSQL_PASSWORD -e "SHOW GLOBAL STATUS LIKE 'wsrep_local_state_comment';" \ |
| 79 | | awk '{print $2}') |
| 80 | sleep 2 |
| 81 | if [[ $CLUSTER_NO == $((REPLICA_COUNT+DEPLOYMENT_REPLICA)) ]] \ |
| 82 | && [[ $CLUSTER_STATE == "Synced" ]] |
| 83 | then |
| 84 | echo "The cluster has $CLUSTER_NO members and $CLUSTER_STATE state." |
| 85 | break |
| 86 | fi |
| 87 | done |
| 88 | |
| 89 | MYSQL_STATUS=$(kubectl exec -n $NAMESPACE_ENV {{ include "common.fullname" . }}-0 -- mysqladmin \ |
| 90 | -uroot -p$MYSQL_ROOT_PASSWORD ping) |
| 91 | |
| 92 | while [[ ! $MYSQL_STATUS == "mysqld is alive" ]] |
| 93 | do |
| 94 | echo "Mariadb deployment is not ready yet." |
| 95 | sleep 2 |
| 96 | MYSQL_STATUS=$(kubectl exec -n $NAMESPACE_ENV {{ include "common.fullname" . }}-0 -- mysqladmin \ |
| 97 | -uroot -p$MYSQL_ROOT_PASSWORD ping) |
| 98 | if [[ $MYSQL_STATUS == "mysqld is alive" ]] |
| 99 | then |
| 100 | echo "Mariadb deployment is ready and cluster size is $CLUSTER_NO" |
| 101 | break |
| 102 | fi |
| 103 | done |
| 104 | |
| 105 | echo "Deleting upgrade deployment now" |
| 106 | |
tringuyen | eb9eb59 | 2020-04-01 17:51:06 +0000 | [diff] [blame] | 107 | kubectl delete deployment -n $NAMESPACE_ENV {{ include "common.fullname" . }}-upgrade-deployment |
| 108 | kubectl delete secret -n $NAMESPACE_ENV {{ include "common.fullname" . }}-temp-upgrade-root |
Marat Salakhutdinov | a72170b | 2020-06-27 23:18:17 -0400 | [diff] [blame^] | 109 | kubectl delete secret -n $NAMESPACE_ENV {{ include "common.fullname" . }}-temp-upgrade-usercred |
| 110 | |
| 111 | CLUSTER_NO=$(kubectl exec -n $NAMESPACE_ENV {{ include "common.fullname" . }}-0 -- \ |
| 112 | mysql --skip-column-names -h{{ $.Values.service.name }} -u$MYSQL_USER \ |
| 113 | -p$MYSQL_PASSWORD -e "SHOW GLOBAL STATUS LIKE 'wsrep_cluster_size';" | \ |
| 114 | awk '{print $2}') |
| 115 | |
| 116 | CLUSTER_STATE=$(kubectl exec -n $NAMESPACE_ENV {{ include "common.fullname" . }}-0 -- \ |
| 117 | mysql --skip-column-names -h{{ $.Values.service.name }} -u$MYSQL_USER \ |
| 118 | -p$MYSQL_PASSWORD -e "SHOW GLOBAL STATUS LIKE 'wsrep_local_state_comment';" \ |
| 119 | | awk '{print $2}') |
| 120 | |
| 121 | while [[ ! $CLUSTER_NO == $REPLICA_COUNT ]] \ |
| 122 | || [[ ! $CLUSTER_STATE == "Synced" ]] |
| 123 | do |
| 124 | echo "$CLUSTER_NO and $CLUSTER_STATE" |
| 125 | CLUSTER_NO=$(kubectl exec -n $NAMESPACE_ENV {{ include "common.fullname" . }}-0 -- mysql \ |
| 126 | --skip-column-names -h{{ $.Values.service.name }} -u$MYSQL_USER \ |
| 127 | -p$MYSQL_PASSWORD -e "SHOW GLOBAL STATUS LIKE 'wsrep_cluster_size';" \ |
| 128 | | awk '{print $2}') |
| 129 | CLUSTER_STATE=$(kubectl exec -n $NAMESPACE_ENV {{ include "common.fullname" . }}-0 -- mysql \ |
| 130 | --skip-column-names -h{{ $.Values.service.name }} -u$MYSQL_USER \ |
| 131 | -p$MYSQL_PASSWORD -e "SHOW GLOBAL STATUS LIKE 'wsrep_local_state_comment';" \ |
| 132 | | awk '{print $2}') |
| 133 | sleep 2 |
| 134 | if [[ $CLUSTER_NO == $REPLICA_COUNT ]] \ |
| 135 | && [[ $CLUSTER_STATE == "Synced" ]] |
| 136 | then |
| 137 | echo "The cluster has $CLUSTER_NO members and $CLUSTER_STATE state." |
| 138 | break |
| 139 | fi |
| 140 | done |
| 141 | |
| 142 | echo "The cluster upgrade is finished now" |