Change:
package stats import ( <del> "fmt" </del> "time" <del> "github.com/Microsoft/hcsshim" </del> <ins> "k8s.io/klog/v2" "github.com/Microsoft/hcsshim" </ins> metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" <del> "k8s.io/apimachinery/pkg/util/sets" "k8s.io/klog/v2" </del> statsapi "k8s.io/kubelet/pkg/apis/stats/v1alpha1" ) <ins> // windowsNetworkStatsProvider creates an interface that allows for testing the logic without needing to create a container type windowsNetworkStatsProvider interface { HNSListEndpointRequest() ([]hcsshim.HNSEndpoint, error) GetHNSEndpointStats(endpointName string) (*hcsshim.HNSEndpointStats, error) } // networkStats exposes the required functionality for hcsshim in this scenario type networkStats struct{} func (s networkStats) HNSListEndpointRequest() ([]hcsshim.HNSEndpoint, error) { return hcsshim.HNSListEndpointRequest() } func (s networkStats) GetHNSEndpointStats(endpointName string) (*hcsshim.HNSEndpointStats, error) { return hcsshim.GetHNSEndpointStats(endpointName) } </ins> // listContainerNetworkStats returns the network stats of all the running containers. func (p *criStatsProvider) listContainerNetworkStats() (map[string]*statsapi.NetworkStats, error) { <del> containers, err := hcsshim.GetContainers(hcsshim.ComputeSystemQuery{ Types: []string{"Container"}, }) </del> <ins> networkStatsProvider := newNetworkStatsProvider(p) endpoints, err := networkStatsProvider.HNSListEndpointRequest() </ins> if err != nil { <ins> klog.ErrorS(err, "Failed to fetch current HNS endpoints") </ins> return nil, err } <del> stats := make(map[string]*statsapi.NetworkStats) for _, c := range containers { cstats, err := fetchContainerStats(c) </del> <ins> networkStats := make(map[string]*statsapi.NetworkStats) for _, endpoint := range endpoints { endpointStats, err := networkStatsProvider.GetHNSEndpointStats(endpoint.Id) </ins> if err != nil { <del> klog.V(4).InfoS("Failed to fetch statistics for container, continue to get stats for other containers", "containerID", c.ID, "err", err) </del> <ins> klog.V(2).InfoS("Failed to fetch statistics for endpoint, continue to get stats for other endpoints", "endpointId", endpoint.Id, "containers", endpoint.SharedContainers) </ins> continue } <del> if len(cstats.Network) > 0 { stats[c.ID] = hcsStatsToNetworkStats(cstats.Timestamp, cstats.Network) } } return stats, nil } func fetchContainerStats(c hcsshim.ContainerProperties) (stats hcsshim.Statistics, err error) { var ( container hcsshim.Container ) container, err = hcsshim.OpenContainer(c.ID) if err != nil { return } defer func() { if closeErr := container.Close(); closeErr != nil { if err != nil { err = fmt.Errorf("failed to close container after error %v; close error: %v", err, closeErr) } else { err = closeErr </del> <ins> // only add the interface for each container if not already in the list for _, cId := range endpoint.SharedContainers { networkStat, found := networkStats[cId] if found && networkStat.Name != endpoint.Name { iStat := hcsStatToInterfaceStat(endpointStats, endpoint.Name) networkStat.Interfaces = append(networkStat.Interfaces, iStat) continue </ins> } <ins> networkStats[cId] = hcsStatsToNetworkStats(p.clock.Now(), endpointStats, endpoint.Name) </ins> } <del> }() </del> <ins> } </ins> <del> return container.Statistics() </del> <ins> return networkStats, nil </ins> } // hcsStatsToNetworkStats converts hcsshim.Statistics.Network to statsapi.NetworkStats <del> func hcsStatsToNetworkStats(timestamp time.Time, hcsStats []hcsshim.NetworkStats) *statsapi.NetworkStats { </del> <ins> func hcsStatsToNetworkStats(timestamp time.Time, hcsStats *hcsshim.HNSEndpointStats, endpointName string) *statsapi.NetworkStats { </ins> result := &statsapi.NetworkStats{ Time:       metav1.NewTime(timestamp), Interfaces: make([]statsapi.InterfaceStats, 0), } <del> adapters := sets.NewString() for _, stat := range hcsStats { iStat, err := hcsStatsToInterfaceStats(stat) if err != nil { klog.InfoS("Failed to get HNS endpoint, continue to get stats for other endpoints", "endpointID", stat.EndpointId, "err", err) continue } // Only count each adapter once. if adapters.Has(iStat.Name) { continue } result.Interfaces = append(result.Interfaces, *iStat) adapters.Insert(iStat.Name) } </del> <ins> iStat := hcsStatToInterfaceStat(hcsStats, endpointName) </ins> <del> // TODO(feiskyer): add support of multiple interfaces for getting default interface. if len(result.Interfaces) > 0 { result.InterfaceStats = result.Interfaces[0] } </del> <ins> // TODO: add support of multiple interfaces for getting default interface. result.Interfaces = append(result.Interfaces, iStat) result.InterfaceStats = iStat </ins> return result } <del> // hcsStatsToInterfaceStats converts hcsshim.NetworkStats to statsapi.InterfaceStats. func hcsStatsToInterfaceStats(stat hcsshim.NetworkStats) (*statsapi.InterfaceStats, error) { endpoint, err := hcsshim.GetHNSEndpointByID(stat.EndpointId) if err != nil { return nil, err </del> <ins> func hcsStatToInterfaceStat(hcsStats *hcsshim.HNSEndpointStats, endpointName string) statsapi.InterfaceStats { iStat := statsapi.InterfaceStats{ Name:    endpointName, RxBytes: &hcsStats.BytesReceived, TxBytes: &hcsStats.BytesSent, </ins> } <ins> return iStat } </ins> <del> return &statsapi.InterfaceStats{ Name:    endpoint.Name, RxBytes: &stat.BytesReceived, TxBytes: &stat.BytesSent, }, nil </del> <ins> // newNetworkStatsProvider uses the real windows hcsshim if not provided otherwise if the interface is provided // by the cristatsprovider in testing scenarios it uses that one func newNetworkStatsProvider(p *criStatsProvider) windowsNetworkStatsProvider { var statsProvider windowsNetworkStatsProvider if p.windowsNetworkStatsProvider == nil { statsProvider = networkStats{} } else { statsProvider = p.windowsNetworkStatsProvider.(windowsNetworkStatsProvider) } return statsProvider </ins> }

Discussion:
<!-- Please use this template while reporting a bug and provide as much info as possible. Not doing so may result in your bug not being addressed in a timely manner. Thanks! If the matter is security related, please disclose it privately via --When curling the endpoint for stats we don't see network stats. Network stats to be included when calling the summary. This works with dockershim for windows. This happens because Windows containerd uses hcsv2 to create containers and we query for the networks stats using hcsv1: This will be address eventually with move to CRI only metrics KEP: but is still several releases away. Kubernetes version (use ): Cloud provider or hardware configuration: OS (e.g: ): Kernel (e.g. ): Install tools: Network plugin and version (if this is a network-related bug): Others: /sig windows /assign
This issue is currently awaiting triage. If a SIG or subproject determines this is a relevant issue, they will accept it by applying the label and provide further guidance. The label can be by org members by writing in a comment. <detailsInstructions for interacting with me using PR comments are available . If you have questions or suggestions related to my behavior, please file an issue against the repository. </details
The Kubernetes project currently lacks enough contributors to adequately respond to all issues and PRs. This bot triages issues and PRs according to the following rules: After 90d of inactivity, is applied After 30d of inactivity since was applied, is applied After 30d of inactivity since was applied, the issue is closed You can: Mark this issue or PR as fresh with Mark this issue or PR as rotten with Close this issue or PR with Offer to help out with [Issue Triage][1] Please send feedback to sig-contributor-experience at . /lifecycle stale [1]: