Skip to content

Commit

Permalink
Add reusable functions for kubectl in namespaces
Browse files Browse the repository at this point in the history
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 <blaine.gardner@redhat.com>
  • Loading branch information
BlaineEXE committed Jul 28, 2022
1 parent 256ca02 commit 38ae984
Showing 1 changed file with 23 additions and 13 deletions.
36 changes: 23 additions & 13 deletions kubectl-rook-ceph.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}

####################################################################################################
Expand All @@ -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"
}

####################################################################################################
Expand All @@ -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 }]"
}

####################################################################################################
Expand All @@ -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'
}

####################################################################################################
Expand Down Expand Up @@ -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
Expand All @@ -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 && \
Expand Down

0 comments on commit 38ae984

Please sign in to comment.