storage.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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 storage
  14. import (
  15. "context"
  16. "fmt"
  17. "k8s.io/apimachinery/pkg/api/errors"
  18. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  19. "k8s.io/apimachinery/pkg/labels"
  20. "k8s.io/apimachinery/pkg/runtime"
  21. "k8s.io/apiserver/pkg/registry/generic"
  22. genericregistry "k8s.io/apiserver/pkg/registry/generic/registry"
  23. "k8s.io/apiserver/pkg/registry/rest"
  24. "k8s.io/kubernetes/pkg/apis/autoscaling"
  25. autoscalingvalidation "k8s.io/kubernetes/pkg/apis/autoscaling/validation"
  26. api "k8s.io/kubernetes/pkg/apis/core"
  27. "k8s.io/kubernetes/pkg/apis/extensions"
  28. controllerstore "k8s.io/kubernetes/pkg/registry/core/replicationcontroller/storage"
  29. )
  30. // Container includes dummy storage for RC pods and experimental storage for Scale.
  31. type ContainerStorage struct {
  32. ReplicationController *RcREST
  33. Scale *ScaleREST
  34. }
  35. func NewStorage(optsGetter generic.RESTOptionsGetter) ContainerStorage {
  36. // scale does not set status, only updates spec so we ignore the status
  37. controllerREST, _ := controllerstore.NewREST(optsGetter)
  38. return ContainerStorage{
  39. ReplicationController: &RcREST{},
  40. Scale: &ScaleREST{store: controllerREST.Store},
  41. }
  42. }
  43. type ScaleREST struct {
  44. store *genericregistry.Store
  45. }
  46. // ScaleREST implements Patcher
  47. var _ = rest.Patcher(&ScaleREST{})
  48. // New creates a new Scale object
  49. func (r *ScaleREST) New() runtime.Object {
  50. return &autoscaling.Scale{}
  51. }
  52. func (r *ScaleREST) Get(ctx context.Context, name string, options *metav1.GetOptions) (runtime.Object, error) {
  53. obj, err := r.store.Get(ctx, name, options)
  54. if err != nil {
  55. return nil, errors.NewNotFound(extensions.Resource("replicationcontrollers/scale"), name)
  56. }
  57. rc := obj.(*api.ReplicationController)
  58. return scaleFromRC(rc), nil
  59. }
  60. func (r *ScaleREST) Update(ctx context.Context, name string, objInfo rest.UpdatedObjectInfo, createValidation rest.ValidateObjectFunc, updateValidation rest.ValidateObjectUpdateFunc, forceAllowCreate bool, options *metav1.UpdateOptions) (runtime.Object, bool, error) {
  61. obj, err := r.store.Get(ctx, name, &metav1.GetOptions{})
  62. if err != nil {
  63. return nil, false, errors.NewNotFound(extensions.Resource("replicationcontrollers/scale"), name)
  64. }
  65. rc := obj.(*api.ReplicationController)
  66. oldScale := scaleFromRC(rc)
  67. obj, err = objInfo.UpdatedObject(ctx, oldScale)
  68. if obj == nil {
  69. return nil, false, errors.NewBadRequest(fmt.Sprintf("nil update passed to Scale"))
  70. }
  71. scale, ok := obj.(*autoscaling.Scale)
  72. if !ok {
  73. return nil, false, errors.NewBadRequest(fmt.Sprintf("wrong object passed to Scale update: %v", obj))
  74. }
  75. if errs := autoscalingvalidation.ValidateScale(scale); len(errs) > 0 {
  76. return nil, false, errors.NewInvalid(extensions.Kind("Scale"), scale.Name, errs)
  77. }
  78. rc.Spec.Replicas = scale.Spec.Replicas
  79. rc.ResourceVersion = scale.ResourceVersion
  80. obj, _, err = r.store.Update(
  81. ctx,
  82. rc.Name,
  83. rest.DefaultUpdatedObjectInfo(rc),
  84. toScaleCreateValidation(createValidation),
  85. toScaleUpdateValidation(updateValidation),
  86. false,
  87. options,
  88. )
  89. if err != nil {
  90. return nil, false, errors.NewConflict(extensions.Resource("replicationcontrollers/scale"), scale.Name, err)
  91. }
  92. rc = obj.(*api.ReplicationController)
  93. return scaleFromRC(rc), false, nil
  94. }
  95. func toScaleCreateValidation(f rest.ValidateObjectFunc) rest.ValidateObjectFunc {
  96. return func(obj runtime.Object) error {
  97. return f(scaleFromRC(obj.(*api.ReplicationController)))
  98. }
  99. }
  100. func toScaleUpdateValidation(f rest.ValidateObjectUpdateFunc) rest.ValidateObjectUpdateFunc {
  101. return func(obj, old runtime.Object) error {
  102. return f(
  103. scaleFromRC(obj.(*api.ReplicationController)),
  104. scaleFromRC(old.(*api.ReplicationController)),
  105. )
  106. }
  107. }
  108. // scaleFromRC returns a scale subresource for a replication controller.
  109. func scaleFromRC(rc *api.ReplicationController) *autoscaling.Scale {
  110. return &autoscaling.Scale{
  111. ObjectMeta: metav1.ObjectMeta{
  112. Name: rc.Name,
  113. Namespace: rc.Namespace,
  114. UID: rc.UID,
  115. ResourceVersion: rc.ResourceVersion,
  116. CreationTimestamp: rc.CreationTimestamp,
  117. },
  118. Spec: autoscaling.ScaleSpec{
  119. Replicas: rc.Spec.Replicas,
  120. },
  121. Status: autoscaling.ScaleStatus{
  122. Replicas: rc.Status.Replicas,
  123. Selector: labels.SelectorFromSet(labels.Set(rc.Spec.Selector)).String(),
  124. },
  125. }
  126. }
  127. // Dummy implementation
  128. type RcREST struct{}
  129. func (r *RcREST) NamespaceScoped() bool {
  130. return true
  131. }
  132. func (r *RcREST) New() runtime.Object {
  133. return &extensions.ReplicationControllerDummy{}
  134. }