From 38ae984c420375b566cf26b4d147705bb846009f Mon Sep 17 00:00:00 2001 From: Blaine Gardner Date: Thu, 28 Jul 2022 12:13:06 -0600 Subject: [PATCH] Add reusable functions for kubectl in namespaces Add KUBECTL_NS_OPERATOR and KUBECTL_NS_CLUSTER functions to replace repeated calls of `TOP_LEVEL_COMMAND --namespace "${ROOK_*_NAMESPACE}"`. Use all caps for the function names so they stand out more clearly. When using `kubectl_ns_operator some-command`, it isn't as clear that it is a special type of call. Signed-off-by: Blaine Gardner --- kubectl-rook-ceph.sh | 36 +++++++++++++++++++++++------------- 1 file changed, 23 insertions(+), 13 deletions(-) diff --git a/kubectl-rook-ceph.sh b/kubectl-rook-ceph.sh index 4b166218..90707e20 100755 --- a/kubectl-rook-ceph.sh +++ b/kubectl-rook-ceph.sh @@ -149,13 +149,23 @@ function end_of_command_parsing() { fi } +# run a kubectl command in the operator namespace +function KUBECTL_NS_OPERATOR() { + $TOP_LEVEL_COMMAND --namespace "$ROOK_OPERATOR_NAMESPACE" "$@" +} + +# run a kubectl command in the cluster namespace +function KUBECTL_NS_CLUSTER() { + $TOP_LEVEL_COMMAND --namespace "$ROOK_CLUSTER_NAMESPACE" "$@" +} + #################################################################################################### # 'kubectl rook-ceph ceph ...' command #################################################################################################### function run_ceph_command() { # do not call end_of_command_parsing here because all remaining input is passed directly to 'ceph' - $TOP_LEVEL_COMMAND --namespace "$ROOK_OPERATOR_NAMESPACE" exec deploy/rook-ceph-operator -- ceph "$@" --conf="$CEPH_CONF_PATH" + KUBECTL_NS_OPERATOR exec deploy/rook-ceph-operator -- ceph "$@" --conf="$CEPH_CONF_PATH" } #################################################################################################### @@ -164,7 +174,7 @@ function run_ceph_command() { function run_rbd_command() { # do not call end_of_command_parsing here because all remaining input is passed directly to 'ceph' - $TOP_LEVEL_COMMAND --namespace "$ROOK_OPERATOR_NAMESPACE" exec deploy/rook-ceph-operator -- rbd "$@" --conf="$CEPH_CONF_PATH" + KUBECTL_NS_OPERATOR exec deploy/rook-ceph-operator -- rbd "$@" --conf="$CEPH_CONF_PATH" } #################################################################################################### @@ -185,14 +195,14 @@ function run_operator_command() { function run_operator_restart_command() { end_of_command_parsing "$@" # end of command tree - $TOP_LEVEL_COMMAND --namespace "$ROOK_OPERATOR_NAMESPACE" rollout restart deploy/rook-ceph-operator + KUBECTL_NS_OPERATOR rollout restart deploy/rook-ceph-operator } function path_cm_rook_ceph_operator_config() { if [[ "$#" -ne 2 ]]; then fail_error "require exactly 2 subcommand: $*" fi - $TOP_LEVEL_COMMAND --namespace "$ROOK_OPERATOR_NAMESPACE" patch configmaps rook-ceph-operator-config --type json --patch "[{ op: replace, path: /data/$1, value: $2 }]" + KUBECTL_NS_OPERATOR patch configmaps rook-ceph-operator-config --type json --patch "[{ op: replace, path: /data/$1, value: $2 }]" } #################################################################################################### @@ -201,7 +211,7 @@ function path_cm_rook_ceph_operator_config() { function fetch_mon_endpoints() { end_of_command_parsing "$@" # end of command tree - $TOP_LEVEL_COMMAND --namespace "$ROOK_CLUSTER_NAMESPACE" get cm rook-ceph-mon-endpoints -o json | jq --monochrome-output '.data.data' | tr -d '"' | tr -d '=' | sed 's/[A-Za-z]*//g' + KUBECTL_NS_CLUSTER get cm rook-ceph-mon-endpoints -o json | jq --monochrome-output '.data.data' | tr -d '"' | tr -d '=' | sed 's/[A-Za-z]*//g' } #################################################################################################### @@ -230,20 +240,20 @@ function rook_version() { function run_rook_version() { end_of_command_parsing "$@" # end of command tree - $TOP_LEVEL_COMMAND --namespace "$ROOK_OPERATOR_NAMESPACE" exec deploy/rook-ceph-operator -- rook version + KUBECTL_NS_OPERATOR exec deploy/rook-ceph-operator -- rook version } function run_rook_cr_status() { if [ "$#" -eq 1 ] && [ "$1" = "all" ]; then - cr_list=$($TOP_LEVEL_COMMAND --namespace "$ROOK_CLUSTER_NAMESPACE" get crd | awk '{print $1}' | sed '1d') + cr_list=$(KUBECTL_NS_CLUSTER get crd | awk '{print $1}' | sed '1d') echo "CR status" for cr in $cr_list; do - echo "$cr": "$($TOP_LEVEL_COMMAND --namespace "$ROOK_CLUSTER_NAMESPACE" get "$cr" -ojson | jq --monochrome-output '.items[].status')" + echo "$cr": "$(KUBECTL_NS_CLUSTER get "$cr" -ojson | jq --monochrome-output '.items[].status')" done elif [[ "$#" -eq 1 ]]; then - $TOP_LEVEL_COMMAND --namespace "$ROOK_CLUSTER_NAMESPACE" get "$1" -ojson | jq --monochrome-output '.items[].status' + KUBECTL_NS_CLUSTER get "$1" -ojson | jq --monochrome-output '.items[].status' elif [[ "$#" -eq 0 ]]; then - $TOP_LEVEL_COMMAND --namespace "$ROOK_CLUSTER_NAMESPACE" get cephclusters.ceph.rook.io -ojson | jq --monochrome-output '.items[].status' + KUBECTL_NS_CLUSTER get cephclusters.ceph.rook.io -ojson | jq --monochrome-output '.items[].status' else fail_error "$# does not exist" fi @@ -254,9 +264,9 @@ function run_purge_osd() { if [ "$#" -eq 2 ] && [ "$2" = "--force" ]; then force_removal=true fi - mon_endpoints=$($TOP_LEVEL_COMMAND --namespace "$ROOK_CLUSTER_NAMESPACE" get cm rook-ceph-mon-endpoints -o jsonpath='{.data.data}' | cut -d "," -f1) - ceph_secret=$($TOP_LEVEL_COMMAND --namespace "$ROOK_OPERATOR_NAMESPACE" exec deploy/rook-ceph-operator -- cat /var/lib/rook/"$ROOK_CLUSTER_NAMESPACE"/client.admin.keyring | grep "key" | awk '{print $3}') - $TOP_LEVEL_COMMAND --namespace "$ROOK_OPERATOR_NAMESPACE" exec deploy/rook-ceph-operator -- sh -c "export ROOK_MON_ENDPOINTS=$mon_endpoints \ + mon_endpoints=$(KUBECTL_NS_CLUSTER get cm rook-ceph-mon-endpoints -o jsonpath='{.data.data}' | cut -d "," -f1) + ceph_secret=$(KUBECTL_NS_OPERATOR exec deploy/rook-ceph-operator -- cat /var/lib/rook/"$ROOK_CLUSTER_NAMESPACE"/client.admin.keyring | grep "key" | awk '{print $3}') + KUBECTL_NS_OPERATOR exec deploy/rook-ceph-operator -- sh -c "export ROOK_MON_ENDPOINTS=$mon_endpoints \ ROOK_CEPH_USERNAME=client.admin \ ROOK_CEPH_SECRET=$ceph_secret \ ROOK_CONFIG_DIR=/var/lib/rook && \