cadvisor.go 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. /*
  2. Copyright 2017 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 monitoring
  14. import (
  15. "fmt"
  16. "time"
  17. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  18. clientset "k8s.io/client-go/kubernetes"
  19. "k8s.io/kubernetes/test/e2e/framework"
  20. "k8s.io/kubernetes/test/e2e/framework/config"
  21. e2elog "k8s.io/kubernetes/test/e2e/framework/log"
  22. instrumentation "k8s.io/kubernetes/test/e2e/instrumentation/common"
  23. "github.com/onsi/ginkgo"
  24. )
  25. var cadvisor struct {
  26. MaxRetries int `default:"6"`
  27. SleepDuration time.Duration `default:"10000ms"`
  28. }
  29. var _ = config.AddOptions(&cadvisor, "instrumentation.monitoring.cadvisor")
  30. var _ = instrumentation.SIGDescribe("Cadvisor", func() {
  31. f := framework.NewDefaultFramework("cadvisor")
  32. ginkgo.It("should be healthy on every node.", func() {
  33. CheckCadvisorHealthOnAllNodes(f.ClientSet, 5*time.Minute)
  34. })
  35. })
  36. // CheckCadvisorHealthOnAllNodes check cadvisor health via kubelet endpoint
  37. func CheckCadvisorHealthOnAllNodes(c clientset.Interface, timeout time.Duration) {
  38. // It should be OK to list unschedulable Nodes here.
  39. ginkgo.By("getting list of nodes")
  40. nodeList, err := c.CoreV1().Nodes().List(metav1.ListOptions{})
  41. framework.ExpectNoError(err)
  42. var errors []error
  43. maxRetries := cadvisor.MaxRetries
  44. for {
  45. errors = []error{}
  46. for _, node := range nodeList.Items {
  47. // cadvisor is not accessible directly unless its port (4194 by default) is exposed.
  48. // Here, we access '/stats/' REST endpoint on the kubelet which polls cadvisor internally.
  49. statsResource := fmt.Sprintf("api/v1/nodes/%s/proxy/stats/", node.Name)
  50. ginkgo.By(fmt.Sprintf("Querying stats from node %s using url %s", node.Name, statsResource))
  51. _, err = c.CoreV1().RESTClient().Get().AbsPath(statsResource).Timeout(timeout).Do().Raw()
  52. if err != nil {
  53. errors = append(errors, err)
  54. }
  55. }
  56. if len(errors) == 0 {
  57. return
  58. }
  59. if maxRetries--; maxRetries <= 0 {
  60. break
  61. }
  62. e2elog.Logf("failed to retrieve kubelet stats -\n %v", errors)
  63. time.Sleep(cadvisor.SleepDuration)
  64. }
  65. framework.Failf("Failed after retrying %d times for cadvisor to be healthy on all nodes. Errors:\n%v", maxRetries, errors)
  66. }