From 81cb1c9fb1d9e8cc7ddb001443e2878735cb227b Mon Sep 17 00:00:00 2001 From: Jason Hawk Harris Date: Fri, 16 Jun 2023 09:19:50 -0500 Subject: [PATCH] remove docker flag from all scout sub-commands (#999) --- cmd/src/scout.go | 5 +- cmd/src/scout_advise.go | 15 ---- cmd/src/scout_resource.go | 14 ---- cmd/src/scout_usage.go | 29 ------- go.mod | 8 -- go.sum | 17 ----- internal/scout/advise/advise.go | 6 -- internal/scout/advise/docker.go | 40 ---------- internal/scout/advise/k8s.go | 7 +- internal/scout/kube/kube.go | 2 +- internal/scout/resource/resource.go | 78 +------------------ internal/scout/types.go | 5 -- internal/scout/usage/docker.go | 114 ---------------------------- internal/scout/usage/k8s.go | 4 - internal/scout/usage/usage.go | 12 --- 15 files changed, 5 insertions(+), 351 deletions(-) delete mode 100644 internal/scout/advise/docker.go delete mode 100644 internal/scout/usage/docker.go diff --git a/cmd/src/scout.go b/cmd/src/scout.go index 0ad74a33c9..d5df574a80 100644 --- a/cmd/src/scout.go +++ b/cmd/src/scout.go @@ -20,9 +20,8 @@ func init() { The commands are: resource print all known sourcegraph resources and their allocations - estimate (coming soon) recommend resource allocation for one or many services - usage (coming soon) get CPU, memory and current disk usage - advise (coming soon) recommend lowering or raising resource allocations based on actual usage + usage get CPU, memory and current disk usage + advise recommend lowering or raising resource allocations based on actual usage Use "src scout [command] -h" for more information about a command. ` diff --git a/cmd/src/scout_advise.go b/cmd/src/scout_advise.go index d58294511b..8deef42a8c 100644 --- a/cmd/src/scout_advise.go +++ b/cmd/src/scout_advise.go @@ -6,7 +6,6 @@ import ( "fmt" "path/filepath" - "github.com/docker/docker/client" "github.com/sourcegraph/src-cli/internal/scout/advise" "k8s.io/client-go/kubernetes" "k8s.io/client-go/tools/clientcmd" @@ -45,9 +44,7 @@ func init() { kubeConfig *string namespace = flagSet.String("namespace", "", "(optional) specify the kubernetes namespace to use") pod = flagSet.String("pod", "", "(optional) specify a single pod") - container = flagSet.String("container", "", "(optional) specify a single container") output = flagSet.String("o", "", "(optional) output advice to file") - docker = flagSet.Bool("docker", false, "(optional) using docker deployment") ) if home := homedir.HomeDir(); home != "" { @@ -91,18 +88,6 @@ func init() { if *output != "" { options = append(options, advise.WithOutput(*output)) } - if *container != "" || *docker { - if *container != "" { - options = append(options, advise.WithContainer(*container)) - } - - dockerClient, err := client.NewClientWithOpts(client.FromEnv) - if err != nil { - return errors.Wrap(err, "error creating docker client: ") - } - - return advise.Docker(context.Background(), *dockerClient, options...) - } return advise.K8s( context.Background(), diff --git a/cmd/src/scout_resource.go b/cmd/src/scout_resource.go index 32accda5f6..f4c8cf2b5b 100644 --- a/cmd/src/scout_resource.go +++ b/cmd/src/scout_resource.go @@ -6,7 +6,6 @@ import ( "fmt" "path/filepath" - "github.com/docker/docker/client" "github.com/sourcegraph/sourcegraph/lib/errors" "k8s.io/client-go/kubernetes" "k8s.io/client-go/tools/clientcmd" @@ -23,9 +22,6 @@ func init() { List pods and resource allocations in a Kubernetes deployment: $ src scout resource - List containers and resource allocations in a Docker deployment: - $ src scout resource --docker - Add namespace if using namespace in a Kubernetes cluster $ src scout resource --namespace sg ` @@ -40,7 +36,6 @@ func init() { var ( kubeConfig *string namespace = flagSet.String("namespace", "", "(optional) specify the kubernetes namespace to use") - docker = flagSet.Bool("docker", false, "(optional) using docker deployment") // TODO: option for getting resource allocation of the Node // nodes = flagSet.Bool("node", false, "(optional) view resources for node(s)") ) @@ -76,15 +71,6 @@ func init() { options = append(options, resource.WithNamespace(*namespace)) } - if *docker { - dockerClient, err := client.NewClientWithOpts(client.FromEnv) - if err != nil { - return errors.Wrap(err, "error creating docker client: ") - } - - return resource.Docker(context.Background(), *dockerClient) - } - return resource.K8s(context.Background(), clientSet, config, options...) } diff --git a/cmd/src/scout_usage.go b/cmd/src/scout_usage.go index 1ac7fc4257..ba12fd6c11 100644 --- a/cmd/src/scout_usage.go +++ b/cmd/src/scout_usage.go @@ -6,7 +6,6 @@ import ( "fmt" "path/filepath" - "github.com/docker/docker/client" "github.com/sourcegraph/sourcegraph/lib/errors" "github.com/sourcegraph/src-cli/internal/scout/usage" "k8s.io/client-go/kubernetes" @@ -23,20 +22,11 @@ func init() { List pods and resource usage in a Kubernetes deployment: $ src scout usage - List containers and resource usage in a Docker deployment: - $ src scout usage --docker - Check usage for specific pod $ src scout usage --pod - Check usage for specific container (docker only) - $ src scout usage --container - Add namespace if using namespace in a Kubernetes cluster $ src scout usage --namespace - - Watch usage in real time - $ src scout usage --spy ` flagSet := flag.NewFlagSet("usage", flag.ExitOnError) @@ -50,9 +40,6 @@ func init() { kubeConfig *string namespace = flagSet.String("namespace", "", "(optional) specify the kubernetes namespace to use") pod = flagSet.String("pod", "", "(optional) specify a single pod") - container = flagSet.String("container", "", "(optional) specify a single container") - docker = flagSet.Bool("docker", false, "(optional) using docker deployment") - spy = flagSet.Bool("spy", false, "(optional) see resource usage in real time") ) if home := homedir.HomeDir(); home != "" { @@ -86,28 +73,12 @@ func init() { } var options []usage.Option - if *namespace != "" { options = append(options, usage.WithNamespace(*namespace)) } - if *spy { - options = append(options, usage.WithSpy(true)) - } if *pod != "" { options = append(options, usage.WithPod(*pod)) } - if *container != "" || *docker { - if *container != "" { - options = append(options, usage.WithContainer(*container)) - } - - dockerClient, err := client.NewClientWithOpts(client.FromEnv) - if err != nil { - return errors.Wrap(err, "error creating docker client: ") - } - - return usage.Docker(context.Background(), *dockerClient, options...) - } return usage.K8s( context.Background(), diff --git a/go.mod b/go.mod index 15e9617bf5..c617b7e784 100644 --- a/go.mod +++ b/go.mod @@ -13,7 +13,6 @@ require ( github.com/creack/goselect v0.1.2 github.com/derision-test/glock v0.0.0-20210316032053-f5b74334bb29 github.com/dineshappavoo/basex v0.0.0-20170425072625-481a6f6dc663 - github.com/docker/docker v23.0.5+incompatible github.com/dustin/go-humanize v1.0.1 github.com/gobwas/glob v0.2.3 github.com/google/go-cmp v0.5.9 @@ -60,7 +59,6 @@ require ( github.com/Masterminds/goutils v1.1.1 // indirect github.com/Masterminds/semver v1.5.0 // indirect github.com/Masterminds/sprig v2.22.0+incompatible // indirect - github.com/Microsoft/go-winio v0.6.1 // indirect github.com/alecthomas/chroma v0.10.0 // indirect github.com/aws/aws-sdk-go-v2 v1.17.5 // indirect github.com/aws/aws-sdk-go-v2/credentials v1.13.13 // indirect @@ -85,9 +83,6 @@ require ( github.com/cpuguy83/go-md2man/v2 v2.0.2 // indirect github.com/davecgh/go-spew v1.1.1 // indirect github.com/dlclark/regexp2 v1.7.0 // indirect - github.com/docker/distribution v2.8.1+incompatible // indirect - github.com/docker/go-connections v0.4.0 // indirect - github.com/docker/go-units v0.5.0 // indirect github.com/emicklei/go-restful/v3 v3.9.0 // indirect github.com/envoyproxy/protoc-gen-validate v0.9.1 // indirect github.com/fatih/color v1.13.0 // indirect @@ -134,7 +129,6 @@ require ( github.com/moby/term v0.0.0-20221205130635-1aeaba878587 // indirect github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect github.com/modern-go/reflect2 v1.0.2 // indirect - github.com/morikuni/aec v1.0.0 // indirect github.com/muesli/ansi v0.0.0-20211018074035-2e021307bc4b // indirect github.com/muesli/cancelreader v0.2.2 // indirect github.com/muesli/reflow v0.3.0 // indirect @@ -143,8 +137,6 @@ require ( github.com/mwitkow/go-proto-validators v0.3.2 // indirect github.com/nightlyone/lockfile v1.0.0 // indirect github.com/olekukonko/tablewriter v0.0.5 // indirect - github.com/opencontainers/go-digest v1.0.0 // indirect - github.com/opencontainers/image-spec v1.0.2 // indirect github.com/pkg/errors v0.9.1 // indirect github.com/pkg/profile v1.6.0 // indirect github.com/pmezard/go-difflib v1.0.0 // indirect diff --git a/go.sum b/go.sum index cb2ad81c22..e6721f4ea6 100644 --- a/go.sum +++ b/go.sum @@ -28,8 +28,6 @@ github.com/Masterminds/semver v1.5.0 h1:H65muMkzWKEuNDnfl9d70GUjFniHKHRbFPGBuZ3Q github.com/Masterminds/semver v1.5.0/go.mod h1:MB6lktGJrhw8PrUyiEoblNEGEQ+RzHPF078ddwwvV3Y= github.com/Masterminds/sprig v2.22.0+incompatible h1:z4yfnGrZ7netVz+0EDJ0Wi+5VZCSYp4Z0m2dk6cEM60= github.com/Masterminds/sprig v2.22.0+incompatible/go.mod h1:y6hNFY5UBTIWBxnzTeuNhlNS5hqE0NB0E6fgfo2Br3o= -github.com/Microsoft/go-winio v0.6.1 h1:9/kr64B9VUZrLm5YYwbGtUJnMgqWVOdUAXu6Migciow= -github.com/Microsoft/go-winio v0.6.1/go.mod h1:LRdKpFKfdobln8UmuiYcKPot9D2v6svN5+sAH+4kjUM= github.com/Shopify/goreferrer v0.0.0-20181106222321-ec9c9a553398/go.mod h1:a1uqRtAwp2Xwc6WNPJEufxJ7fx3npB4UV/JOLmbu5I0= github.com/ajg/form v1.5.1/go.mod h1:uL1WgH+h2mgNtvBq0339dVnzXdBETtL2LeUXaIv25UY= github.com/alecthomas/chroma v0.10.0 h1:7XDcGkCQopCNKjZHfYrNLraA+M7e0fMiJ/Mfikbfjek= @@ -131,14 +129,6 @@ github.com/dineshappavoo/basex v0.0.0-20170425072625-481a6f6dc663/go.mod h1:Kad2 github.com/dlclark/regexp2 v1.4.0/go.mod h1:2pZnwuY/m+8K6iRw6wQdMtk+rH5tNGR1i55kozfMjCc= github.com/dlclark/regexp2 v1.7.0 h1:7lJfhqlPssTb1WQx4yvTHN0uElPEv52sbaECrAQxjAo= github.com/dlclark/regexp2 v1.7.0/go.mod h1:DHkYz0B9wPfa6wondMfaivmHpzrQ3v9q8cnmRbL6yW8= -github.com/docker/distribution v2.8.1+incompatible h1:Q50tZOPR6T/hjNsyc9g8/syEs6bk8XXApsHjKukMl68= -github.com/docker/distribution v2.8.1+incompatible/go.mod h1:J2gT2udsDAN96Uj4KfcMRqY0/ypR+oyYUYmja8H+y+w= -github.com/docker/docker v23.0.5+incompatible h1:DaxtlTJjFSnLOXVNUBU1+6kXGz2lpDoEAH6QoxaSg8k= -github.com/docker/docker v23.0.5+incompatible/go.mod h1:eEKB0N0r5NX/I1kEveEz05bcu8tLC/8azJZsviup8Sk= -github.com/docker/go-connections v0.4.0 h1:El9xVISelRB7BuFusrZozjnkIM5YnzCViNKohAFqRJQ= -github.com/docker/go-connections v0.4.0/go.mod h1:Gbd7IOopHjR8Iph03tsViu4nIes5XhDvyHbTtUxmeec= -github.com/docker/go-units v0.5.0 h1:69rxXcBk27SvSaaxTtLh/8llcHD8vYHT7WSdRZ/jvr4= -github.com/docker/go-units v0.5.0/go.mod h1:fgPhTUdO+D/Jk86RDLlptpiXQzgHJF7gydDDbaIK4Dk= github.com/docopt/docopt-go v0.0.0-20180111231733-ee0de3bc6815/go.mod h1:WwZ+bS3ebgob9U8Nd0kOddGdZWjyMGR8Wziv+TBNwSE= github.com/dustin/go-humanize v1.0.0/go.mod h1:HtrtbFcZ19U5GC7JDqmcUSB87Iq5E25KnS6fMYU6eOk= github.com/dustin/go-humanize v1.0.1 h1:GzkhY7T5VNhEkwH0PVJgjz+fX1rhBrR7pRT3mDkpeCY= @@ -405,8 +395,6 @@ github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742/go.mod h1:bx2lN github.com/modern-go/reflect2 v1.0.1/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0= github.com/modern-go/reflect2 v1.0.2 h1:xBagoLtFs94CBntxluKeaWgTMpvLxC4ur3nMaC9Gz0M= github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk= -github.com/morikuni/aec v1.0.0 h1:nP9CBfwrvYnBRgY6qfDQkygYDmYwOilePFkwzv4dU8A= -github.com/morikuni/aec v1.0.0/go.mod h1:BbKIizmSmc5MMPqRYbxO4ZU0S0+P200+tUnFx7PXmsc= github.com/moul/http2curl v1.0.0/go.mod h1:8UbvGypXm98wA/IqH45anm5Y2Z6ep6O31QGOAZ3H0fQ= github.com/muesli/ansi v0.0.0-20211018074035-2e021307bc4b h1:1XF24mVaiu7u+CFywTdcDo2ie1pzzhwjt6RHqzpMU34= github.com/muesli/ansi v0.0.0-20211018074035-2e021307bc4b/go.mod h1:fQuZ0gauxyBcmsdE3ZT4NasjaRdxmbCS0jRHsrWu3Ho= @@ -447,10 +435,6 @@ github.com/onsi/ginkgo/v2 v2.9.1 h1:zie5Ly042PD3bsCvsSOPvRnFwyo3rKe64TJlD6nu0mk= github.com/onsi/gomega v1.7.1/go.mod h1:XdKZgCCFLUoM/7CFJVPcG8C1xQ1AJ0vpAezJrB7JYyY= github.com/onsi/gomega v1.10.1/go.mod h1:iN09h71vgCQne3DLsj+A5owkum+a2tYe+TOCB1ybHNo= github.com/onsi/gomega v1.27.4 h1:Z2AnStgsdSayCMDiCU42qIz+HLqEPcgiOCXjAU/w+8E= -github.com/opencontainers/go-digest v1.0.0 h1:apOUWs51W5PlhuyGyz9FCeeBIOUDA/6nW8Oi/yOhh5U= -github.com/opencontainers/go-digest v1.0.0/go.mod h1:0JzlMkj0TRzQZfJkVvzbP0HBR3IKzErnv2BNG4W4MAM= -github.com/opencontainers/image-spec v1.0.2 h1:9yCKha/T5XdGtO0q9Q9a6T5NUCsTn/DrBg0D7ufOcFM= -github.com/opencontainers/image-spec v1.0.2/go.mod h1:BtxoFyWECRxE4U/7sNtV5W15zMzWCbyJoFRP3s7yZA0= github.com/pelletier/go-toml v1.2.0/go.mod h1:5z9KED0ma1S8pY6P1sdut58dfprrGBbd/94hg7ilaic= github.com/pingcap/errors v0.11.4 h1:lFuQV/oaUMGcD2tqt+01ROSmJs75VG1ToEOkZIZ4nE4= github.com/pingcap/errors v0.11.4/go.mod h1:Oi8TUi2kEtXXLMJk9l1cGmz20kV3TaQ0usTwv5KuLY8= @@ -813,7 +797,6 @@ gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C gopkg.in/yaml.v3 v3.0.0/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= -gotest.tools/v3 v3.0.3 h1:4AuOwCGf4lLR9u3YOe2awrHygurzhO/HeQ6laiA6Sx0= honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4= honnef.co/go/tools v0.0.0-20190523083050-ea95bdfd59fc/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4= jaytaylor.com/html2text v0.0.0-20200412013138-3577fbdbcff7 h1:mub0MmFLOn8XLikZOAhgLD1kXJq8jgftSrrv7m00xFo= diff --git a/internal/scout/advise/advise.go b/internal/scout/advise/advise.go index ac08b73ee5..bdcd977acc 100644 --- a/internal/scout/advise/advise.go +++ b/internal/scout/advise/advise.go @@ -23,12 +23,6 @@ func WithPod(podname string) Option { } } -func WithContainer(containerName string) Option { - return func(config *scout.Config) { - config.Container = containerName - } -} - func WithOutput(pathToFile string) Option { return func(config *scout.Config) { config.Output = pathToFile diff --git a/internal/scout/advise/docker.go b/internal/scout/advise/docker.go deleted file mode 100644 index 319a5ceb4e..0000000000 --- a/internal/scout/advise/docker.go +++ /dev/null @@ -1,40 +0,0 @@ -package advise - -import ( - "context" - "fmt" - - "github.com/docker/docker/api/types" - "github.com/docker/docker/client" - "github.com/sourcegraph/sourcegraph/lib/errors" - "github.com/sourcegraph/src-cli/internal/scout" -) - -func Docker(ctx context.Context, client client.Client, opts ...Option) error { - cfg := &scout.Config{ - Namespace: "default", - Docker: true, - Pod: "", - Container: "", - Spy: false, - DockerClient: &client, - } - - for _, opt := range opts { - opt(cfg) - } - - containers, err := client.ContainerList(ctx, types.ContainerListOptions{}) - if err != nil { - return errors.Wrap(err, "could not get list of containers") - } - - PrintContainers(containers) - return nil -} - -func PrintContainers(containers []types.Container) { - for _, c := range containers { - fmt.Println(c.Image) - } -} diff --git a/internal/scout/advise/k8s.go b/internal/scout/advise/k8s.go index eb0b505c70..f10e58924f 100644 --- a/internal/scout/advise/k8s.go +++ b/internal/scout/advise/k8s.go @@ -5,7 +5,6 @@ import ( "fmt" "os" - "github.com/sourcegraph/sourcegraph/lib/errors" "github.com/sourcegraph/src-cli/internal/scout" "github.com/sourcegraph/src-cli/internal/scout/kube" @@ -25,13 +24,9 @@ func K8s( cfg := &scout.Config{ Namespace: "default", Pod: "", - Container: "", Output: "", - Spy: false, - Docker: false, RestConfig: restConfig, K8sClient: k8sClient, - DockerClient: nil, MetricsClient: metricsClient, } @@ -94,7 +89,7 @@ func Advise(ctx context.Context, cfg *scout.Config, pod v1.Pod) error { if cfg.Output != "" { outputToFile(ctx, cfg, pod, advice) } else { - for _, msg := range advice { + for _, msg := range advice { fmt.Println(msg) } } diff --git a/internal/scout/kube/kube.go b/internal/scout/kube/kube.go index 5e5523266d..7dc0a6f0e0 100644 --- a/internal/scout/kube/kube.go +++ b/internal/scout/kube/kube.go @@ -105,7 +105,6 @@ func AddLimits(ctx context.Context, cfg *scout.Config, pod *corev1.Pod, containe } containerMetrics.Limits[containerName] = rsrcs } - return nil } @@ -287,6 +286,7 @@ func GetStorageUsage( "worker", } + // if pod is stateless, return 0 for capacity and usage if scout.Contains(stateless, containerName) { return storageCapacity, storageUsage, nil } diff --git a/internal/scout/resource/resource.go b/internal/scout/resource/resource.go index ddfd99da8a..6be8a873e0 100644 --- a/internal/scout/resource/resource.go +++ b/internal/scout/resource/resource.go @@ -7,11 +7,9 @@ import ( "github.com/charmbracelet/bubbles/table" "github.com/charmbracelet/lipgloss" - "github.com/docker/docker/api/types" - "github.com/docker/docker/client" "github.com/sourcegraph/sourcegraph/lib/errors" "github.com/sourcegraph/src-cli/internal/scout" - kube "github.com/sourcegraph/src-cli/internal/scout/kube" + "github.com/sourcegraph/src-cli/internal/scout/kube" "github.com/sourcegraph/src-cli/internal/scout/style" "k8s.io/client-go/kubernetes" "k8s.io/client-go/rest" @@ -29,13 +27,9 @@ func WithNamespace(namespace string) Option { func K8s(ctx context.Context, clientSet *kubernetes.Clientset, restConfig *rest.Config, opts ...Option) error { cfg := &scout.Config{ Namespace: "default", - Docker: false, Pod: "", - Container: "", - Spy: false, RestConfig: restConfig, K8sClient: clientSet, - DockerClient: nil, MetricsClient: nil, } @@ -106,76 +100,6 @@ func listPodResources(ctx context.Context, cfg *scout.Config) error { return nil } -// Docker prints the CPU and memory resource limits and requests for running Docker containers. -func Docker(ctx context.Context, dockerClient client.Client) error { - containers, err := dockerClient.ContainerList(ctx, types.ContainerListOptions{}) - if err != nil { - return fmt.Errorf("error listing docker containers: %v", err) - } - - if len(containers) == 0 { - msg := lipgloss.NewStyle().Foreground(lipgloss.Color("#FFA500")) - fmt.Println(msg.Render(` - There are no containers, or the Docker Daemon is not running - `)) - os.Exit(1) - } - - columns := []table.Column{ - {Title: "Container", Width: 20}, - {Title: "CPU Cores", Width: 15}, - {Title: "CPU Shares", Width: 15}, - {Title: "Mem Limits", Width: 15}, - {Title: "Mem Reservations", Width: 17}, - } - - var rows []table.Row - - for _, container := range containers { - containerInfo, err := dockerClient.ContainerInspect(ctx, container.ID) - if err != nil { - return fmt.Errorf("error inspecting container %s: %v", container.ID, err) - } - - row, err := getResourceInfo(&containerInfo, rows) - if err != nil { - return errors.Wrap(err, "error while getting resource info from container: ") - } - - rows = append(rows, row) - } - - style.ResourceTable(columns, rows) - return nil -} - -func getResourceInfo(container *types.ContainerJSON, rows []table.Row) (table.Row, error) { - cpuCores := container.HostConfig.NanoCPUs - cpuShares := container.HostConfig.CPUShares - memLimits := container.HostConfig.Memory - memReservations := container.HostConfig.MemoryReservation - - reqUnit, reqVal, err := getMemUnits(memReservations) - if err != nil { - return table.Row{}, errors.Wrap(err, "error while getting request units") - } - - limUnit, limVal, err := getMemUnits(memLimits) - if err != nil { - return table.Row{}, errors.Wrap(err, "error while getting limit units") - } - - row := table.Row{ - container.Name, - fmt.Sprintf("%v", float64(cpuCores/1e9)), - fmt.Sprint(cpuShares), - fmt.Sprintf("%d%s", limVal, limUnit), - fmt.Sprintf("%d%s", reqVal, reqUnit), - } - - return row, nil -} - // getMemUnits converts a byte value to the appropriate memory unit. func getMemUnits(valToConvert int64) (string, int64, error) { if valToConvert < 0 { diff --git a/internal/scout/types.go b/internal/scout/types.go index 5934f8804b..9703fac20c 100644 --- a/internal/scout/types.go +++ b/internal/scout/types.go @@ -1,7 +1,6 @@ package scout import ( - "github.com/docker/docker/client" "k8s.io/apimachinery/pkg/api/resource" "k8s.io/client-go/kubernetes" "k8s.io/client-go/rest" @@ -11,13 +10,9 @@ import ( type Config struct { Namespace string Pod string - Container string Output string - Spy bool - Docker bool RestConfig *rest.Config K8sClient *kubernetes.Clientset - DockerClient *client.Client MetricsClient *metricsv.Clientset } diff --git a/internal/scout/usage/docker.go b/internal/scout/usage/docker.go deleted file mode 100644 index c46cded08e..0000000000 --- a/internal/scout/usage/docker.go +++ /dev/null @@ -1,114 +0,0 @@ -package usage - -import ( - "context" - "encoding/json" - "fmt" - "os" - - "github.com/charmbracelet/bubbles/table" - "github.com/charmbracelet/lipgloss" - "github.com/docker/docker/api/types" - "github.com/docker/docker/client" - "github.com/sourcegraph/sourcegraph/lib/errors" - "github.com/sourcegraph/src-cli/internal/scout" - "github.com/sourcegraph/src-cli/internal/scout/style" -) - -func Docker(ctx context.Context, client client.Client, opts ...Option) error { - cfg := &scout.Config{ - Namespace: "default", - Docker: true, - Pod: "", - Container: "", - Spy: false, - DockerClient: &client, - } - - for _, opt := range opts { - opt(cfg) - } - - containers, err := cfg.DockerClient.ContainerList(ctx, types.ContainerListOptions{}) - if err != nil { - return errors.Wrap(err, "could not get list of containers") - } - - return renderDockerUsageTable(ctx, cfg, containers) -} - -// renderDockerUsageTable renders a table displaying CPU and memory usage for Docker containers. -func renderDockerUsageTable(ctx context.Context, cfg *scout.Config, containers []types.Container) error { - columns := []table.Column{ - {Title: "Container", Width: 20}, - {Title: "Cores", Width: 10}, - {Title: "Usage", Width: 10}, - {Title: "Memory", Width: 10}, - {Title: "Usage", Width: 10}, - } - rows := []table.Row{} - - for _, container := range containers { - containerInfo, err := cfg.DockerClient.ContainerInspect(ctx, container.ID) - if err != nil { - return errors.Wrap(err, "failed to get container info") - } - - if cfg.Container != "" { - if containerInfo.Name == cfg.Container { - row := makeDockerUsageRow(ctx, cfg, containerInfo) - rows = append(rows, row) - break - } else { - continue - } - } - - row := makeDockerUsageRow(ctx, cfg, containerInfo) - rows = append(rows, row) - } - - if len(rows) == 0 { - msg := lipgloss.NewStyle().Foreground(lipgloss.Color("#FFA500")) - if cfg.Container == "" { - fmt.Println(msg.Render(`No docker containers are running.`)) - os.Exit(1) - } - fmt.Println(msg.Render( - fmt.Sprintf(`No container with name '%s' running.`, cfg.Container), - )) - os.Exit(1) - } - - style.UsageTable(columns, rows) - return nil -} - -// makeDockerUsageRow generates a table row displaying CPU and memory usage for a Docker container. -func makeDockerUsageRow(ctx context.Context, cfg *scout.Config, container types.ContainerJSON) table.Row { - stats, err := cfg.DockerClient.ContainerStats(ctx, container.ID, false) - if err != nil { - errors.Wrap(err, "could not get container stats") - os.Exit(1) - } - defer func() { _ = stats.Body.Close() }() - - var usage types.StatsJSON - if err := json.NewDecoder(stats.Body).Decode(&usage); err != nil { - errors.Wrap(err, "could not get container stats") - os.Exit(1) - } - - cpuCores := float64(container.HostConfig.NanoCPUs) - memory := float64(container.HostConfig.Memory) - cpuUsage := float64(usage.CPUStats.CPUUsage.TotalUsage) - memoryUsage := float64(usage.MemoryStats.Usage) - - return table.Row{ - container.Name, - fmt.Sprintf("%.2f", cpuCores/1_000_000_000), - fmt.Sprintf("%.2f%%", scout.GetPercentage(cpuUsage, cpuCores)), - fmt.Sprintf("%.2fG", memory/1_000_000_000), - fmt.Sprintf("%.2f%%", scout.GetPercentage(memoryUsage, memory)), - } -} diff --git a/internal/scout/usage/k8s.go b/internal/scout/usage/k8s.go index 1ca9977eba..c3805938a0 100644 --- a/internal/scout/usage/k8s.go +++ b/internal/scout/usage/k8s.go @@ -25,13 +25,9 @@ func K8s( ) error { cfg := &scout.Config{ Namespace: "default", - Docker: false, Pod: "", - Container: "", - Spy: false, RestConfig: restConfig, K8sClient: clientSet, - DockerClient: nil, MetricsClient: metricsClient, } diff --git a/internal/scout/usage/usage.go b/internal/scout/usage/usage.go index fbdaad3a72..df4cb31b7a 100644 --- a/internal/scout/usage/usage.go +++ b/internal/scout/usage/usage.go @@ -17,15 +17,3 @@ func WithPod(podname string) Option { config.Pod = podname } } - -func WithContainer(containerName string) Option { - return func(config *scout.Config) { - config.Container = containerName - } -} - -func WithSpy(spy bool) Option { - return func(config *scout.Config) { - config.Spy = true - } -}