k8s.io/kubernetes@v1.29.3/pkg/registry/admissionregistration/validatingadmissionpolicy/storage/storage.go (about)

     1  /*
     2  Copyright 2022 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/authorization/authorizer"
    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/admissionregistration"
    29  	"k8s.io/kubernetes/pkg/printers"
    30  	printersinternal "k8s.io/kubernetes/pkg/printers/internalversion"
    31  	printerstorage "k8s.io/kubernetes/pkg/printers/storage"
    32  	"k8s.io/kubernetes/pkg/registry/admissionregistration/resolver"
    33  	"k8s.io/kubernetes/pkg/registry/admissionregistration/validatingadmissionpolicy"
    34  	"sigs.k8s.io/structured-merge-diff/v4/fieldpath"
    35  )
    36  
    37  // REST implements a RESTStorage for validatingAdmissionPolicy against etcd
    38  type REST struct {
    39  	*genericregistry.Store
    40  }
    41  
    42  // StatusREST implements a RESTStorage for ValidatingAdmissionPolicyStatus
    43  type StatusREST struct {
    44  	// DO NOT embed Store, manually select function to export.
    45  	store *genericregistry.Store
    46  }
    47  
    48  var groupResource = admissionregistration.Resource("validatingadmissionpolicies")
    49  
    50  // NewREST returns two RESTStorage objects that will work against validatingAdmissionPolicy and its status.
    51  func NewREST(optsGetter generic.RESTOptionsGetter, authorizer authorizer.Authorizer, resourceResolver resolver.ResourceResolver) (*REST, *StatusREST, error) {
    52  	r := &REST{}
    53  	strategy := validatingadmissionpolicy.NewStrategy(authorizer, resourceResolver)
    54  	store := &genericregistry.Store{
    55  		NewFunc:     func() runtime.Object { return &admissionregistration.ValidatingAdmissionPolicy{} },
    56  		NewListFunc: func() runtime.Object { return &admissionregistration.ValidatingAdmissionPolicyList{} },
    57  		ObjectNameFunc: func(obj runtime.Object) (string, error) {
    58  			return obj.(*admissionregistration.ValidatingAdmissionPolicy).Name, nil
    59  		},
    60  		DefaultQualifiedResource:  groupResource,
    61  		SingularQualifiedResource: admissionregistration.Resource("validatingadmissionpolicy"),
    62  
    63  		CreateStrategy:      strategy,
    64  		UpdateStrategy:      strategy,
    65  		DeleteStrategy:      strategy,
    66  		ResetFieldsStrategy: strategy,
    67  
    68  		TableConvertor: printerstorage.TableConvertor{TableGenerator: printers.NewTableGenerator().With(printersinternal.AddHandlers)},
    69  	}
    70  	options := &generic.StoreOptions{RESTOptions: optsGetter}
    71  	if err := store.CompleteWithOptions(options); err != nil {
    72  		return nil, nil, err
    73  	}
    74  	r.Store = store
    75  	statusStrategy := validatingadmissionpolicy.NewStatusStrategy(strategy)
    76  	statusStore := *store
    77  	statusStore.UpdateStrategy = statusStrategy
    78  	statusStore.ResetFieldsStrategy = statusStrategy
    79  	sr := &StatusREST{store: &statusStore}
    80  	return r, sr, nil
    81  }
    82  
    83  // Implement CategoriesProvider
    84  var _ rest.CategoriesProvider = &REST{}
    85  
    86  // Categories implements the CategoriesProvider interface. Returns a list of categories a resource is part of.
    87  func (r *REST) Categories() []string {
    88  	return []string{"api-extensions"}
    89  }
    90  
    91  // New generates a new ValidatingAdmissionPolicy object
    92  func (r *StatusREST) New() runtime.Object {
    93  	return &admissionregistration.ValidatingAdmissionPolicy{}
    94  }
    95  
    96  // Destroy disposes the store object. For the StatusREST, this is a no-op.
    97  func (r *StatusREST) Destroy() {
    98  	// Given that underlying store is shared with REST,
    99  	// we don't destroy it here explicitly.
   100  }
   101  
   102  // Get retrieves the object from the storage. It is required to support Patch.
   103  func (r *StatusREST) Get(ctx context.Context, name string, options *metav1.GetOptions) (runtime.Object, error) {
   104  	return r.store.Get(ctx, name, options)
   105  }
   106  
   107  // GetResetFields returns the fields that got reset by the REST endpoint
   108  func (r *StatusREST) GetResetFields() map[fieldpath.APIVersion]*fieldpath.Set {
   109  	return r.store.GetResetFields()
   110  }
   111  
   112  // ConvertToTable delegates to the store, implements TableConverter
   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  }
   116  
   117  // Update alters the status subset of an object. Delegates to the store
   118  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) {
   119  	return r.store.Update(ctx, name, objInfo, createValidation, updateValidation, forceAllowCreate, options)
   120  }