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. "k8s.io/kubernetes/pkg/printers"
  25. printersinternal "k8s.io/kubernetes/pkg/printers/internalversion"
  26. printerstorage "k8s.io/kubernetes/pkg/printers/storage"
  27. "k8s.io/kubernetes/pkg/registry/scheduling/priorityclass"
  28. )
  29. // REST implements a RESTStorage for priority classes against etcd
  30. type REST struct {
  31. *genericregistry.Store
  32. }
  33. // NewREST returns a RESTStorage object that will work against priority classes.
  34. func NewREST(optsGetter generic.RESTOptionsGetter) *REST {
  35. store := &genericregistry.Store{
  36. NewFunc: func() runtime.Object { return &scheduling.PriorityClass{} },
  37. NewListFunc: func() runtime.Object { return &scheduling.PriorityClassList{} },
  38. DefaultQualifiedResource: scheduling.Resource("priorityclasses"),
  39. CreateStrategy: priorityclass.Strategy,
  40. UpdateStrategy: priorityclass.Strategy,
  41. DeleteStrategy: priorityclass.Strategy,
  42. TableConvertor: printerstorage.TableConvertor{TableGenerator: printers.NewTableGenerator().With(printersinternal.AddHandlers)},
  43. }
  44. options := &generic.StoreOptions{RESTOptions: optsGetter}
  45. if err := store.CompleteWithOptions(options); err != nil {
  46. panic(err) // TODO: Propagate error up
  47. }
  48. return &REST{store}
  49. }
  50. // Implement ShortNamesProvider
  51. var _ rest.ShortNamesProvider = &REST{}
  52. // ShortNames implements the ShortNamesProvider interface. Returns a list of short names for a resource.
  53. func (r *REST) ShortNames() []string {
  54. return []string{"pc"}
  55. }
  56. // Delete ensures that system priority classes are not deleted.
  57. func (r *REST) Delete(ctx context.Context, name string, deleteValidation rest.ValidateObjectFunc, options *metav1.DeleteOptions) (runtime.Object, bool, error) {
  58. for _, spc := range scheduling.SystemPriorityClasses() {
  59. if name == spc.Name {
  60. return nil, false, apierrors.NewForbidden(scheduling.Resource("priorityclasses"), spc.Name, errors.New("this is a system priority class and cannot be deleted"))
  61. }
  62. }
  63. return r.Store.Delete(ctx, name, deleteValidation, options)
  64. }