Repository: canonical/microk8s

Issue:
MacOS - cannot generate a 'microk8s inspect'
- macOS Big Sur 11.5.1
- microk8s 2.1.0 installed from home-brew today
- 'microk8s inspect' not working (will raise a different issue on this)
- Additional features installed including 'dashboard' (no apps/customization yet)

Experienced an issue #2507 so went to gather output from 'microk8s inspect'.

However whilst this seemed to work, no file output was evident (could be long location, in vm etc...)

```
jonesn:~/ $ microk8s inspect                                                                                                                                   [17:26:32]
Inspecting Certificates
Inspecting services
  Service snap.microk8s.daemon-cluster-agent is running
  Service snap.microk8s.daemon-containerd is running
  Service snap.microk8s.daemon-apiserver-kicker is running
  Service snap.microk8s.daemon-kubelite is running
  Copy service arguments to the final report tarball
Inspecting AppArmor configuration
Gathering system information
  Copy processes list to the final report tarball
  Copy snap list to the final report tarball
  Copy VM name (or none) to the final report tarball
  Copy disk usage information to the final report tarball
  Copy memory usage information to the final report tarball
  Copy server uptime to the final report tarball
  Copy current linux distribution to the final report tarball
  Copy openSSL information to the final report tarball
  Copy network configuration to the final report tarball
Inspecting kubernetes cluster
  Inspect kubernetes cluster
Inspecting juju
  Inspect Juju
Inspecting kubeflow
  Inspect Kubeflow

Building the report tarball
  Report tarball is at /var/snap/microk8s/2346/inspection-report-20210809_172728.tar.gz
jonesn:~/ $ cp /var/snap/microk8s/2346/inspection-report-20210809_172728.tar.gz ~                                                                              [17:27:28]
cp: /var/snap/microk8s/2346/inspection-report-20210809_172728.tar.gz: No such file or directory
jonesn:~/ $ sudo cp /var/snap/microk8s/2346/inspection-report-20210809_172728.tar.gz ~                                                                         [17:27:33]
Password:
cp: /var/snap/microk8s/2346/inspection-report-20210809_172728.tar.gz: No such file or directory
jonesn:~/ $
```



Discussion:
The reason for this is that the output from the inspection is written into a local filesystem on the vm ie:
```
jonesn:~/ $ multipass shell microk8s-vm                                                                                                                        [17:37:41]
Welcome to Ubuntu 18.04.5 LTS (GNU/Linux 4.15.0-153-generic x86_64)

 * Documentation:  https://help.ubuntu.com
 * Management:     https://landscape.canonical.com
 * Support:        https://ubuntu.com/advantage

  System information as of Mon Aug  9 17:37:56 BST 2021

  System load:  0.54              Processes:             146
  Usage of /:   5.7% of 48.29GB   Users logged in:       0
  Memory usage: 25%               IP address for enp0s2: 192.168.64.2
  Swap usage:   0%


0 updates can be applied immediately.

New release '20.04.2 LTS' available.
Run 'do-release-upgrade' to upgrade to it.


Last login: Mon Aug  9 17:28:21 2021 from 192.168.64.1
ubuntu@microk8s-vm:~$ ls /var/snap
core18  core20  microk8s  microk8s-integrator-macos  snapd
ubuntu@microk8s-vm:~$ ls /var/snap/microk8s/2346/inspection-report-20210809_172728.tar.gz
/var/snap/microk8s/2346/inspection-report-20210809_172728.tar.gz
```

The docs should make this clear - and really on macOS we would hope the 'wrapper' commands would retrieve this to the host?
You are right this is not a good experience. We will need a wrapper of the `microk8s inspect` command for win and mac that would go in [1].

[1] https://github.com/ubuntu/microk8s/blob/master/installer/cli/microk8s.py
@ktsakalozos I would like to work on this issue. Any suggestion on how to go about it?
Hi @farazmd, that's great news!

The inspection command `microk8s inspect` [1] produces a tarball. On Mac and Windows MicroK8s runs in a VM so the inspection tarball is created inside the VM. We need to transfer the inspection tarball out of the VM so that the user can reach it locally.

The windows and MacOS MicroK8s installers puts a script in the user PATH so that when you call `microk8s foo` this command is called in the VM and the output is fetched back to the user. You can see this "proxy" script in [2]. In most cases calling a `microk8s` command is a passthrough, for example `microk8s status` is just forwarded in the `microk8s status` in the VM. In some special cases the `microk8s` script does some extra things, for example the `mcirok8s stop` stops microk8s and the VM it is running on. `microk8s inspect` should be one such special case. `microk8s inspect` should call `microk8s inspect` in side the VM and fetch the created tarball on the host machine. To fetch the inspection tarball you should call `multipass transfer` as the VM is created with multipass.

[1] https://github.com/ubuntu/microk8s/blob/master/scripts/inspect.sh
[2] https://github.com/ubuntu/microk8s/blob/master/installer/cli/microk8s.py

Resolving patch:
diff --git a/installer/cli/microk8s.py b/installer/cli/microk8s.py
--- a/installer/cli/microk8s.py
+++ b/installer/cli/microk8s.py
@@ -3,6 +3,7 @@
 import traceback
 from typing import List
 from sys import exit, platform
+from os import getcwd
 
 import click
 
@@ -56,6 +57,9 @@ def cli(ctx, help):
         elif ctx.args[0] == "dashboard-proxy":
             dashboard_proxy()
             exit(0)
+        elif ctx.args[0] == "inspect":
+            inspect()
+            exit(0)
         else:
             run(ctx.args)
             exit(0)
@@ -211,6 +215,44 @@ def kubectl(args) -> int:
         return Linux(args).kubectl()
 
 
+def inspect() -> None:
+    vm_provider_name = "multipass"
+    vm_provider_class = get_provider_for(vm_provider_name)
+    echo = Echo()
+    try:
+        vm_provider_class.ensure_provider()
+        instance = vm_provider_class(echoer=echo)
+        instance.get_instance_info()
+
+        command = ["microk8s.inspect"]
+        output = instance.run(command, hide_output=True)
+        tarball_location = None
+        host_destination = getcwd()
+        if b"Report tarball is at" not in output:
+            echo.error("Report tarball not generated")
+        else:
+            for line_out in output.split(b"\n"):
+                line_out = line_out.decode()
+                line = line_out.strip()
+                if line.startswith("Report tarball is at "):
+                    tarball_location = line.split("Report tarball is at ")[1]
+                    break
+                echo.wrapped(line_out)
+            if not tarball_location:
+                echo.error("Cannot find tarball file location")
+            else:
+                instance.pull_file(name=tarball_location, destination=host_destination)
+                echo.wrapped(
+                    "The report tarball {} is stored on the current directory".format(
+                        tarball_location.split("/")[-1]
+                    )
+                )
+
+    except ProviderInstanceNotFoundError:
+        _not_installed(echo)
+        return 1
+
+
 def dashboard_proxy() -> None:
     vm_provider_name = "multipass"
     vm_provider_class = get_provider_for(vm_provider_name)


Test patch: