-
Notifications
You must be signed in to change notification settings - Fork 38.7k
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
Add pod-level metric for CPU and memory stats #55969
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -45,6 +45,8 @@ const ( | |
libcontainerCgroupfs libcontainerCgroupManagerType = "cgroupfs" | ||
// libcontainerSystemd means use libcontainer with systemd | ||
libcontainerSystemd libcontainerCgroupManagerType = "systemd" | ||
// systemdSuffix is the cgroup name suffix for systemd | ||
systemdSuffix string = ".slice" | ||
) | ||
|
||
// hugePageSizeList is useful for converting to the hugetlb canonical unit | ||
|
@@ -68,8 +70,8 @@ func ConvertCgroupNameToSystemd(cgroupName CgroupName, outputToCgroupFs bool) st | |
} | ||
// detect if we are given a systemd style name. | ||
// if so, we do not want to do double encoding. | ||
if strings.HasSuffix(part, ".slice") { | ||
part = strings.TrimSuffix(part, ".slice") | ||
if IsSystemdStyleName(part) { | ||
part = strings.TrimSuffix(part, systemdSuffix) | ||
separatorIndex := strings.LastIndex(part, "-") | ||
if separatorIndex >= 0 && separatorIndex < len(part) { | ||
part = part[separatorIndex+1:] | ||
|
@@ -87,8 +89,8 @@ func ConvertCgroupNameToSystemd(cgroupName CgroupName, outputToCgroupFs bool) st | |
result = "-" | ||
} | ||
// always have a .slice suffix | ||
if !strings.HasSuffix(result, ".slice") { | ||
result = result + ".slice" | ||
if !IsSystemdStyleName(result) { | ||
result = result + systemdSuffix | ||
} | ||
|
||
// if the caller desired the result in cgroupfs format... | ||
|
@@ -114,6 +116,13 @@ func ConvertCgroupFsNameToSystemd(cgroupfsName string) (string, error) { | |
return path.Base(cgroupfsName), nil | ||
} | ||
|
||
func IsSystemdStyleName(name string) bool { | ||
if strings.HasSuffix(name, systemdSuffix) { | ||
return true | ||
} | ||
return false | ||
} | ||
|
||
// libcontainerAdapter provides a simplified interface to libcontainer based on libcontainer type. | ||
type libcontainerAdapter struct { | ||
// cgroupManagerType defines how to interface with libcontainer | ||
|
@@ -151,15 +160,18 @@ func (l *libcontainerAdapter) revertName(name string) CgroupName { | |
if l.cgroupManagerType != libcontainerSystemd { | ||
return CgroupName(name) | ||
} | ||
return CgroupName(RevertFromSystemdToCgroupStyleName(name)) | ||
} | ||
|
||
func RevertFromSystemdToCgroupStyleName(name string) string { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. the typical convention in past has been outside of the cm package, users always worked with |
||
driverName, err := ConvertCgroupFsNameToSystemd(name) | ||
if err != nil { | ||
panic(err) | ||
} | ||
driverName = strings.TrimSuffix(driverName, ".slice") | ||
driverName = strings.TrimSuffix(driverName, systemdSuffix) | ||
driverName = strings.Replace(driverName, "-", "/", -1) | ||
driverName = strings.Replace(driverName, "_", "-", -1) | ||
return CgroupName(driverName) | ||
return driverName | ||
} | ||
|
||
// adaptName converts a CgroupName identifier to a driver specific conversion value. | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,6 +18,7 @@ package stats | |
|
||
import ( | ||
"fmt" | ||
"path" | ||
"sort" | ||
"strings" | ||
|
||
|
@@ -28,6 +29,7 @@ import ( | |
"k8s.io/apimachinery/pkg/types" | ||
statsapi "k8s.io/kubernetes/pkg/kubelet/apis/stats/v1alpha1" | ||
"k8s.io/kubernetes/pkg/kubelet/cadvisor" | ||
"k8s.io/kubernetes/pkg/kubelet/cm" | ||
kubecontainer "k8s.io/kubernetes/pkg/kubelet/container" | ||
"k8s.io/kubernetes/pkg/kubelet/leaky" | ||
"k8s.io/kubernetes/pkg/kubelet/server/stats" | ||
|
@@ -89,9 +91,9 @@ func (p *cadvisorStatsProvider) ListPodStats() ([]statsapi.PodStats, error) { | |
return nil, fmt.Errorf("failed to get root cgroup stats: %v", err) | ||
} | ||
} | ||
|
||
// removeTerminatedContainerInfo will also remove pod level cgroups, so save the infos into allInfos first | ||
allInfos := infos | ||
infos = removeTerminatedContainerInfo(infos) | ||
|
||
// Map each container to a pod and update the PodStats with container data. | ||
podToStats := map[statsapi.PodReference]*statsapi.PodStats{} | ||
for key, cinfo := range infos { | ||
|
@@ -141,6 +143,13 @@ func (p *cadvisorStatsProvider) ListPodStats() ([]statsapi.PodStats, error) { | |
podStats.VolumeStats = append(vstats.EphemeralVolumes, vstats.PersistentVolumes...) | ||
} | ||
podStats.EphemeralStorage = calcEphemeralStorage(podStats.Containers, ephemeralStats, &rootFsInfo) | ||
// Lookup the pod-level cgroup's CPU and memory stats | ||
podInfo := getcadvisorPodInfoFromPodUID(podUID, allInfos) | ||
if podInfo != nil { | ||
cpu, memory := cadvisorInfoToCPUandMemoryStats(podInfo) | ||
podStats.CPU = cpu | ||
podStats.Memory = memory | ||
} | ||
result = append(result, *podStats) | ||
} | ||
|
||
|
@@ -243,6 +252,19 @@ func isPodManagedContainer(cinfo *cadvisorapiv2.ContainerInfo) bool { | |
return managed | ||
} | ||
|
||
// getcadvisorPodInfoFromPodUID returns a pod cgroup information by matching the podUID with its CgroupName identifier base name | ||
func getcadvisorPodInfoFromPodUID(podUID types.UID, infos map[string]cadvisorapiv2.ContainerInfo) *cadvisorapiv2.ContainerInfo { | ||
for key, info := range infos { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this is hacky, we should be able to not iterate over every container on the host. why cant we get the cgroup for a given pod and just do a direct map lookup? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We looked into that approach, e.g., GetPodContainerName https://github.com/kubernetes/kubernetes/blob/master/pkg/kubelet/cm/pod_container_manager_linux.go#L95 can return the pod cgroup name, but it requires pod spec to figure out the qos of a pod. Here cadvisor does not have a pod object There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. i guess this is similar to what we had to do here: in that code block we iterate over the literal cgroupfs, convert the full path to our internal canonical name, get the base path, and check for match. maybe we can find a way to share that code into a common utility so that we can take this knowledge out of this method and keep it localized in cm package in a follow-on? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, I will work on that as a follow up PR. Thanks! |
||
if cm.IsSystemdStyleName(key) { | ||
key = cm.RevertFromSystemdToCgroupStyleName(key) | ||
} | ||
if cm.GetPodCgroupNameSuffix(podUID) == path.Base(key) { | ||
return &info | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
// removeTerminatedContainerInfo returns the specified containerInfo but with | ||
// the stats of the terminated containers removed. | ||
// | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: should we just have a /kubelet/cm/systemd subpackage in future and move this there? it will be odd when _windows is having to implement this.