Skip to content
Merged
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
20 changes: 12 additions & 8 deletions client/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ import (
"k8s.io/klog/v2"
)

func GetPodResourceUsage(pc promv1.API, obj metav1.ObjectMeta) core.ResourceList {
func GetPodResourceUsage(pc promv1.API, podMeta metav1.ObjectMeta) core.ResourceList {
resUsage := core.ResourceList{}

// Previous way: sum(node_namespace_pod_container:container_cpu_usage_seconds_total:sum_irate{namespace="%s", pod="%s", container!=""})
Expand All @@ -46,9 +46,8 @@ func GetPodResourceUsage(pc promv1.API, obj metav1.ObjectMeta) core.ResourceList
kube_pod_info{node!="",namespace="%s", pod="%s"}
)
)
) by (pod)`, obj.Namespace, obj.Name, obj.Namespace, obj.Name)
promMemoryQuery := fmt.Sprintf(`sum(container_memory_working_set_bytes{namespace="%s", pod="%s", container!="", image!=""})`, obj.Namespace, obj.Name)
promStorageQuery := fmt.Sprintf(`avg(container_blkio_device_usage_total{namespace="%s", pod="%s"})`, obj.Namespace, obj.Name)
) by (pod)`, podMeta.Namespace, podMeta.Name, podMeta.Namespace, podMeta.Name)
promMemoryQuery := fmt.Sprintf(`sum(container_memory_working_set_bytes{namespace="%s", pod="%s", container!="", image!=""})`, podMeta.Namespace, podMeta.Name)

if res, err := getPromQueryResult(pc, promCPUQuery); err == nil {
cpu := float64(0)
Expand Down Expand Up @@ -80,6 +79,12 @@ func GetPodResourceUsage(pc promv1.API, obj metav1.ObjectMeta) core.ResourceList
klog.Infoln("failed to execute prometheus memory query")
}

return resUsage
}

func GetPVCUsage(pc promv1.API, pvcMeta metav1.ObjectMeta) resource.Quantity {
promStorageQuery := fmt.Sprintf(`kubelet_volume_stats_used_bytes{namespace="%s", persistentvolumeclaim="%s"}`, pvcMeta.Namespace, pvcMeta.Name)
var quan resource.Quantity
if res, err := getPromQueryResult(pc, promStorageQuery); err == nil {
storage := float64(0)
for _, v := range res {
Expand All @@ -88,14 +93,13 @@ func GetPodResourceUsage(pc promv1.API, obj metav1.ObjectMeta) core.ResourceList
storageQuantity, err := resource.ParseQuantity(convertBytesToSize(storage))
if err != nil {
klog.Errorf("failed to parse memory quantity, reason: %v", err)
return resUsage
return quan
}
resUsage[core.ResourceStorage] = storageQuantity
return storageQuantity
} else {
klog.Infoln("failed to execute prometheus storage query")
}

return resUsage
return quan
}

func getPromQueryResult(pc promv1.API, promQuery string) (map[string]float64, error) {
Expand Down
Loading