storage.go 3.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  17. "k8s.io/apimachinery/pkg/runtime"
  18. "k8s.io/apiserver/pkg/registry/generic"
  19. genericregistry "k8s.io/apiserver/pkg/registry/generic/registry"
  20. "k8s.io/apiserver/pkg/registry/rest"
  21. "k8s.io/kubernetes/pkg/apis/autoscaling"
  22. "k8s.io/kubernetes/pkg/printers"
  23. printersinternal "k8s.io/kubernetes/pkg/printers/internalversion"
  24. printerstorage "k8s.io/kubernetes/pkg/printers/storage"
  25. "k8s.io/kubernetes/pkg/registry/autoscaling/horizontalpodautoscaler"
  26. )
  27. type REST struct {
  28. *genericregistry.Store
  29. }
  30. // NewREST returns a RESTStorage object that will work against horizontal pod autoscalers.
  31. func NewREST(optsGetter generic.RESTOptionsGetter) (*REST, *StatusREST) {
  32. store := &genericregistry.Store{
  33. NewFunc: func() runtime.Object { return &autoscaling.HorizontalPodAutoscaler{} },
  34. NewListFunc: func() runtime.Object { return &autoscaling.HorizontalPodAutoscalerList{} },
  35. DefaultQualifiedResource: autoscaling.Resource("horizontalpodautoscalers"),
  36. CreateStrategy: horizontalpodautoscaler.Strategy,
  37. UpdateStrategy: horizontalpodautoscaler.Strategy,
  38. DeleteStrategy: horizontalpodautoscaler.Strategy,
  39. TableConvertor: printerstorage.TableConvertor{TableGenerator: printers.NewTableGenerator().With(printersinternal.AddHandlers)},
  40. }
  41. options := &generic.StoreOptions{RESTOptions: optsGetter}
  42. if err := store.CompleteWithOptions(options); err != nil {
  43. panic(err) // TODO: Propagate error up
  44. }
  45. statusStore := *store
  46. statusStore.UpdateStrategy = horizontalpodautoscaler.StatusStrategy
  47. return &REST{store}, &StatusREST{store: &statusStore}
  48. }
  49. // Implement ShortNamesProvider
  50. var _ rest.ShortNamesProvider = &REST{}
  51. // ShortNames implements the ShortNamesProvider interface. Returns a list of short names for a resource.
  52. func (r *REST) ShortNames() []string {
  53. return []string{"hpa"}
  54. }
  55. // Implement CategoriesProvider
  56. var _ rest.CategoriesProvider = &REST{}
  57. // Categories implements the CategoriesProvider interface. Returns a list of categories a resource is part of.
  58. func (r *REST) Categories() []string {
  59. return []string{"all"}
  60. }
  61. /// StatusREST implements the REST endpoint for changing the status of a daemonset
  62. type StatusREST struct {
  63. store *genericregistry.Store
  64. }
  65. func (r *StatusREST) New() runtime.Object {
  66. return &autoscaling.HorizontalPodAutoscaler{}
  67. }
  68. // Get retrieves the object from the storage. It is required to support Patch.
  69. func (r *StatusREST) Get(ctx context.Context, name string, options *metav1.GetOptions) (runtime.Object, error) {
  70. return r.store.Get(ctx, name, options)
  71. }
  72. // Update alters the status subset of an object.
  73. func (r *StatusREST) Update(ctx context.Context, name string, objInfo rest.UpdatedObjectInfo, createValidation rest.ValidateObjectFunc, updateValidation rest.ValidateObjectUpdateFunc, forceAllowCreate bool, options *metav1.UpdateOptions) (runtime.Object, bool, error) {
  74. // We are explicitly setting forceAllowCreate to false in the call to the underlying storage because
  75. // subresources should never allow create on update.
  76. return r.store.Update(ctx, name, objInfo, createValidation, updateValidation, false, options)
  77. }