Skip to content

Commit

Permalink
Restructure directories (#3)
Browse files Browse the repository at this point in the history
  • Loading branch information
howardjohn authored Jun 29, 2019
1 parent 2a32093 commit 1a34067
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 56 deletions.
10 changes: 6 additions & 4 deletions cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@ import (
"os"
"path"

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

"github.com/spf13/cobra"

"github.com/howardjohn/kubectl-resources/client"
"github.com/howardjohn/kubectl-resources/pkg/client"
)

var (
Expand Down Expand Up @@ -54,11 +56,11 @@ var rootCmd = &cobra.Command{
Use: "kubectl-resources",
Short: "Plugin to access Kubernetes resource requests, limits, and usage.",
RunE: func(cmd *cobra.Command, a []string) error {
aggregation := client.Pod
aggregation := model.Pod
if showContainers {
aggregation = client.None
aggregation = model.None
}
args := &client.Args{
args := &model.Args{
Namespace: namespace,
KubeConfig: kubeConfig,
NamespaceBlacklist: namespaceBlacklist,
Expand Down
60 changes: 23 additions & 37 deletions client/client.go → pkg/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ import (
"fmt"
"os"

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

metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
_ "k8s.io/client-go/plugin/pkg/client/auth"
Expand All @@ -12,30 +15,13 @@ import (
metrics "k8s.io/metrics/pkg/client/clientset/versioned"
)

type Aggregation int

const (
None Aggregation = iota
Pod
Namespace
)

type Args struct {
Namespace string
KubeConfig string
NamespaceBlacklist []string
Aggregation Aggregation
Verbose bool
ShowNodes bool
}

func Run(args *Args) error {
func Run(args *model.Args) error {
config, err := clientcmd.BuildConfigFromFlags("", args.KubeConfig)
if err != nil {
return fmt.Errorf("failed to get kubeconfig: %v", err)
}

responseChan := make(chan map[string]*PodResource, 2)
responseChan := make(chan map[string]*model.PodResource, 2)
errChan := make(chan error, 2)
go func() {
metricsResponse, err := FetchMetrics(config, args.Namespace)
Expand All @@ -54,7 +40,7 @@ func Run(args *Args) error {
}
}()

var responses []map[string]*PodResource
var responses []map[string]*model.PodResource
got := 0
for got < 2 {
select {
Expand All @@ -66,20 +52,20 @@ func Run(args *Args) error {
got++
}

resources, err := MergePodResources(responses...)
resources, err := model.MergePodResources(responses...)
if err != nil {
return fmt.Errorf("failed to merge responses: %v", err)
}

filterBlacklist(resources, args.NamespaceBlacklist)

if err := Write(resources, args); err != nil {
if err := writer.Write(resources, args); err != nil {
return fmt.Errorf("faild to write: %v", err)
}
return nil
}

func filterBlacklist(resources map[string]*PodResource, blacklist []string) {
func filterBlacklist(resources map[string]*model.PodResource, blacklist []string) {
blMap := make(map[string]struct{})
for _, ns := range blacklist {
blMap[ns] = struct{}{}
Expand All @@ -91,7 +77,7 @@ func filterBlacklist(resources map[string]*PodResource, blacklist []string) {
}
}

func FetchMetrics(cfg *rest.Config, ns string) (map[string]*PodResource, error) {
func FetchMetrics(cfg *rest.Config, ns string) (map[string]*model.PodResource, error) {
metricsclient, err := metrics.NewForConfig(cfg)
if err != nil {
return nil, fmt.Errorf("failed to create metrics client: %v", err)
Expand All @@ -105,21 +91,21 @@ func FetchMetrics(cfg *rest.Config, ns string) (map[string]*PodResource, error)
fmt.Println("Continue:", podList.Continue)
}

res := map[string]*PodResource{}
res := map[string]*model.PodResource{}
for _, pod := range podList.Items {
key := uid(pod.Name, pod.Namespace)
res[key] = &PodResource{
res[key] = &model.PodResource{
Name: pod.Name,
Namespace: pod.Namespace,
Containers: make(map[string]*ContainerResource),
Containers: make(map[string]*model.ContainerResource),
}
for _, container := range pod.Containers {
res[key].Containers[container.Name] = &ContainerResource{
res[key].Containers[container.Name] = &model.ContainerResource{
Name: container.Name,
Cpu: &Resource{
Cpu: &model.Resource{
Usage: container.Usage.Cpu().MilliValue(),
},
Memory: &Resource{
Memory: &model.Resource{
Usage: container.Usage.Memory().MilliValue(),
},
}
Expand All @@ -129,7 +115,7 @@ func FetchMetrics(cfg *rest.Config, ns string) (map[string]*PodResource, error)
return res, nil
}

func FetchPods(cfg *rest.Config, ns string) (map[string]*PodResource, error) {
func FetchPods(cfg *rest.Config, ns string) (map[string]*model.PodResource, error) {
clientset, err := kubernetes.NewForConfig(cfg)
if err != nil {
return nil, err
Expand All @@ -142,24 +128,24 @@ func FetchPods(cfg *rest.Config, ns string) (map[string]*PodResource, error) {
fmt.Println("Continue:", podList.Continue)
}

res := map[string]*PodResource{}
res := map[string]*model.PodResource{}
for _, pod := range podList.Items {
key := uid(pod.Name, pod.Namespace)

res[key] = &PodResource{
res[key] = &model.PodResource{
Name: pod.Name,
Namespace: pod.Namespace,
Node: pod.Spec.NodeName,
Containers: make(map[string]*ContainerResource),
Containers: make(map[string]*model.ContainerResource),
}
for _, container := range pod.Spec.Containers {
res[key].Containers[container.Name] = &ContainerResource{
res[key].Containers[container.Name] = &model.ContainerResource{
Name: container.Name,
Cpu: &Resource{
Cpu: &model.Resource{
Request: container.Resources.Requests.Cpu().MilliValue(),
Limit: container.Resources.Limits.Cpu().MilliValue(),
},
Memory: &Resource{
Memory: &model.Resource{
Request: container.Resources.Requests.Memory().MilliValue(),
Limit: container.Resources.Limits.Memory().MilliValue(),
},
Expand Down
18 changes: 18 additions & 0 deletions pkg/model/args.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package model

type Args struct {
Namespace string
KubeConfig string
NamespaceBlacklist []string
Aggregation Aggregation
Verbose bool
ShowNodes bool
}

type Aggregation int

const (
None Aggregation = iota
Pod
Namespace
)
2 changes: 1 addition & 1 deletion client/podresource.go → pkg/model/podresource.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package client
package model

import "fmt"

Expand Down
File renamed without changes.
30 changes: 16 additions & 14 deletions client/writer.go → pkg/writer/writer.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package client
package writer

import (
"fmt"
Expand All @@ -9,7 +9,9 @@ import (
"strings"
"text/tabwriter"

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

"github.com/howardjohn/kubectl-resources/pkg/util"
)

const (
Expand All @@ -19,7 +21,7 @@ const (
tabwriterPadChar = '\t'
)

func sortPodResources(res []*PodResource) {
func sortPodResources(res []*model.PodResource) {
sort.Slice(res, func(i, j int) bool {
if res[i].Namespace != res[j].Namespace {
return res[i].Namespace < res[j].Namespace
Expand All @@ -28,8 +30,8 @@ func sortPodResources(res []*PodResource) {
})
}

func Write(response map[string]*PodResource, args *Args) error {
resources := make([]*PodResource, 0, len(response))
func Write(response map[string]*model.PodResource, args *model.Args) error {
resources := make([]*model.PodResource, 0, len(response))
for _, res := range response {
resources = append(resources, res)
}
Expand All @@ -55,14 +57,14 @@ func Write(response map[string]*PodResource, args *Args) error {
return w.Flush()
}

func formatHeader(args *Args) string {
func formatHeader(args *model.Args) string {
var headers []string
switch args.Aggregation {
case None:
case model.None:
headers = append(headers, "NAMESPACE", "POD", "CONTAINER")
case Pod:
case model.Pod:
headers = append(headers, "NAMESPACE", "POD")
case Namespace:
case model.Namespace:
headers = append(headers, "NAMESPACE")
}
if args.ShowNodes {
Expand All @@ -80,10 +82,10 @@ func formatHeader(args *Args) string {
return strings.Join(headers, "\t")
}

func formatRow(pod *PodResource, args *Args) []string {
func formatRow(pod *model.PodResource, args *model.Args) []string {
rows := []string{}
switch args.Aggregation {
case None:
case model.None:
for _, c := range pod.Containers {
row := []string{
pod.Namespace,
Expand All @@ -104,7 +106,7 @@ func formatRow(pod *PodResource, args *Args) []string {
)
rows = append(rows, strings.Join(row, "\t"))
}
case Pod:
case model.Pod:
row := []string{
pod.Namespace,
pod.Name,
Expand Down Expand Up @@ -141,7 +143,7 @@ func formatMemory(i int64) string {
return strconv.FormatInt(mb, 10) + "Mi"
}

func simplifyPodNames(resources []*PodResource) {
func simplifyPodNames(resources []*model.PodResource) {
names := map[string]int{}
for _, pod := range resources {
parts := strings.Split(pod.Name, "-")
Expand All @@ -163,7 +165,7 @@ func simplifyPodNames(resources []*PodResource) {
}
}

func simplifyNodeNames(resources []*PodResource) {
func simplifyNodeNames(resources []*model.PodResource) {
var nameParts []util.Part
for _, pod := range resources {
nameParts = append(nameParts, strings.Split(pod.Node, "-"))
Expand Down

0 comments on commit 1a34067

Please sign in to comment.