Unverified Commit 65aa5ef8 authored by Konstantinos Tsakalozos's avatar Konstantinos Tsakalozos Committed by GitHub
Browse files

Add status command (#178)

parent b1cf6f28
......@@ -20,6 +20,18 @@ snap install microk8s --classic --channel=1.12/stable
You can read more on the microk8s release channels in the [Release Channels and Upgrades](docs/release-channels.md) doc.
At any point you can check microk8s' availability with:
```
microk8s.status
```
During installation you can use the `--wait-ready` flag to wait for the kubernetes services to initialise:
```
microk8s.status --wait-ready
```
> In order to install microk8s make sure
> - port 8080 is not used and
> - if you have AppArmor enabled (check with `sudo apparmor_status`) you do not have any other [dockerd installed](docs/dockerd.md). You can use the dockerd coming with microk8s.
......@@ -67,7 +79,7 @@ These addons can be disabled at anytime using the `disable` command
microk8s.disable dashboard dns
```
You can find the addon manifests and/or scripts under `${SNAP}/actions/`, with `${SNAP}` pointing by default to `/snap/microk8s/current`.
With `microk8s.status` you can see the list of available addons and which ones are currently enabled. You can find the addon manifests and/or scripts under `${SNAP}/actions/`, with `${SNAP}` pointing by default to `/snap/microk8s/current`.
#### List of Available Addons
- **dns**: Deploy kube dns. This addon may be required by others thus we recommend you always enable it.
......
......@@ -76,5 +76,6 @@ use_manifest() {
"$SNAP/bin/sed" -i '[email protected]'$i'@'"${items[$i]}"'@g' "${tmp_manifest}"
done
"$SNAP/kubectl" "--kubeconfig=$SNAP/client.config" "$action" -f "${tmp_manifest}"
use_manifest_result="$?"
rm "${tmp_manifest}"
}
......@@ -18,20 +18,35 @@ if echo "$*" | grep -q -- '--help'; then
for action in $actions; do
echo " $action"
done
exit
exit 0
fi
exit_if_stopped
result=1
for action in "[email protected]"; do
# If there is a script to execute for the action $1 run the script and ignore any yamls
if [ -f "${SNAP}/actions/disable.$action.sh" ]; then
"${SNAP}/actions/disable.$action.sh"
if "${SNAP}/actions/disable.$action.sh"
then
result=0
else
echo "Failed to disable $action"
exit 1
fi
elif [ -f "${SNAP}/actions/$action.yaml" ]; then
echo "Disabling $action"
use_manifest $action delete
echo "$action disabled"
if [ "$use_manifest_result" = "0" ]
then
echo "$action disabled"
result=0
else
echo "Failed to disable $action"
exit 1
fi
else
echo "Nothing to do for $action"
exit 1
exit 2
fi
done
exit $result
......@@ -18,20 +18,35 @@ if echo "$*" | grep -q -- '--help'; then
for action in $actions; do
echo " $action"
done
exit
exit 0
fi
exit_if_stopped
result=1
for action in "[email protected]"; do
# If there is a script to execute for the $action run the script and ignore any yamls
if [ -f "${SNAP}/actions/enable.$action.sh" ]; then
"${SNAP}/actions/enable.$action.sh"
if "${SNAP}/actions/enable.$action.sh"
then
result=0
else
echo "Failed to enable $action"
exit 1
fi
elif [ -f "${SNAP}/actions/$action.yaml" ]; then
echo "Enabling $action"
use_manifest $action apply
echo "$action enabled"
if [ "$use_manifest_result" = "0" ]
then
echo "$action enabled"
result=0
else
echo "Failed to enable $action"
exit 1
fi
else
echo "Nothing to do for $action"
exit 1
exit 2
fi
done
exit $result
......@@ -3,7 +3,7 @@
set -eu
source $SNAP/actions/common/utils.sh
KUBECTL=/snap/bin/microk8s.kubectl
KUBECTL="$SNAP/kubectl --kubeconfig=$SNAP/client.config"
clean_cluster() {
echo "Calling clean_cluster"
......
#!/bin/bash
export PATH="$SNAP/usr/sbin:$SNAP/usr/bin:$SNAP/sbin:$SNAP/bin:$PATH"
export LD_LIBRARY_PATH="$LD_LIBRARY_PATH:$SNAP/lib:$SNAP/usr/lib:$SNAP/lib/x86_64-linux-gnu:$SNAP/usr/lib/x86_64-linux-gnu"
export LD_LIBRARY_PATH=$SNAP_LIBRARY_PATH:$LD_LIBRARY_PATH
source $SNAP/actions/common/utils.sh
KUBECTL="$SNAP/kubectl --kubeconfig=$SNAP/client.config"
# Arrray of what we query per addon
declare -A addon
addon[dns]="pod/kube-dns"
addon[dashboard]="pod/kubernetes-dashboard"
addon[dashboard]="pod/kubernetes-dashboard"
addon[ingress]="pod/nginx-ingress-microk8s-controller"
addon[storage]="pod/hostpath-provisioner"
addon[registry]="pod/registry"
addon[metrics-server]="pod/metrics-server"
addon[istio]="pod/istio"
addon[gpu]="daemonset.apps/nvidia-device-plugin-daemonset"
function show_help() {
prog=$(basename -s.wrapper "$0" | tr - .)
echo "Usage: $prog [--yaml] [--wait-ready [--timeout <seconds>]]"
echo "Show the status of microks8. Use --yaml to get the response in yaml."
echo "To wait for microk8s to become ready use the --wait-ready flag and provide a"
echo "waiting timeout."
echo ""
echo "Example: $prog --wait-ready --timeout 60"
echo ""
}
# Parse arguments. The variables yaml, wait_ready and timeout are filled.
function parse_args() {
while [[ "$#" > 0 ]]
do
case "$1" in
"--help") show_help ; exit 0 ;;
"--yaml") yaml=1 ;;
"--wait-ready") wait_ready=1 ;;
"--timeout") timeout=$2 ; shift ;;
esac
shift
done
}
# Wait for the timeout of microk8s to say it has a node. If timeout variable is set this method will not block.
function wait_for_ready() {
start_timer="$(date +%s)"
while ! ($KUBECTL get all --all-namespaces | grep -z "service/kubernetes") &> /dev/null
do
now="$(date +%s)"
if ! [ -z $timeout ] && [[ "$now" > "$(($start_timer + $timeout))" ]] ; then
break
fi
sleep 2
done
while ! ($KUBECTL get no | grep -z " Ready") &> /dev/null
do
now="$(date +%s)"
if ! [ -z $timeout ] && [[ "$now" > "$(($start_timer + $timeout))" ]] ; then
break
fi
sleep 2
done
}
# Format the output based on the yaml variable. The status variable should be set.
function format_output() {
if [ "$status" = "Running" ]
then
get_all="$($KUBECTL get all --all-namespaces 2>&1)"
for a in "${!addon[@]}"
do
if echo "$get_all" | grep "${addon[$a]}" &> /dev/null
then
addon["$a"]="enabled"
else
addon["$a"]="disabled"
fi
done
fi
if ! [ -z ${yaml} ]
then
if [ "$status" = "Running" ]
then
echo "microk8s:"
echo " running: true"
echo "addons:"
for a in "${!addon[@]}"
do
echo " $a: ${addon[$a]}"
done
exit 0
else
echo "microk8s:"
echo " running: false"
exit 1
fi
else
if [ "$status" = "Running" ]
then
echo "microk8s is running"
echo "addons:"
for a in "${!addon[@]}"
do
echo "$a: ${addon[$a]}"
done
exit 0
else
echo "microk8s is not running. Use microk8s.ispect for a deeper inspection."
exit 1
fi
fi
}
# See if we have timed out or we are running. Sets the status variable.
function check_ready() {
status="Not running"
if ($KUBECTL get all --all-namespaces | grep -z "service/kubernetes") &> /dev/null &&
($KUBECTL get no | grep -z " Ready") &> /dev/null
then
status="Running"
fi
}
# Main flow #
parse_args "[email protected]"
if ! [ -z ${wait_ready} ]
then
wait_for_ready
fi
check_ready
format_output
......@@ -47,6 +47,8 @@ apps:
command: microk8s-start.wrapper
stop:
command: microk8s-stop.wrapper
status:
command: microk8s-status.wrapper
config:
command: microk8s-config.wrapper
reset:
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment