storage.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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 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/apps"
  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/apps/daemonset"
  26. )
  27. // REST implements a RESTStorage for DaemonSets
  28. type REST struct {
  29. *genericregistry.Store
  30. categories []string
  31. }
  32. // NewREST returns a RESTStorage object that will work against DaemonSets.
  33. func NewREST(optsGetter generic.RESTOptionsGetter) (*REST, *StatusREST, error) {
  34. store := &genericregistry.Store{
  35. NewFunc: func() runtime.Object { return &apps.DaemonSet{} },
  36. NewListFunc: func() runtime.Object { return &apps.DaemonSetList{} },
  37. DefaultQualifiedResource: apps.Resource("daemonsets"),
  38. CreateStrategy: daemonset.Strategy,
  39. UpdateStrategy: daemonset.Strategy,
  40. DeleteStrategy: daemonset.Strategy,
  41. TableConvertor: printerstorage.TableConvertor{TableGenerator: printers.NewTableGenerator().With(printersinternal.AddHandlers)},
  42. }
  43. options := &generic.StoreOptions{RESTOptions: optsGetter}
  44. if err := store.CompleteWithOptions(options); err != nil {
  45. return nil, nil, err
  46. }
  47. statusStore := *store
  48. statusStore.UpdateStrategy = daemonset.StatusStrategy
  49. return &REST{store, []string{"all"}}, &StatusREST{store: &statusStore}, nil
  50. }
  51. // Implement ShortNamesProvider
  52. var _ rest.ShortNamesProvider = &REST{}
  53. // ShortNames implements the ShortNamesProvider interface. Returns a list of short names for a resource.
  54. func (r *REST) ShortNames() []string {
  55. return []string{"ds"}
  56. }
  57. var _ rest.CategoriesProvider = &REST{}
  58. // Categories implements the CategoriesProvider interface. Returns a list of categories a resource is part of.
  59. func (r *REST) Categories() []string {
  60. return r.categories
  61. }
  62. // WithCategories sets categories for REST.
  63. func (r *REST) WithCategories(categories []string) *REST {
  64. r.categories = categories
  65. return r
  66. }
  67. // StatusREST implements the REST endpoint for changing the status of a daemonset
  68. type StatusREST struct {
  69. store *genericregistry.Store
  70. }
  71. // New creates a new DaemonSet object.
  72. func (r *StatusREST) New() runtime.Object {
  73. return &apps.DaemonSet{}
  74. }
  75. // Get retrieves the object from the storage. It is required to support Patch.
  76. func (r *StatusREST) Get(ctx context.Context, name string, options *metav1.GetOptions) (runtime.Object, error) {
  77. return r.store.Get(ctx, name, options)
  78. }
  79. // Update alters the status subset of an object.
  80. 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) {
  81. // We are explicitly setting forceAllowCreate to false in the call to the underlying storage because
  82. // subresources should never allow create on update.
  83. return r.store.Update(ctx, name, objInfo, createValidation, updateValidation, false, options)
  84. }