cri_stats_provider_windows.go 3.4 KB

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