Skip to content

Commit

Permalink
Proper namespaces (#31)
Browse files Browse the repository at this point in the history
* Use generic cli library

* Update readme

* Fix gomod

* tidy
  • Loading branch information
howardjohn authored Jan 5, 2020
1 parent 32e2c0c commit f45d0ce
Show file tree
Hide file tree
Showing 6 changed files with 319 additions and 238 deletions.
35 changes: 22 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,27 +6,32 @@ A plugin to access Kubernetes resource requests, limits, and usage.

## Install

`go get -u github.com/howardjohn/kubectl-resources`
`go get github.com/howardjohn/kubectl-resources`

## Usage

```
kubectl resources --help
Plugin to access Kubernetes resource requests, limits, and usage.
Usage:
kubectl-resources [flags]
Flags:
-h, --help help for kubectl-resources
-n, --namespace string namespace to query. If not set, all namespaces are included
-c, --show-containers include container level details
-d, --show-nodes include node names
-v, --verbose show full resource names
-A, --all-namespaces If present, list the requested object(s) across all namespaces. Namespace in current context is ignored even if specified with --namespace.
-b, --by string column to aggregate on. Default is pod (default "POD")
-c, --color show colors for pods using excessive resources (default true)
-h, --help help for kubectl-resources
-n, --namespace string If present, the namespace scope for this CLI request
-l, --selector string Selector (label query) to filter on, supports '=', '==', and '!='.(e.g. -l key1=value1,key2=value2)
-d, --show-nodes include node names
-v, --verbose show full resource names
-w, --warnings only show resources using excessive resources
```

Example output:

```
kubectl resources
$ kubectl resources
NAMESPACE POD CPU USE CPU REQ CPU LIM MEM USE MEM REQ MEM LIM
default details-v1 6m 110m 2000m 36Mi 39Mi 1000Mi
default productpage-v1 12m 110m 2000m 71Mi 39Mi 1000Mi
Expand All @@ -43,11 +48,15 @@ In the above example, the `details-v1` deployment has just one replica, so only
For `shell`, which has multiple replicas, just the replicaset's identifier is shown.
For nodes, only the unique segment of the name is shown.

To show more details, you can add the `-c` flag to show containers, and the `-d` flag to show nodes.
You can also aggregate info at different levels. By default this is at the pod level, but can also be namespace, node, or container.

For example, to see resource utilization by namespace

```
kubectl resources -v -c -d
NAMESPACE POD CONTAINER NODE CPU USE CPU REQ CPU LIM MEM USE MEM REQ MEM LIM
default details-v1-5cb65fd66c-6mtt2 details gke-default-pool-f93f0b08-zwt6 1m 100m - 10Mi - -
default details-v1-5cb65fd66c-6mtt2 istio-proxy gke-default-pool-f93f0b08-zwt6 5m 10m 2000m 25Mi 39Mi 1000Mi
$ kubectl resources --by namespace
NAMESPACE CPU USE CPU REQ CPU LIM MEM USE MEM REQ MEM LIM
default 3m 110m 2100m 29Mi 144Mi 1152Mi
istio-system 70m 2140m 14800m 570Mi 3641Mi 8934Mi
kube-system 82m 1831m 3222m 1312Mi 1203Mi 2365Mi
155m 4081m 20122m 1912Mi 4989Mi 12452Mi
```
62 changes: 36 additions & 26 deletions cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ package cmd
import (
"fmt"
"os"
"path"

isatty "github.com/mattn/go-isatty"
"github.com/spf13/pflag"
"k8s.io/cli-runtime/pkg/genericclioptions"

"github.com/howardjohn/kubectl-resources/pkg/model"

Expand All @@ -15,24 +16,15 @@ import (
)

var (
namespace = ""
kubeConfig = path.Join(os.Getenv("HOME"), ".kube", "config")
namespaceBlacklist = []string{"kube-system"}
color = isatty.IsTerminal(os.Stdout.Fd())
showNodes = false
verbose = false
aggregation = "POD"
onlyWarnings = false
color = isatty.IsTerminal(os.Stdout.Fd())
showNodes = false
verbose = false
aggregation = "POD"
onlyWarnings = false
allNamespaces = false
)

func init() {
rootCmd.PersistentFlags().StringVarP(
&namespace,
"namespace",
"n",
namespace,
"namespace to query. If not set, all namespaces are included",
)
rootCmd.PersistentFlags().BoolVarP(
&showNodes,
"show-nodes",
Expand Down Expand Up @@ -70,6 +62,14 @@ func init() {
)
}

var (
kubeConfigFlags = genericclioptions.NewConfigFlags(false)

kubeResouceBuilderFlags = genericclioptions.NewResourceBuilderFlags().
WithAllNamespaces(false).
WithLabelSelector("")
)

var rootCmd = &cobra.Command{
Use: "kubectl-resources",
Short: "Plugin to access Kubernetes resource requests, limits, and usage.",
Expand All @@ -79,24 +79,34 @@ var rootCmd = &cobra.Command{
return err
}

if kc, f := os.LookupEnv("KUBECONFIG"); f {
kubeConfig = kc
switch agg {
case model.Node, model.Namespace:
kubeResouceBuilderFlags = kubeResouceBuilderFlags.WithAllNamespaces(true)
}

resourceFinder := kubeResouceBuilderFlags.WithAll(true).ToBuilder(kubeConfigFlags, []string{
"pods.metrics.k8s.io,pods",
})
args := &model.Args{
Namespace: namespace,
KubeConfig: kubeConfig,
NamespaceBlacklist: namespaceBlacklist,
Aggregation: agg,
Verbose: verbose,
ShowNodes: showNodes,
ColoredOutput: color,
OnlyWarnings: onlyWarnings,
ResourceFinder: resourceFinder,
AllNamespaces: allNamespaces,
Aggregation: agg,
Verbose: verbose,
ShowNodes: showNodes,
ColoredOutput: color,
OnlyWarnings: onlyWarnings,
}
return client.Run(args)
},
}

func Execute() {
flags := pflag.NewFlagSet("kubectl-resources", pflag.ExitOnError)
pflag.CommandLine = flags

kubeConfigFlags.AddFlags(flags)
kubeResouceBuilderFlags.AddFlags(flags)
flags.AddFlagSet(rootCmd.PersistentFlags())
if err := rootCmd.Execute(); err != nil {
fmt.Println(err)
os.Exit(1)
Expand Down
22 changes: 8 additions & 14 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,18 @@ module github.com/howardjohn/kubectl-resources
go 1.12

require (
github.com/dgrijalva/jwt-go v3.2.0+incompatible // indirect
github.com/gogo/protobuf v1.2.1 // indirect
github.com/golang/protobuf v1.3.2 // indirect
github.com/logrusorgru/aurora v0.0.0-20190428105938-cea283e61946
github.com/magefile/mage v1.9.0
github.com/mattn/go-isatty v0.0.9
github.com/rogpeppe/go-internal v1.3.1
github.com/mattn/go-isatty v0.0.11
github.com/rogpeppe/go-internal v1.5.1
github.com/spf13/cobra v0.0.5
github.com/spf13/pflag v1.0.5 // indirect
github.com/stretchr/testify v1.4.0 // indirect
github.com/spf13/pflag v1.0.5
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550 // indirect
golang.org/x/net v0.0.0-20190923162816-aa69164e4478 // indirect
golang.org/x/sys v0.0.0-20190922100055-0a153f010e69 // indirect
golang.org/x/text v0.3.2 // indirect
golang.org/x/time v0.0.0-20190308202827-9d24e82272b4 // indirect
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 // indirect
gopkg.in/yaml.v2 v2.2.7 // indirect
k8s.io/apimachinery v0.0.0-20190624085041-961b39a1baa0
k8s.io/client-go v0.0.0-20190624085356-2c6e35a5b9cf
k8s.io/metrics v0.0.0-20190624090658-79adfaea0da2
k8s.io/api v0.17.0
k8s.io/apimachinery v0.17.0
k8s.io/cli-runtime v0.17.0
k8s.io/client-go v0.17.0
k8s.io/metrics v0.17.0
)
Loading

0 comments on commit f45d0ce

Please sign in to comment.