kubelet_stats.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. /*
  2. Copyright 2020 The Kubernetes Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package windows
  14. import (
  15. "context"
  16. "time"
  17. v1 "k8s.io/api/core/v1"
  18. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  19. "k8s.io/apimachinery/pkg/labels"
  20. "k8s.io/apimachinery/pkg/util/uuid"
  21. "k8s.io/kubernetes/test/e2e/framework"
  22. e2ekubelet "k8s.io/kubernetes/test/e2e/framework/kubelet"
  23. e2enode "k8s.io/kubernetes/test/e2e/framework/node"
  24. e2epod "k8s.io/kubernetes/test/e2e/framework/pod"
  25. e2eskipper "k8s.io/kubernetes/test/e2e/framework/skipper"
  26. imageutils "k8s.io/kubernetes/test/utils/image"
  27. "github.com/onsi/ginkgo"
  28. )
  29. var _ = SIGDescribe("[Feature:Windows] Kubelet-Stats [Serial]", func() {
  30. f := framework.NewDefaultFramework("kubelet-stats-test-windows")
  31. ginkgo.Describe("Kubelet stats collection for Windows nodes", func() {
  32. ginkgo.Context("when running 10 pods", func() {
  33. // 10 seconds is the default scrape timeout for metrics-server and kube-prometheus
  34. ginkgo.It("should return within 10 seconds", func() {
  35. ginkgo.By("Selecting a Windows node")
  36. targetNode, err := findWindowsNode(f)
  37. framework.ExpectNoError(err, "Error finding Windows node")
  38. framework.Logf("Using node: %v", targetNode.Name)
  39. ginkgo.By("Scheduling 10 pods")
  40. powershellImage := imageutils.GetConfig(imageutils.BusyBox)
  41. pods := newKubeletStatsTestPods(10, powershellImage, targetNode.Name)
  42. f.PodClient().CreateBatch(pods)
  43. ginkgo.By("Waiting up to 3 minutes for pods to be running")
  44. timeout := 3 * time.Minute
  45. e2epod.WaitForPodsRunningReady(f.ClientSet, f.Namespace.Name, 10, 0, timeout, make(map[string]string))
  46. ginkgo.By("Getting kubelet stats 5 times and checking average duration")
  47. iterations := 5
  48. var totalDurationMs int64
  49. for i := 0; i < iterations; i++ {
  50. start := time.Now()
  51. nodeStats, err := e2ekubelet.GetStatsSummary(f.ClientSet, targetNode.Name)
  52. duration := time.Since(start)
  53. totalDurationMs += duration.Milliseconds()
  54. framework.ExpectNoError(err, "Error getting kubelet stats")
  55. // Perform some basic sanity checks on retrieved stats for pods in this test's namespace
  56. statsChecked := 0
  57. for _, podStats := range nodeStats.Pods {
  58. if podStats.PodRef.Namespace != f.Namespace.Name {
  59. continue
  60. }
  61. statsChecked = statsChecked + 1
  62. framework.ExpectEqual(*podStats.CPU.UsageCoreNanoSeconds > 0, true, "Pod stats should not report 0 cpu usage")
  63. framework.ExpectEqual(*podStats.Memory.WorkingSetBytes > 0, true, "Pod stats should not report 0 bytes for memory working set ")
  64. }
  65. framework.ExpectEqual(statsChecked, 10, "Should find stats for 10 pods in kubelet stats")
  66. time.Sleep(5 * time.Second)
  67. }
  68. avgDurationMs := totalDurationMs / int64(iterations)
  69. durationMatch := avgDurationMs <= time.Duration(10*time.Second).Milliseconds()
  70. framework.Logf("Getting kubelet stats for node %v took an average of %v milliseconds over %v iterations", targetNode.Name, avgDurationMs, iterations)
  71. framework.ExpectEqual(durationMatch, true, "Collecting kubelet stats should not take longer than 10 seconds")
  72. })
  73. })
  74. })
  75. })
  76. // findWindowsNode finds a Windows node that is Ready and Schedulable
  77. func findWindowsNode(f *framework.Framework) (v1.Node, error) {
  78. selector := labels.Set{"kubernetes.io/os": "windows"}.AsSelector()
  79. nodeList, err := f.ClientSet.CoreV1().Nodes().List(context.TODO(), metav1.ListOptions{LabelSelector: selector.String()})
  80. if err != nil {
  81. return v1.Node{}, err
  82. }
  83. var targetNode v1.Node
  84. foundNode := false
  85. for _, n := range nodeList.Items {
  86. if e2enode.IsNodeReady(&n) && e2enode.IsNodeSchedulable(&n) {
  87. targetNode = n
  88. foundNode = true
  89. break
  90. }
  91. }
  92. if foundNode == false {
  93. e2eskipper.Skipf("Could not find and ready and schedulable Windows nodes")
  94. }
  95. return targetNode, nil
  96. }
  97. // newKubeletStatsTestPods creates a list of pods (specification) for test.
  98. func newKubeletStatsTestPods(numPods int, image imageutils.Config, nodeName string) []*v1.Pod {
  99. var pods []*v1.Pod
  100. for i := 0; i < numPods; i++ {
  101. podName := "statscollectiontest-" + string(uuid.NewUUID())
  102. pod := v1.Pod{
  103. ObjectMeta: metav1.ObjectMeta{
  104. Name: podName,
  105. Labels: map[string]string{
  106. "name": podName,
  107. "testapp": "stats-collection",
  108. },
  109. },
  110. Spec: v1.PodSpec{
  111. Containers: []v1.Container{
  112. {
  113. Image: image.GetE2EImage(),
  114. Name: podName,
  115. Command: []string{
  116. "powershell.exe",
  117. "-Command",
  118. "sleep -Seconds 600",
  119. },
  120. },
  121. },
  122. NodeName: nodeName,
  123. },
  124. }
  125. pods = append(pods, &pod)
  126. }
  127. return pods
  128. }