Skip to content
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
39760f3
Adds 'scout' command and 'resources' sub command.
jasonhawkharris Apr 28, 2023
5da36f4
Merge branch 'main' into jhh/src-resource
jasonhawkharris Apr 28, 2023
718560a
This commit changes output pattern from unformatted lines to a table
jasonhawkharris Apr 28, 2023
5340cff
adds unit test for ResroucesK8s
jasonhawkharris May 1, 2023
0bb6274
formatting/retabbing
jasonhawkharris May 1, 2023
2bd386d
Adds --docker flag to print resources of docker deployment
jasonhawkharris May 2, 2023
6dfdfda
edits docs to reflect which sub commands are currently available to use
jasonhawkharris May 2, 2023
5831881
refactor
jasonhawkharris May 2, 2023
a66ed4c
fix build issues
jasonhawkharris May 2, 2023
bd01abc
replace hard coded file path with path generated by homedir package
jasonhawkharris May 2, 2023
3263141
Update cmd/src/scout_resources.go
jasonhawkharris May 2, 2023
94642d9
Update cmd/src/scout_resources.go
jasonhawkharris May 2, 2023
94bcd30
Update internal/scout/resources/resources.go
jasonhawkharris May 2, 2023
3b95e21
Update internal/scout/resources/resources.go
jasonhawkharris May 2, 2023
a1768d4
Update internal/scout/resources/resources.go
jasonhawkharris May 2, 2023
0a9d3c1
don't capitalize error messages
jasonhawkharris May 2, 2023
2780bcc
remove capitalization on error message
jasonhawkharris May 2, 2023
36f2b7a
capitalization
jasonhawkharris May 2, 2023
3163878
add defer functions for closing tabwriter
jasonhawkharris May 2, 2023
3698d1f
capitalization
jasonhawkharris May 2, 2023
faf2878
change function invocations to reflect new function names and retab
jasonhawkharris May 2, 2023
60b8c73
reformats output for --docker flag
jasonhawkharris May 2, 2023
24d2fa5
retab
jasonhawkharris May 2, 2023
d6c9acf
Requested changes
jasonhawkharris May 4, 2023
79eec1a
test skeleton
jasonhawkharris May 11, 2023
c11677c
test for getMemUnits added
jasonhawkharris May 12, 2023
29225df
replace 'spy' with 'advise' in command instructions
jasonhawkharris May 12, 2023
8dc2bab
Add bubbles/bubble tea components to CLI for better UI/UX
jasonhawkharris May 15, 2023
d49544b
remove unnecessary tests. Formatting
jasonhawkharris May 15, 2023
1b4b89d
formatting and removes placeholder code/comments
jasonhawkharris May 15, 2023
21f66e9
remove redundant return statement
jasonhawkharris May 15, 2023
69f4216
fix import order (linter caught)
jasonhawkharris May 16, 2023
e3dddef
remove unnecessary integration test
jasonhawkharris May 16, 2023
3ee7c08
import order
jasonhawkharris May 16, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions cmd/src/scout.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package main

import (
"flag"
"fmt"
)

var scoutCommands commander

func init() {
usage := `'src scout' is a tool that provides monitoring for Sourcegraph resource allocations

EXPERIMENTAL: 'scout' is an experimental command in the 'src' tool. To use, you must
point your .kube config to your Sourcegraph instance.

Usage:

src scout command [command options]

The commands are:

resources print all known sourcegraph resources
estimate (coming soon) reccommend resource allocation for one or many services
usage (coming soon) get CPU, memory and current disk usage
spy (coming soon) track resource usage in real time

Use "src scout [command] -h" for more information about a command.
`

flagSet := flag.NewFlagSet("scout", flag.ExitOnError)
handler := func(args []string) error {
scoutCommands.run(flagSet, "src scout", usage, args)
return nil
}

commands = append(commands, &command{
flagSet: flagSet,
aliases: []string{"scout"},
handler: handler,
usageFunc: func() {
fmt.Println(usage)
},
})
}
98 changes: 98 additions & 0 deletions cmd/src/scout_resources.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
package main

import (
"context"
"flag"
"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"
"k8s.io/client-go/util/homedir"

"github.com/sourcegraph/src-cli/internal/scout/resources"
)

func init() {
usage := `'src scout resources' is a tool that provides an overview of resource usage
across an instance of Sourcegraph. Part of the EXPERIMENTAL "src scout" tool.

Examples
List pods and resource allocations in a Kubernetes deployment:
$ src scout resources

List containers and resource allocations in a Docker deployment:
$ src scout resources --docker

Add namespace if using namespace in a Kubernetes cluster
$ src scout resources --namespace sg
`

flagSet := flag.NewFlagSet("resources", flag.ExitOnError)
usageFunc := func() {
fmt.Fprintf(flag.CommandLine.Output(), "Usage of 'src scout %s':\n", flagSet.Name())
flagSet.PrintDefaults()
fmt.Println(usage)
}

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)")
)

if home := homedir.HomeDir(); home != "" {
kubeConfig = flagSet.String(
"kubeconfig",
filepath.Join(home, ".kube", "config"),
"(optional) absolute path to the kubeconfig file",
)
} else {
kubeConfig = flagSet.String("kubeconfig", "", "absolute path to the kubeconfig file")
}

handler := func(args []string) error {
if err := flagSet.Parse(args); err != nil {
return err
}

config, err := clientcmd.BuildConfigFromFlags("", *kubeConfig)
if err != nil {
// todo: switch out for sourcegraph error package
return errors.New(fmt.Sprintf("%v: failed to load kubernetes config", err))
}

clientSet, err := kubernetes.NewForConfig(config)
if err != nil {
// todo: switch out for sourcegraph error package
return errors.New(fmt.Sprintf("%v: failed to load kubernetes config", err))
}

var options []resources.Option

if *namespace != "" {
options = append(options, resources.WithNamespace(*namespace))
}

if *docker {
dockerClient, err := client.NewClientWithOpts(client.FromEnv)
if err != nil {
return errors.Wrap(err, "Error creating docker client: ")
}

return resources.ResourcesDocker(context.Background(), dockerClient)
}

return resources.ResourcesK8s(context.Background(), clientSet, config, options...)
}

scoutCommands = append(scoutCommands, &command{
flagSet: flagSet,
handler: handler,
usageFunc: usageFunc,
})
}
8 changes: 8 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ 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
Expand Down Expand Up @@ -49,6 +50,7 @@ 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
Expand All @@ -70,6 +72,9 @@ 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
Expand Down Expand Up @@ -115,12 +120,15 @@ require (
github.com/moby/term v0.0.0-20220808134915-39b0c02b01ae // 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/reflow v0.3.0 // indirect
github.com/muesli/termenv v0.12.0 // indirect
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
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
Expand Down
16 changes: 16 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ 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=
Expand Down Expand Up @@ -114,6 +116,14 @@ 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=
Expand Down Expand Up @@ -377,6 +387,8 @@ 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/reflow v0.3.0 h1:IFsN6K9NfGtjeggFP+68I4chLZV2yIKsXJFNZ+eWh6s=
github.com/muesli/reflow v0.3.0/go.mod h1:pbwTDkVPibjO2kyvBQRBxTWEEGDGq0FlB1BIKtnHY/8=
Expand Down Expand Up @@ -410,6 +422,10 @@ github.com/onsi/ginkgo/v2 v2.4.0 h1:+Ig9nvqgS5OBSACXNk15PLdp0U9XPYROt9CFzVdFGIs=
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.23.0 h1:/oxKu9c2HVap+F3PfKort2Hw5DEU+HGlW8n+tguWsys=
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=
Expand Down
Empty file added internal/scout/README.md
Empty file.
Loading