-
Notifications
You must be signed in to change notification settings - Fork 70
add usage command to 'src scout' #983
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 55 commits
Commits
Show all changes
63 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 ee1e64e
export to file now works for docker as well.
jasonhawkharris 575a290
refactor
jasonhawkharris 941bc31
add file export functionality for docker
jasonhawkharris 20a6c47
fix errors linter caught
jasonhawkharris 69f4216
fix import order (linter caught)
jasonhawkharris e3dddef
remove unnecessary integration test
jasonhawkharris 3ee7c08
import order
jasonhawkharris a0695a0
fix merge conflict
jasonhawkharris d5ce0af
add the ability to dump all container resource data txt file
jasonhawkharris cee0234
fix merge conflicts
jasonhawkharris dbc69f0
Merge branch 'main' into jhh/src-resource
jasonhawkharris 992739c
Merge branch 'jhh/copy-all' into jhh/src-resource
jasonhawkharris 7986b69
skeleton of usage cmd
jasonhawkharris da12526
skeleton of usage command
jasonhawkharris 5392d86
stash changes
jasonhawkharris ba95097
move style folder out of resources directory and into scout directory…
jasonhawkharris 18e764e
adds metrics api client
jasonhawkharris 30fe6de
refactor
jasonhawkharris 80c379d
connect metrics api
jasonhawkharris eb445de
merge with main
jasonhawkharris 211d738
borked percentages
jasonhawkharris 2493b21
WIP - incorrect usage percentages for some containers
jasonhawkharris e08a8a4
fixes bug that was causing certain containers to return bad usage per…
jasonhawkharris 55b43b8
adds percentage of storage capacity used column
jasonhawkharris b5bdd77
add tests and use remotecommand package instead of exec
jasonhawkharris 0970e44
delete stray line
jasonhawkharris d7aae2f
add line breaks for readability
jasonhawkharris fd30e80
fix incorrect flag name
jasonhawkharris 23a3f11
refactor to ensure all functions are doing one thing
jasonhawkharris 2a493fa
formatting
jasonhawkharris d551652
formatting
jasonhawkharris fa4ac06
remove duplicate import, remove todo
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,112 @@ | ||
| package main | ||
|
|
||
| import ( | ||
| "context" | ||
| "flag" | ||
| "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" | ||
| "k8s.io/client-go/tools/clientcmd" | ||
| "k8s.io/client-go/util/homedir" | ||
| metricsv "k8s.io/metrics/pkg/client/clientset/versioned" | ||
| ) | ||
|
|
||
| func init() { | ||
| cmdUsage := `'src scout usage' is a tool that tracks resource usage for Sourcegraph instances. | ||
| Part of the EXPERIMENTAL "src scout" tool. | ||
|
|
||
| Examples | ||
| 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 <podname> | ||
|
|
||
| Add namespace if using namespace in a Kubernetes cluster | ||
| $ src scout usage --namespace <namespace> | ||
|
|
||
| Watch usage in real time | ||
| $ src scout usage --spy | ||
| ` | ||
|
|
||
| flagSet := flag.NewFlagSet("usage", flag.ExitOnError) | ||
| usageFunc := func() { | ||
| fmt.Fprintf(flag.CommandLine.Output(), "Usage of 'src scout %s':\n", flagSet.Name()) | ||
| flagSet.PrintDefaults() | ||
| fmt.Println(cmdUsage) | ||
| } | ||
|
|
||
| var ( | ||
| kubeConfig *string | ||
| namespace = flagSet.String("namespace", "", "(optional) specify the kubernetes namespace to use") | ||
| docker = flagSet.Bool("docker", false, "(optional) using docker deployment") | ||
| spy = flagSet.Bool("spy", false, "(optional) see resource usage in real time") | ||
| // 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 { | ||
| return errors.Wrap(err, "failed to load .kube config: ") | ||
| } | ||
|
|
||
| clientSet, err := kubernetes.NewForConfig(config) | ||
| if err != nil { | ||
| return errors.Wrap(err, "failed to initiate kubernetes client: ") | ||
| } | ||
|
|
||
| metricsClient, err := metricsv.NewForConfig(config) | ||
| if err != nil { | ||
| return errors.Wrap(err, "failed to initiate metrics client") | ||
| } | ||
|
|
||
| var options []usage.Option | ||
|
|
||
| if *namespace != "" { | ||
| options = append(options, usage.WithNamespace(*namespace)) | ||
| } | ||
| if *spy { | ||
| options = append(options, usage.WithSpy(true)) | ||
| } | ||
|
|
||
| if *docker { | ||
| dockerClient, err := client.NewClientWithOpts(client.FromEnv) | ||
| if err != nil { | ||
| return errors.Wrap(err, "error creating docker client: ") | ||
| } | ||
|
|
||
| return usage.Docker(context.Background(), *dockerClient) | ||
| } | ||
|
|
||
| return usage.K8s(context.Background(), clientSet, metricsClient, 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
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.