Skip to content

Commit

Permalink
Human readable values as part of struct (Cores and GiB) and now inclu…
Browse files Browse the repository at this point in the history
…ded in yaml/json output
  • Loading branch information
akrzos committed Jan 23, 2021
1 parent 4134041 commit 3fcc8b5
Show file tree
Hide file tree
Showing 6 changed files with 106 additions and 52 deletions.
21 changes: 11 additions & 10 deletions cmd/capacity/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"fmt"
"os"

"github.com/akrzos/kubeSize/internal/capacity"
"github.com/akrzos/kubeSize/internal/kube"
"github.com/akrzos/kubeSize/internal/output"
"github.com/pkg/errors"
Expand Down Expand Up @@ -107,16 +108,16 @@ var clusterCmd = &cobra.Command{
clusterCapacityData.TotalAvailableMemory.Sub(clusterCapacityData.TotalRequestsMemory)

// Populate "Human" readable capacity data values
clusterCapacityData.TotalCapacityCPUCores = output.ReadableCPU(clusterCapacityData.TotalCapacityCPU)
clusterCapacityData.TotalCapacityMemoryGiB = output.ReadableMem(clusterCapacityData.TotalCapacityMemory)
clusterCapacityData.TotalAllocatableCPUCores = output.ReadableCPU(clusterCapacityData.TotalAllocatableCPU)
clusterCapacityData.TotalAllocatableMemoryGiB = output.ReadableMem(clusterCapacityData.TotalAllocatableMemory)
clusterCapacityData.TotalAvailableCPUCores = output.ReadableCPU(clusterCapacityData.TotalAvailableCPU)
clusterCapacityData.TotalAvailableMemoryGiB = output.ReadableMem(clusterCapacityData.TotalAvailableMemory)
clusterCapacityData.TotalRequestsCPUCores = output.ReadableCPU(clusterCapacityData.TotalRequestsCPU)
clusterCapacityData.TotalLimitsCPUCores = output.ReadableCPU(clusterCapacityData.TotalLimitsCPU)
clusterCapacityData.TotalRequestsMemoryGiB = output.ReadableMem(clusterCapacityData.TotalRequestsMemory)
clusterCapacityData.TotalLimitsMemoryGiB = output.ReadableMem(clusterCapacityData.TotalLimitsMemory)
clusterCapacityData.TotalCapacityCPUCores = capacity.ReadableCPU(clusterCapacityData.TotalCapacityCPU)
clusterCapacityData.TotalCapacityMemoryGiB = capacity.ReadableMem(clusterCapacityData.TotalCapacityMemory)
clusterCapacityData.TotalAllocatableCPUCores = capacity.ReadableCPU(clusterCapacityData.TotalAllocatableCPU)
clusterCapacityData.TotalAllocatableMemoryGiB = capacity.ReadableMem(clusterCapacityData.TotalAllocatableMemory)
clusterCapacityData.TotalAvailableCPUCores = capacity.ReadableCPU(clusterCapacityData.TotalAvailableCPU)
clusterCapacityData.TotalAvailableMemoryGiB = capacity.ReadableMem(clusterCapacityData.TotalAvailableMemory)
clusterCapacityData.TotalRequestsCPUCores = capacity.ReadableCPU(clusterCapacityData.TotalRequestsCPU)
clusterCapacityData.TotalLimitsCPUCores = capacity.ReadableCPU(clusterCapacityData.TotalLimitsCPU)
clusterCapacityData.TotalRequestsMemoryGiB = capacity.ReadableMem(clusterCapacityData.TotalRequestsMemory)
clusterCapacityData.TotalLimitsMemoryGiB = capacity.ReadableMem(clusterCapacityData.TotalLimitsMemory)

displayDefault, _ := cmd.Flags().GetBool("default-format")

Expand Down
8 changes: 8 additions & 0 deletions cmd/capacity/namespace.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,14 @@ var namespaceCmd = &cobra.Command{
}
}

// Populate "Human" readable capacity data values
for _, namespace := range namespaceNames {
namespaceCapacityData[namespace].TotalRequestsCPUCores = capacity.ReadableCPU(namespaceCapacityData[namespace].TotalRequestsCPU)
namespaceCapacityData[namespace].TotalLimitsCPUCores = capacity.ReadableCPU(namespaceCapacityData[namespace].TotalLimitsCPU)
namespaceCapacityData[namespace].TotalRequestsMemoryGiB = capacity.ReadableMem(namespaceCapacityData[namespace].TotalRequestsMemory)
namespaceCapacityData[namespace].TotalLimitsMemoryGiB = capacity.ReadableMem(namespaceCapacityData[namespace].TotalLimitsMemory)
}

sort.Strings(namespaceNames)

displayDefault, _ := cmd.Flags().GetBool("default-format")
Expand Down
15 changes: 15 additions & 0 deletions cmd/capacity/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"sort"
"strings"

"github.com/akrzos/kubeSize/internal/capacity"
"github.com/akrzos/kubeSize/internal/kube"
"github.com/akrzos/kubeSize/internal/output"
"github.com/pkg/errors"
Expand Down Expand Up @@ -141,6 +142,20 @@ var nodeCmd = &cobra.Command{
nodesByRole["~"] = append(nodesByRole["~"], "unassigned")
}

// Populate "Human" readable capacity data values
for _, node := range nodeNames {
nodesCapacityData[node].TotalCapacityCPUCores = capacity.ReadableCPU(nodesCapacityData[node].TotalCapacityCPU)
nodesCapacityData[node].TotalCapacityMemoryGiB = capacity.ReadableMem(nodesCapacityData[node].TotalCapacityMemory)
nodesCapacityData[node].TotalAllocatableCPUCores = capacity.ReadableCPU(nodesCapacityData[node].TotalAllocatableCPU)
nodesCapacityData[node].TotalAllocatableMemoryGiB = capacity.ReadableMem(nodesCapacityData[node].TotalAllocatableMemory)
nodesCapacityData[node].TotalRequestsCPUCores = capacity.ReadableCPU(nodesCapacityData[node].TotalRequestsCPU)
nodesCapacityData[node].TotalLimitsCPUCores = capacity.ReadableCPU(nodesCapacityData[node].TotalLimitsCPU)
nodesCapacityData[node].TotalAvailableCPUCores = capacity.ReadableCPU(nodesCapacityData[node].TotalAvailableCPU)
nodesCapacityData[node].TotalRequestsMemoryGiB = capacity.ReadableMem(nodesCapacityData[node].TotalRequestsMemory)
nodesCapacityData[node].TotalLimitsMemoryGiB = capacity.ReadableMem(nodesCapacityData[node].TotalLimitsMemory)
nodesCapacityData[node].TotalAvailableMemoryGiB = capacity.ReadableMem(nodesCapacityData[node].TotalAvailableMemory)
}

sortByRole, _ := cmd.Flags().GetBool("sort-by-role")

output.DisplayNodeData(nodesCapacityData, nodeNames, displayDefault, !displayNoHeaders, displayFormat, sortByRole, nodesByRole)
Expand Down
14 changes: 14 additions & 0 deletions cmd/capacity/noderole.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,20 @@ var nodeRoleCmd = &cobra.Command{
roleNames = append(roleNames, "unassigned")
}

// Populate "Human" readable capacity data values
for _, role := range roleNames {
nodeRoleCapacityData[role].TotalCapacityCPUCores = capacity.ReadableCPU(nodeRoleCapacityData[role].TotalCapacityCPU)
nodeRoleCapacityData[role].TotalCapacityMemoryGiB = capacity.ReadableMem(nodeRoleCapacityData[role].TotalCapacityMemory)
nodeRoleCapacityData[role].TotalAllocatableCPUCores = capacity.ReadableCPU(nodeRoleCapacityData[role].TotalAllocatableCPU)
nodeRoleCapacityData[role].TotalAllocatableMemoryGiB = capacity.ReadableMem(nodeRoleCapacityData[role].TotalAllocatableMemory)
nodeRoleCapacityData[role].TotalRequestsCPUCores = capacity.ReadableCPU(nodeRoleCapacityData[role].TotalRequestsCPU)
nodeRoleCapacityData[role].TotalLimitsCPUCores = capacity.ReadableCPU(nodeRoleCapacityData[role].TotalLimitsCPU)
nodeRoleCapacityData[role].TotalAvailableCPUCores = capacity.ReadableCPU(nodeRoleCapacityData[role].TotalAvailableCPU)
nodeRoleCapacityData[role].TotalRequestsMemoryGiB = capacity.ReadableMem(nodeRoleCapacityData[role].TotalRequestsMemory)
nodeRoleCapacityData[role].TotalLimitsMemoryGiB = capacity.ReadableMem(nodeRoleCapacityData[role].TotalLimitsMemory)
nodeRoleCapacityData[role].TotalAvailableMemoryGiB = capacity.ReadableMem(nodeRoleCapacityData[role].TotalAvailableMemory)
}

output.DisplayNodeRoleData(nodeRoleCapacityData, roleNames, displayDefault, !displayNoHeaders, displayFormat)

return nil
Expand Down
12 changes: 12 additions & 0 deletions internal/capacity/capacity.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ limitations under the License.
*/
package capacity

import "k8s.io/apimachinery/pkg/api/resource"

func StringInSlice(a string, list []string) bool {
for _, b := range list {
if b == a {
Expand All @@ -23,3 +25,13 @@ func StringInSlice(a string, list []string) bool {
}
return false
}

func ReadableCPU(cpu resource.Quantity) float64 {
// Convert millicores to cores
return float64(cpu.MilliValue()) / 1000
}

func ReadableMem(mem resource.Quantity) float64 {
// Convert from KiB to GiB
return float64(mem.Value()) / 1024 / 1024 / 1024
}
88 changes: 46 additions & 42 deletions internal/output/output.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,34 +69,48 @@ type ClusterCapacityData struct {
}

type NodeCapacityData struct {
TotalPodCount int
TotalNonTermPodCount int
Roles sets.String
Ready bool
Schedulable bool
TotalCapacityPods resource.Quantity
TotalCapacityCPU resource.Quantity
TotalCapacityMemory resource.Quantity
TotalAllocatablePods resource.Quantity
TotalAllocatableCPU resource.Quantity
TotalAllocatableMemory resource.Quantity
TotalAvailablePods int
TotalRequestsCPU resource.Quantity
TotalLimitsCPU resource.Quantity
TotalAvailableCPU resource.Quantity
TotalRequestsMemory resource.Quantity
TotalLimitsMemory resource.Quantity
TotalAvailableMemory resource.Quantity
TotalPodCount int
TotalNonTermPodCount int
Roles sets.String
Ready bool
Schedulable bool
TotalCapacityPods resource.Quantity
TotalCapacityCPU resource.Quantity
TotalCapacityCPUCores float64
TotalCapacityMemory resource.Quantity
TotalCapacityMemoryGiB float64
TotalAllocatablePods resource.Quantity
TotalAllocatableCPU resource.Quantity
TotalAllocatableCPUCores float64
TotalAllocatableMemory resource.Quantity
TotalAllocatableMemoryGiB float64
TotalAvailablePods int
TotalRequestsCPU resource.Quantity
TotalRequestsCPUCores float64
TotalLimitsCPU resource.Quantity
TotalLimitsCPUCores float64
TotalAvailableCPU resource.Quantity
TotalAvailableCPUCores float64
TotalRequestsMemory resource.Quantity
TotalRequestsMemoryGiB float64
TotalLimitsMemory resource.Quantity
TotalLimitsMemoryGiB float64
TotalAvailableMemory resource.Quantity
TotalAvailableMemoryGiB float64
}

type NamespaceCapacityData struct {
TotalPodCount int
TotalNonTermPodCount int
TotalUnassignedNodePodCount int
TotalRequestsCPU resource.Quantity
TotalRequestsCPUCores float64
TotalLimitsCPU resource.Quantity
TotalLimitsCPUCores float64
TotalRequestsMemory resource.Quantity
TotalRequestsMemoryGiB float64
TotalLimitsMemory resource.Quantity
TotalLimitsMemoryGiB float64
}

func DisplayClusterData(clusterCapacityData ClusterCapacityData, displayDefault bool, displayHeaders bool, displayFormat string) {
Expand Down Expand Up @@ -190,12 +204,12 @@ func DisplayNodeRoleData(nodeRoleCapacityData map[string]*ClusterCapacityData, s
fmt.Fprintf(w, "%s\t%s\t", &nodeRoleCapacityData[k].TotalRequestsMemory, &nodeRoleCapacityData[k].TotalLimitsMemory)
fmt.Fprintf(w, "%s\t\n", &nodeRoleCapacityData[k].TotalAvailableMemory)
} else {
fmt.Fprintf(w, "%.1f\t%.1f\t", ReadableCPU(nodeRoleCapacityData[k].TotalCapacityCPU), ReadableCPU(nodeRoleCapacityData[k].TotalAllocatableCPU))
fmt.Fprintf(w, "%.1f\t%.1f\t", ReadableCPU(nodeRoleCapacityData[k].TotalRequestsCPU), ReadableCPU(nodeRoleCapacityData[k].TotalLimitsCPU))
fmt.Fprintf(w, "%.1f\t", ReadableCPU(nodeRoleCapacityData[k].TotalAvailableCPU))
fmt.Fprintf(w, "%.1f\t%.1f\t", ReadableMem(nodeRoleCapacityData[k].TotalCapacityMemory), ReadableMem(nodeRoleCapacityData[k].TotalAllocatableMemory))
fmt.Fprintf(w, "%.1f\t%.1f\t", ReadableMem(nodeRoleCapacityData[k].TotalRequestsMemory), ReadableMem(nodeRoleCapacityData[k].TotalLimitsMemory))
fmt.Fprintf(w, "%.1f\t\n", ReadableMem(nodeRoleCapacityData[k].TotalAvailableMemory))
fmt.Fprintf(w, "%.1f\t%.1f\t", nodeRoleCapacityData[k].TotalCapacityCPUCores, nodeRoleCapacityData[k].TotalAllocatableCPUCores)
fmt.Fprintf(w, "%.1f\t%.1f\t", nodeRoleCapacityData[k].TotalRequestsCPUCores, nodeRoleCapacityData[k].TotalLimitsCPUCores)
fmt.Fprintf(w, "%.1f\t", nodeRoleCapacityData[k].TotalAvailableCPUCores)
fmt.Fprintf(w, "%.1f\t%.1f\t", nodeRoleCapacityData[k].TotalCapacityMemoryGiB, nodeRoleCapacityData[k].TotalAllocatableMemoryGiB)
fmt.Fprintf(w, "%.1f\t%.1f\t", nodeRoleCapacityData[k].TotalRequestsMemoryGiB, nodeRoleCapacityData[k].TotalLimitsMemoryGiB)
fmt.Fprintf(w, "%.1f\t\n", nodeRoleCapacityData[k].TotalAvailableMemoryGiB)
}
}
w.Flush()
Expand Down Expand Up @@ -279,12 +293,12 @@ func printNodeData(w *tabwriter.Writer, nodeName string, nodeData *NodeCapacityD
fmt.Fprintf(w, "%s\t%s\t", &nodeData.TotalRequestsMemory, &nodeData.TotalLimitsMemory)
fmt.Fprintf(w, "%s\t\n", &nodeData.TotalAvailableMemory)
} else {
fmt.Fprintf(w, "%.1f\t%.1f\t", ReadableCPU(nodeData.TotalCapacityCPU), ReadableCPU(nodeData.TotalAllocatableCPU))
fmt.Fprintf(w, "%.1f\t%.1f\t", ReadableCPU(nodeData.TotalRequestsCPU), ReadableCPU(nodeData.TotalLimitsCPU))
fmt.Fprintf(w, "%.1f\t", ReadableCPU(nodeData.TotalAvailableCPU))
fmt.Fprintf(w, "%.1f\t%.1f\t", ReadableMem(nodeData.TotalCapacityMemory), ReadableMem(nodeData.TotalAllocatableMemory))
fmt.Fprintf(w, "%.1f\t%.1f\t", ReadableMem(nodeData.TotalRequestsMemory), ReadableMem(nodeData.TotalLimitsMemory))
fmt.Fprintf(w, "%.1f\t\n", ReadableMem(nodeData.TotalAvailableMemory))
fmt.Fprintf(w, "%.1f\t%.1f\t", nodeData.TotalCapacityCPUCores, nodeData.TotalAllocatableCPUCores)
fmt.Fprintf(w, "%.1f\t%.1f\t", nodeData.TotalRequestsCPUCores, nodeData.TotalLimitsCPUCores)
fmt.Fprintf(w, "%.1f\t", nodeData.TotalAvailableCPUCores)
fmt.Fprintf(w, "%.1f\t%.1f\t", nodeData.TotalCapacityMemoryGiB, nodeData.TotalAllocatableMemoryGiB)
fmt.Fprintf(w, "%.1f\t%.1f\t", nodeData.TotalRequestsMemoryGiB, nodeData.TotalLimitsMemoryGiB)
fmt.Fprintf(w, "%.1f\t\n", nodeData.TotalAvailableMemoryGiB)
}
}

Expand Down Expand Up @@ -323,8 +337,8 @@ func DisplayNamespaceData(namespaceCapacityData map[string]*NamespaceCapacityDat
fmt.Fprintf(w, "%s\t%s\t", &namespaceCapacityData[k].TotalRequestsCPU, &namespaceCapacityData[k].TotalLimitsCPU)
fmt.Fprintf(w, "%s\t%s\t\n", &namespaceCapacityData[k].TotalRequestsMemory, &namespaceCapacityData[k].TotalLimitsMemory)
} else {
fmt.Fprintf(w, "%.1f\t%.1f\t", ReadableCPU(namespaceCapacityData[k].TotalRequestsCPU), ReadableCPU(namespaceCapacityData[k].TotalLimitsCPU))
fmt.Fprintf(w, "%.1f\t%.1f\t\n", ReadableMem(namespaceCapacityData[k].TotalRequestsMemory), ReadableMem(namespaceCapacityData[k].TotalLimitsMemory))
fmt.Fprintf(w, "%.1f\t%.1f\t", namespaceCapacityData[k].TotalRequestsCPUCores, namespaceCapacityData[k].TotalLimitsCPUCores)
fmt.Fprintf(w, "%.1f\t%.1f\t\n", namespaceCapacityData[k].TotalRequestsMemoryGiB, namespaceCapacityData[k].TotalLimitsMemoryGiB)
}
}
}
Expand All @@ -345,13 +359,3 @@ func ValidateOutput(cmd cobra.Command) error {
}
return fmt.Errorf("Display Format \"%s\" is invalid. Valid values are %v", displayFormat, validOutputs)
}

func ReadableCPU(cpu resource.Quantity) float64 {
// Convert millicores to cores
return float64(cpu.MilliValue()) / 1000
}

func ReadableMem(mem resource.Quantity) float64 {
// Convert from KiB to GiB
return float64(mem.Value()) / 1024 / 1024 / 1024
}

0 comments on commit 3fcc8b5

Please sign in to comment.