k8s.io/kubernetes@v1.29.3/pkg/registry/resource/podschedulingcontext/strategy.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 podschedulingcontext 18 19 import ( 20 "context" 21 "errors" 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/storage" 29 "k8s.io/apiserver/pkg/storage/names" 30 "k8s.io/kubernetes/pkg/api/legacyscheme" 31 "k8s.io/kubernetes/pkg/apis/resource" 32 "k8s.io/kubernetes/pkg/apis/resource/validation" 33 "sigs.k8s.io/structured-merge-diff/v4/fieldpath" 34 ) 35 36 // podSchedulingStrategy implements behavior for PodSchedulingContext objects 37 type podSchedulingStrategy struct { 38 runtime.ObjectTyper 39 names.NameGenerator 40 } 41 42 // Strategy is the default logic that applies when creating and updating 43 // ResourceClaim objects via the REST API. 44 var Strategy = podSchedulingStrategy{legacyscheme.Scheme, names.SimpleNameGenerator} 45 46 func (podSchedulingStrategy) NamespaceScoped() bool { 47 return true 48 } 49 50 // GetResetFields returns the set of fields that get reset by the strategy and 51 // should not be modified by the user. For a new PodSchedulingContext that is the 52 // status. 53 func (podSchedulingStrategy) GetResetFields() map[fieldpath.APIVersion]*fieldpath.Set { 54 fields := map[fieldpath.APIVersion]*fieldpath.Set{ 55 "resource.k8s.io/v1alpha2": fieldpath.NewSet( 56 fieldpath.MakePathOrDie("status"), 57 ), 58 } 59 60 return fields 61 } 62 63 func (podSchedulingStrategy) PrepareForCreate(ctx context.Context, obj runtime.Object) { 64 scheduling := obj.(*resource.PodSchedulingContext) 65 // Status must not be set by user on create. 66 scheduling.Status = resource.PodSchedulingContextStatus{} 67 } 68 69 func (podSchedulingStrategy) Validate(ctx context.Context, obj runtime.Object) field.ErrorList { 70 scheduling := obj.(*resource.PodSchedulingContext) 71 return validation.ValidatePodSchedulingContexts(scheduling) 72 } 73 74 func (podSchedulingStrategy) WarningsOnCreate(ctx context.Context, obj runtime.Object) []string { 75 return nil 76 } 77 78 func (podSchedulingStrategy) Canonicalize(obj runtime.Object) { 79 } 80 81 func (podSchedulingStrategy) AllowCreateOnUpdate() bool { 82 return false 83 } 84 85 func (podSchedulingStrategy) PrepareForUpdate(ctx context.Context, obj, old runtime.Object) { 86 newScheduling := obj.(*resource.PodSchedulingContext) 87 oldScheduling := old.(*resource.PodSchedulingContext) 88 newScheduling.Status = oldScheduling.Status 89 } 90 91 func (podSchedulingStrategy) ValidateUpdate(ctx context.Context, obj, old runtime.Object) field.ErrorList { 92 newScheduling := obj.(*resource.PodSchedulingContext) 93 oldScheduling := old.(*resource.PodSchedulingContext) 94 errorList := validation.ValidatePodSchedulingContexts(newScheduling) 95 return append(errorList, validation.ValidatePodSchedulingContextUpdate(newScheduling, oldScheduling)...) 96 } 97 98 func (podSchedulingStrategy) WarningsOnUpdate(ctx context.Context, obj, old runtime.Object) []string { 99 return nil 100 } 101 102 func (podSchedulingStrategy) AllowUnconditionalUpdate() bool { 103 return true 104 } 105 106 type podSchedulingStatusStrategy struct { 107 podSchedulingStrategy 108 } 109 110 var StatusStrategy = podSchedulingStatusStrategy{Strategy} 111 112 // GetResetFields returns the set of fields that get reset by the strategy and 113 // should not be modified by the user. For a status update that is the spec. 114 func (podSchedulingStatusStrategy) GetResetFields() map[fieldpath.APIVersion]*fieldpath.Set { 115 fields := map[fieldpath.APIVersion]*fieldpath.Set{ 116 "resource.k8s.io/v1alpha2": fieldpath.NewSet( 117 fieldpath.MakePathOrDie("spec"), 118 ), 119 } 120 121 return fields 122 } 123 124 func (podSchedulingStatusStrategy) PrepareForUpdate(ctx context.Context, obj, old runtime.Object) { 125 newScheduling := obj.(*resource.PodSchedulingContext) 126 oldScheduling := old.(*resource.PodSchedulingContext) 127 newScheduling.Spec = oldScheduling.Spec 128 } 129 130 func (podSchedulingStatusStrategy) ValidateUpdate(ctx context.Context, obj, old runtime.Object) field.ErrorList { 131 newScheduling := obj.(*resource.PodSchedulingContext) 132 oldScheduling := old.(*resource.PodSchedulingContext) 133 return validation.ValidatePodSchedulingContextStatusUpdate(newScheduling, oldScheduling) 134 } 135 136 // WarningsOnUpdate returns warnings for the given update. 137 func (podSchedulingStatusStrategy) WarningsOnUpdate(ctx context.Context, obj, old runtime.Object) []string { 138 return nil 139 } 140 141 // Match returns a generic matcher for a given label and field selector. 142 func Match(label labels.Selector, field fields.Selector) storage.SelectionPredicate { 143 return storage.SelectionPredicate{ 144 Label: label, 145 Field: field, 146 GetAttrs: GetAttrs, 147 } 148 } 149 150 // GetAttrs returns labels and fields of a given object for filtering purposes. 151 func GetAttrs(obj runtime.Object) (labels.Set, fields.Set, error) { 152 scheduling, ok := obj.(*resource.PodSchedulingContext) 153 if !ok { 154 return nil, nil, errors.New("not a PodSchedulingContext") 155 } 156 return labels.Set(scheduling.Labels), toSelectableFields(scheduling), nil 157 } 158 159 // toSelectableFields returns a field set that represents the object 160 func toSelectableFields(scheduling *resource.PodSchedulingContext) fields.Set { 161 fields := generic.ObjectMetaFieldsSet(&scheduling.ObjectMeta, true) 162 return fields 163 }