storage.go 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. /*
  2. Copyright 2019 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/flowcontrol"
  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/flowcontrol/flowschema"
  26. )
  27. // FlowSchemaStorage implements storage for flow schema.
  28. type FlowSchemaStorage struct {
  29. FlowSchema *REST
  30. Status *StatusREST
  31. }
  32. // REST implements a RESTStorage for flow schema against etcd
  33. type REST struct {
  34. *genericregistry.Store
  35. }
  36. // NewREST returns a RESTStorage object that will work against flow schemas.
  37. func NewREST(optsGetter generic.RESTOptionsGetter) (*REST, *StatusREST, error) {
  38. store := &genericregistry.Store{
  39. NewFunc: func() runtime.Object { return &flowcontrol.FlowSchema{} },
  40. NewListFunc: func() runtime.Object { return &flowcontrol.FlowSchemaList{} },
  41. DefaultQualifiedResource: flowcontrol.Resource("flowschemas"),
  42. CreateStrategy: flowschema.Strategy,
  43. UpdateStrategy: flowschema.Strategy,
  44. DeleteStrategy: flowschema.Strategy,
  45. TableConvertor: printerstorage.TableConvertor{TableGenerator: printers.NewTableGenerator().With(printersinternal.AddHandlers)},
  46. }
  47. options := &generic.StoreOptions{RESTOptions: optsGetter}
  48. if err := store.CompleteWithOptions(options); err != nil {
  49. return nil, nil, err
  50. }
  51. statusStore := *store
  52. statusStore.CreateStrategy = nil
  53. statusStore.UpdateStrategy = flowschema.StatusStrategy
  54. statusStore.DeleteStrategy = nil
  55. return &REST{store}, &StatusREST{store: &statusStore}, nil
  56. }
  57. // StatusREST implements the REST endpoint for changing the status of a flow schema.
  58. type StatusREST struct {
  59. store *genericregistry.Store
  60. }
  61. // New creates a new flow schema object.
  62. func (r *StatusREST) New() runtime.Object {
  63. return &flowcontrol.FlowSchema{}
  64. }
  65. // Get retrieves the object from the storage. It is required to support Patch.
  66. func (r *StatusREST) Get(ctx context.Context, name string, options *metav1.GetOptions) (runtime.Object, error) {
  67. return r.store.Get(ctx, name, options)
  68. }
  69. // Update alters the status subset of an object.
  70. 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) {
  71. // We are explicitly setting forceAllowCreate to false in the call to the underlying storage because
  72. // subresources should never allow create on update.
  73. return r.store.Update(ctx, name, objInfo, createValidation, updateValidation, false, options)
  74. }