kubelet_metrics.go 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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 metrics
  14. import (
  15. "fmt"
  16. "io/ioutil"
  17. "net/http"
  18. "time"
  19. )
  20. type KubeletMetrics Metrics
  21. func (m *KubeletMetrics) Equal(o KubeletMetrics) bool {
  22. return (*Metrics)(m).Equal(Metrics(o))
  23. }
  24. func NewKubeletMetrics() KubeletMetrics {
  25. result := NewMetrics()
  26. return KubeletMetrics(result)
  27. }
  28. // GrabKubeletMetricsWithoutProxy retrieve metrics from the kubelet on the given node using a simple GET over http.
  29. // Currently only used in integration tests.
  30. func GrabKubeletMetricsWithoutProxy(nodeName string) (KubeletMetrics, error) {
  31. metricsEndpoint := "http://%s/metrics"
  32. resp, err := http.Get(fmt.Sprintf(metricsEndpoint, nodeName))
  33. if err != nil {
  34. return KubeletMetrics{}, err
  35. }
  36. defer resp.Body.Close()
  37. body, err := ioutil.ReadAll(resp.Body)
  38. if err != nil {
  39. return KubeletMetrics{}, err
  40. }
  41. return parseKubeletMetrics(string(body))
  42. }
  43. func parseKubeletMetrics(data string) (KubeletMetrics, error) {
  44. result := NewKubeletMetrics()
  45. if err := parseMetrics(data, (*Metrics)(&result)); err != nil {
  46. return KubeletMetrics{}, err
  47. }
  48. return result, nil
  49. }
  50. func (g *MetricsGrabber) getMetricsFromNode(nodeName string, kubeletPort int) (string, error) {
  51. // There's a problem with timing out during proxy. Wrapping this in a goroutine to prevent deadlock.
  52. // Hanging goroutine will be leaked.
  53. finished := make(chan struct{})
  54. var err error
  55. var rawOutput []byte
  56. go func() {
  57. rawOutput, err = g.client.Get().
  58. Prefix("proxy").
  59. Resource("nodes").
  60. Name(fmt.Sprintf("%v:%v", nodeName, kubeletPort)).
  61. Suffix("metrics").
  62. Do().Raw()
  63. finished <- struct{}{}
  64. }()
  65. select {
  66. case <-time.After(ProxyTimeout):
  67. return "", fmt.Errorf("Timed out when waiting for proxy to gather metrics from %v", nodeName)
  68. case <-finished:
  69. if err != nil {
  70. return "", err
  71. }
  72. return string(rawOutput), nil
  73. }
  74. }