rest.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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. "k8s.io/apimachinery/pkg/fields"
  19. "k8s.io/apimachinery/pkg/labels"
  20. "k8s.io/apiserver/pkg/registry/generic"
  21. "k8s.io/apiserver/pkg/storage"
  22. metainternalversion "k8s.io/apimachinery/pkg/apis/meta/internalversion"
  23. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  24. "k8s.io/apimachinery/pkg/runtime"
  25. "k8s.io/apiserver/pkg/registry/rest"
  26. api "k8s.io/kubernetes/pkg/apis/core"
  27. "k8s.io/kubernetes/pkg/printers"
  28. printersinternal "k8s.io/kubernetes/pkg/printers/internalversion"
  29. printerstorage "k8s.io/kubernetes/pkg/printers/storage"
  30. "k8s.io/kubernetes/pkg/probe"
  31. )
  32. type REST struct {
  33. GetServersToValidate func() map[string]*Server
  34. rest.TableConvertor
  35. }
  36. // NewStorage returns a new REST.
  37. func NewStorage(serverRetriever func() map[string]*Server) *REST {
  38. return &REST{
  39. GetServersToValidate: serverRetriever,
  40. TableConvertor: printerstorage.TableConvertor{TableGenerator: printers.NewTableGenerator().With(printersinternal.AddHandlers)},
  41. }
  42. }
  43. func (*REST) NamespaceScoped() bool {
  44. return false
  45. }
  46. func (rs *REST) New() runtime.Object {
  47. return &api.ComponentStatus{}
  48. }
  49. func (rs *REST) NewList() runtime.Object {
  50. return &api.ComponentStatusList{}
  51. }
  52. // Returns the list of component status. Note that the label and field are both ignored.
  53. // Note that this call doesn't support labels or selectors.
  54. func (rs *REST) List(ctx context.Context, options *metainternalversion.ListOptions) (runtime.Object, error) {
  55. servers := rs.GetServersToValidate()
  56. wait := sync.WaitGroup{}
  57. wait.Add(len(servers))
  58. statuses := make(chan api.ComponentStatus, len(servers))
  59. for k, v := range servers {
  60. go func(name string, server *Server) {
  61. defer wait.Done()
  62. status := rs.getComponentStatus(name, server)
  63. statuses <- *status
  64. }(k, v)
  65. }
  66. wait.Wait()
  67. close(statuses)
  68. pred := componentStatusPredicate(options)
  69. reply := []api.ComponentStatus{}
  70. for status := range statuses {
  71. // ComponentStatus resources currently (v1.14) do not support labeling, however the filtering is executed
  72. // nonetheless in case the request contains Label or Field selectors (which will effectively filter out
  73. // all of the results and return an empty response).
  74. if matched := matchesPredicate(status, &pred); matched {
  75. reply = append(reply, status)
  76. }
  77. }
  78. return &api.ComponentStatusList{Items: reply}, nil
  79. }
  80. func componentStatusPredicate(options *metainternalversion.ListOptions) storage.SelectionPredicate {
  81. pred := storage.SelectionPredicate{
  82. Label: labels.Everything(),
  83. Field: fields.Everything(),
  84. GetAttrs: nil,
  85. IndexFields: []string{},
  86. }
  87. if options != nil {
  88. if options.LabelSelector != nil {
  89. pred.Label = options.LabelSelector
  90. }
  91. if options.FieldSelector != nil {
  92. pred.Field = options.FieldSelector
  93. }
  94. }
  95. return pred
  96. }
  97. func matchesPredicate(status api.ComponentStatus, pred *storage.SelectionPredicate) bool {
  98. // currently no fields except the generic meta fields are supported for predicate matching
  99. fieldsSet := generic.AddObjectMetaFieldsSet(make(fields.Set, 2), &status.ObjectMeta, true)
  100. return pred.MatchesObjectAttributes(
  101. status.ObjectMeta.Labels,
  102. fieldsSet,
  103. )
  104. }
  105. func (rs *REST) Get(ctx context.Context, name string, options *metav1.GetOptions) (runtime.Object, error) {
  106. servers := rs.GetServersToValidate()
  107. if server, ok := servers[name]; !ok {
  108. return nil, fmt.Errorf("Component not found: %s", name)
  109. } else {
  110. return rs.getComponentStatus(name, server), nil
  111. }
  112. }
  113. func ToConditionStatus(s probe.Result) api.ConditionStatus {
  114. switch s {
  115. case probe.Success:
  116. return api.ConditionTrue
  117. case probe.Failure:
  118. return api.ConditionFalse
  119. default:
  120. return api.ConditionUnknown
  121. }
  122. }
  123. func (rs *REST) getComponentStatus(name string, server *Server) *api.ComponentStatus {
  124. status, msg, err := server.DoServerCheck()
  125. errorMsg := ""
  126. if err != nil {
  127. errorMsg = err.Error()
  128. }
  129. c := &api.ComponentCondition{
  130. Type: api.ComponentHealthy,
  131. Status: ToConditionStatus(status),
  132. Message: msg,
  133. Error: errorMsg,
  134. }
  135. retVal := &api.ComponentStatus{
  136. Conditions: []api.ComponentCondition{*c},
  137. }
  138. retVal.Name = name
  139. return retVal
  140. }
  141. // Implement ShortNamesProvider
  142. var _ rest.ShortNamesProvider = &REST{}
  143. // ShortNames implements the ShortNamesProvider interface. Returns a list of short names for a resource.
  144. func (r *REST) ShortNames() []string {
  145. return []string{"cs"}
  146. }