containerinfo.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. /*
  2. Copyright 2014 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 unversioned
  14. import (
  15. "bytes"
  16. "encoding/json"
  17. "fmt"
  18. "io"
  19. "net"
  20. "net/http"
  21. "strconv"
  22. cadvisorapi "github.com/google/cadvisor/info/v1"
  23. )
  24. type ContainerInfoGetter interface {
  25. // GetContainerInfo returns information about a container.
  26. GetContainerInfo(host, podID, containerID string, req *cadvisorapi.ContainerInfoRequest) (*cadvisorapi.ContainerInfo, error)
  27. // GetRootInfo returns information about the root container on a machine.
  28. GetRootInfo(host string, req *cadvisorapi.ContainerInfoRequest) (*cadvisorapi.ContainerInfo, error)
  29. // GetMachineInfo returns the machine's information like number of cores, memory capacity.
  30. GetMachineInfo(host string) (*cadvisorapi.MachineInfo, error)
  31. }
  32. type HTTPContainerInfoGetter struct {
  33. Client *http.Client
  34. Port int
  35. }
  36. func (self *HTTPContainerInfoGetter) GetMachineInfo(host string) (*cadvisorapi.MachineInfo, error) {
  37. request, err := http.NewRequest(
  38. "GET",
  39. fmt.Sprintf("http://%v/spec",
  40. net.JoinHostPort(host, strconv.Itoa(self.Port)),
  41. ),
  42. nil,
  43. )
  44. if err != nil {
  45. return nil, err
  46. }
  47. response, err := self.Client.Do(request)
  48. if err != nil {
  49. return nil, err
  50. }
  51. defer response.Body.Close()
  52. if response.StatusCode != http.StatusOK {
  53. return nil, fmt.Errorf("trying to get machine spec from %v; received status %v",
  54. host, response.Status)
  55. }
  56. var minfo cadvisorapi.MachineInfo
  57. err = json.NewDecoder(response.Body).Decode(&minfo)
  58. if err != nil {
  59. return nil, err
  60. }
  61. return &minfo, nil
  62. }
  63. func (self *HTTPContainerInfoGetter) getContainerInfo(host, path string, req *cadvisorapi.ContainerInfoRequest) (*cadvisorapi.ContainerInfo, error) {
  64. var body io.Reader
  65. if req != nil {
  66. content, err := json.Marshal(req)
  67. if err != nil {
  68. return nil, err
  69. }
  70. body = bytes.NewBuffer(content)
  71. }
  72. request, err := http.NewRequest(
  73. "GET",
  74. fmt.Sprintf("http://%v/stats/%v",
  75. net.JoinHostPort(host, strconv.Itoa(self.Port)),
  76. path,
  77. ),
  78. body,
  79. )
  80. if err != nil {
  81. return nil, err
  82. }
  83. response, err := self.Client.Do(request)
  84. if err != nil {
  85. return nil, err
  86. }
  87. defer response.Body.Close()
  88. if response.StatusCode != http.StatusOK {
  89. return nil, fmt.Errorf("trying to get info for %v from %v; received status %v",
  90. path, host, response.Status)
  91. }
  92. var cinfo cadvisorapi.ContainerInfo
  93. err = json.NewDecoder(response.Body).Decode(&cinfo)
  94. if err != nil {
  95. return nil, err
  96. }
  97. return &cinfo, nil
  98. }
  99. func (self *HTTPContainerInfoGetter) GetContainerInfo(host, podID, containerID string, req *cadvisorapi.ContainerInfoRequest) (*cadvisorapi.ContainerInfo, error) {
  100. return self.getContainerInfo(
  101. host,
  102. fmt.Sprintf("%v/%v", podID, containerID),
  103. req,
  104. )
  105. }
  106. func (self *HTTPContainerInfoGetter) GetRootInfo(host string, req *cadvisorapi.ContainerInfoRequest) (*cadvisorapi.ContainerInfo, error) {
  107. return self.getContainerInfo(host, "", req)
  108. }