etcd.go 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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. // If you make changes to this file, you should also make the corresponding change in ReplicaSet.
  14. package etcd
  15. import (
  16. "fmt"
  17. "k8s.io/kubernetes/pkg/api"
  18. "k8s.io/kubernetes/pkg/api/errors"
  19. "k8s.io/kubernetes/pkg/api/rest"
  20. "k8s.io/kubernetes/pkg/apis/autoscaling"
  21. "k8s.io/kubernetes/pkg/apis/autoscaling/validation"
  22. "k8s.io/kubernetes/pkg/labels"
  23. "k8s.io/kubernetes/pkg/registry/cachesize"
  24. "k8s.io/kubernetes/pkg/registry/controller"
  25. "k8s.io/kubernetes/pkg/registry/generic"
  26. "k8s.io/kubernetes/pkg/registry/generic/registry"
  27. "k8s.io/kubernetes/pkg/runtime"
  28. "k8s.io/kubernetes/pkg/storage"
  29. )
  30. // ControllerStorage includes dummy storage for Replication Controllers and for Scale subresource.
  31. type ControllerStorage struct {
  32. Controller *REST
  33. Status *StatusREST
  34. Scale *ScaleREST
  35. }
  36. func NewStorage(opts generic.RESTOptions) ControllerStorage {
  37. controllerREST, statusREST := NewREST(opts)
  38. controllerRegistry := controller.NewRegistry(controllerREST)
  39. return ControllerStorage{
  40. Controller: controllerREST,
  41. Status: statusREST,
  42. Scale: &ScaleREST{registry: controllerRegistry},
  43. }
  44. }
  45. type REST struct {
  46. *registry.Store
  47. }
  48. // NewREST returns a RESTStorage object that will work against replication controllers.
  49. func NewREST(opts generic.RESTOptions) (*REST, *StatusREST) {
  50. prefix := "/" + opts.ResourcePrefix
  51. newListFunc := func() runtime.Object { return &api.ReplicationControllerList{} }
  52. storageInterface, _ := opts.Decorator(
  53. opts.StorageConfig,
  54. cachesize.GetWatchCacheSizeByResource(cachesize.Controllers),
  55. &api.ReplicationController{},
  56. prefix,
  57. controller.Strategy,
  58. newListFunc,
  59. storage.NoTriggerPublisher,
  60. )
  61. store := &registry.Store{
  62. NewFunc: func() runtime.Object { return &api.ReplicationController{} },
  63. // NewListFunc returns an object capable of storing results of an etcd list.
  64. NewListFunc: newListFunc,
  65. // Produces a path that etcd understands, to the root of the resource
  66. // by combining the namespace in the context with the given prefix
  67. KeyRootFunc: func(ctx api.Context) string {
  68. return registry.NamespaceKeyRootFunc(ctx, prefix)
  69. },
  70. // Produces a path that etcd understands, to the resource by combining
  71. // the namespace in the context with the given prefix
  72. KeyFunc: func(ctx api.Context, name string) (string, error) {
  73. return registry.NamespaceKeyFunc(ctx, prefix, name)
  74. },
  75. // Retrieve the name field of a replication controller
  76. ObjectNameFunc: func(obj runtime.Object) (string, error) {
  77. return obj.(*api.ReplicationController).Name, nil
  78. },
  79. // Used to match objects based on labels/fields for list and watch
  80. PredicateFunc: controller.MatchController,
  81. QualifiedResource: api.Resource("replicationcontrollers"),
  82. DeleteCollectionWorkers: opts.DeleteCollectionWorkers,
  83. // Used to validate controller creation
  84. CreateStrategy: controller.Strategy,
  85. // Used to validate controller updates
  86. UpdateStrategy: controller.Strategy,
  87. DeleteStrategy: controller.Strategy,
  88. Storage: storageInterface,
  89. }
  90. statusStore := *store
  91. statusStore.UpdateStrategy = controller.StatusStrategy
  92. return &REST{store}, &StatusREST{store: &statusStore}
  93. }
  94. // StatusREST implements the REST endpoint for changing the status of a replication controller
  95. type StatusREST struct {
  96. store *registry.Store
  97. }
  98. func (r *StatusREST) New() runtime.Object {
  99. return &api.ReplicationController{}
  100. }
  101. // Get retrieves the object from the storage. It is required to support Patch.
  102. func (r *StatusREST) Get(ctx api.Context, name string) (runtime.Object, error) {
  103. return r.store.Get(ctx, name)
  104. }
  105. // Update alters the status subset of an object.
  106. func (r *StatusREST) Update(ctx api.Context, name string, objInfo rest.UpdatedObjectInfo) (runtime.Object, bool, error) {
  107. return r.store.Update(ctx, name, objInfo)
  108. }
  109. type ScaleREST struct {
  110. registry controller.Registry
  111. }
  112. // ScaleREST implements Patcher
  113. var _ = rest.Patcher(&ScaleREST{})
  114. // New creates a new Scale object
  115. func (r *ScaleREST) New() runtime.Object {
  116. return &autoscaling.Scale{}
  117. }
  118. func (r *ScaleREST) Get(ctx api.Context, name string) (runtime.Object, error) {
  119. rc, err := r.registry.GetController(ctx, name)
  120. if err != nil {
  121. return nil, errors.NewNotFound(autoscaling.Resource("replicationcontrollers/scale"), name)
  122. }
  123. return scaleFromRC(rc), nil
  124. }
  125. func (r *ScaleREST) Update(ctx api.Context, name string, objInfo rest.UpdatedObjectInfo) (runtime.Object, bool, error) {
  126. rc, err := r.registry.GetController(ctx, name)
  127. if err != nil {
  128. return nil, false, errors.NewNotFound(autoscaling.Resource("replicationcontrollers/scale"), name)
  129. }
  130. oldScale := scaleFromRC(rc)
  131. obj, err := objInfo.UpdatedObject(ctx, oldScale)
  132. if err != nil {
  133. return nil, false, err
  134. }
  135. if obj == nil {
  136. return nil, false, errors.NewBadRequest("nil update passed to Scale")
  137. }
  138. scale, ok := obj.(*autoscaling.Scale)
  139. if !ok {
  140. return nil, false, errors.NewBadRequest(fmt.Sprintf("wrong object passed to Scale update: %v", obj))
  141. }
  142. if errs := validation.ValidateScale(scale); len(errs) > 0 {
  143. return nil, false, errors.NewInvalid(autoscaling.Kind("Scale"), scale.Name, errs)
  144. }
  145. rc.Spec.Replicas = scale.Spec.Replicas
  146. rc.ResourceVersion = scale.ResourceVersion
  147. rc, err = r.registry.UpdateController(ctx, rc)
  148. if err != nil {
  149. return nil, false, err
  150. }
  151. return scaleFromRC(rc), false, nil
  152. }
  153. // scaleFromRC returns a scale subresource for a replication controller.
  154. func scaleFromRC(rc *api.ReplicationController) *autoscaling.Scale {
  155. return &autoscaling.Scale{
  156. ObjectMeta: api.ObjectMeta{
  157. Name: rc.Name,
  158. Namespace: rc.Namespace,
  159. UID: rc.UID,
  160. ResourceVersion: rc.ResourceVersion,
  161. CreationTimestamp: rc.CreationTimestamp,
  162. },
  163. Spec: autoscaling.ScaleSpec{
  164. Replicas: rc.Spec.Replicas,
  165. },
  166. Status: autoscaling.ScaleStatus{
  167. Replicas: rc.Status.Replicas,
  168. Selector: labels.SelectorFromSet(rc.Spec.Selector).String(),
  169. },
  170. }
  171. }