kubelet_metrics.go 2.5 KB

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