Skip to content

Commit

Permalink
chore: properly print Quantities
Browse files Browse the repository at this point in the history
Signed-off-by: Philippe Scorsolini <p.scorsolini@gmail.com>
  • Loading branch information
phisco committed Jan 24, 2024
1 parent 098cf0f commit 0d475e3
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 21 deletions.
18 changes: 8 additions & 10 deletions cmd/crank/beta/top/top.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import (
"context"
"fmt"
"io"
"os"
"sort"
"strings"

Expand All @@ -43,13 +42,11 @@ const (
errCreateK8sClientset = "could not create the clientset for Kubernetes"
errCreateMetricsClientset = "could not create the clientset for Metrics"
errFetchAllPods = "could not fetch pods"
errNamespaceNotFound = "namespace does not exist"
errGetPodMetrics = "error getting metrics for pod"
errPrintingPodsTable = "error creating pods table"
errAddingPodMetrics = "error adding metrics to pod, check if metrics-server is running or wait until metrics are available for the pod"
errWriteHeader = "cannot write header"
errWriteRow = "cannot write row"
errFlushTabWriter = "cannot flush tab writer"
)

// Cmd represents the top command.
Expand Down Expand Up @@ -139,7 +136,7 @@ func (c *Cmd) Run(k *kong.Context, logger logging.Logger) error { //nolint:gocyc
}

crossplanePods := getCrossplanePods(pods.Items)
logger.Debug("Fetched all Crossplane pods", "pods", crossplanePods, " from namespace", c.Namespace)
logger.Debug("Fetched all Crossplane pods", "pods", crossplanePods, "namespace", c.Namespace)

if len(crossplanePods) == 0 {
fmt.Println("No Crossplane pods found in the namespace", c.Namespace)
Expand Down Expand Up @@ -174,7 +171,7 @@ func (c *Cmd) Run(k *kong.Context, logger logging.Logger) error { //nolint:gocyc
})

if c.Summary {
printPodsSummary(os.Stdout, crossplanePods)
printPodsSummary(k.Stdout, crossplanePods)
logger.Debug("Printed pods summary")
fmt.Println()
}
Expand All @@ -193,7 +190,7 @@ func printPodsTable(w io.Writer, crossplanePods []topMetrics) error {
podType: "TYPE",
namespace: "NAMESPACE",
name: "NAME",
cpu: "CPU(milliCPU)",
cpu: "CPU(cores)",
memory: "MEMORY",
}
_, err := fmt.Fprintln(tw, headers.String())
Expand All @@ -207,8 +204,9 @@ func printPodsTable(w io.Writer, crossplanePods []topMetrics) error {
podType: pod.PodType,
namespace: pod.PodNamespace,
name: pod.PodName,
cpu: pod.CPUUsage.String(),
memory: pod.MemoryUsage.String(),
// NOTE(phisco): inspired by https://github.com/kubernetes/kubectl/blob/97bd96adbceb24fd598bdc698da8794cb0b88e3b/pkg/metricsutil/metrics_printer.go#L209C6-L209C30
cpu: fmt.Sprintf("%vm", pod.CPUUsage.MilliValue()),
memory: fmt.Sprintf("%vMi", pod.MemoryUsage.Value()/(1024*1024)),
}
_, err := fmt.Fprintln(tw, row.String())
if err != nil {
Expand Down Expand Up @@ -243,8 +241,8 @@ func printPodsSummary(w io.Writer, pods []topMetrics) {
for _, category := range categories {
fmt.Fprintf(w, "%s: %d\n", capitalizeFirst(category), categoryCounts[category])
}
fmt.Fprintf(w, "Memory: %s\n", totalMemoryUsage.String())
fmt.Fprintf(w, "CPU(milliCPU): %s\n", totalCPUUsage.String())
fmt.Fprintf(w, "Memory: %s\n", fmt.Sprintf("%vMi", totalMemoryUsage.Value()/(1024*1024)))
fmt.Fprintf(w, "CPU(cores): %s\n", fmt.Sprintf("%vm", totalCPUUsage.MilliValue()))
}

func getCrossplanePods(pods []v1.Pod) []topMetrics {
Expand Down
22 changes: 11 additions & 11 deletions cmd/crank/beta/top/top_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ func TestPrintPodsTable(t *testing.T) {
crossplanePods: []topMetrics{},
want: want{
results: `
TYPE NAMESPACE NAME CPU(milliCPU) MEMORY
TYPE NAMESPACE NAME CPU(cores) MEMORY
`,
err: nil,
},
Expand All @@ -182,14 +182,14 @@ TYPE NAMESPACE NAME CPU(milliCPU) MEMORY
PodType: "crossplane",
PodName: "crossplane-123",
PodNamespace: "crossplane-system",
CPUUsage: resource.MustParse("100"),
CPUUsage: resource.MustParse("100m"),
MemoryUsage: resource.MustParse("512Mi"),
},
},
want: want{
results: `
TYPE NAMESPACE NAME CPU(milliCPU) MEMORY
crossplane crossplane-system crossplane-123 100 512Mi
TYPE NAMESPACE NAME CPU(cores) MEMORY
crossplane crossplane-system crossplane-123 100m 512Mi
`,
err: nil,
},
Expand All @@ -201,22 +201,22 @@ crossplane crossplane-system crossplane-123 100 512Mi
PodType: "crossplane",
PodName: "crossplane-123",
PodNamespace: "crossplane-system",
CPUUsage: resource.MustParse("100"),
CPUUsage: resource.MustParse("100m"),
MemoryUsage: resource.MustParse("512Mi"),
},
{
PodType: "function",
PodName: "function-123",
PodNamespace: "crossplane-system",
CPUUsage: resource.MustParse("200"),
CPUUsage: resource.MustParse("200m"),
MemoryUsage: resource.MustParse("1024Mi"),
},
},
want: want{
results: `
TYPE NAMESPACE NAME CPU(milliCPU) MEMORY
crossplane crossplane-system crossplane-123 100 512Mi
function crossplane-system function-123 200 1Gi
TYPE NAMESPACE NAME CPU(cores) MEMORY
crossplane crossplane-system crossplane-123 100m 512Mi
function crossplane-system function-123 200m 1024Mi
`,
err: nil,
},
Expand Down Expand Up @@ -282,8 +282,8 @@ func TestPrintPodsSummary(t *testing.T) {
Nr of Crossplane pods: 4
Crossplane: 2
Function: 2
Memory: 3Gi
CPU(milliCPU): 900
Memory: 3072Mi
CPU(cores): 900000m
`,
},
},
Expand Down

0 comments on commit 0d475e3

Please sign in to comment.