storage.go 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. /*
  2. Copyright 2017 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. "errors"
  17. apierrors "k8s.io/apimachinery/pkg/api/errors"
  18. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  19. "k8s.io/apimachinery/pkg/runtime"
  20. "k8s.io/apiserver/pkg/registry/generic"
  21. genericregistry "k8s.io/apiserver/pkg/registry/generic/registry"
  22. "k8s.io/apiserver/pkg/registry/rest"
  23. "k8s.io/kubernetes/pkg/apis/scheduling"
  24. schedulingapiv1 "k8s.io/kubernetes/pkg/apis/scheduling/v1"
  25. "k8s.io/kubernetes/pkg/printers"
  26. printersinternal "k8s.io/kubernetes/pkg/printers/internalversion"
  27. printerstorage "k8s.io/kubernetes/pkg/printers/storage"
  28. "k8s.io/kubernetes/pkg/registry/scheduling/priorityclass"
  29. )
  30. // REST implements a RESTStorage for priority classes against etcd
  31. type REST struct {
  32. *genericregistry.Store
  33. }
  34. // NewREST returns a RESTStorage object that will work against priority classes.
  35. func NewREST(optsGetter generic.RESTOptionsGetter) (*REST, error) {
  36. store := &genericregistry.Store{
  37. NewFunc: func() runtime.Object { return &scheduling.PriorityClass{} },
  38. NewListFunc: func() runtime.Object { return &scheduling.PriorityClassList{} },
  39. DefaultQualifiedResource: scheduling.Resource("priorityclasses"),
  40. CreateStrategy: priorityclass.Strategy,
  41. UpdateStrategy: priorityclass.Strategy,
  42. DeleteStrategy: priorityclass.Strategy,
  43. TableConvertor: printerstorage.TableConvertor{TableGenerator: printers.NewTableGenerator().With(printersinternal.AddHandlers)},
  44. }
  45. options := &generic.StoreOptions{RESTOptions: optsGetter}
  46. if err := store.CompleteWithOptions(options); err != nil {
  47. return nil, err
  48. }
  49. return &REST{store}, 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{"pc"}
  56. }
  57. // Delete ensures that system priority classes are not deleted.
  58. func (r *REST) Delete(ctx context.Context, name string, deleteValidation rest.ValidateObjectFunc, options *metav1.DeleteOptions) (runtime.Object, bool, error) {
  59. for _, spc := range schedulingapiv1.SystemPriorityClasses() {
  60. if name == spc.Name {
  61. return nil, false, apierrors.NewForbidden(scheduling.Resource("priorityclasses"), spc.Name, errors.New("this is a system priority class and cannot be deleted"))
  62. }
  63. }
  64. return r.Store.Delete(ctx, name, deleteValidation, options)
  65. }