Skip to content

Commit

Permalink
Subcommand aliases and node status on node subcommand
Browse files Browse the repository at this point in the history
  • Loading branch information
akrzos committed Jan 8, 2021
1 parent b909816 commit 252ec62
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 19 deletions.
9 changes: 4 additions & 5 deletions cmd/capacity/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,10 @@ import (
)

var clusterCmd = &cobra.Command{
Use: "cluster",
Short: "Get cluster size and capacity",
Long: `Get Kubernetes cluster size and capacity metrics`,
SilenceErrors: true,
SilenceUsage: true,
Use: "cluster",
Aliases: []string{"c"},
Short: "Get cluster size and capacity",
Long: `Get Kubernetes cluster size and capacity metrics`,
PreRun: func(cmd *cobra.Command, args []string) {
viper.BindPFlags(cmd.Flags())
if err := output.ValidateOutput(*cmd); err != nil {
Expand Down
7 changes: 4 additions & 3 deletions cmd/capacity/namespace.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,10 @@ import (
)

var namespaceCmd = &cobra.Command{
Use: "namespace",
Short: "Get namespace size",
Long: `Get namespace size and capacity metrics`,
Use: "namespace",
Aliases: []string{"na"},
Short: "Get namespace size",
Long: `Get namespace size and capacity metrics`,
PreRun: func(cmd *cobra.Command, args []string) {
viper.BindPFlags(cmd.Flags())
if err := output.ValidateOutput(*cmd); err != nil {
Expand Down
17 changes: 12 additions & 5 deletions cmd/capacity/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,10 @@ import (
)

var nodeCmd = &cobra.Command{
Use: "node",
Short: "Get individual node capacity",
Long: `Get individual node size and capacity metrics grouped by node role`,
SilenceErrors: true,
SilenceUsage: true,
Use: "node",
Aliases: []string{"no"},
Short: "Get individual node capacity",
Long: `Get individual node size and capacity metrics grouped by node role`,
PreRun: func(cmd *cobra.Command, args []string) {
viper.BindPFlags(cmd.Flags())
if err := output.ValidateOutput(*cmd); err != nil {
Expand Down Expand Up @@ -93,6 +92,14 @@ var nodeCmd = &cobra.Command{
newNodeData := new(output.NodeCapacityData)
newNodeData.TotalPodCount = len(nodePodsList.Items)
newNodeData.TotalNonTermPodCount = len(totalNonTermPodsList.Items)
newNodeData.Ready = false
for _, condition := range v.Status.Conditions {
if (condition.Type == "Ready") && condition.Status == corev1.ConditionTrue {
newNodeData.Ready = true
break
}
}
newNodeData.Schedulable = !v.Spec.Unschedulable
newNodeData.Roles = roles
newNodeData.TotalCapacityPods.Add(*v.Status.Capacity.Pods())
newNodeData.TotalCapacityCPU.Add(*v.Status.Capacity.Cpu())
Expand Down
7 changes: 4 additions & 3 deletions cmd/capacity/noderole.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,10 @@ import (
)

var nodeRoleCmd = &cobra.Command{
Use: "node-role",
Short: "Get cluster capacity grouped by node role",
Long: `Get Kubernetes cluster size and capacity metrics grouped by node role`,
Use: "node-role",
Aliases: []string{"nr"},
Short: "Get cluster capacity grouped by node role",
Long: `Get Kubernetes cluster size and capacity metrics grouped by node role`,
PreRun: func(cmd *cobra.Command, args []string) {
viper.BindPFlags(cmd.Flags())
if err := output.ValidateOutput(*cmd); err != nil {
Expand Down
17 changes: 14 additions & 3 deletions internal/output/output.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ type NodeCapacityData struct {
TotalPodCount int
TotalNonTermPodCount int
Roles sets.String
Ready bool
Schedulable bool
TotalCapacityPods resource.Quantity
TotalCapacityCPU resource.Quantity
TotalCapacityMemory resource.Quantity
Expand Down Expand Up @@ -165,14 +167,23 @@ func DisplayNodeData(nodesCapacityData map[string]*NodeCapacityData, sortedNodeN
w := new(tabwriter.Writer)
w.Init(os.Stdout, 0, 5, 1, ' ', 0)
if displayReadable == true {
fmt.Fprintln(w, "NAME\tROLES\tPODS\t\t\t\tCPU (cores)\t\t\t\tMEMORY (GiB)\t\t")
fmt.Fprintln(w, "NAME\tSTATUS\tROLES\tPODS\t\t\t\tCPU (cores)\t\t\t\tMEMORY (GiB)\t\t")
} else {
fmt.Fprintln(w, "NAME\tROLES\tPODS\t\t\t\tCPU\t\t\t\tMEMORY\t\t")
fmt.Fprintln(w, "NAME\tSTATUS\tROLES\tPODS\t\t\t\tCPU\t\t\t\tMEMORY\t\t")
}
fmt.Fprintln(w, "\t\tCapacity\tAllocatable\tTotal\tNon-Term\tCapacity\tAllocatable\tRequests\tLimits\tCapacity\tAllocatable\tRequests\tLimits")
fmt.Fprintln(w, "\t\t\tCapacity\tAllocatable\tTotal\tNon-Term\tCapacity\tAllocatable\tRequests\tLimits\tCapacity\tAllocatable\tRequests\tLimits")

for _, k := range sortedNodeNames {
fmt.Fprintf(w, "%s\t", k)
if nodesCapacityData[k].Ready {
fmt.Fprint(w, "Ready")
} else {
fmt.Fprint(w, "NotReady")
}
if !nodesCapacityData[k].Schedulable {
fmt.Fprintf(w, ",Unschedulable")
}
fmt.Fprintf(w, "\t")
fmt.Fprintf(w, "%s\t", strings.Join(nodesCapacityData[k].Roles.List(), ","))
fmt.Fprintf(w, "%s\t%s\t", &nodesCapacityData[k].TotalCapacityPods, &nodesCapacityData[k].TotalCapacityPods)
fmt.Fprintf(w, "%d\t%d\t", nodesCapacityData[k].TotalPodCount, nodesCapacityData[k].TotalNonTermPodCount)
Expand Down

0 comments on commit 252ec62

Please sign in to comment.