k8s.io/kubernetes@v1.31.0-alpha.0.0.20240520171757-56147500dadc/pkg/registry/core/configmap/strategy.go (about) 1 /* 2 Copyright 2015 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 configmap 18 19 import ( 20 "context" 21 "fmt" 22 23 "k8s.io/apimachinery/pkg/fields" 24 "k8s.io/apimachinery/pkg/labels" 25 "k8s.io/apimachinery/pkg/runtime" 26 "k8s.io/apimachinery/pkg/util/validation/field" 27 "k8s.io/apiserver/pkg/registry/generic" 28 "k8s.io/apiserver/pkg/registry/rest" 29 pkgstorage "k8s.io/apiserver/pkg/storage" 30 "k8s.io/apiserver/pkg/storage/names" 31 "k8s.io/kubernetes/pkg/api/legacyscheme" 32 api "k8s.io/kubernetes/pkg/apis/core" 33 "k8s.io/kubernetes/pkg/apis/core/validation" 34 ) 35 36 // strategy implements behavior for ConfigMap objects 37 type strategy struct { 38 runtime.ObjectTyper 39 names.NameGenerator 40 } 41 42 // Strategy is the default logic that applies when creating and updating ConfigMap 43 // objects via the REST API. 44 var Strategy = strategy{legacyscheme.Scheme, names.SimpleNameGenerator} 45 46 // Strategy should implement rest.RESTCreateStrategy 47 var _ rest.RESTCreateStrategy = Strategy 48 49 // Strategy should implement rest.RESTUpdateStrategy 50 var _ rest.RESTUpdateStrategy = Strategy 51 52 func (strategy) NamespaceScoped() bool { 53 return true 54 } 55 56 func (strategy) PrepareForCreate(ctx context.Context, obj runtime.Object) { 57 configMap := obj.(*api.ConfigMap) 58 dropDisabledFields(configMap, nil) 59 } 60 61 func (strategy) Validate(ctx context.Context, obj runtime.Object) field.ErrorList { 62 cfg := obj.(*api.ConfigMap) 63 64 return validation.ValidateConfigMap(cfg) 65 } 66 67 // WarningsOnCreate returns warnings for the creation of the given object. 68 func (strategy) WarningsOnCreate(ctx context.Context, obj runtime.Object) []string { return nil } 69 70 // Canonicalize normalizes the object after validation. 71 func (strategy) Canonicalize(obj runtime.Object) { 72 } 73 74 func (strategy) AllowCreateOnUpdate() bool { 75 return false 76 } 77 78 func (strategy) PrepareForUpdate(ctx context.Context, newObj, oldObj runtime.Object) { 79 oldConfigMap := oldObj.(*api.ConfigMap) 80 newConfigMap := newObj.(*api.ConfigMap) 81 dropDisabledFields(newConfigMap, oldConfigMap) 82 } 83 84 func (strategy) ValidateUpdate(ctx context.Context, newObj, oldObj runtime.Object) field.ErrorList { 85 oldCfg, newCfg := oldObj.(*api.ConfigMap), newObj.(*api.ConfigMap) 86 87 return validation.ValidateConfigMapUpdate(newCfg, oldCfg) 88 } 89 90 // WarningsOnUpdate returns warnings for the given update. 91 func (strategy) WarningsOnUpdate(ctx context.Context, obj, old runtime.Object) []string { return nil } 92 93 func dropDisabledFields(configMap *api.ConfigMap, oldConfigMap *api.ConfigMap) { 94 } 95 96 func (strategy) AllowUnconditionalUpdate() bool { 97 return true 98 } 99 100 // GetAttrs returns labels and fields of a given object for filtering purposes. 101 func GetAttrs(obj runtime.Object) (labels.Set, fields.Set, error) { 102 configMap, ok := obj.(*api.ConfigMap) 103 if !ok { 104 return nil, nil, fmt.Errorf("not a configmap") 105 } 106 return labels.Set(configMap.Labels), SelectableFields(configMap), nil 107 } 108 109 // Matcher returns a selection predicate for a given label and field selector. 110 func Matcher(label labels.Selector, field fields.Selector) pkgstorage.SelectionPredicate { 111 return pkgstorage.SelectionPredicate{ 112 Label: label, 113 Field: field, 114 GetAttrs: GetAttrs, 115 } 116 } 117 118 // SelectableFields returns a field set that can be used for filter selection 119 func SelectableFields(obj *api.ConfigMap) fields.Set { 120 return generic.ObjectMetaFieldsSet(&obj.ObjectMeta, true) 121 }