Skip to content

Commit

Permalink
updating flags to allow for showing pods and/or util separately, rela…
Browse files Browse the repository at this point in the history
…ted refactoring
  • Loading branch information
robscott committed Feb 3, 2019
1 parent f88864e commit 96dab13
Show file tree
Hide file tree
Showing 3 changed files with 163 additions and 65 deletions.
64 changes: 2 additions & 62 deletions pkg/capacity/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,12 @@ package capacity

import (
"fmt"
"os"
"sort"
"text/tabwriter"

"github.com/robscott/kube-capacity/pkg/kube"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

func List(args []string, outputFormat string) {
func List(args []string, showPods bool, showUtil bool) {
clientset, err := kube.NewClientSet()
if err != nil {
fmt.Println("Error connecting to Kubernetes")
Expand Down Expand Up @@ -104,62 +101,5 @@ func List(args []string, outputFormat string) {
}
}

printList(cm, outputFormat)
}

func printList(cm clusterMetric, outputFormat string) {
names := make([]string, len(cm.nodeMetrics))

i := 0
for name := range cm.nodeMetrics {
names[i] = name
i++
}
sort.Strings(names)

w := new(tabwriter.Writer)
w.Init(os.Stdout, 0, 8, 2, ' ', 0)

if outputFormat == "wide" {
fmt.Fprintln(w, "NODE\t NAMESPACE\t POD\t CPU REQUESTS \t CPU LIMITS \t CPU UTIL \t MEMORY REQUESTS \t MEMORY LIMITS \t MEMORY UTIL")

if len(names) > 1 {
fmt.Fprintf(w, "* \t *\t *\t %s \t %s \t %s \t %s \t %s \t %s \n",
cm.cpu.requestString(), cm.cpu.limitString(), cm.cpu.utilString(),
cm.memory.requestString(), cm.memory.limitString(), cm.memory.utilString())
fmt.Fprintln(w, "\t\t\t\t\t\t\t\t")
}
} else {
fmt.Fprintln(w, "NODE\t CPU REQUESTS \t CPU LIMITS \t MEMORY REQUESTS \t MEMORY LIMITS")

if len(names) > 1 {
fmt.Fprintf(w, "* \t %s \t %s \t %s \t %s \n",
cm.cpu.requestString(), cm.cpu.limitString(),
cm.memory.requestString(), cm.memory.limitString())
}
}

for _, name := range names {
nm := cm.nodeMetrics[name]

if outputFormat == "wide" {
fmt.Fprintf(w, "%s \t *\t *\t %s \t %s \t %s \t %s \t %s \t %s \n", name,
nm.cpu.requestString(), nm.cpu.limitString(), nm.cpu.utilString(),
nm.memory.requestString(), nm.memory.limitString(), nm.memory.utilString())

for _, pm := range nm.podMetrics {
fmt.Fprintf(w, "%s \t %s \t %s \t %s \t %s \t %s \t %s \t %s \t %s \n", name,
pm.namespace, pm.name,
pm.cpu.requestStringPar(nm.cpu), pm.cpu.limitStringPar(nm.cpu), pm.cpu.utilStringPar(nm.cpu),
pm.memory.requestStringPar(nm.memory), pm.memory.limitStringPar(nm.memory), pm.memory.utilStringPar(nm.memory))
}
fmt.Fprintln(w, "\t\t\t\t\t\t\t\t")
} else {
fmt.Fprintf(w, "%s \t %s \t %s \t %s \t %s \n", name,
nm.cpu.requestString(), nm.cpu.limitString(),
nm.memory.requestString(), nm.memory.limitString())
}
}

w.Flush()
printList(&cm, showPods, showUtil)
}
156 changes: 156 additions & 0 deletions pkg/capacity/printer.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
// Copyright 2019 Rob Scott
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package capacity

import (
"fmt"
"os"
"sort"
"text/tabwriter"
)

func printList(cm *clusterMetric, showPods bool, showUtil bool) {
names := make([]string, len(cm.nodeMetrics))

i := 0
for name := range cm.nodeMetrics {
names[i] = name
i++
}
sort.Strings(names)

w := new(tabwriter.Writer)
w.Init(os.Stdout, 0, 8, 2, ' ', 0)

printHeaders(w, cm, showPods, showUtil)

for _, name := range names {
printNode(w, name, cm.nodeMetrics[name], showPods, showUtil)
}

w.Flush()
}

func printHeaders(w *tabwriter.Writer, cm *clusterMetric, showPods bool, showUtil bool) {
if showPods && showUtil {
fmt.Fprintln(w, "NODE\t NAMESPACE\t POD\t CPU REQUESTS \t CPU LIMITS \t CPU UTIL \t MEMORY REQUESTS \t MEMORY LIMITS \t MEMORY UTIL")

if len(cm.nodeMetrics) > 1 {
fmt.Fprintf(w, "* \t *\t *\t %s \t %s \t %s \t %s \t %s \t %s \n",
cm.cpu.requestString(),
cm.cpu.limitString(),
cm.cpu.utilString(),
cm.memory.requestString(),
cm.memory.limitString(),
cm.memory.utilString())

fmt.Fprintln(w, "\t\t\t\t\t\t\t\t")
}
} else if showPods {
fmt.Fprintln(w, "NODE\t NAMESPACE\t POD\t CPU REQUESTS \t CPU LIMITS \t MEMORY REQUESTS \t MEMORY LIMITS")

fmt.Fprintf(w, "* \t *\t *\t %s \t %s \t %s \t %s \n",
cm.cpu.requestString(),
cm.cpu.limitString(),
cm.memory.requestString(),
cm.memory.limitString())

fmt.Fprintln(w, "\t\t\t\t\t\t")

} else if showUtil {
fmt.Fprintln(w, "NODE\t CPU REQUESTS \t CPU LIMITS \t CPU UTIL \t MEMORY REQUESTS \t MEMORY LIMITS \t MEMORY UTIL")

fmt.Fprintf(w, "* \t %s \t %s \t %s \t %s \t %s \t %s \n",
cm.cpu.requestString(),
cm.cpu.limitString(),
cm.cpu.utilString(),
cm.memory.requestString(),
cm.memory.limitString(),
cm.memory.utilString())

} else {
fmt.Fprintln(w, "NODE\t CPU REQUESTS \t CPU LIMITS \t MEMORY REQUESTS \t MEMORY LIMITS")

if len(cm.nodeMetrics) > 1 {
fmt.Fprintf(w, "* \t %s \t %s \t %s \t %s \n",
cm.cpu.requestString(), cm.cpu.limitString(),
cm.memory.requestString(), cm.memory.limitString())
}
}
}

func printNode(w *tabwriter.Writer, name string, nm *nodeMetric, showPods bool, showUtil bool) {
if showPods && showUtil {
fmt.Fprintf(w, "%s \t *\t *\t %s \t %s \t %s \t %s \t %s \t %s \n",
name,
nm.cpu.requestString(),
nm.cpu.limitString(),
nm.cpu.utilString(),
nm.memory.requestString(),
nm.memory.limitString(),
nm.memory.utilString())

for _, pm := range nm.podMetrics {
fmt.Fprintf(w, "%s \t %s \t %s \t %s \t %s \t %s \t %s \t %s \t %s \n",
name,
pm.namespace,
pm.name,
pm.cpu.requestStringPar(nm.cpu),
pm.cpu.limitStringPar(nm.cpu),
pm.cpu.utilStringPar(nm.cpu),
pm.memory.requestStringPar(nm.memory),
pm.memory.limitStringPar(nm.memory),
pm.memory.utilStringPar(nm.memory))
}

fmt.Fprintln(w, "\t\t\t\t\t\t\t\t")

} else if showPods {
fmt.Fprintf(w, "%s \t *\t *\t %s \t %s \t %s \t %s \n",
name,
nm.cpu.requestString(),
nm.cpu.limitString(),
nm.memory.requestString(),
nm.memory.limitString())

for _, pm := range nm.podMetrics {
fmt.Fprintf(w, "%s \t %s \t %s \t %s \t %s \t %s \t %s \n",
name,
pm.namespace,
pm.name,
pm.cpu.requestStringPar(nm.cpu),
pm.cpu.limitStringPar(nm.cpu),
pm.memory.requestStringPar(nm.memory),
pm.memory.limitStringPar(nm.memory))
}

fmt.Fprintln(w, "\t\t\t\t\t\t")

} else if showUtil {
fmt.Fprintf(w, "%s \t %s \t %s \t %s \t %s \t %s \t %s \n",
name,
nm.cpu.requestString(),
nm.cpu.limitString(),
nm.cpu.utilString(),
nm.memory.requestString(),
nm.memory.limitString(),
nm.memory.utilString())

} else {
fmt.Fprintf(w, "%s \t %s \t %s \t %s \t %s \n", name,
nm.cpu.requestString(), nm.cpu.limitString(),
nm.memory.requestString(), nm.memory.limitString())
}
}
8 changes: 5 additions & 3 deletions pkg/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ import (
"github.com/spf13/cobra"
)

var output string
var showPods bool
var showUtil bool

var rootCmd = &cobra.Command{
Use: "kube-capacity",
Expand All @@ -34,12 +35,13 @@ var rootCmd = &cobra.Command{
fmt.Printf("Error parsing flags: %v", err)
}

capacity.List(args, output)
capacity.List(args, showPods, showUtil)
},
}

func init() {
rootCmd.PersistentFlags().StringVarP(&output, "output", "o", "", "output format (normal,wide)")
rootCmd.PersistentFlags().BoolVarP(&showPods, "pods", "p", false, "Set this flag to include pods in output")
rootCmd.PersistentFlags().BoolVarP(&showUtil, "util", "u", false, "Set this flag to include resource utilization in output")
}

// Execute is the primary entrypoint for this CLI
Expand Down

0 comments on commit 96dab13

Please sign in to comment.