k8s.io/kubernetes@v1.29.3/pkg/registry/rbac/role/strategy.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 role 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/registry/rest" 25 "k8s.io/apiserver/pkg/storage/names" 26 "k8s.io/kubernetes/pkg/api/legacyscheme" 27 "k8s.io/kubernetes/pkg/apis/rbac" 28 "k8s.io/kubernetes/pkg/apis/rbac/validation" 29 ) 30 31 // strategy implements behavior for Roles 32 type strategy struct { 33 runtime.ObjectTyper 34 names.NameGenerator 35 } 36 37 // Strategy is the default logic that applies when creating and updating 38 // Role objects. 39 var Strategy = strategy{legacyscheme.Scheme, names.SimpleNameGenerator} 40 41 // Strategy should implement rest.RESTCreateStrategy 42 var _ rest.RESTCreateStrategy = Strategy 43 44 // Strategy should implement rest.RESTUpdateStrategy 45 var _ rest.RESTUpdateStrategy = Strategy 46 47 // NamespaceScoped is true for Roles. 48 func (strategy) NamespaceScoped() bool { 49 return true 50 } 51 52 // AllowCreateOnUpdate is true for Roles. 53 func (strategy) AllowCreateOnUpdate() bool { 54 return true 55 } 56 57 // PrepareForCreate clears fields that are not allowed to be set by end users 58 // on creation. 59 func (strategy) PrepareForCreate(ctx context.Context, obj runtime.Object) { 60 _ = obj.(*rbac.Role) 61 } 62 63 // PrepareForUpdate clears fields that are not allowed to be set by end users on update. 64 func (strategy) PrepareForUpdate(ctx context.Context, obj, old runtime.Object) { 65 newRole := obj.(*rbac.Role) 66 oldRole := old.(*rbac.Role) 67 68 _, _ = newRole, oldRole 69 } 70 71 // Validate validates a new Role. Validation must check for a correct signature. 72 func (strategy) Validate(ctx context.Context, obj runtime.Object) field.ErrorList { 73 role := obj.(*rbac.Role) 74 return validation.ValidateRole(role) 75 } 76 77 // WarningsOnCreate returns warnings for the creation of the given object. 78 func (strategy) WarningsOnCreate(ctx context.Context, obj runtime.Object) []string { return nil } 79 80 // Canonicalize normalizes the object after validation. 81 func (strategy) Canonicalize(obj runtime.Object) { 82 _ = obj.(*rbac.Role) 83 } 84 85 // ValidateUpdate is the default update validation for an end user. 86 func (strategy) ValidateUpdate(ctx context.Context, obj, old runtime.Object) field.ErrorList { 87 newObj := obj.(*rbac.Role) 88 errorList := validation.ValidateRole(newObj) 89 return append(errorList, validation.ValidateRoleUpdate(newObj, old.(*rbac.Role))...) 90 } 91 92 // WarningsOnUpdate returns warnings for the given update. 93 func (strategy) WarningsOnUpdate(ctx context.Context, obj, old runtime.Object) []string { 94 return nil 95 } 96 97 // If AllowUnconditionalUpdate() is true and the object specified by 98 // the user does not have a resource version, then generic Update() 99 // populates it with the latest version. Else, it checks that the 100 // version specified by the user matches the version of latest etcd 101 // object. 102 func (strategy) AllowUnconditionalUpdate() bool { 103 return true 104 }