Skip to content

Commit

Permalink
Add 'kubectl rabbitmq version' command (#450)
Browse files Browse the repository at this point in the history
Relates #440
  • Loading branch information
ansd authored Nov 9, 2020
1 parent 4a06999 commit a456659
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 0 deletions.
30 changes: 30 additions & 0 deletions bin/kubectl-rabbitmq
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,38 @@ USAGE:
Print this help
kubectl rabbitmq help
Print kubectl-rabbitmq plugin version
kubectl rabbitmq version
END
)
echo "$usage"
}

version() {
# Since we require to install this plugin via krew, we get the version from krew instead of hardcoding a version to this file.
failure_msg="version cannot be determined because plugin was not installed via krew"
if ! command -v kubectl-krew &> /dev/null
then
echo "$failure_msg"
exit 1
fi

# We can't use `krew info` because it provides versions about available - not installed - plugins.
# `krew list` provides versions of installed plugins.
# We can't redirect stdout of `krew list` because this will suppress the version number (see `kubectl krew list --help`)
# Therefore, we have to get the version number from the `krew list` logs.
version_line=$(kubectl krew -v 4 list 2>&1 | grep 'rabbitmq: version=' || true)
if [[ -z "$version_line" ]];
then
echo "$failure_msg"
exit 1
fi

version="${version_line##*rabbitmq: version=}"
echo "kubectl-rabbitmq $version"
}

get_instance_details() {
instance=${1}
username=$(kubectl get secret "${instance}-default-user" -o jsonpath="{.data.username}" | base64 --decode)
Expand Down Expand Up @@ -305,6 +332,9 @@ main() {
"-h")
usage
;;
"version")
version
;;
*)
usage
exit 1
Expand Down
14 changes: 14 additions & 0 deletions bin/kubectl-rabbitmq.bats
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,20 @@ eventually() {
return 1
}

@test "version outputs version" {
run kubectl rabbitmq version

if ! command -v kubectl-krew &> /dev/null
then
[ "$status" -eq 1 ]
[ "${lines[0]}" = "version cannot be determined because plugin was not installed via krew" ]
else
[ "$status" -eq 0 ]
version_regex='kubectl-rabbitmq v([0-9]+)\.([0-9]+)\.([0-9]+)$'
[[ "${lines[0]}" =~ $version_regex ]]
fi
}

@test "install-cluster-operator with too many args fails" {
run kubectl rabbitmq install-cluster-operator too-many-args

Expand Down

0 comments on commit a456659

Please sign in to comment.