github.com/percona/percona-xtradb-cluster-operator@v1.14.0/e2e-tests/smart-update1/run (about) 1 #!/bin/bash 2 # CASES: 3 # - Update ProxySQL PXC cluster (upgrade order and images) 4 # - Update HAProxy PXC cluster (upgrade order and images) 5 # - Update PXC with version service available but disabled 6 7 set -o errexit 8 9 test_dir=$(realpath $(dirname $0)) 10 . ${test_dir}/../functions 11 12 set_debug 13 14 API='pxc.percona.com/v9-9-9' 15 TARGET_IMAGE_PXC=${IMAGE_PXC} 16 CLUSTER="smart-update" 17 CLUSTER_SIZE=3 18 PROXY_SIZE=2 19 20 if [[ ${TARGET_IMAGE_PXC} == *"percona-xtradb-cluster-operator"* ]]; then 21 PXC_VER=$(echo -n "${TARGET_IMAGE_PXC}" | $sed -r 's/.*([0-9].[0-9])$/\1/') 22 else 23 PXC_VER=$(echo -n "${TARGET_IMAGE_PXC}" | $sed -r 's/.*:([0-9]+\.[0-9]+).*$/\1/') 24 fi 25 TARGET_IMAGE_PXC_VS="perconalab/percona-xtradb-cluster-operator:main-pxc${PXC_VER}" 26 VS_URL="http://version-service" 27 VS_PORT="11000" 28 VS_ENDPOINT="${VS_URL}:${VS_PORT}" 29 30 function get_pod_names_images { 31 local cluster=${1} 32 local type=${2:-pxc} 33 34 echo -e $(kubectl_bin get pods -l "app.kubernetes.io/instance=${cluster},app.kubernetes.io/component=${type}" \ 35 -o jsonpath="{range .items[*]}{.metadata.name}{\",\"}{.spec.containers[?(@.name == \"${type}\")].image}{\"\n\"}{end}") 36 } 37 38 function check_last_pod_to_update { 39 local cluster=${1} 40 local initial_primary=${2} 41 local pxc_size=${3} 42 local target_image=${4} 43 44 set +x 45 echo -n "Waiting for the last pod to update" 46 local i=0 47 local max=720 48 until [[ "$(kubectl_bin get pxc "${cluster}" -o jsonpath='{.status.state}')" == "ready" ]]; do 49 echo -n "." 50 updated_pods_count=0 51 for entry in $(get_pod_names_images "${cluster}"); do 52 if [[ -n "$(echo ${entry} | grep ${target_image})" ]]; then 53 ((updated_pods_count += 1)) 54 fi 55 done 56 57 if [[ ${updated_pods_count} == $((pxc_size - 1)) ]]; then 58 if [[ -n $(get_pod_names_images "${cluster}" | grep "${initial_primary}" | grep "${IMAGE_PXC}") ]]; then 59 echo 60 echo "${initial_primary} is REALLY the last one to update" 61 break 62 else 63 echo "${initial_primary} is not the last one to update. Exiting..." 64 exit 1 65 fi 66 fi 67 if [[ $i -ge $max ]]; then 68 echo "Something went wrong waiting for the last pod to update!" 69 exit 1 70 fi 71 let i+=1 72 sleep 1 73 done 74 set -x 75 } 76 77 function deploy_version_service { 78 desc 'install version service' 79 kubectl_bin create configmap versions \ 80 --from-file "${test_dir}/conf/operator.9.9.9.pxc-operator.dep.json" \ 81 --from-file "${test_dir}/conf/operator.9.9.9.pxc-operator.json" 82 kubectl_bin apply -f "${test_dir}/conf/vs.yml" 83 sleep 10 84 } 85 86 function main() { 87 create_infra "${namespace}" 88 deploy_version_service 89 deploy_cert_manager 90 IMAGE_PXC=$(kubectl_bin exec -ti "$(get_operator_pod)" ${OPERATOR_NS:+-n $OPERATOR_NS} -- curl -s "${VS_URL}.${namespace}.svc.cluster.local:${VS_PORT}/versions/v1/pxc-operator/9.9.9" | jq -r '.versions[].matrix.pxc[].imagePath' | grep ":${PXC_VER}" | sort -V | tail -n3 | head -n1) 91 92 desc "patch crd" 93 kubectl_bin patch crd perconaxtradbclusters.pxc.percona.com --type='json' -p '[{"op":"add","path":"/spec/versions/-", "value":{"name": "v9-9-9","schema": {"openAPIV3Schema": {"properties": {"spec": {"type": "object","x-kubernetes-preserve-unknown-fields": true},"status": {"type": "object", "x-kubernetes-preserve-unknown-fields": true}}, "type": "object" }}, "served": true, "storage": false, "subresources": { "status": {}}}}]' 94 95 ################################################## 96 desc 'Updating ProxySQL PXC cluster' 97 cp -f "${test_dir}/conf/${CLUSTER}.yml" "${tmp_dir}/${CLUSTER}.yml" 98 yq -i eval ".spec.initContainer.image = \"${IMAGE}\"" "${tmp_dir}/${CLUSTER}.yml" 99 spinup_pxc "${CLUSTER}" "${tmp_dir}/${CLUSTER}.yml" 100 101 initial_primary=$(get_proxy_primary "-h127.0.0.1 -P6032 -uproxyadmin -padmin_password" "$(get_proxy ${CLUSTER})-0") 102 kubectl_bin patch pxc/"${CLUSTER}" --type=merge -p '{"spec":{"pxc":{"image":"'"${TARGET_IMAGE_PXC}"'"}}}' 103 sleep 7 # wait for two reconcile loops ;) 3 sec x 2 times + 1 sec = 7 seconds 104 105 desc "check last pod to update" 106 check_last_pod_to_update "${CLUSTER}" "${initial_primary}" "${CLUSTER_SIZE}" "${TARGET_IMAGE_PXC}" 107 wait_cluster_consistency "${CLUSTER}" "${CLUSTER_SIZE}" "${PROXY_SIZE}" 108 for i in $(seq 0 $((CLUSTER_SIZE - 1))); do 109 compare_mysql_cmd "select-1" "SELECT * from myApp.myApp;" "-h ${CLUSTER}-pxc-${i}.${CLUSTER}-pxc -uroot -proot_password" 110 done 111 112 kubectl_bin delete -f "${test_dir}/conf/${CLUSTER}.yml" 113 kubectl_bin delete pvc --all 114 115 ################################################## 116 desc 'Updating HAProxy PXC cluster' 117 cp -f "${test_dir}/conf/${CLUSTER}-haproxy.yml" "${tmp_dir}/${CLUSTER}-haproxy.yml" 118 yq -i eval ".spec.initContainer.image = \"${IMAGE}\"" "${tmp_dir}/${CLUSTER}-haproxy.yml" 119 spinup_pxc "${CLUSTER}" "${tmp_dir}/${CLUSTER}-haproxy.yml" 120 121 initial_primary=$(run_mysql 'SELECT @@hostname hostname;' "-h ${CLUSTER}-haproxy -uroot -proot_password") 122 kubectl_bin patch pxc/${CLUSTER} --type=merge -p '{"spec":{"pxc":{"image":"'"${TARGET_IMAGE_PXC}"'"}}}' 123 sleep 7 # wait for two reconcile loops ;) 3 sec x 2 times + 1 sec = 7 seconds 124 125 desc "check last pod to update" 126 check_last_pod_to_update "${CLUSTER}" "${initial_primary}" "${CLUSTER_SIZE}" "${TARGET_IMAGE_PXC}" 127 wait_cluster_consistency "${CLUSTER}" "${CLUSTER_SIZE}" "${PROXY_SIZE}" 128 for i in $(seq 0 $((CLUSTER_SIZE - 1))); do 129 compare_mysql_cmd "select-1" "SELECT * from myApp.myApp;" "-h ${CLUSTER}-pxc-${i}.${CLUSTER}-pxc -uroot -proot_password" 130 done 131 132 kubectl_bin delete -f "${tmp_dir}/${CLUSTER}-haproxy.yml" 133 kubectl_bin delete pvc --all 134 135 ################################################## 136 desc 'Updating PXC cluster with version service available but disabled' 137 cp -f "${test_dir}/conf/${CLUSTER}-version-service-reachable.yml" "${tmp_dir}/${CLUSTER}-version-service-reachable.yml" 138 yq -i eval ".spec.initContainer.image = \"${IMAGE}\"" "${tmp_dir}/${CLUSTER}-version-service-reachable.yml" 139 spinup_pxc "${CLUSTER}" "${tmp_dir}/${CLUSTER}-version-service-reachable.yml" 140 141 initial_primary=$(run_mysql 'SELECT @@hostname hostname;' "-h ${CLUSTER}-haproxy -uroot -proot_password") 142 kubectl_bin patch pxc/${CLUSTER} --type=merge -p '{"spec":{"pxc":{"image":"'"${TARGET_IMAGE_PXC}"'"}}}' 143 sleep 7 # wait for two reconcile loops ;) 3 sec x 2 times + 1 sec = 7 seconds 144 145 desc "check last pod to update" 146 check_last_pod_to_update "${CLUSTER}" "${initial_primary}" "${CLUSTER_SIZE}" "${TARGET_IMAGE_PXC}" 147 wait_cluster_consistency "${CLUSTER}" "${CLUSTER_SIZE}" "${PROXY_SIZE}" 148 for i in $(seq 0 $((CLUSTER_SIZE - 1))); do 149 compare_mysql_cmd "select-1" "SELECT * from myApp.myApp;" "-h ${CLUSTER}-pxc-${i}.${CLUSTER}-pxc -uroot -proot_password" 150 done 151 152 kubectl_bin delete -f "${tmp_dir}/${CLUSTER}-version-service-reachable.yml" 153 kubectl_bin delete pvc --all 154 155 desc 'cleanup' 156 kubectl_bin delete -f "${test_dir}/conf/vs.yml" 157 destroy "${namespace}" 158 desc "test passed" 159 } 160 161 main