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