Skip to content

Commit 4766869

Browse files
add usage command to 'src scout' (#983)
* move style folder out of resources directory and into scout directory for ease of reuse * adds metrics api client * refactor * connect metrics api * borked percentages * WIP - incorrect usage percentages for some containers * fixes bug that was causing certain containers to return bad usage percentages * adds percentage of storage capacity used column * add tests and use remotecommand package instead of exec * delete stray line * add line breaks for readability * fix incorrect flag name * refactor to ensure all functions are doing one thing * formatting * formatting * remove duplicate import, remove todo --------- Co-authored-by: Jacob Pleiness <jdpleiness@users.noreply.github.com>
1 parent 0890929 commit 4766869

File tree

9 files changed

+867
-59
lines changed

9 files changed

+867
-59
lines changed

cmd/src/scout_usage.go

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
package main
2+
3+
import (
4+
"context"
5+
"flag"
6+
"fmt"
7+
"path/filepath"
8+
9+
"github.com/docker/docker/client"
10+
"github.com/sourcegraph/sourcegraph/lib/errors"
11+
"github.com/sourcegraph/src-cli/internal/scout/usage"
12+
"k8s.io/client-go/kubernetes"
13+
"k8s.io/client-go/tools/clientcmd"
14+
"k8s.io/client-go/util/homedir"
15+
metricsv "k8s.io/metrics/pkg/client/clientset/versioned"
16+
)
17+
18+
func init() {
19+
cmdUsage := `'src scout usage' is a tool that tracks resource usage for Sourcegraph instances.
20+
Part of the EXPERIMENTAL "src scout" tool.
21+
22+
Examples
23+
List pods and resource usage in a Kubernetes deployment:
24+
$ src scout usage
25+
26+
List containers and resource usage in a Docker deployment:
27+
$ src scout usage --docker
28+
29+
Check usage for specific pod
30+
$ src scout usage --pod <podname>
31+
32+
Add namespace if using namespace in a Kubernetes cluster
33+
$ src scout usage --namespace <namespace>
34+
35+
Watch usage in real time
36+
$ src scout usage --spy
37+
`
38+
39+
flagSet := flag.NewFlagSet("usage", flag.ExitOnError)
40+
usageFunc := func() {
41+
fmt.Fprintf(flag.CommandLine.Output(), "Usage of 'src scout %s':\n", flagSet.Name())
42+
flagSet.PrintDefaults()
43+
fmt.Println(cmdUsage)
44+
}
45+
46+
var (
47+
kubeConfig *string
48+
namespace = flagSet.String("namespace", "", "(optional) specify the kubernetes namespace to use")
49+
pod = flagSet.String("pod", "", "(optional) specify a single pod")
50+
docker = flagSet.Bool("docker", false, "(optional) using docker deployment")
51+
spy = flagSet.Bool("spy", false, "(optional) see resource usage in real time")
52+
)
53+
54+
if home := homedir.HomeDir(); home != "" {
55+
kubeConfig = flagSet.String(
56+
"kubeconfig",
57+
filepath.Join(home, ".kube", "config"),
58+
"(optional) absolute path to the kubeconfig file",
59+
)
60+
} else {
61+
kubeConfig = flagSet.String("kubeconfig", "", "absolute path to the kubeconfig file")
62+
}
63+
64+
handler := func(args []string) error {
65+
if err := flagSet.Parse(args); err != nil {
66+
return err
67+
}
68+
69+
config, err := clientcmd.BuildConfigFromFlags("", *kubeConfig)
70+
if err != nil {
71+
return errors.Wrap(err, "failed to load .kube config: ")
72+
}
73+
74+
clientSet, err := kubernetes.NewForConfig(config)
75+
if err != nil {
76+
return errors.Wrap(err, "failed to initiate kubernetes client: ")
77+
}
78+
79+
metricsClient, err := metricsv.NewForConfig(config)
80+
if err != nil {
81+
return errors.Wrap(err, "failed to initiate metrics client")
82+
}
83+
84+
var options []usage.Option
85+
86+
if *namespace != "" {
87+
options = append(options, usage.WithNamespace(*namespace))
88+
}
89+
if *spy {
90+
options = append(options, usage.WithSpy(true))
91+
}
92+
if *pod != "" {
93+
options = append(options, usage.WithPod(*pod))
94+
}
95+
96+
if *docker {
97+
dockerClient, err := client.NewClientWithOpts(client.FromEnv)
98+
if err != nil {
99+
return errors.Wrap(err, "error creating docker client: ")
100+
}
101+
102+
return usage.Docker(context.Background(), *dockerClient)
103+
}
104+
105+
return usage.K8s(
106+
context.Background(),
107+
clientSet,
108+
metricsClient,
109+
config,
110+
options...,
111+
)
112+
}
113+
114+
scoutCommands = append(scoutCommands, &command{
115+
flagSet: flagSet,
116+
handler: handler,
117+
usageFunc: usageFunc,
118+
})
119+
120+
}

go.mod

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,14 @@ require (
3939
google.golang.org/protobuf v1.28.1
4040
gopkg.in/yaml.v3 v3.0.1
4141
jaytaylor.com/html2text v0.0.0-20200412013138-3577fbdbcff7
42-
k8s.io/api v0.26.1
43-
k8s.io/apimachinery v0.26.1
44-
k8s.io/client-go v0.26.1
42+
k8s.io/api v0.27.1
43+
k8s.io/apimachinery v0.27.1
44+
k8s.io/client-go v0.27.1
45+
k8s.io/metrics v0.27.1
4546
)
4647

4748
require (
48-
golang.org/x/exp v0.0.0-20190731235908-ec7cb31e5a56 // indirect
49+
golang.org/x/exp v0.0.0-20200224162631-6cc2880d07d6 // indirect
4950
golang.org/x/image v0.6.0 // indirect
5051
golang.org/x/mobile v0.0.0-20230301163155-e0f57694e12c // indirect
5152
)
@@ -93,14 +94,14 @@ require (
9394
github.com/getsentry/sentry-go v0.15.0 // indirect
9495
github.com/ghodss/yaml v1.0.0 // indirect
9596
github.com/go-logr/logr v1.2.3 // indirect
96-
github.com/go-openapi/jsonpointer v0.19.5 // indirect
97-
github.com/go-openapi/jsonreference v0.20.0 // indirect
98-
github.com/go-openapi/swag v0.19.14 // indirect
97+
github.com/go-openapi/jsonpointer v0.19.6 // indirect
98+
github.com/go-openapi/jsonreference v0.20.1 // indirect
99+
github.com/go-openapi/swag v0.22.3 // indirect
99100
github.com/gofrs/flock v0.8.1 // indirect
100101
github.com/gofrs/uuid v4.2.0+incompatible // indirect
101102
github.com/gogo/protobuf v1.3.2 // indirect
102103
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
103-
github.com/golang/protobuf v1.5.2 // indirect
104+
github.com/golang/protobuf v1.5.3 // indirect
104105
github.com/google/gnostic v0.5.7-v3refs // indirect
105106
github.com/google/gofuzz v1.1.0 // indirect
106107
github.com/google/uuid v1.3.0 // indirect
@@ -130,7 +131,7 @@ require (
130131
github.com/mitchellh/copystructure v1.2.0 // indirect
131132
github.com/mitchellh/reflectwalk v1.0.2 // indirect
132133
github.com/moby/spdystream v0.2.0 // indirect
133-
github.com/moby/term v0.0.0-20220808134915-39b0c02b01ae // indirect
134+
github.com/moby/term v0.0.0-20221205130635-1aeaba878587 // indirect
134135
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
135136
github.com/modern-go/reflect2 v1.0.2 // indirect
136137
github.com/morikuni/aec v1.0.0 // indirect
@@ -150,7 +151,7 @@ require (
150151
github.com/pseudomuto/protoc-gen-doc v1.5.1 // indirect
151152
github.com/pseudomuto/protokit v0.2.0 // indirect
152153
github.com/rivo/uniseg v0.4.2 // indirect
153-
github.com/rogpeppe/go-internal v1.9.0 // indirect
154+
github.com/rogpeppe/go-internal v1.10.0 // indirect
154155
github.com/russross/blackfriday/v2 v2.1.0 // indirect
155156
github.com/sourcegraph/log v0.0.0-20221206163500-7d93c6ad7037 // indirect
156157
github.com/spf13/cobra v1.6.0 // indirect
@@ -178,13 +179,13 @@ require (
178179
google.golang.org/appengine v1.6.7 // indirect
179180
google.golang.org/genproto v0.0.0-20230216225411-c8e22ba71e44 // indirect
180181
google.golang.org/grpc v1.53.0 // indirect
181-
gopkg.in/inf.v0 v0.9.1 // indirect
182+
gopkg.in/inf.v0 v0.9.1 // direct
182183
gopkg.in/yaml.v2 v2.4.0 // indirect
183-
k8s.io/klog/v2 v2.80.1 // indirect
184-
k8s.io/kube-openapi v0.0.0-20221012153701-172d655c2280 // indirect
185-
k8s.io/utils v0.0.0-20221107191617-1a15be271d1d // indirect
184+
k8s.io/klog/v2 v2.90.1 // indirect
185+
k8s.io/kube-openapi v0.0.0-20230308215209-15aac26d736a // indirect
186+
k8s.io/utils v0.0.0-20230209194617-a36077c30491 // indirect
186187
mvdan.cc/gofumpt v0.4.0 // indirect
187-
sigs.k8s.io/json v0.0.0-20220713155537-f223a00ba0e2 // indirect
188+
sigs.k8s.io/json v0.0.0-20221116044647-bc3834ca7abd // indirect
188189
sigs.k8s.io/structured-merge-diff/v4 v4.2.3 // indirect
189190
sigs.k8s.io/yaml v1.3.0 // indirect
190191
)

0 commit comments

Comments
 (0)