cadvisor.go 2.1 KB

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