Skip to content

Commit

Permalink
Add SwapStats to summary API through cadvisor
Browse files Browse the repository at this point in the history
Signed-off-by: Itamar Holder <iholder@redhat.com>
  • Loading branch information
iholder101 committed Jul 17, 2023
1 parent a05d200 commit c74ee80
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
2 changes: 2 additions & 0 deletions pkg/kubelet/stats/cadvisor_stats_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ func (p *cadvisorStatsProvider) ListPodStats(_ context.Context) ([]statsapi.PodS
cpu, memory := cadvisorInfoToCPUandMemoryStats(podInfo)
podStats.CPU = cpu
podStats.Memory = memory
podStats.Swap = cadvisorInfoToSwapStats(podInfo)
podStats.ProcessStats = cadvisorInfoToProcessStats(podInfo)
}

Expand Down Expand Up @@ -227,6 +228,7 @@ func (p *cadvisorStatsProvider) ListPodCPUAndMemoryStats(_ context.Context) ([]s
cpu, memory := cadvisorInfoToCPUandMemoryStats(podInfo)
podStats.CPU = cpu
podStats.Memory = memory
podStats.Swap = cadvisorInfoToSwapStats(podInfo)
}
result = append(result, *podStats)
}
Expand Down
24 changes: 24 additions & 0 deletions pkg/kubelet/stats/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ func cadvisorInfoToContainerStats(name string, info *cadvisorapiv2.ContainerInfo
cpu, memory := cadvisorInfoToCPUandMemoryStats(info)
result.CPU = cpu
result.Memory = memory
result.Swap = cadvisorInfoToSwapStats(info)

// NOTE: if they can be found, log stats will be overwritten
// by the caller, as it knows more information about the pod,
Expand Down Expand Up @@ -257,6 +258,29 @@ func cadvisorInfoToUserDefinedMetrics(info *cadvisorapiv2.ContainerInfo) []stats
return udm
}

func cadvisorInfoToSwapStats(info *cadvisorapiv2.ContainerInfo) *statsapi.SwapStats {
cstat, found := latestContainerStats(info)
if !found {
return nil
}

var swapStats *statsapi.SwapStats

if info.Spec.HasMemory && cstat.Memory != nil {
swapStats = &statsapi.SwapStats{
Time: metav1.NewTime(cstat.Timestamp),
SwapUsageBytes: &cstat.Memory.Swap,
}

if !isMemoryUnlimited(info.Spec.Memory.SwapLimit) {
swapAvailableBytes := info.Spec.Memory.SwapLimit - cstat.Memory.Swap
swapStats.SwapAvailableBytes = &swapAvailableBytes
}
}

return swapStats
}

// latestContainerStats returns the latest container stats from cadvisor, or nil if none exist
func latestContainerStats(info *cadvisorapiv2.ContainerInfo) (*cadvisorapiv2.ContainerStats, bool) {
stats := info.Stats
Expand Down

0 comments on commit c74ee80

Please sign in to comment.