|
| 1 | +// +build windows |
| 2 | + |
| 3 | +/* |
| 4 | +Copyright 2018 The Kubernetes Authors. |
| 5 | +
|
| 6 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | +you may not use this file except in compliance with the License. |
| 8 | +You may obtain a copy of the License at |
| 9 | +
|
| 10 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | +
|
| 12 | +Unless required by applicable law or agreed to in writing, software |
| 13 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | +See the License for the specific language governing permissions and |
| 16 | +limitations under the License. |
| 17 | +*/ |
| 18 | + |
| 19 | +package stats |
| 20 | + |
| 21 | +import ( |
| 22 | + "time" |
| 23 | + |
| 24 | + "github.com/Microsoft/hcsshim" |
| 25 | + |
| 26 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 27 | + "k8s.io/klog" |
| 28 | + statsapi "k8s.io/kubernetes/pkg/kubelet/apis/stats/v1alpha1" |
| 29 | +) |
| 30 | + |
| 31 | +// listContainerNetworkStats returns the network stats of all the running containers. |
| 32 | +func (p *criStatsProvider) listContainerNetworkStats() (map[string]*statsapi.NetworkStats, error) { |
| 33 | + containers, err := hcsshim.GetContainers(hcsshim.ComputeSystemQuery{ |
| 34 | + Types: []string{"Container"}, |
| 35 | + }) |
| 36 | + if err != nil { |
| 37 | + return nil, err |
| 38 | + } |
| 39 | + |
| 40 | + stats := make(map[string]*statsapi.NetworkStats) |
| 41 | + for _, c := range containers { |
| 42 | + container, err := hcsshim.OpenContainer(c.ID) |
| 43 | + if err != nil { |
| 44 | + klog.Warningf("Failed to open container %q with error '%v', continue to get stats for other containers", c.ID, err) |
| 45 | + continue |
| 46 | + } |
| 47 | + |
| 48 | + cstats, err := container.Statistics() |
| 49 | + if err != nil { |
| 50 | + klog.Warningf("Failed to get statistics for container %q with error '%v', continue to get stats for other containers", c.ID, err) |
| 51 | + continue |
| 52 | + } |
| 53 | + |
| 54 | + if len(cstats.Network) > 0 { |
| 55 | + stats[c.ID] = hcsStatsToNetworkStats(cstats.Timestamp, cstats.Network) |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + return stats, nil |
| 60 | +} |
| 61 | + |
| 62 | +// hcsStatsToNetworkStats converts hcsshim.Statistics.Network to statsapi.NetworkStats |
| 63 | +func hcsStatsToNetworkStats(timestamp time.Time, hcsStats []hcsshim.NetworkStats) *statsapi.NetworkStats { |
| 64 | + result := &statsapi.NetworkStats{ |
| 65 | + Time: metav1.NewTime(timestamp), |
| 66 | + Interfaces: make([]statsapi.InterfaceStats, 0), |
| 67 | + } |
| 68 | + |
| 69 | + for _, stat := range hcsStats { |
| 70 | + iStat := hcsStatsToInterfaceStats(stat) |
| 71 | + if iStat != nil { |
| 72 | + result.Interfaces = append(result.Interfaces, *iStat) |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + // TODO(feiskyer): add support of multiple interfaces for getting default interface. |
| 77 | + if len(result.Interfaces) > 0 { |
| 78 | + result.InterfaceStats = result.Interfaces[0] |
| 79 | + } |
| 80 | + |
| 81 | + return result |
| 82 | +} |
| 83 | + |
| 84 | +// hcsStatsToInterfaceStats converts hcsshim.NetworkStats to statsapi.InterfaceStats. |
| 85 | +func hcsStatsToInterfaceStats(stat hcsshim.NetworkStats) *statsapi.InterfaceStats { |
| 86 | + return &statsapi.InterfaceStats{ |
| 87 | + Name: stat.EndpointId, |
| 88 | + RxBytes: &stat.BytesReceived, |
| 89 | + TxBytes: &stat.BytesSent, |
| 90 | + } |
| 91 | +} |
0 commit comments