k8s.io/kubernetes@v1.29.3/pkg/registry/scheduling/priorityclass/storage/storage.go (about) 1 /* 2 Copyright 2017 The Kubernetes Authors. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package storage 18 19 import ( 20 "context" 21 "errors" 22 apierrors "k8s.io/apimachinery/pkg/api/errors" 23 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" 24 "k8s.io/apimachinery/pkg/runtime" 25 "k8s.io/apiserver/pkg/registry/generic" 26 genericregistry "k8s.io/apiserver/pkg/registry/generic/registry" 27 "k8s.io/apiserver/pkg/registry/rest" 28 "k8s.io/kubernetes/pkg/apis/scheduling" 29 schedulingapiv1 "k8s.io/kubernetes/pkg/apis/scheduling/v1" 30 "k8s.io/kubernetes/pkg/printers" 31 printersinternal "k8s.io/kubernetes/pkg/printers/internalversion" 32 printerstorage "k8s.io/kubernetes/pkg/printers/storage" 33 "k8s.io/kubernetes/pkg/registry/scheduling/priorityclass" 34 ) 35 36 // REST implements a RESTStorage for priority classes against etcd 37 type REST struct { 38 *genericregistry.Store 39 } 40 41 // NewREST returns a RESTStorage object that will work against priority classes. 42 func NewREST(optsGetter generic.RESTOptionsGetter) (*REST, error) { 43 store := &genericregistry.Store{ 44 NewFunc: func() runtime.Object { return &scheduling.PriorityClass{} }, 45 NewListFunc: func() runtime.Object { return &scheduling.PriorityClassList{} }, 46 DefaultQualifiedResource: scheduling.Resource("priorityclasses"), 47 SingularQualifiedResource: scheduling.Resource("priorityclass"), 48 49 CreateStrategy: priorityclass.Strategy, 50 UpdateStrategy: priorityclass.Strategy, 51 DeleteStrategy: priorityclass.Strategy, 52 53 TableConvertor: printerstorage.TableConvertor{TableGenerator: printers.NewTableGenerator().With(printersinternal.AddHandlers)}, 54 } 55 options := &generic.StoreOptions{RESTOptions: optsGetter} 56 if err := store.CompleteWithOptions(options); err != nil { 57 return nil, err 58 } 59 60 return &REST{store}, nil 61 } 62 63 // Implement ShortNamesProvider 64 var _ rest.ShortNamesProvider = &REST{} 65 66 // ShortNames implements the ShortNamesProvider interface. Returns a list of short names for a resource. 67 func (r *REST) ShortNames() []string { 68 return []string{"pc"} 69 } 70 71 // Delete ensures that system priority classes are not deleted. 72 func (r *REST) Delete(ctx context.Context, name string, deleteValidation rest.ValidateObjectFunc, options *metav1.DeleteOptions) (runtime.Object, bool, error) { 73 for _, spc := range schedulingapiv1.SystemPriorityClasses() { 74 if name == spc.Name { 75 return nil, false, apierrors.NewForbidden(scheduling.Resource("priorityclasses"), spc.Name, errors.New("this is a system priority class and cannot be deleted")) 76 } 77 } 78 79 return r.Store.Delete(ctx, name, deleteValidation, options) 80 }