Skip to content
Permalink
Browse files

Fix memory leak from not closing hcs container handles

  • Loading branch information...
benmoss committed May 31, 2019
1 parent 8b7e777 commit 287b1ba515c5e277fea60c1be806aaa9bb22ff5a
Showing with 16 additions and 9 deletions.
  1. +16 −9 pkg/kubelet/stats/cri_stats_provider_windows.go
@@ -40,18 +40,11 @@ func (p *criStatsProvider) listContainerNetworkStats() (map[string]*statsapi.Net

stats := make(map[string]*statsapi.NetworkStats)
for _, c := range containers {
container, err := hcsshim.OpenContainer(c.ID)
cstats, err := fetchContainerStats(c)
if err != nil {
klog.V(4).Infof("Failed to open container %q with error '%v', continue to get stats for other containers", c.ID, err)
klog.V(4).Infof("Failed to fetch statistics for container %q with error '%v', continue to get stats for other containers", c.ID, err)
continue
}

cstats, err := container.Statistics()
if err != nil {
klog.V(4).Infof("Failed to get statistics for container %q with error '%v', continue to get stats for other containers", c.ID, err)
continue
}

if len(cstats.Network) > 0 {
stats[c.ID] = hcsStatsToNetworkStats(cstats.Timestamp, cstats.Network)
}
@@ -60,6 +53,20 @@ func (p *criStatsProvider) listContainerNetworkStats() (map[string]*statsapi.Net
return stats, nil
}

func fetchContainerStats(c hcsshim.ContainerProperties) (*hcsshim.Statistics, error) {
container, err := hcsshim.OpenContainer(c.ID)
if err != nil {
return nil, err
}
defer container.Close()

cstats, err := container.Statistics()
if err != nil {
return nil, err
}
return &cstats, nil
}

// hcsStatsToNetworkStats converts hcsshim.Statistics.Network to statsapi.NetworkStats
func hcsStatsToNetworkStats(timestamp time.Time, hcsStats []hcsshim.NetworkStats) *statsapi.NetworkStats {
result := &statsapi.NetworkStats{

0 comments on commit 287b1ba

Please sign in to comment.
You can’t perform that action at this time.