Skip to content

Commit 8fb7d3a

Browse files
authored
fix: add retry logic to reset_iks_api_key.sh (#685)
1 parent 8b40881 commit 8fb7d3a

File tree

1 file changed

+33
-23
lines changed

1 file changed

+33
-23
lines changed

scripts/reset_iks_api_key.sh

Lines changed: 33 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ RESOURCE_GROUP_ID="$2"
77
APIKEY_KEY_NAME="containers-kubernetes-key"
88
PRIVATE_ENV="$3"
99
CLUSTER_ENDPOINT="$4"
10+
MAX_ATTEMPTS=10
1011

1112
if [[ -z "${REGION}" ]]; then
1213
echo "Region must be passed as first input script argument" >&2
@@ -65,36 +66,45 @@ fetch_data() {
6566

6667
fetch_data
6768

69+
attempt=0
70+
retry_wait_time=5
71+
6872
if [ "${reset}" == true ]; then
69-
if [ "$IBMCLOUD_CS_API_ENDPOINT" = "containers.cloud.ibm.com" ]; then
70-
if [ "$PRIVATE_ENV" = true ]; then
71-
if [ "$CLUSTER_ENDPOINT" == "private" ] || [ "$CLUSTER_ENDPOINT" == "default" ]; then
72-
RESET_URL="https://private.$REGION.$IBMCLOUD_CS_API_ENDPOINT/v1/keys"
73-
result=$(curl -i -H "accept: application/json" -H "Authorization: $IAM_TOKEN" -H "X-Auth-Resource-Group: $RESOURCE_GROUP_ID" -X POST "$RESET_URL" 2>/dev/null)
74-
status_code=$(echo "$result" | head -n 1 | cut -d$' ' -f2)
75-
elif [ "$CLUSTER_ENDPOINT" == "vpe" ]; then
76-
RESET_URL="https://api.$REGION.$IBMCLOUD_CS_API_ENDPOINT/v1/keys"
77-
result=$(curl -i -H "accept: application/json" -H "Authorization: $IAM_TOKEN" -H "X-Auth-Resource-Group: $RESOURCE_GROUP_ID" -X POST "$RESET_URL" 2>/dev/null)
73+
while [ $attempt -lt $MAX_ATTEMPTS ]; do
74+
if [ "$IBMCLOUD_CS_API_ENDPOINT" = "containers.cloud.ibm.com" ]; then
75+
if [ "$PRIVATE_ENV" = true ]; then
76+
if [ "$CLUSTER_ENDPOINT" == "private" ] || [ "$CLUSTER_ENDPOINT" == "default" ]; then
77+
RESET_URL="https://private.$REGION.$IBMCLOUD_CS_API_ENDPOINT/v1/keys"
78+
result=$(curl -i -H "accept: application/json" -H "Authorization: $IAM_TOKEN" -H "X-Auth-Resource-Group: $RESOURCE_GROUP_ID" -X POST "$RESET_URL" 2>/dev/null)
79+
status_code=$(echo "$result" | head -n 1 | cut -d$' ' -f2)
80+
elif [ "$CLUSTER_ENDPOINT" == "vpe" ]; then
81+
RESET_URL="https://api.$REGION.$IBMCLOUD_CS_API_ENDPOINT/v1/keys"
82+
result=$(curl -i -H "accept: application/json" -H "Authorization: $IAM_TOKEN" -H "X-Auth-Resource-Group: $RESOURCE_GROUP_ID" -X POST "$RESET_URL" 2>/dev/null)
83+
status_code=$(echo "$result" | head -n 1 | cut -d$' ' -f2)
84+
fi
85+
else
86+
RESET_URL="https://$IBMCLOUD_CS_API_ENDPOINT/global/v1/keys"
87+
result=$(curl -i -H "accept: application/json" -H "X-Region: $REGION" -H "Authorization: $IAM_TOKEN" -H "X-Auth-Resource-Group: $RESOURCE_GROUP_ID" -X POST "$RESET_URL" -d '' 2>/dev/null)
7888
status_code=$(echo "$result" | head -n 1 | cut -d$' ' -f2)
7989
fi
8090
else
8191
RESET_URL="https://$IBMCLOUD_CS_API_ENDPOINT/global/v1/keys"
8292
result=$(curl -i -H "accept: application/json" -H "X-Region: $REGION" -H "Authorization: $IAM_TOKEN" -H "X-Auth-Resource-Group: $RESOURCE_GROUP_ID" -X POST "$RESET_URL" -d '' 2>/dev/null)
8393
status_code=$(echo "$result" | head -n 1 | cut -d$' ' -f2)
8494
fi
85-
else
86-
RESET_URL="https://$IBMCLOUD_CS_API_ENDPOINT/global/v1/keys"
87-
result=$(curl -i -H "accept: application/json" -H "X-Region: $REGION" -H "Authorization: $IAM_TOKEN" -H "X-Auth-Resource-Group: $RESOURCE_GROUP_ID" -X POST "$RESET_URL" -d '' 2>/dev/null)
88-
status_code=$(echo "$result" | head -n 1 | cut -d$' ' -f2)
89-
fi
9095

91-
if [ "${status_code}" == "204" ] || [ "${status_code}" == "200" ]; then
92-
echo "The IAM API key is successfully reset."
93-
else
94-
echo "ERROR:: FAILED TO RESET THE IAM API KEY"
95-
echo "$result"
96-
exit 1
97-
fi
98-
# sleep for 10 secs to allow the new key to be replicated across backend DB instances before attempting to create cluster
99-
sleep 10
96+
if [ "${status_code}" == "204" ] || [ "${status_code}" == "200" ]; then
97+
echo "The IAM API key is successfully reset."
98+
sleep 10
99+
exit 0
100+
else
101+
echo "ERROR:: FAILED TO RESET THE IAM API KEY"
102+
echo "$result"
103+
sleep $retry_wait_time
104+
((attempt++))
105+
fi
106+
# sleep for 10 secs to allow the new key to be replicated across backend DB instances before attempting to create cluster
107+
done
108+
echo "Maximum retry attempts reached. Could not reset api key."
109+
exit 1
100110
fi

0 commit comments

Comments
 (0)