Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support ephemeral storage request/limits #175

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 24 additions & 16 deletions pkg/capacity/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,25 +22,28 @@ import (
)

type listNodeMetric struct {
Name string `json:"name"`
CPU *listResourceOutput `json:"cpu,omitempty"`
Memory *listResourceOutput `json:"memory,omitempty"`
Pods []*listPod `json:"pods,omitempty"`
PodCount string `json:"podCount,omitempty"`
Name string `json:"name"`
CPU *listResourceOutput `json:"cpu,omitempty"`
Memory *listResourceOutput `json:"memory,omitempty"`
EphemeralStorage *listResourceOutput `json:"ephemeralStorage,omitempty"`
Pods []*listPod `json:"pods,omitempty"`
PodCount string `json:"podCount,omitempty"`
}

type listPod struct {
Name string `json:"name"`
Namespace string `json:"namespace"`
CPU *listResourceOutput `json:"cpu"`
Memory *listResourceOutput `json:"memory"`
Containers []listContainer `json:"containers,omitempty"`
Name string `json:"name"`
Namespace string `json:"namespace"`
CPU *listResourceOutput `json:"cpu"`
Memory *listResourceOutput `json:"memory"`
EphemeralStorage *listResourceOutput `json:"ephemeralStorage,omitempty"`
Containers []listContainer `json:"containers,omitempty"`
}

type listContainer struct {
Name string `json:"name"`
CPU *listResourceOutput `json:"cpu"`
Memory *listResourceOutput `json:"memory"`
Name string `json:"name"`
CPU *listResourceOutput `json:"cpu"`
Memory *listResourceOutput `json:"memory"`
EphemeralStorage *listResourceOutput `json:"ephemeralStorage,omitempty"`
}

type listResourceOutput struct {
Expand All @@ -58,9 +61,10 @@ type listClusterMetrics struct {
}

type listClusterTotals struct {
CPU *listResourceOutput `json:"cpu"`
Memory *listResourceOutput `json:"memory"`
PodCount string `json:"podCount,omitempty"`
CPU *listResourceOutput `json:"cpu"`
Memory *listResourceOutput `json:"memory"`
EphemeralStorage *listResourceOutput `json:"ephemeralStorage,omitempty"`
PodCount string `json:"podCount,omitempty"`
}

type listPrinter struct {
Expand Down Expand Up @@ -103,6 +107,7 @@ func (lp *listPrinter) buildListClusterMetrics() listClusterMetrics {
response.ClusterTotals = &listClusterTotals{
CPU: lp.buildListResourceOutput(lp.cm.cpu),
Memory: lp.buildListResourceOutput(lp.cm.memory),
EphemeralStorage: lp.buildListResourceOutput(lp.cm.ephemeralStorage),
}

if lp.showPodCount {
Expand All @@ -114,6 +119,7 @@ func (lp *listPrinter) buildListClusterMetrics() listClusterMetrics {
node.Name = nodeMetric.name
node.CPU = lp.buildListResourceOutput(nodeMetric.cpu)
node.Memory = lp.buildListResourceOutput(nodeMetric.memory)
node.EphemeralStorage = lp.buildListResourceOutput(nodeMetric.ephemeralStorage)

if lp.showPodCount {
node.PodCount = nodeMetric.podCount.podCountString()
Expand All @@ -126,13 +132,15 @@ func (lp *listPrinter) buildListClusterMetrics() listClusterMetrics {
pod.Namespace = podMetric.namespace
pod.CPU = lp.buildListResourceOutput(podMetric.cpu)
pod.Memory = lp.buildListResourceOutput(podMetric.memory)
pod.EphemeralStorage = lp.buildListResourceOutput(podMetric.ephemeralStorage)

if lp.showContainers {
for _, containerMetric := range podMetric.getSortedContainerMetrics(lp.sortBy) {
pod.Containers = append(pod.Containers, listContainer{
Name: containerMetric.name,
Memory: lp.buildListResourceOutput(containerMetric.memory),
CPU: lp.buildListResourceOutput(containerMetric.cpu),
EphemeralStorage: lp.buildListResourceOutput(containerMetric.ephemeralStorage),
})
}
}
Expand Down
1 change: 1 addition & 0 deletions pkg/capacity/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ type Options struct {
ShowPods bool
ShowUtil bool
ShowPodCount bool
ShowEphemeralStorage bool // New flag for ephemeral storage
PodLabels string
NodeLabels string
NodeTaints string
Expand Down
65 changes: 51 additions & 14 deletions pkg/capacity/resources.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,30 +55,34 @@ type resourceMetric struct {
type clusterMetric struct {
cpu *resourceMetric
memory *resourceMetric
ephemeralStorage *resourceMetric
nodeMetrics map[string]*nodeMetric
podCount *podCount
}

type nodeMetric struct {
name string
cpu *resourceMetric
memory *resourceMetric
podMetrics map[string]*podMetric
podCount *podCount
name string
cpu *resourceMetric
memory *resourceMetric
ephemeralStorage *resourceMetric
podMetrics map[string]*podMetric
podCount *podCount
}

type podMetric struct {
name string
namespace string
cpu *resourceMetric
memory *resourceMetric
containerMetrics map[string]*containerMetric
name string
namespace string
cpu *resourceMetric
memory *resourceMetric
ephemeralStorage *resourceMetric
containerMetrics map[string]*containerMetric
}

type containerMetric struct {
name string
cpu *resourceMetric
memory *resourceMetric
name string
cpu *resourceMetric
memory *resourceMetric
ephemeralStorage *resourceMetric
}

type podCount struct {
Expand All @@ -91,6 +95,7 @@ func buildClusterMetric(podList *corev1.PodList, pmList *v1beta1.PodMetricsList,
cm := clusterMetric{
cpu: &resourceMetric{resourceType: "cpu"},
memory: &resourceMetric{resourceType: "memory"},
ephemeralStorage: &resourceMetric{resourceType: "ephemeral-storage"},
nodeMetrics: map[string]*nodeMetric{},
podCount: &podCount{},
}
Expand All @@ -116,6 +121,10 @@ func buildClusterMetric(podList *corev1.PodList, pmList *v1beta1.PodMetricsList,
resourceType: "memory",
allocatable: node.Status.Allocatable["memory"],
},
ephemeralStorage: &resourceMetric{
resourceType: "ephemeral-storage",
allocatable: node.Status.Allocatable["ephemeral-storage"],
},
podMetrics: map[string]*podMetric{},
podCount: &podCount{
current: tmpPodCount,
Expand All @@ -134,6 +143,7 @@ func buildClusterMetric(podList *corev1.PodList, pmList *v1beta1.PodMetricsList,
}
cm.nodeMetrics[nm.Name].cpu.utilization = nm.Usage["cpu"]
cm.nodeMetrics[nm.Name].memory.utilization = nm.Usage["memory"]
cm.nodeMetrics[nm.Name].ephemeralStorage.utilization = nm.Usage["ephemeral-storage"]
}
}

Expand Down Expand Up @@ -189,6 +199,11 @@ func (cm *clusterMetric) addPodMetric(pod *corev1.Pod, podMetrics v1beta1.PodMet
request: req["memory"],
limit: limit["memory"],
},
ephemeralStorage: &resourceMetric{
resourceType: "ephemeral-storage",
request: req["ephemeral-storage"],
limit: limit["ephemeral-storage"],
},
containerMetrics: map[string]*containerMetric{},
}

Expand All @@ -207,18 +222,27 @@ func (cm *clusterMetric) addPodMetric(pod *corev1.Pod, podMetrics v1beta1.PodMet
limit: container.Resources.Limits["memory"],
allocatable: nm.memory.allocatable,
},
ephemeralStorage: &resourceMetric{
resourceType: "ephemeral-storage",
request: container.Resources.Requests["ephemeral-storage"],
limit: container.Resources.Limits["ephemeral-storage"],
allocatable: nm.ephemeralStorage.allocatable,
},
}
}

if nm != nil {
nm.podMetrics[key] = pm
nm.podMetrics[key].cpu.allocatable = nm.cpu.allocatable
nm.podMetrics[key].memory.allocatable = nm.memory.allocatable
nm.podMetrics[key].ephemeralStorage.allocatable = nm.ephemeralStorage.allocatable

nm.cpu.request.Add(req["cpu"])
nm.cpu.limit.Add(limit["cpu"])
nm.memory.request.Add(req["memory"])
nm.memory.limit.Add(limit["memory"])
nm.ephemeralStorage.request.Add(req["ephemeral-storage"])
nm.ephemeralStorage.limit.Add(limit["ephemeral-storage"])
}

for _, container := range podMetrics.Containers {
Expand All @@ -228,13 +252,16 @@ func (cm *clusterMetric) addPodMetric(pod *corev1.Pod, podMetrics v1beta1.PodMet
pm.cpu.utilization.Add(container.Usage["cpu"])
pm.containerMetrics[container.Name].memory.utilization = container.Usage["memory"]
pm.memory.utilization.Add(container.Usage["memory"])
pm.containerMetrics[container.Name].ephemeralStorage.utilization = container.Usage["ephemeral-storage"]
pm.ephemeralStorage.utilization.Add(container.Usage["ephemeral-storage"])
}
}
}

func (cm *clusterMetric) addNodeMetric(nm *nodeMetric) {
cm.cpu.addMetric(nm.cpu)
cm.memory.addMetric(nm.memory)
cm.ephemeralStorage.addMetric(nm.ephemeralStorage)
}

func (cm *clusterMetric) getSortedNodeMetrics(sortBy string) []*nodeMetric {
Expand Down Expand Up @@ -333,6 +360,7 @@ func (nm *nodeMetric) addPodUtilization() {
for _, pm := range nm.podMetrics {
nm.cpu.utilization.Add(pm.cpu.utilization)
nm.memory.utilization.Add(pm.memory.utilization)
nm.ephemeralStorage.utilization.Add(pm.ephemeralStorage.utilization)
}
}

Expand Down Expand Up @@ -403,6 +431,9 @@ func resourceString(resourceType string, actual, allocatable resource.Quantity,
case "memory":
actualStr = fmt.Sprintf("%dMi", formatToMegiBytes(allocatable)-formatToMegiBytes(actual))
allocatableStr = fmt.Sprintf("%dMi", formatToMegiBytes(allocatable))
case "ephemeral-storage":
actualStr = fmt.Sprintf("%dMi", formatToMegiBytes(allocatable)-formatToMegiBytes(actual))
allocatableStr = fmt.Sprintf("%dMi", formatToMegiBytes(allocatable))
default:
actualStr = fmt.Sprintf("%d", allocatable.Value()-actual.Value())
allocatableStr = fmt.Sprintf("%d", allocatable.Value())
Expand All @@ -416,6 +447,8 @@ func resourceString(resourceType string, actual, allocatable resource.Quantity,
actualStr = fmt.Sprintf("%dm", actual.MilliValue())
case "memory":
actualStr = fmt.Sprintf("%dMi", formatToMegiBytes(actual))
case "ephemeral-storage":
actualStr = fmt.Sprintf("%dMi", formatToMegiBytes(actual))
default:
actualStr = fmt.Sprintf("%d", actual.Value())
}
Expand All @@ -442,6 +475,10 @@ func (rm resourceMetric) valueFunction() (f func(r resource.Quantity) string) {
f = func(r resource.Quantity) string {
return fmt.Sprintf("%dMi", formatToMegiBytes(r))
}
case "ephemeral-storage":
f = func(r resource.Quantity) string {
return fmt.Sprintf("%dMi", formatToMegiBytes(r))
}
}
return f
}
Expand All @@ -462,7 +499,7 @@ func (rm resourceMetric) percent(r resource.Quantity) int64 {
// -----------------------------------------

func resourceCSVString(resourceType string, actual resource.Quantity) string {
if resourceType == "memory" {
if resourceType == "memory" || resourceType == "ephemeral-storage" {
return fmt.Sprintf("%d", formatToMegiBytes(actual))
}
return fmt.Sprintf("%d", actual.MilliValue())
Expand Down
Loading