Skip to content

Commit

Permalink
Add k8s inspect command
Browse files Browse the repository at this point in the history
We're adding a "k8s inspect" command that will invoke the "inspect.sh"
script, aiming to improve the user experience.

Note that we'll avoid parsing the arguments twice since that's
unnecessary and would complicate the process of adding new
parameters.
  • Loading branch information
petrutlucian94 committed Jan 28, 2025
1 parent afa585a commit 7e7162b
Show file tree
Hide file tree
Showing 7 changed files with 82 additions and 8 deletions.
8 changes: 4 additions & 4 deletions docs/src/charm/howto/troubleshooting.md
Original file line number Diff line number Diff line change
Expand Up @@ -201,17 +201,17 @@ kubectl --kubeconfig cluster-kubeconfig.yaml logs <pod-name> -n <namespace>
You can check out the upstream [debug pods documentation][] for more
information.

## Use the built-in inspection script
## Use the built-in inspection command

{{product}} ships with a script to compile a complete report on {{product}} and
{{product}} ships with a command to compile a complete report on {{product}} and
its underlying system. This is an essential tool for bug reports and for
investigating whether a system is (or isn’t) working.

Inspection script can be executed on a specific unit by running the following
The inspection command can be executed on a specific unit by running the following
commands:

```
juju exec --unit <k8s/unit#> -- sudo /snap/k8s/current/k8s/scripts/inspect.sh /home/ubuntu/inspection-report.tar.gz
juju exec --unit <k8s/unit#> -- sudo k8s inspect /home/ubuntu/inspection-report.tar.gz
juju scp <k8s/unit#>:/home/ubuntu/inspection-report.tar.gz ./
```

Expand Down
8 changes: 4 additions & 4 deletions docs/src/snap/howto/troubleshooting.md
Original file line number Diff line number Diff line change
Expand Up @@ -175,17 +175,17 @@ sudo k8s kubectl logs <pod-name> -n <namespace>
You can check out the upstream [debug pods documentation][] for more
information.

## Using the built-in inspection script
## Using the built-in inspection command

{{product}} ships with a script to compile a complete report on {{product}} and
{{product}} ships with a command to compile a complete report on {{product}} and
its underlying system. This is an essential tool for bug reports and for
investigating whether a system is (or isn’t) working.

Run the inspection script, by entering the command (admin privileges are
Run the inspection command, by entering the command (admin privileges are
required to collect all the data):

```
sudo /snap/k8s/current/k8s/scripts/inspect.sh
sudo k8s inspect
```

The command output is similar to the following:
Expand Down
1 change: 1 addition & 0 deletions src/k8s/cmd/k8s/k8s.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ func NewRootCmd(env cmdutil.ExecutionEnvironment) *cobra.Command {
newRefreshCertsCmd(env),
newSetCmd(env),
newGetCmd(env),
newInspectCmd(env),
)

// hidden commands
Expand Down
51 changes: 51 additions & 0 deletions src/k8s/cmd/k8s/k8s_inspect.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package k8s

import (
"syscall"

cmdutil "github.com/canonical/k8s/cmd/util"
"github.com/spf13/cobra"
)

func newInspectCmd(env cmdutil.ExecutionEnvironment) *cobra.Command {
// We're copying the help string from the "inspect.sh" script with
// only minor adjustments. At the same time, we'll avoid parsing the
// same arguments twice.
return &cobra.Command{
Use: "inspect <output-file>",
Short: "generate inspection report",
Long: `
This command collects diagnostics and other relevant information from a Kubernetes
node (either control-plane or worker node) and compiles them into a tarball report.
The collected data includes service arguments, Kubernetes cluster info, SBOM, system
diagnostics, network diagnostics, and more. The command needs to be run with
elevated permissions (sudo).
Arguments:
output_file (Optional) The full path and filename for the generated tarball.
If not provided, a default filename based on the current date
and time will be used.
--all-namespaces (Optional) Acquire detailed debugging information, including logs
from all Kubernetes namespaces.
--num-snap-log-entries (Optional) The maximum number of log entries to collect
from snap services. Default: 100000.
--timeout (Optional) The maximum time in seconds to wait for a command.
Default: 180s.
`,
DisableFlagParsing: true,
PreRun: chainPreRunHooks(hookRequireRoot(env)),
Run: func(cmd *cobra.Command, args []string) {
inspectScriptPath := env.Snap.K8sInspectScriptPath()

command := append([]string{inspectScriptPath}, args...)
environ := cmdutil.EnvironWithDefaults(
env.Environ,
)
if err := syscall.Exec(inspectScriptPath, command, environ); err != nil {
cmd.PrintErrf("Failed to run %s.\n\nError: %v\n", command, err)
env.Exit(1)
return
}
},
}
}
3 changes: 3 additions & 0 deletions src/k8s/pkg/snap/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ type Snap interface {
ContainerdSocketPath() string // classic confinement: /run/containerd/containerd.sock, strict confinement: /var/snap/k8s/common/run/containerd/containerd.sock
ContainerdStateDir() string // classic confinement: /run/containerd, strict confinement: /var/snap/k8s/common/run/containerd

K8sScriptsDir() string // /snap/k8s/current/k8s/scripts
K8sInspectScriptPath() string // /snap/k8s/current/k8s/scripts/inspect.sh

K8sdStateDir() string // /var/snap/k8s/common/var/lib/k8sd/state
K8sDqliteStateDir() string // /var/snap/k8s/common/var/lib/k8s-dqlite

Expand Down
11 changes: 11 additions & 0 deletions src/k8s/pkg/snap/mock/mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ type Mock struct {
ContainerdSocketDir string
ContainerdSocketPath string
ContainerdStateDir string
K8sScriptsDir string
K8sInspectScriptPath string
K8sdStateDir string
K8sDqliteStateDir string
ServiceArgumentsDir string
Expand Down Expand Up @@ -168,6 +170,15 @@ func (s *Snap) ContainerdRegistryConfigDir() string {
return s.Mock.ContainerdRegistryConfigDir
}

func (s *snap) K8sScriptsDir() string {
return s.Mock.K8sScriptsDir
}

func (s *snap) K8sInspectScriptPath() string {
return s.Mock.K8sInspectScriptPath
}


func (s *Snap) KubernetesConfigDir() string {
return s.Mock.KubernetesConfigDir
}
Expand Down
8 changes: 8 additions & 0 deletions src/k8s/pkg/snap/snap.go
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,14 @@ func (s *snap) ContainerdRegistryConfigDir() string {
return filepath.Join(s.containerdBaseDir, "etc", "containerd", "hosts.d")
}

func (s *snap) K8sScriptsDir() string {
return filepath.Join(s.snapDir, "k8s", "scripts")
}

func (s *snap) K8sInspectScriptPath() string {
return filepath.Join(s.K8sScriptsDir(), "inspect.sh")
}

func (s *snap) restClientGetter(path string, namespace string) genericclioptions.RESTClientGetter {
flags := &genericclioptions.ConfigFlags{
KubeConfig: utils.Pointer(path),
Expand Down

0 comments on commit 7e7162b

Please sign in to comment.