-
Notifications
You must be signed in to change notification settings - Fork 70
Adds the src scout utility to the CLI
#978
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
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 5da36f4
Merge branch 'main' into jhh/src-resource
jasonhawkharris 718560a
This commit changes output pattern from unformatted lines to a table
jasonhawkharris 5340cff
adds unit test for ResroucesK8s
jasonhawkharris 0bb6274
formatting/retabbing
jasonhawkharris 2bd386d
Adds --docker flag to print resources of docker deployment
jasonhawkharris 6dfdfda
edits docs to reflect which sub commands are currently available to use
jasonhawkharris 5831881
refactor
jasonhawkharris a66ed4c
fix build issues
jasonhawkharris bd01abc
replace hard coded file path with path generated by homedir package
jasonhawkharris 3263141
Update cmd/src/scout_resources.go
jasonhawkharris 94642d9
Update cmd/src/scout_resources.go
jasonhawkharris 94bcd30
Update internal/scout/resources/resources.go
jasonhawkharris 3b95e21
Update internal/scout/resources/resources.go
jasonhawkharris a1768d4
Update internal/scout/resources/resources.go
jasonhawkharris 0a9d3c1
don't capitalize error messages
jasonhawkharris 2780bcc
remove capitalization on error message
jasonhawkharris 36f2b7a
capitalization
jasonhawkharris 3163878
add defer functions for closing tabwriter
jasonhawkharris 3698d1f
capitalization
jasonhawkharris faf2878
change function invocations to reflect new function names and retab
jasonhawkharris 60b8c73
reformats output for --docker flag
jasonhawkharris 24d2fa5
retab
jasonhawkharris d6c9acf
Requested changes
jasonhawkharris 79eec1a
test skeleton
jasonhawkharris c11677c
test for getMemUnits added
jasonhawkharris 29225df
replace 'spy' with 'advise' in command instructions
jasonhawkharris 8dc2bab
Add bubbles/bubble tea components to CLI for better UI/UX
jasonhawkharris d49544b
remove unnecessary tests. Formatting
jasonhawkharris 1b4b89d
formatting and removes placeholder code/comments
jasonhawkharris 21f66e9
remove redundant return statement
jasonhawkharris 69f4216
fix import order (linter caught)
jasonhawkharris e3dddef
remove unnecessary integration test
jasonhawkharris 3ee7c08
import order
jasonhawkharris File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) | ||
| }, | ||
| }) | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 | ||
jasonhawkharris marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| 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 | ||
jasonhawkharris marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| 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: ") | ||
jasonhawkharris marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| return resources.ResourcesDocker(context.Background(), dockerClient) | ||
| } | ||
|
|
||
| return resources.ResourcesK8s(context.Background(), clientSet, config, options...) | ||
| } | ||
|
|
||
| scoutCommands = append(scoutCommands, &command{ | ||
| flagSet: flagSet, | ||
| handler: handler, | ||
| usageFunc: usageFunc, | ||
| }) | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
jasonhawkharris marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
Empty file.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.