k8s.io/kubernetes@v1.29.3/pkg/registry/core/resourcequota/strategy.go (about) 1 /* 2 Copyright 2014 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 resourcequota 18 19 import ( 20 "context" 21 22 "k8s.io/apimachinery/pkg/runtime" 23 "k8s.io/apimachinery/pkg/util/validation/field" 24 "k8s.io/apiserver/pkg/storage/names" 25 "k8s.io/kubernetes/pkg/api/legacyscheme" 26 api "k8s.io/kubernetes/pkg/apis/core" 27 "k8s.io/kubernetes/pkg/apis/core/validation" 28 "sigs.k8s.io/structured-merge-diff/v4/fieldpath" 29 ) 30 31 // resourcequotaStrategy implements behavior for ResourceQuota objects 32 type resourcequotaStrategy struct { 33 runtime.ObjectTyper 34 names.NameGenerator 35 } 36 37 // Strategy is the default logic that applies when creating and updating ResourceQuota 38 // objects via the REST API. 39 var Strategy = resourcequotaStrategy{legacyscheme.Scheme, names.SimpleNameGenerator} 40 41 // NamespaceScoped is true for resourcequotas. 42 func (resourcequotaStrategy) NamespaceScoped() bool { 43 return true 44 } 45 46 // GetResetFields returns the set of fields that get reset by the strategy 47 // and should not be modified by the user. 48 func (resourcequotaStrategy) GetResetFields() map[fieldpath.APIVersion]*fieldpath.Set { 49 fields := map[fieldpath.APIVersion]*fieldpath.Set{ 50 "v1": fieldpath.NewSet( 51 fieldpath.MakePathOrDie("status"), 52 ), 53 } 54 55 return fields 56 } 57 58 // PrepareForCreate clears fields that are not allowed to be set by end users on creation. 59 func (resourcequotaStrategy) PrepareForCreate(ctx context.Context, obj runtime.Object) { 60 resourcequota := obj.(*api.ResourceQuota) 61 resourcequota.Status = api.ResourceQuotaStatus{} 62 } 63 64 // PrepareForUpdate clears fields that are not allowed to be set by end users on update. 65 func (resourcequotaStrategy) PrepareForUpdate(ctx context.Context, obj, old runtime.Object) { 66 newResourcequota := obj.(*api.ResourceQuota) 67 oldResourcequota := old.(*api.ResourceQuota) 68 newResourcequota.Status = oldResourcequota.Status 69 } 70 71 // Validate validates a new resourcequota. 72 func (resourcequotaStrategy) Validate(ctx context.Context, obj runtime.Object) field.ErrorList { 73 resourcequota := obj.(*api.ResourceQuota) 74 return validation.ValidateResourceQuota(resourcequota) 75 } 76 77 // WarningsOnCreate returns warnings for the creation of the given object. 78 func (resourcequotaStrategy) WarningsOnCreate(ctx context.Context, obj runtime.Object) []string { 79 return nil 80 } 81 82 // Canonicalize normalizes the object after validation. 83 func (resourcequotaStrategy) Canonicalize(obj runtime.Object) { 84 } 85 86 // AllowCreateOnUpdate is false for resourcequotas. 87 func (resourcequotaStrategy) AllowCreateOnUpdate() bool { 88 return false 89 } 90 91 // ValidateUpdate is the default update validation for an end user. 92 func (resourcequotaStrategy) ValidateUpdate(ctx context.Context, obj, old runtime.Object) field.ErrorList { 93 newObj, oldObj := obj.(*api.ResourceQuota), old.(*api.ResourceQuota) 94 return validation.ValidateResourceQuotaUpdate(newObj, oldObj) 95 } 96 97 // WarningsOnUpdate returns warnings for the given update. 98 func (resourcequotaStrategy) WarningsOnUpdate(ctx context.Context, obj, old runtime.Object) []string { 99 return nil 100 } 101 102 func (resourcequotaStrategy) AllowUnconditionalUpdate() bool { 103 return true 104 } 105 106 type resourcequotaStatusStrategy struct { 107 resourcequotaStrategy 108 } 109 110 // StatusStrategy is the default logic invoked when updating object status. 111 var StatusStrategy = resourcequotaStatusStrategy{Strategy} 112 113 // GetResetFields returns the set of fields that get reset by the strategy 114 // and should not be modified by the user. 115 func (resourcequotaStatusStrategy) GetResetFields() map[fieldpath.APIVersion]*fieldpath.Set { 116 fields := map[fieldpath.APIVersion]*fieldpath.Set{ 117 "v1": fieldpath.NewSet( 118 fieldpath.MakePathOrDie("spec"), 119 ), 120 } 121 122 return fields 123 } 124 125 func (resourcequotaStatusStrategy) PrepareForUpdate(ctx context.Context, obj, old runtime.Object) { 126 newResourcequota := obj.(*api.ResourceQuota) 127 oldResourcequota := old.(*api.ResourceQuota) 128 newResourcequota.Spec = oldResourcequota.Spec 129 } 130 131 func (resourcequotaStatusStrategy) ValidateUpdate(ctx context.Context, obj, old runtime.Object) field.ErrorList { 132 return validation.ValidateResourceQuotaStatusUpdate(obj.(*api.ResourceQuota), old.(*api.ResourceQuota)) 133 } 134 135 // WarningsOnUpdate returns warnings for the given update. 136 func (resourcequotaStatusStrategy) WarningsOnUpdate(ctx context.Context, obj, old runtime.Object) []string { 137 return nil 138 }