rest.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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 componentstatus
  14. import (
  15. "fmt"
  16. "sync"
  17. "k8s.io/kubernetes/pkg/api"
  18. "k8s.io/kubernetes/pkg/apiserver"
  19. "k8s.io/kubernetes/pkg/probe"
  20. httpprober "k8s.io/kubernetes/pkg/probe/http"
  21. "k8s.io/kubernetes/pkg/runtime"
  22. )
  23. type REST struct {
  24. GetServersToValidate func() map[string]apiserver.Server
  25. prober httpprober.HTTPProber
  26. }
  27. // NewStorage returns a new REST.
  28. func NewStorage(serverRetriever func() map[string]apiserver.Server) *REST {
  29. return &REST{
  30. GetServersToValidate: serverRetriever,
  31. prober: httpprober.New(),
  32. }
  33. }
  34. func (rs *REST) New() runtime.Object {
  35. return &api.ComponentStatus{}
  36. }
  37. func (rs *REST) NewList() runtime.Object {
  38. return &api.ComponentStatusList{}
  39. }
  40. // Returns the list of component status. Note that the label and field are both ignored.
  41. // Note that this call doesn't support labels or selectors.
  42. func (rs *REST) List(ctx api.Context, options *api.ListOptions) (runtime.Object, error) {
  43. servers := rs.GetServersToValidate()
  44. wait := sync.WaitGroup{}
  45. wait.Add(len(servers))
  46. statuses := make(chan api.ComponentStatus, len(servers))
  47. for k, v := range servers {
  48. go func(name string, server apiserver.Server) {
  49. defer wait.Done()
  50. status := rs.getComponentStatus(name, server)
  51. statuses <- *status
  52. }(k, v)
  53. }
  54. wait.Wait()
  55. close(statuses)
  56. reply := []api.ComponentStatus{}
  57. for status := range statuses {
  58. reply = append(reply, status)
  59. }
  60. return &api.ComponentStatusList{Items: reply}, nil
  61. }
  62. func (rs *REST) Get(ctx api.Context, name string) (runtime.Object, error) {
  63. servers := rs.GetServersToValidate()
  64. if server, ok := servers[name]; !ok {
  65. return nil, fmt.Errorf("Component not found: %s", name)
  66. } else {
  67. return rs.getComponentStatus(name, server), nil
  68. }
  69. }
  70. func ToConditionStatus(s probe.Result) api.ConditionStatus {
  71. switch s {
  72. case probe.Success:
  73. return api.ConditionTrue
  74. case probe.Failure:
  75. return api.ConditionFalse
  76. default:
  77. return api.ConditionUnknown
  78. }
  79. }
  80. func (rs *REST) getComponentStatus(name string, server apiserver.Server) *api.ComponentStatus {
  81. status, msg, err := server.DoServerCheck(rs.prober)
  82. errorMsg := ""
  83. if err != nil {
  84. errorMsg = err.Error()
  85. }
  86. c := &api.ComponentCondition{
  87. Type: api.ComponentHealthy,
  88. Status: ToConditionStatus(status),
  89. Message: msg,
  90. Error: errorMsg,
  91. }
  92. retVal := &api.ComponentStatus{
  93. Conditions: []api.ComponentCondition{*c},
  94. }
  95. retVal.Name = name
  96. return retVal
  97. }