rest.go 3.3 KB

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