Repository: canonical/microk8s

Issue:
Indentation error in yaml output of microk8s.status
The output of `microk8s.status` is

```
microk8s:
  running: False
high-availability:
  enabled: False
   message: microk8s is not running. Use microk8s inspect for a deeper inspection.
```

which confuses some parsers (e.g. the built-in Python 3.8) due to the extraneous space before `message`.


Discussion:
I'm working on a fix in https://github.com/ubuntu/microk8s/pull/2148. The issue is found.

I'd like to cover it with tests and stumbled over an issue. I'm wondering how to hit the case where `microk8s.status` prints yaml for a non-running instance in test cases. If the instance is not running `exit_if_stopped()` in `scripts/wrappers/status.py` prints non-yaml and exits. However, I got the yaml output, otherwise I wouldn't have hit the bug. Maybe it's only printed in rare conditions during startup or after reset. It's odd that `microk8s.status --format=yaml` prints yaml and non-yaml in different scenario.
Thank you for reporting this @krichter722 and for working on a fix.

> If the instance is not running `exit_if_stopped()` in `scripts/wrappers/status.py` prints non-yaml and exits.

You are right, there must be a race condition where you could hit this if `microk8s.status` is called while the cluster is stopping.
> You are right, there must be a race condition where you could hit this if microk8s.status is called while the cluster is stopping.

@ktsakalozos This question might sound odd, but it make sense since it's more of a product spec race condition rather than a technical/implementation race condition: Do you want to keep this behaviour? Maybe you need some time and having a chat with your product manager. I'd like to clarify that before working further on the PR. In case someone else is experiencing it, you can take the fix without the tests from the PR, but I'd like to submit a PR with tests only and catching up on tests for `microk8s.status` is a good investment afaik.
I hit this also
```
$ microk8s.status --wait-ready --timeout 15 --yaml
microk8s:
  running: False
   message: microk8s is not running. Use microk8s inspect for a deeper inspection.
```
Hi @krichter722, thank you for this PR. I think there are two places in the `microk8s.status` that need to be patched, see the PR https://github.com/ubuntu/microk8s/pull/2155 .

I was not able to reproduce this issue with a normal workflow that would involve a `microk8s.stop`. Instead what I had to `sudo killall -9 kubelite` and before systemd restarts the service I called `microk8s.status --yaml`. Arguably this is a rather convoluted scenario to try to cover in tests so I would be ok with a fix without a corresponding test. Note that we need to backport this fix to 1.20, 1.19, and 1.18 branches so we should have 4 PRs in total. Let me know if this is something you are willing to work on. Thanks.

Resolving patch:
diff --git a/scripts/wrappers/status.py b/scripts/wrappers/status.py
--- a/scripts/wrappers/status.py
+++ b/scripts/wrappers/status.py
@@ -98,7 +98,7 @@ def print_short_yaml(isReady, enabled_addons, disabled_addons):
             print("  {}: disabled".format(disabled["name"]))
     else:
         print(
-            "{:>2} {} {}".format(
+            "{:>2}{} {}".format(
                 "",
                 "message:",
                 "microk8s is not running. Use microk8s inspect for a deeper inspection.",
@@ -135,7 +135,7 @@ def print_yaml(isReady, enabled_addons, disabled_addons):
             print("{:>4}status: disabled".format(""))
     else:
         print(
-            "{:>2} {} {}".format(
+            "{:>2}{} {}".format(
                 "",
                 "message:",
                 "microk8s is not running. Use microk8s inspect for a deeper inspection.",


Test patch: