|
| 1 | +#!/usr/bin/env bash |
| 2 | + |
| 3 | +# This script captures the steps required to successfully |
| 4 | +# deploy the hostpath plugin driver. This should be considered |
| 5 | +# authoritative and all updates for this process should be |
| 6 | +# done here and referenced elsewhere. |
| 7 | + |
| 8 | +# The script assumes that kubectl is available on the OS path |
| 9 | +# where it is executed. |
| 10 | + |
| 11 | +set -e |
| 12 | +set -o pipefail |
| 13 | + |
| 14 | +BASE_DIR=$(dirname "$0") |
| 15 | +K8S_RELEASE=${K8S_RELEASE:-"release-1.13"} |
| 16 | + |
| 17 | +# If set, the following env variables override image registry and/or tag for each of the images. |
| 18 | +# They are named after the image name, with hyphen replaced by underscore and in upper case. |
| 19 | +# |
| 20 | +# - CSI_ATTACHER_REGISTRY |
| 21 | +# - CSI_ATTACHER_TAG |
| 22 | +# - CSI_NODE_DRIVER_REGISTRAR_REGISTRY |
| 23 | +# - CSI_NODE_DRIVER_REGISTRAR_TAG |
| 24 | +# - CSI_PROVISIONER_REGISTRY |
| 25 | +# - CSI_PROVISIONER_TAG |
| 26 | +# - CSI_SNAPSHOTTER_REGISTRY |
| 27 | +# - CSI_SNAPSHOTTER_TAG |
| 28 | +# - HOSTPATHPLUGIN_REGISTRY |
| 29 | +# - HOSTPATHPLUGIN_TAG |
| 30 | +# |
| 31 | +# Alternatively, it is possible to override all registries or tags with: |
| 32 | +# - IMAGE_REGISTRY |
| 33 | +# - IMAGE_TAG |
| 34 | +# These are used as fallback when the more specific variables are unset or empty. |
| 35 | +# |
| 36 | +# Beware that the .yaml files do not have "imagePullPolicy: Always". That means that |
| 37 | +# also the "canary" images will only be pulled once. This is good for testing |
| 38 | +# (starting a pod multiple times will always run with the same canary image), but |
| 39 | +# implies that refreshing that image has to be done manually. |
| 40 | +# |
| 41 | +# As a special case, 'none' as registry removes the registry name. |
| 42 | + |
| 43 | +function image_version () { |
| 44 | + yaml="$1" |
| 45 | + image="$2" |
| 46 | + |
| 47 | + # get version from `image: quay.io/k8scsi/csi-attacher:v1.0.1` |
| 48 | + version="$(grep "image:.*$image" "$yaml" | sed -e 's/.*:v/v/')" |
| 49 | + |
| 50 | + # apply overrides |
| 51 | + varname=$(echo $image | tr - _ | tr a-z A-Z) |
| 52 | + eval version=\${${varname}_TAG:-\${IMAGE_TAG:-\$version}} |
| 53 | + |
| 54 | + # When using canary images, we have to assume that the |
| 55 | + # canary images were built from the corresponding branch. |
| 56 | + case "$version" in canary) version=master;; |
| 57 | + *-canary) version="$(echo "$version" | sed -e 's/\(.*\)-canary/release-\1/')";; |
| 58 | + esac |
| 59 | + echo "$version" |
| 60 | +} |
| 61 | + |
| 62 | +# In addition, the RBAC rules can be overridden for provisioner and attacher. |
| 63 | +CSI_PROVISIONER_RBAC=${PROVISIONER_RBAC:-https://raw.githubusercontent.com/kubernetes-csi/external-provisioner/$(image_version "${BASE_DIR}/hostpath/csi-hostpath-provisioner.yaml" csi-provisioner)/deploy/kubernetes/rbac.yaml} |
| 64 | +CSI_ATTACHER_RBAC=${ATTACHER_RBAC:-https://raw.githubusercontent.com/kubernetes-csi/external-attacher/$(image_version "${BASE_DIR}/hostpath/csi-hostpath-attacher.yaml" csi-attacher)/deploy/kubernetes/rbac.yaml} |
| 65 | +CSI_SNAPSHOTTER_RBAC=${CSI_SNAPSHOTTER_RBAC:-https://raw.githubusercontent.com/kubernetes-csi/external-snapshotter/$(image_version "${BASE_DIR}/hostpath/csi-hostpath-snapshotter.yaml" csi-snapshotter)/deploy/kubernetes/rbac.yaml} |
| 66 | + |
| 67 | +INSTALL_CRD=${INSTALL_CRD:-"false"} |
| 68 | + |
| 69 | +run () { |
| 70 | + echo "$@" >&2 |
| 71 | + "$@" |
| 72 | +} |
| 73 | + |
| 74 | +# apply CSIDriver and CSINodeInfo API objects |
| 75 | +if [[ "${INSTALL_CRD}" =~ ^(y|Y|yes|true)$ ]] ; then |
| 76 | + echo "installing CRDs" |
| 77 | + run kubectl apply -f https://raw.githubusercontent.com/kubernetes/csi-api/${K8S_RELEASE}/pkg/crd/manifests/csidriver.yaml --validate=false |
| 78 | + run kubectl apply -f https://raw.githubusercontent.com/kubernetes/csi-api/${K8S_RELEASE}/pkg/crd/manifests/csinodeinfo.yaml --validate=false |
| 79 | +fi |
| 80 | + |
| 81 | +# rbac rules |
| 82 | +echo "applying RBAC rules" |
| 83 | +run kubectl apply -f "${CSI_PROVISIONER_RBAC}" |
| 84 | +run kubectl apply -f "${CSI_ATTACHER_RBAC}" |
| 85 | +run kubectl apply -f "${CSI_SNAPSHOTTER_RBAC}" |
| 86 | + |
| 87 | +# deploy hostpath plugin and registrar sidecar |
| 88 | +echo "deploying hostpath components" |
| 89 | +for i in ${BASE_DIR}/hostpath/*.yaml; do |
| 90 | + echo " $i" |
| 91 | + modified="$(cat "$i" | while IFS= read -r line; do |
| 92 | + if echo "$line" | grep -q '^\s*image:\s*'; then |
| 93 | + # Split 'image: quay.io/k8scsi/csi-attacher:v1.0.1' |
| 94 | + # into image (quay.io/k8scsi/csi-attacher:v1.0.1), |
| 95 | + # registry (quay.io/k8scsi), |
| 96 | + # name (csi-attacher), |
| 97 | + # tag (v1.0.1). |
| 98 | + image=$(echo "$line" | sed -e 's;.*image:\s*;;') |
| 99 | + registry=$(echo "$image" | sed -e 's;\(.*\)/.*;\1;') |
| 100 | + name=$(echo "$image" | sed -e 's;.*/\([^:]*\).*;\1;') |
| 101 | + tag=$(echo "$image" | sed -e 's;.*:;;') |
| 102 | +
|
| 103 | + # Variables are with underscores and upper case. |
| 104 | + varname=$(echo $name | tr - _ | tr a-z A-Z) |
| 105 | +
|
| 106 | + # Now replace registry and/or tag, if set as env variables. |
| 107 | + # If not set, the replacement is the same as the original value. |
| 108 | + prefix=$(eval echo \${${varname}_REGISTRY:-${IMAGE_REGISTRY:-${registry}}}/ | sed -e 's;none/;;') |
| 109 | + suffix=$(eval echo :\${${varname}_TAG:-${IMAGE_TAG:-${tag}}}) |
| 110 | + line="$(echo "$line" | sed -e "s;$image;${prefix}${name}${suffix};")" |
| 111 | + echo " using $line" >&2 |
| 112 | + fi |
| 113 | + echo "$line" |
| 114 | + done)" |
| 115 | + if ! echo "$modified" | kubectl apply -f -; then |
| 116 | + echo "modified version of $i:" |
| 117 | + echo "$modified" |
| 118 | + exit 1 |
| 119 | + fi |
| 120 | +done |
| 121 | +
|
| 122 | +# Wait until all pods are running. We have to make some assumptions |
| 123 | +# about the deployment here, otherwise we wouldn't know what to wait |
| 124 | +# for: the expectation is that we run attacher, provisioner, |
| 125 | +# snapshotter, socat and hostpath plugin in the default namespace. |
| 126 | +cnt=0 |
| 127 | +while [ $(kubectl get pods 2>/dev/null | grep '^csi-hostpath.* Running ' | wc -l) -lt 5 ]; do |
| 128 | + if [ $cnt -gt 30 ]; then |
| 129 | + echo "Running pods:" |
| 130 | + kubectl describe pods |
| 131 | +
|
| 132 | + echo >&2 "ERROR: hostpath deployment not ready after over 5min" |
| 133 | + exit 1 |
| 134 | + fi |
| 135 | + echo $(date +%H:%M:%S) "waiting for hostpath deployment to complete, attempt #$cnt" |
| 136 | + cnt=$(($cnt + 1)) |
| 137 | + sleep 10 |
| 138 | +done |
0 commit comments