k8s.io/kubernetes@v1.29.3/pkg/registry/core/limitrange/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 limitrange 18 19 import ( 20 "context" 21 22 "k8s.io/apimachinery/pkg/runtime" 23 "k8s.io/apimachinery/pkg/util/uuid" 24 "k8s.io/apimachinery/pkg/util/validation/field" 25 "k8s.io/apiserver/pkg/storage/names" 26 "k8s.io/kubernetes/pkg/api/legacyscheme" 27 api "k8s.io/kubernetes/pkg/apis/core" 28 "k8s.io/kubernetes/pkg/apis/core/validation" 29 ) 30 31 type limitrangeStrategy struct { 32 runtime.ObjectTyper 33 names.NameGenerator 34 } 35 36 // Strategy is the default logic that applies when creating and updating 37 // LimitRange objects via the REST API. 38 var Strategy = limitrangeStrategy{legacyscheme.Scheme, names.SimpleNameGenerator} 39 40 func (limitrangeStrategy) NamespaceScoped() bool { 41 return true 42 } 43 44 func (limitrangeStrategy) PrepareForCreate(ctx context.Context, obj runtime.Object) { 45 limitRange := obj.(*api.LimitRange) 46 if len(limitRange.Name) == 0 { 47 limitRange.Name = string(uuid.NewUUID()) 48 } 49 } 50 51 func (limitrangeStrategy) PrepareForUpdate(ctx context.Context, obj, old runtime.Object) { 52 } 53 54 func (limitrangeStrategy) Validate(ctx context.Context, obj runtime.Object) field.ErrorList { 55 limitRange := obj.(*api.LimitRange) 56 return validation.ValidateLimitRange(limitRange) 57 } 58 59 // WarningsOnCreate returns warnings for the creation of the given object. 60 func (limitrangeStrategy) WarningsOnCreate(ctx context.Context, obj runtime.Object) []string { 61 return nil 62 } 63 64 // Canonicalize normalizes the object after validation. 65 func (limitrangeStrategy) Canonicalize(obj runtime.Object) { 66 } 67 68 func (limitrangeStrategy) AllowCreateOnUpdate() bool { 69 return true 70 } 71 72 func (limitrangeStrategy) ValidateUpdate(ctx context.Context, obj, old runtime.Object) field.ErrorList { 73 limitRange := obj.(*api.LimitRange) 74 return validation.ValidateLimitRange(limitRange) 75 } 76 77 // WarningsOnUpdate returns warnings for the given update. 78 func (limitrangeStrategy) WarningsOnUpdate(ctx context.Context, obj, old runtime.Object) []string { 79 return nil 80 } 81 82 func (limitrangeStrategy) AllowUnconditionalUpdate() bool { 83 return true 84 }