Skip to content

Commit

Permalink
CLOUDP-268554 Update atlas diagnostics command to add more information
Browse files Browse the repository at this point in the history
  • Loading branch information
fmenezes committed Sep 9, 2024
1 parent 6d44014 commit 4dc07d4
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 41 deletions.
64 changes: 43 additions & 21 deletions internal/cli/deployments/diagnostics.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,15 @@ package deployments

import (
"context"
"errors"
"runtime"

"github.com/mongodb/mongodb-atlas-cli/atlascli/internal/cli"
"github.com/mongodb/mongodb-atlas-cli/atlascli/internal/cli/deployments/options"
"github.com/mongodb/mongodb-atlas-cli/atlascli/internal/cli/require"
"github.com/mongodb/mongodb-atlas-cli/atlascli/internal/container"
"github.com/shirou/gopsutil/v4/cpu"
"github.com/shirou/gopsutil/v4/mem"
"github.com/spf13/cobra"
)

Expand All @@ -37,12 +40,14 @@ type diagnostic struct {
Version map[string]any
Images []string
Containers []*containerDiagnostic
Errors []error
Err error
}

type machineDiagnostic struct {
OS string
Arch string
OS string
Arch string
Memory uint64
CPU int
}

type containerDiagnostic struct {
Expand All @@ -59,34 +64,51 @@ func (opts *diagnosticsOpts) Run(ctx context.Context) error {
Engine: opts.ContainerEngine.Name(),
}

images, err := opts.ContainerEngine.ImageList(ctx)
cores, err := cpu.Counts(true)
if err != nil {
d.Errors = append(d.Errors, err)
} else {
for _, image := range images {
d.Images = append(d.Images, image.Names...)
}
d.Err = errors.Join(d.Err, err)
cores = -1
}
d.Machine.CPU = cores

d.Version, err = opts.ContainerEngine.Version(ctx)
v, err := mem.VirtualMemory()
if err != nil {
d.Errors = append(d.Errors, err)
d.Err = errors.Join(d.Err, err)
d.Machine.Memory = 0
} else {
d.Machine.Memory = v.Available
}

inspectData, err := opts.ContainerEngine.ContainerInspect(ctx, opts.LocalMongodHostname())
images, err := opts.ContainerEngine.ImageList(ctx, "mongodb/mongodb-atlas-local")
if err != nil {
d.Errors = append(d.Errors, err)
d.Err = errors.Join(d.Err, err)
} else {
for _, image := range images {
d.Images = append(d.Images, image.Repository+":"+image.Tag)
}
}

logs, err := opts.ContainerEngine.ContainerLogs(ctx, opts.LocalMongodHostname())
d.Version, err = opts.ContainerEngine.Version(ctx)
if err != nil {
d.Errors = append(d.Errors, err)
d.Err = errors.Join(d.Err, err)
}

d.Containers = append(d.Containers, &containerDiagnostic{
Inspect: firstOrNil(inspectData),
Logs: logs,
})
if opts.LocalMongodHostname() != "" {
inspectData, err := opts.ContainerEngine.ContainerInspect(ctx, opts.LocalMongodHostname())
if err != nil {
d.Err = errors.Join(d.Err, err)
}

logs, err := opts.ContainerEngine.ContainerLogs(ctx, opts.LocalMongodHostname())
if err != nil {
d.Err = errors.Join(d.Err, err)
}

d.Containers = append(d.Containers, &containerDiagnostic{
Inspect: firstOrNil(inspectData),
Logs: logs,
})
}

return opts.Print(d)
}
Expand All @@ -106,11 +128,11 @@ func DiagnosticsBuilder() *cobra.Command {
},
}
cmd := &cobra.Command{
Use: "diagnostics <deploymentName>",
Use: "diagnostics [<deploymentName>]",
Short: "Fetch detailed information about all your deployments and system processes.",
Hidden: true, // always hidden
Aliases: []string{"diagnostic", "diag", "diags", "inspect"},
Args: require.ExactArgs(1),
Args: require.MaximumNArgs(1),
Annotations: map[string]string{
"deploymentNameDesc": "Name of the deployment you want to setup.",
},
Expand Down
21 changes: 4 additions & 17 deletions internal/container/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,23 +69,10 @@ type Engine interface {
}

type Image struct {
ID string
RepoTags string
RepoDigests []string
Created int
CreatedAt string
Size int
SharedSize int
VirtualSize int
Labels struct {
Architecture string `json:"architecture"`
BuildDate string `json:"build-date"`
Description string `json:"description"`
Name string `json:"name"`
Version string `json:"version"`
}
Containers int
Names []string
ID string
Repository string
Tag string
Digest string
}

type ImageHealthCheck struct {
Expand Down
26 changes: 23 additions & 3 deletions internal/container/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"encoding/json"
"errors"
"fmt"
"io"
"os/exec"
"strconv"
"strings"
Expand Down Expand Up @@ -376,7 +377,7 @@ func (e *dockerImpl) ContainerInspect(ctx context.Context, names ...string) ([]*
}

func (e *dockerImpl) ImageList(ctx context.Context, references ...string) ([]Image, error) {
args := []string{"image", "ls", "--format", "json"}
args := []string{"image", "ls", "--format", "{{.}}"}

if len(references) > 0 {
for _, name := range references {
Expand All @@ -392,13 +393,32 @@ func (e *dockerImpl) ImageList(ctx context.Context, references ...string) ([]Ima
return nil, nil
}

result := []Image{}
if err := json.Unmarshal(buf, &result); err != nil {
result, err := readJsonl[Image](bytes.NewBuffer(buf))
if err != nil {
return nil, err
}

return result, nil
}

func readJsonl[T any](r io.Reader) ([]T, error) {
data := []T{}
decoder := json.NewDecoder(r)
for decoder.More() {
var entry T
if err := decoder.Decode(&entry); err != nil {
return data, err
}
data = append(data, entry)
}

if len(data) == 0 {
return nil, nil
}

return data, nil
}

func (e *dockerImpl) ImagePull(ctx context.Context, name string) error {
_, err := e.run(ctx, "image", "pull", name)
return err
Expand Down

0 comments on commit 4dc07d4

Please sign in to comment.