k8s.io/kubernetes@v1.31.0-alpha.0.0.20240520171757-56147500dadc/pkg/registry/batch/cronjob/storage/storage.go (about) 1 /* 2 Copyright 2016 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 22 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" 23 "k8s.io/apimachinery/pkg/runtime" 24 "k8s.io/apiserver/pkg/registry/generic" 25 genericregistry "k8s.io/apiserver/pkg/registry/generic/registry" 26 "k8s.io/apiserver/pkg/registry/rest" 27 "k8s.io/kubernetes/pkg/apis/batch" 28 "k8s.io/kubernetes/pkg/printers" 29 printersinternal "k8s.io/kubernetes/pkg/printers/internalversion" 30 printerstorage "k8s.io/kubernetes/pkg/printers/storage" 31 "k8s.io/kubernetes/pkg/registry/batch/cronjob" 32 "sigs.k8s.io/structured-merge-diff/v4/fieldpath" 33 ) 34 35 // REST implements a RESTStorage for scheduled jobs against etcd 36 type REST struct { 37 *genericregistry.Store 38 } 39 40 // NewREST returns a RESTStorage object that will work against CronJobs. 41 func NewREST(optsGetter generic.RESTOptionsGetter) (*REST, *StatusREST, error) { 42 store := &genericregistry.Store{ 43 NewFunc: func() runtime.Object { return &batch.CronJob{} }, 44 NewListFunc: func() runtime.Object { return &batch.CronJobList{} }, 45 DefaultQualifiedResource: batch.Resource("cronjobs"), 46 SingularQualifiedResource: batch.Resource("cronjob"), 47 48 CreateStrategy: cronjob.Strategy, 49 UpdateStrategy: cronjob.Strategy, 50 DeleteStrategy: cronjob.Strategy, 51 ResetFieldsStrategy: cronjob.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, nil, err 58 } 59 60 statusStore := *store 61 statusStore.UpdateStrategy = cronjob.StatusStrategy 62 statusStore.ResetFieldsStrategy = cronjob.StatusStrategy 63 64 return &REST{store}, &StatusREST{store: &statusStore}, nil 65 } 66 67 var _ rest.CategoriesProvider = &REST{} 68 var _ rest.ShortNamesProvider = &REST{} 69 70 // Categories implements the CategoriesProvider interface. Returns a list of categories a resource is part of. 71 func (r *REST) Categories() []string { 72 return []string{"all"} 73 } 74 75 // ShortNames implements the ShortNamesProvider interface. Returns a list of short names for a resource. 76 func (r *REST) ShortNames() []string { 77 return []string{"cj"} 78 } 79 80 // StatusREST implements the REST endpoint for changing the status of a resourcequota. 81 type StatusREST struct { 82 store *genericregistry.Store 83 } 84 85 // New creates a new CronJob object. 86 func (r *StatusREST) New() runtime.Object { 87 return &batch.CronJob{} 88 } 89 90 // Destroy cleans up resources on shutdown. 91 func (r *StatusREST) Destroy() { 92 // Given that underlying store is shared with REST, 93 // we don't destroy it here explicitly. 94 } 95 96 // Get retrieves the object from the storage. It is required to support Patch. 97 func (r *StatusREST) Get(ctx context.Context, name string, options *metav1.GetOptions) (runtime.Object, error) { 98 return r.store.Get(ctx, name, options) 99 } 100 101 // Update alters the status subset of an object. 102 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) { 103 // We are explicitly setting forceAllowCreate to false in the call to the underlying storage because 104 // subresources should never allow create on update. 105 return r.store.Update(ctx, name, objInfo, createValidation, updateValidation, false, options) 106 } 107 108 // GetResetFields implements rest.ResetFieldsStrategy 109 func (r *StatusREST) GetResetFields() map[fieldpath.APIVersion]*fieldpath.Set { 110 return r.store.GetResetFields() 111 } 112 113 func (r *StatusREST) ConvertToTable(ctx context.Context, object runtime.Object, tableOptions runtime.Object) (*metav1.Table, error) { 114 return r.store.ConvertToTable(ctx, object, tableOptions) 115 }