stark, steven | f351942 | 2019-12-05 14:14:32 -0800 | [diff] [blame] | 1 | #!/bin/bash |
| 2 | # Copyright 2019 AT&T Intellectual Property. All rights reserved. |
| 3 | # |
| 4 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | # you may not use this file except in compliance with the License. |
| 6 | # You may obtain a copy of the License at |
| 7 | # |
| 8 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | # |
| 10 | # Unless required by applicable law or agreed to in writing, software |
| 11 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | # See the License for the specific language governing permissions and |
| 14 | # limitations under the License. |
| 15 | |
| 16 | KUBE_VERSION=$1 |
| 17 | LOCATION=$2 |
| 18 | |
| 19 | COMMANDS="kubectl helm make java az" |
| 20 | |
| 21 | CLI_MAJOR="2" |
| 22 | CLI_MINOR="0" |
| 23 | CLI_INC="75" |
| 24 | |
| 25 | function check_requirement() { |
| 26 | req=$1 |
| 27 | |
| 28 | command -v $1 |
| 29 | if [ $? -ne 0 ]; then |
| 30 | echo "$1 was not found on machine. Please install it before proceeding." |
| 31 | exit 1 |
| 32 | fi |
| 33 | } |
| 34 | |
| 35 | echo "Checking requirements are installed..." |
| 36 | |
| 37 | for req in $COMMANDS; do |
| 38 | check_requirement $req |
| 39 | done |
| 40 | |
| 41 | echo "Checking K8 version is available in Azure..." |
| 42 | if [ -z "$KUBE_VERSION" ]; then |
| 43 | echo "K8 version not provided in cloud.conf." |
| 44 | echo "Update cloud.conf with the desired version." |
| 45 | exit 1 |
| 46 | fi |
| 47 | |
| 48 | if [ -z "$LOCATION" ]; then |
| 49 | echo "Location not provided in cloud.conf." |
| 50 | echo "Update cloud.conf with the desired location." |
| 51 | exit 1 |
| 52 | fi |
| 53 | |
| 54 | supported_k8_versions=`az aks get-versions --location $LOCATION --output json --query 'orchestrators[].orchestratorVersion'` |
| 55 | echo $supported_k8_versions | grep -q $KUBE_VERSION |
| 56 | if [ $? -ne 0 ]; then |
| 57 | echo "K8 version $KUBE_VERSION is not supported in location $LOCATION" |
| 58 | echo "The supported versions are $supported_k8_versions." |
| 59 | echo "Update cloud.conf with a supported version." |
| 60 | exit 1 |
| 61 | fi |
| 62 | |
| 63 | echo "Checking Azure CLI version..." |
| 64 | installed_cli_version=`az --version | grep -e "^azure-cli" | awk '{print $2}'` |
| 65 | installed_major=`echo $installed_cli_version | cut -d "." -f 1` |
| 66 | installed_minor=`echo $installed_cli_version | cut -d "." -f 2` |
| 67 | installed_inc=`echo $installed_cli_version | cut -d "." -f 3` |
| 68 | |
| 69 | if [ $installed_major -lt $CLI_MAJOR ]; then |
| 70 | echo "Azure cli version is out of date." |
| 71 | echo "Major version required is $CLI_MAJOR but $installed_major is installed." |
| 72 | exit 1 |
| 73 | fi |
| 74 | |
| 75 | if [ $installed_minor -lt $CLI_MINOR ]; then |
| 76 | echo "Azure cli version is out of date." |
| 77 | echo "Minor version required is $CLI_INC but $installed_inc is installed." |
| 78 | exit 1 |
| 79 | fi |
| 80 | |
| 81 | if [ $installed_inc -lt $CLI_INC ]; then |
| 82 | echo "Azure cli version is out of date." |
| 83 | echo "Incremental version required is $CLI_INC but $installed_inc is installed." |
| 84 | exit 1 |
| 85 | fi |
| 86 | |
| 87 | echo "Checking kubectl version is compatible with the K8 version..." |
| 88 | kubectl_version=`kubectl version --client --short | awk '{print $3}'` |
| 89 | kubectl_major=`echo $kubectl_version | cut -d "." -f 1 | sed 's/v//'` |
| 90 | kubectl_minor=`echo $kubectl_version | cut -d "." -f 2` |
| 91 | k8_major=`echo $KUBE_VERSION | cut -d "." -f 1` |
| 92 | k8_minor=`echo $KUBE_VERSION | cut -d "." -f 2` |
| 93 | |
| 94 | if [ $kubectl_major -ne $k8_major ]; then |
| 95 | echo "kubectl major version $kubectl_major doesn't equal kubernetes server version $k8_major" |
| 96 | exit 1 |
| 97 | fi |
| 98 | |
| 99 | minor_difference=`echo "$(($kubectl_minor-$k8_minor))"` |
| 100 | minor_abs_diff=`echo $minor_difference | tr -d -` |
| 101 | if [ $minor_abs_diff -gt 1 ]; then |
| 102 | echo "The difference between k8 minor version $KUBE_VERSION and kubectl minor version $kubectl_version is greater than 1" |
| 103 | echo "Kubernetes supports kubectl within 1 minor version." |
| 104 | exit 1 |
| 105 | fi |
| 106 | |
| 107 | echo "All requirements satisfied..." |
| 108 | sleep 1 |