Skip to content
Merged
Show file tree
Hide file tree
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 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
ee1e64e
export to file now works for docker as well.
jasonhawkharris May 15, 2023
575a290
refactor
jasonhawkharris May 15, 2023
941bc31
add file export functionality for docker
jasonhawkharris May 15, 2023
20a6c47
fix errors linter caught
jasonhawkharris May 16, 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
a0695a0
fix merge conflict
jasonhawkharris May 16, 2023
d5ce0af
add the ability to dump all container resource data txt file
jasonhawkharris May 16, 2023
cee0234
fix merge conflicts
jasonhawkharris May 16, 2023
dbc69f0
Merge branch 'main' into jhh/src-resource
jasonhawkharris May 16, 2023
992739c
Merge branch 'jhh/copy-all' into jhh/src-resource
jasonhawkharris May 16, 2023
7986b69
skeleton of usage cmd
jasonhawkharris May 16, 2023
da12526
skeleton of usage command
jasonhawkharris May 16, 2023
5392d86
stash changes
jasonhawkharris May 16, 2023
ba95097
move style folder out of resources directory and into scout directory…
jasonhawkharris May 16, 2023
18e764e
adds metrics api client
jasonhawkharris May 16, 2023
30fe6de
refactor
jasonhawkharris May 16, 2023
80c379d
connect metrics api
jasonhawkharris May 16, 2023
eb445de
merge with main
jasonhawkharris May 16, 2023
211d738
borked percentages
jasonhawkharris May 18, 2023
2493b21
WIP - incorrect usage percentages for some containers
jasonhawkharris May 18, 2023
e08a8a4
fixes bug that was causing certain containers to return bad usage per…
jasonhawkharris May 19, 2023
55b43b8
adds percentage of storage capacity used column
jasonhawkharris May 19, 2023
b5bdd77
add tests and use remotecommand package instead of exec
jasonhawkharris May 22, 2023
0970e44
delete stray line
jasonhawkharris May 22, 2023
d7aae2f
add line breaks for readability
jasonhawkharris May 22, 2023
fd30e80
fix incorrect flag name
jasonhawkharris May 22, 2023
23a3f11
refactor to ensure all functions are doing one thing
jasonhawkharris May 23, 2023
2a493fa
formatting
jasonhawkharris May 23, 2023
d551652
formatting
jasonhawkharris May 23, 2023
fa4ac06
remove duplicate import, remove todo
jasonhawkharris May 23, 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
112 changes: 112 additions & 0 deletions cmd/src/scout_usage.go
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,
})

}
31 changes: 16 additions & 15 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,14 @@ require (
google.golang.org/protobuf v1.28.1
gopkg.in/yaml.v3 v3.0.1
jaytaylor.com/html2text v0.0.0-20200412013138-3577fbdbcff7
k8s.io/api v0.26.1
k8s.io/apimachinery v0.26.1
k8s.io/client-go v0.26.1
k8s.io/api v0.27.1
k8s.io/apimachinery v0.27.1
k8s.io/client-go v0.27.1
k8s.io/metrics v0.27.1
)

require (
golang.org/x/exp v0.0.0-20190731235908-ec7cb31e5a56 // indirect
golang.org/x/exp v0.0.0-20200224162631-6cc2880d07d6 // indirect
golang.org/x/image v0.6.0 // indirect
golang.org/x/mobile v0.0.0-20230301163155-e0f57694e12c // indirect
)
Expand Down Expand Up @@ -93,14 +94,14 @@ require (
github.com/getsentry/sentry-go v0.15.0 // indirect
github.com/ghodss/yaml v1.0.0 // indirect
github.com/go-logr/logr v1.2.3 // indirect
github.com/go-openapi/jsonpointer v0.19.5 // indirect
github.com/go-openapi/jsonreference v0.20.0 // indirect
github.com/go-openapi/swag v0.19.14 // indirect
github.com/go-openapi/jsonpointer v0.19.6 // indirect
github.com/go-openapi/jsonreference v0.20.1 // indirect
github.com/go-openapi/swag v0.22.3 // indirect
github.com/gofrs/flock v0.8.1 // indirect
github.com/gofrs/uuid v4.2.0+incompatible // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/golang/protobuf v1.5.3 // indirect
github.com/google/gnostic v0.5.7-v3refs // indirect
github.com/google/gofuzz v1.1.0 // indirect
github.com/google/uuid v1.3.0 // indirect
Expand Down Expand Up @@ -130,7 +131,7 @@ require (
github.com/mitchellh/copystructure v1.2.0 // indirect
github.com/mitchellh/reflectwalk v1.0.2 // indirect
github.com/moby/spdystream v0.2.0 // indirect
github.com/moby/term v0.0.0-20220808134915-39b0c02b01ae // indirect
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
Expand All @@ -150,7 +151,7 @@ require (
github.com/pseudomuto/protoc-gen-doc v1.5.1 // indirect
github.com/pseudomuto/protokit v0.2.0 // indirect
github.com/rivo/uniseg v0.4.2 // indirect
github.com/rogpeppe/go-internal v1.9.0 // indirect
github.com/rogpeppe/go-internal v1.10.0 // indirect
github.com/russross/blackfriday/v2 v2.1.0 // indirect
github.com/sourcegraph/log v0.0.0-20221206163500-7d93c6ad7037 // indirect
github.com/spf13/cobra v1.6.0 // indirect
Expand Down Expand Up @@ -178,13 +179,13 @@ require (
google.golang.org/appengine v1.6.7 // indirect
google.golang.org/genproto v0.0.0-20230216225411-c8e22ba71e44 // indirect
google.golang.org/grpc v1.53.0 // indirect
gopkg.in/inf.v0 v0.9.1 // indirect
gopkg.in/inf.v0 v0.9.1 // direct
gopkg.in/yaml.v2 v2.4.0 // indirect
k8s.io/klog/v2 v2.80.1 // indirect
k8s.io/kube-openapi v0.0.0-20221012153701-172d655c2280 // indirect
k8s.io/utils v0.0.0-20221107191617-1a15be271d1d // indirect
k8s.io/klog/v2 v2.90.1 // indirect
k8s.io/kube-openapi v0.0.0-20230308215209-15aac26d736a // indirect
k8s.io/utils v0.0.0-20230209194617-a36077c30491 // indirect
mvdan.cc/gofumpt v0.4.0 // indirect
sigs.k8s.io/json v0.0.0-20220713155537-f223a00ba0e2 // indirect
sigs.k8s.io/json v0.0.0-20221116044647-bc3834ca7abd // indirect
sigs.k8s.io/structured-merge-diff/v4 v4.2.3 // indirect
sigs.k8s.io/yaml v1.3.0 // indirect
)
Expand Down
Loading