-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathdefault_kube_version.sh
57 lines (49 loc) · 1.84 KB
/
default_kube_version.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
#!/bin/bash
# IBM Cloud API Key to access Conteiners API
API_KEY=$1
# Use openshift versions, set to true if openshift should be used. Otherwise leave blank
OPENSHIFT="true"
AUTHORIZATION="Basic Yng6Yng=" # Bluemix Authorization to get refreshtoken
SUFFIX="" # Suffix to add to the version
CLUSTER_TYPE="kubernetes"
# If openshift is not empty set suffix and cluster type
if [ "$OPENSHIFT" == "true" ]; then
SUFFIX="_openshift"
CLUSTER_TYPE="openshift"
fi
# Fetch access token to use IKS API
TOKEN=$(
echo $(
curl -i -s -k -X POST \
--header "Content-Type: application/x-www-form-urlencoded" \
--header "Authorization: $AUTHORIZATION" \
--data-urlencode "apikey=$API_KEY" \
--data-urlencode "grant_type=urn:ibm:params:oauth:grant-type:apikey" \
"https://iam.cloud.ibm.com/identity/token"
)
)
# IAM Access Token
ACCESS_TOKEN=$(echo $TOKEN | sed -e s/.*access_token\":\"//g | sed -e s/\".*//g)
VERSION_DATA=$(
curl -s -X GET \
https://containers.cloud.ibm.com/global/v1/versions \
-H "Authorization: Bearer $ACCESS_TOKEN" | jq -r ".$CLUSTER_TYPE"
)
VERSIONS_COUNT=$(echo $VERSION_DATA | jq '. | length') # Count of keys
VERSIONS_LENGTH=$((VERSIONS_COUNT - 1)) # Count based with 0
DEFAULT_VERSION=""
# For each version
for i in $(seq 0 $VERSIONS_LENGTH)
do
# If the version is default
IS_DEFAULT=$(echo $VERSION_DATA | jq -r ".[$i].default")
if [ "$IS_DEFAULT" == "true" ]; then
MAJOR=$(echo $VERSION_DATA | jq -r ".[$i].major")
MINOR=$(echo $VERSION_DATA | jq -r ".[$i].minor")
PATCH="$(echo $VERSION_DATA | jq -r ".[$i].patch")"
# Create string
DEFAULT_VERSION="$MAJOR.$MINOR.$PATCH$SUFFIX"
fi
done
# Return data
jq -n --arg default_version "$DEFAULT_VERSION" '{default_version: $default_version}'