Pawel Wieczorek | 216bd6a | 2019-11-29 15:15:51 +0100 | [diff] [blame] | 1 | #!/usr/bin/env bash |
| 2 | |
| 3 | # |
| 4 | # @file test/security/k8s/tools/dublin/setup_kubectl.sh |
| 5 | # @author Pawel Wieczorek <p.wieczorek2@samsung.com> |
| 6 | # @brief Utility for setting up kubectl tool for Dublin cluster |
| 7 | # |
| 8 | |
| 9 | # Dependencies: |
| 10 | # coreutils |
| 11 | # |
| 12 | # Privileges: |
| 13 | # Script expects to be run with administrative privileges for accessing /usr/local/bin |
| 14 | # |
| 15 | # Usage: |
| 16 | # # ./setup_kubectl.sh [RKE_CONFIG [KUBE_DIR [KUBE_CONFIG [KUBE_CONTEXT]]]] |
| 17 | # |
| 18 | |
| 19 | # Constants |
| 20 | BASHRC='.bashrc' |
| 21 | BASH_ALIASES='.bash_aliases' |
| 22 | USE_ONAP_ALIAS='useonap' |
| 23 | |
| 24 | DEFAULT_RKE_CONFIG='kube_config_cluster.yml' |
| 25 | DEFAULT_KUBE_DIR='.kube' |
| 26 | DEFAULT_KUBE_CONFIG='config.onap' |
| 27 | DEFAULT_KUBE_CONTEXT='onap' |
| 28 | |
| 29 | # Variables |
| 30 | RKE_CONFIG="${1:-$DEFAULT_RKE_CONFIG}" |
| 31 | KUBE_DIR="${2:-${HOME}/${DEFAULT_KUBE_DIR}}" |
| 32 | KUBE_CONFIG="${3:-$DEFAULT_KUBE_CONFIG}" |
| 33 | KUBE_CONTEXT="${4:-$DEFAULT_KUBE_CONTEXT}" |
| 34 | |
| 35 | USE_ONAP="f() { export KUBECONFIG=${KUBE_DIR}/${KUBE_CONFIG}; kubectl config use-context ${KUBE_CONTEXT}; }; f" |
| 36 | USE_ONAP_CONFIG="$(cat<<CONFIG |
| 37 | |
| 38 | # Use ONAP context for kubectl utility (defined in ${HOME}/${BASH_ALIASES}) |
| 39 | ${USE_ONAP_ALIAS} |
| 40 | CONFIG |
| 41 | )" |
| 42 | |
| 43 | |
| 44 | # Prerequistes |
| 45 | mkdir -p "$KUBE_DIR" |
| 46 | echo "alias ${USE_ONAP_ALIAS}='${USE_ONAP}'" >> "${HOME}/${BASH_ALIASES}" |
| 47 | |
| 48 | # Setup |
| 49 | cp "$RKE_CONFIG" "${KUBE_DIR}/${KUBE_CONFIG}" |
| 50 | |
| 51 | # Post-setup |
| 52 | echo "$USE_ONAP_CONFIG" >> "${HOME}/${BASHRC}" |