mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-28 14:07:14 +00:00
kubelet: slightly move around some cri stats functions
to reduce duplication and add clarity Signed-off-by: Peter Hunt <pehunt@redhat.com>
This commit is contained in:
parent
d2c436700e
commit
ffdb4b9c4a
@ -125,32 +125,18 @@ func (p *criStatsProvider) listPodStats(updateCPUNanoCoreUsage bool) ([]statsapi
|
||||
return nil, fmt.Errorf("failed to get rootFs info: %v", err)
|
||||
}
|
||||
|
||||
containers, err := p.runtimeService.ListContainers(&runtimeapi.ContainerFilter{})
|
||||
containerMap, podSandboxMap, err := p.getPodAndContainerMaps()
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to list all containers: %v", err)
|
||||
}
|
||||
|
||||
// Creates pod sandbox map.
|
||||
podSandboxMap := make(map[string]*runtimeapi.PodSandbox)
|
||||
podSandboxes, err := p.runtimeService.ListPodSandbox(&runtimeapi.PodSandboxFilter{})
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to list all pod sandboxes: %v", err)
|
||||
}
|
||||
podSandboxes = removeTerminatedPods(podSandboxes)
|
||||
for _, s := range podSandboxes {
|
||||
podSandboxMap[s.Id] = s
|
||||
}
|
||||
|
||||
containers = removeTerminatedContainers(containers)
|
||||
// Creates container map.
|
||||
containerMap := make(map[string]*runtimeapi.Container)
|
||||
for _, c := range containers {
|
||||
containerMap[c.Id] = c
|
||||
return nil, fmt.Errorf("failed to get pod or container map: %v", err)
|
||||
}
|
||||
|
||||
if p.podAndContainerStatsFromCRI {
|
||||
return p.listPodStatsStrictlyFromCRI(updateCPUNanoCoreUsage, containerMap, podSandboxMap, &rootFsInfo)
|
||||
}
|
||||
return p.listPodStatsPartiallyFromCRI(updateCPUNanoCoreUsage, containerMap, podSandboxMap, &rootFsInfo)
|
||||
}
|
||||
|
||||
func (p *criStatsProvider) listPodStatsPartiallyFromCRI(updateCPUNanoCoreUsage bool, containerMap map[string]*runtimeapi.Container, podSandboxMap map[string]*runtimeapi.PodSandbox, rootFsInfo *cadvisorapiv2.FsInfo) ([]statsapi.PodStats, error) {
|
||||
// fsIDtoInfo is a map from filesystem id to its stats. This will be used
|
||||
// as a cache to avoid querying cAdvisor for the filesystem stats with the
|
||||
// same filesystem id many times.
|
||||
@ -198,7 +184,7 @@ func (p *criStatsProvider) listPodStats(updateCPUNanoCoreUsage bool) ([]statsapi
|
||||
}
|
||||
|
||||
// Fill available stats for full set of required pod stats
|
||||
cs := p.makeContainerStats(stats, container, &rootFsInfo, fsIDtoInfo, podSandbox.GetMetadata(), updateCPUNanoCoreUsage)
|
||||
cs := p.makeContainerStats(stats, container, rootFsInfo, fsIDtoInfo, podSandbox.GetMetadata(), updateCPUNanoCoreUsage)
|
||||
p.addPodNetworkStats(ps, podSandboxID, caInfos, cs, containerNetworkStats[podSandboxID])
|
||||
p.addPodCPUMemoryStats(ps, types.UID(podSandbox.Metadata.Uid), allInfos, cs)
|
||||
p.addProcessStats(ps, types.UID(podSandbox.Metadata.Uid), allInfos, cs)
|
||||
@ -218,7 +204,7 @@ func (p *criStatsProvider) listPodStats(updateCPUNanoCoreUsage bool) ([]statsapi
|
||||
|
||||
result := make([]statsapi.PodStats, 0, len(sandboxIDToPodStats))
|
||||
for _, s := range sandboxIDToPodStats {
|
||||
p.makePodStorageStats(s, &rootFsInfo)
|
||||
p.makePodStorageStats(s, rootFsInfo)
|
||||
result = append(result, *s)
|
||||
}
|
||||
return result, nil
|
||||
@ -238,7 +224,6 @@ func (p *criStatsProvider) listPodStatsStrictlyFromCRI(updateCPUNanoCoreUsage bo
|
||||
continue
|
||||
}
|
||||
ps := buildPodStats(podSandbox)
|
||||
// TODO FIXME(haircommander): resolve timestamp by taking the latest of each that were collected
|
||||
for _, criContainerStat := range criSandboxStat.Linux.Containers {
|
||||
container, found := containerMap[criContainerStat.Attributes.Id]
|
||||
if !found {
|
||||
@ -260,39 +245,14 @@ func (p *criStatsProvider) listPodStatsStrictlyFromCRI(updateCPUNanoCoreUsage bo
|
||||
|
||||
// ListPodCPUAndMemoryStats returns the CPU and Memory stats of all the pod-managed containers.
|
||||
func (p *criStatsProvider) ListPodCPUAndMemoryStats() ([]statsapi.PodStats, error) {
|
||||
containers, err := p.runtimeService.ListContainers(&runtimeapi.ContainerFilter{})
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to list all containers: %v", err)
|
||||
}
|
||||
|
||||
// Creates pod sandbox map.
|
||||
podSandboxMap := make(map[string]*runtimeapi.PodSandbox)
|
||||
podSandboxes, err := p.runtimeService.ListPodSandbox(&runtimeapi.PodSandboxFilter{})
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to list all pod sandboxes: %v", err)
|
||||
}
|
||||
podSandboxes = removeTerminatedPods(podSandboxes)
|
||||
for _, s := range podSandboxes {
|
||||
podSandboxMap[s.Id] = s
|
||||
}
|
||||
|
||||
// sandboxIDToPodStats is a temporary map from sandbox ID to its pod stats.
|
||||
sandboxIDToPodStats := make(map[string]*statsapi.PodStats)
|
||||
|
||||
resp, err := p.runtimeService.ListContainerStats(&runtimeapi.ContainerStatsFilter{})
|
||||
containerMap, podSandboxMap, err := p.getPodAndContainerMaps()
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to list all container stats: %v", err)
|
||||
return nil, fmt.Errorf("failed to get pod or container map: %v", err)
|
||||
}
|
||||
|
||||
containers = removeTerminatedContainers(containers)
|
||||
// Creates container map.
|
||||
containerMap := make(map[string]*runtimeapi.Container)
|
||||
for _, c := range containers {
|
||||
containerMap[c.Id] = c
|
||||
}
|
||||
|
||||
result := make([]statsapi.PodStats, 0, len(sandboxIDToPodStats))
|
||||
|
||||
result := make([]statsapi.PodStats, 0, len(podSandboxMap))
|
||||
if p.podAndContainerStatsFromCRI {
|
||||
criSandboxStats, err := p.runtimeService.ListPodSandboxStats(&runtimeapi.PodSandboxStatsFilter{})
|
||||
if err != nil {
|
||||
@ -311,6 +271,11 @@ func (p *criStatsProvider) ListPodCPUAndMemoryStats() ([]statsapi.PodStats, erro
|
||||
return result, err
|
||||
}
|
||||
|
||||
resp, err := p.runtimeService.ListContainerStats(&runtimeapi.ContainerStatsFilter{})
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to list all container stats: %v", err)
|
||||
}
|
||||
|
||||
allInfos, err := getCadvisorContainerInfo(p.cadvisor)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to fetch cadvisor stats: %v", err)
|
||||
@ -361,6 +326,32 @@ func (p *criStatsProvider) ListPodCPUAndMemoryStats() ([]statsapi.PodStats, erro
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func (p *criStatsProvider) getPodAndContainerMaps() (map[string]*runtimeapi.Container, map[string]*runtimeapi.PodSandbox, error) {
|
||||
containers, err := p.runtimeService.ListContainers(&runtimeapi.ContainerFilter{})
|
||||
if err != nil {
|
||||
return nil, nil, fmt.Errorf("failed to list all containers: %v", err)
|
||||
}
|
||||
|
||||
// Creates pod sandbox map between the pod sandbox ID and the PodSandbox object.
|
||||
podSandboxMap := make(map[string]*runtimeapi.PodSandbox)
|
||||
podSandboxes, err := p.runtimeService.ListPodSandbox(&runtimeapi.PodSandboxFilter{})
|
||||
if err != nil {
|
||||
return nil, nil, fmt.Errorf("failed to list all pod sandboxes: %v", err)
|
||||
}
|
||||
podSandboxes = removeTerminatedPods(podSandboxes)
|
||||
for _, s := range podSandboxes {
|
||||
podSandboxMap[s.Id] = s
|
||||
}
|
||||
|
||||
containers = removeTerminatedContainers(containers)
|
||||
// Creates container map between the container ID and the Container object.
|
||||
containerMap := make(map[string]*runtimeapi.Container)
|
||||
for _, c := range containers {
|
||||
containerMap[c.Id] = c
|
||||
}
|
||||
return containerMap, podSandboxMap, nil
|
||||
}
|
||||
|
||||
// ImageFsStats returns the stats of the image filesystem.
|
||||
func (p *criStatsProvider) ImageFsStats() (*statsapi.FsStats, error) {
|
||||
resp, err := p.imageService.ImageFsInfo()
|
||||
|
Loading…
Reference in New Issue
Block a user