Skip to content

Commit

Permalink
fix limit and request availability with -a with json and yaml
Browse files Browse the repository at this point in the history
  • Loading branch information
ben.elyess@cgm.com committed Jan 7, 2025
1 parent 5c0f8c9 commit 4ae983a
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 18 deletions.
37 changes: 25 additions & 12 deletions pkg/capacity/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,14 @@ type listContainer struct {
}

type listResourceOutput struct {
Requests string `json:"requests"`
RequestsPct string `json:"requestsPercent"`
Limits string `json:"limits"`
LimitsPct string `json:"limitsPercent"`
Utilization string `json:"utilization,omitempty"`
UtilizationPct string `json:"utilizationPercent,omitempty"`
Requests string `json:"requests"`
RequestsPct string `json:"requestsPercent"`
RequestsAvailable string `json:"requestsAvailable,omitempty"`
Limits string `json:"limits"`
LimitsPct string `json:"limitsPercent"`
LimitsAvailable string `json:"limitsAvailable,omitempty"`
Utilization string `json:"utilization,omitempty"`
UtilizationPct string `json:"utilizationPercent,omitempty"`
}

type listClusterMetrics struct {
Expand All @@ -64,12 +66,13 @@ type listClusterTotals struct {
}

type listPrinter struct {
cm *clusterMetric
showPods bool
showContainers bool
showUtil bool
showPodCount bool
sortBy string
cm *clusterMetric
showPods bool
showContainers bool
showUtil bool
showPodCount bool
sortBy string
availableFormat bool
}

func (lp listPrinter) Print(outputType string) {
Expand Down Expand Up @@ -156,6 +159,16 @@ func (lp *listPrinter) buildListResourceOutput(item *resourceMetric) *listResour
LimitsPct: percentCalculator(item.limit),
}

if lp.availableFormat {
available := item.allocatable.DeepCopy()
available.Sub(item.request)
out.RequestsAvailable = valueCalculator(available)

available = item.allocatable.DeepCopy()
available.Sub(item.limit)
out.LimitsAvailable = valueCalculator(available)
}

if lp.showUtil {
out.Utilization = valueCalculator(item.utilization)
out.UtilizationPct = percentCalculator(item.utilization)
Expand Down
13 changes: 7 additions & 6 deletions pkg/capacity/printer.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,13 @@ func SupportedOutputs() []string {
func printList(cm *clusterMetric, showContainers, showPods, showUtil, showPodCount, showNamespace bool, output, sortBy string, availableFormat bool) {
if output == JSONOutput || output == YAMLOutput {
lp := &listPrinter{
cm: cm,
showPods: showPods,
showUtil: showUtil,
showContainers: showContainers,
showPodCount: showPodCount,
sortBy: sortBy,
cm: cm,
showPods: showPods,
showUtil: showUtil,
showContainers: showContainers,
showPodCount: showPodCount,
sortBy: sortBy,
availableFormat: availableFormat,
}
lp.Print(output)
} else if output == TableOutput {
Expand Down

0 comments on commit 4ae983a

Please sign in to comment.