|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +# Copyright 2021 The OpenEBS Authors. All rights reserved. |
| 4 | +# |
| 5 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | +# you may not use this file except in compliance with the License. |
| 7 | +# You may obtain a copy of the License at |
| 8 | +# |
| 9 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, software |
| 12 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +# See the License for the specific language governing permissions and |
| 15 | +# limitations under the License. |
| 16 | +# |
| 17 | + |
| 18 | +set -e |
| 19 | + |
| 20 | +print_help() { |
| 21 | +cat << EOF |
| 22 | +Usage: |
| 23 | + $0 <options> IMAGE_TAG |
| 24 | +
|
| 25 | +Example: |
| 26 | + $0 -n nfs-ns 0.7.1 |
| 27 | +
|
| 28 | +IMAGE_TAG is required to execute this script. |
| 29 | +
|
| 30 | +By default, this script uses 'openebs' namespace to search nfs-server deployment. |
| 31 | +If you have used different namespace for nfs-server deployment then you must provide |
| 32 | +the namespace using option(-n). |
| 33 | +
|
| 34 | +Supported options are as below: |
| 35 | + -h Show this message and exit |
| 36 | + -n namespace for nfs-server deployment. Default namespace used is 'openebs' |
| 37 | + -d nfs-server deployment name. If option '-d' is not mentioned then this script will |
| 38 | + upgrade all the nfs-server deployments available in 'openebs' or (-n NS) namespace. |
| 39 | +EOF |
| 40 | +} |
| 41 | + |
| 42 | +# NFS_SERVER_NS represent the namespace for nfs server deployment |
| 43 | +# By default, it is set to 'openebs'. |
| 44 | +NFS_SERVER_NS=openebs |
| 45 | + |
| 46 | +# IMAGE_TAG represent the version for nfs server image |
| 47 | +IMAGE_TAG= |
| 48 | + |
| 49 | +# DEPLOYMENTS represent the nfs-server deployment name/name-list |
| 50 | +DEPLOYMENTS= |
| 51 | + |
| 52 | +# list_deployment list nfs-server deployment in NFS_SERVER_NS namespace |
| 53 | +list_deployment() { |
| 54 | + local -n deploy=$1 |
| 55 | + deploy=$(kubectl get deployment -n ${NFS_SERVER_NS} -l openebs.io/nfs-server --no-headers -o custom-columns=:.metadata.name) |
| 56 | +} |
| 57 | + |
| 58 | +# upgrade_deployment patch the given deployment in NFS_SERVER_NS with image-tag IMAGE_TAG |
| 59 | +upgrade_deployment() { |
| 60 | + deploymentName=$1 |
| 61 | + |
| 62 | + existingImage=$(kubectl get deployment -n openebs ${deploymentName} -o jsonpath='{.spec.template.spec.containers[0].image}') |
| 63 | + |
| 64 | + repo=${existingImage%:*} |
| 65 | + newImage=${repo}:${IMAGE_TAG} |
| 66 | + patchJson="{\"spec\": {\"template\": {\"spec\": {\"containers\" : [{\"name\" : \"nfs-server\", \"image\" : \"${newImage}\"}]}}}}" |
| 67 | + |
| 68 | + kubectl patch deploy -n ${NFS_SERVER_NS} ${deploymentName} -p "${patchJson}" > /dev/null |
| 69 | + exitCode=$? |
| 70 | + if [ $exitCode -ne 0 ]; then |
| 71 | + echo "ERROR: Failed to patch ${deploymentName} exit code: $exitCode" |
| 72 | + exit |
| 73 | + fi |
| 74 | + |
| 75 | + rolloutStatus=$(kubectl rollout status -n ${NFS_SERVER_NS} deployment/${deploymentName}) |
| 76 | + exitCode=$? |
| 77 | + if [[ ($exitCode -ne 0) || ! (${rolloutStatus} =~ "successfully rolled out") ]]; then |
| 78 | + echo "ERROR: Failed to rollout status for ${deploymentName} exit code: $exitCode" |
| 79 | + exit |
| 80 | + fi |
| 81 | +} |
| 82 | + |
| 83 | +# options followed by ':' needs an argument |
| 84 | +# see `man getopt` |
| 85 | +shortOpts=hn:d: |
| 86 | + |
| 87 | +# store the output of getopt so that we can assign it to "$@" using set command |
| 88 | +# since we are using "--options" in getopt, arguments are passed via -- "$@" |
| 89 | +PARSED=$(getopt --options ${shortOpts} --name "$0" -- "$@") |
| 90 | +if [[ $? -ne 0 ]]; then |
| 91 | + echo "invalid arguments" |
| 92 | + exit 1 |
| 93 | +fi |
| 94 | +# assign arguments to "$@" |
| 95 | +eval set -- "${PARSED}" |
| 96 | + |
| 97 | +while [[ $# -gt 0 ]] |
| 98 | +do |
| 99 | + case "$1" in |
| 100 | + -h) |
| 101 | + print_help |
| 102 | + exit 0 |
| 103 | + ;; |
| 104 | + -n) |
| 105 | + shift |
| 106 | + NFS_SERVER_NS=$1 |
| 107 | + ;; |
| 108 | + -d) |
| 109 | + shift |
| 110 | + DEPLOYMENTS=$1 |
| 111 | + ;; |
| 112 | + ## argument without options is mentioned after '--' |
| 113 | + --) |
| 114 | + shift |
| 115 | + IMAGE_TAG=$1 |
| 116 | + break |
| 117 | + esac |
| 118 | + shift # Expose the next argument |
| 119 | +done |
| 120 | + |
| 121 | +[[ -z $IMAGE_TAG ]] && print_help && exit 0 |
| 122 | + |
| 123 | +[[ -z $DEPLOYMENTS ]] && list_deployment DEPLOYMENTS |
| 124 | + |
| 125 | +for i in ${DEPLOYMENTS}; do |
| 126 | + upgrade_deployment ${i} |
| 127 | + echo "Deployment ${NFS_SERVER_NS}/${i} updated with image tag ${IMAGE_TAG}" |
| 128 | +done |
0 commit comments