k8s.io/kubernetes@v1.31.0-alpha.0.0.20240520171757-56147500dadc/pkg/registry/apps/daemonset/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 daemonset 18 19 import ( 20 "context" 21 22 extensionsv1beta1 "k8s.io/api/extensions/v1beta1" 23 apiequality "k8s.io/apimachinery/pkg/api/equality" 24 apivalidation "k8s.io/apimachinery/pkg/api/validation" 25 metav1validation "k8s.io/apimachinery/pkg/apis/meta/v1/validation" 26 "k8s.io/apimachinery/pkg/runtime" 27 "k8s.io/apimachinery/pkg/runtime/schema" 28 "k8s.io/apimachinery/pkg/util/validation/field" 29 genericapirequest "k8s.io/apiserver/pkg/endpoints/request" 30 "k8s.io/apiserver/pkg/registry/rest" 31 "k8s.io/apiserver/pkg/storage/names" 32 "k8s.io/kubernetes/pkg/api/legacyscheme" 33 "k8s.io/kubernetes/pkg/api/pod" 34 "k8s.io/kubernetes/pkg/apis/apps" 35 "k8s.io/kubernetes/pkg/apis/apps/validation" 36 "sigs.k8s.io/structured-merge-diff/v4/fieldpath" 37 ) 38 39 // daemonSetStrategy implements verification logic for daemon sets. 40 type daemonSetStrategy struct { 41 runtime.ObjectTyper 42 names.NameGenerator 43 } 44 45 // Strategy is the default logic that applies when creating and updating DaemonSet objects. 46 var Strategy = daemonSetStrategy{legacyscheme.Scheme, names.SimpleNameGenerator} 47 48 // Make sure we correctly implement the interface. 49 var _ = rest.GarbageCollectionDeleteStrategy(Strategy) 50 51 // DefaultGarbageCollectionPolicy returns DeleteDependents for all currently served versions. 52 func (daemonSetStrategy) DefaultGarbageCollectionPolicy(ctx context.Context) rest.GarbageCollectionPolicy { 53 return rest.DeleteDependents 54 } 55 56 // NamespaceScoped returns true because all DaemonSets need to be within a namespace. 57 func (daemonSetStrategy) NamespaceScoped() bool { 58 return true 59 } 60 61 // GetResetFields returns the set of fields that get reset by the strategy 62 // and should not be modified by the user. 63 func (daemonSetStrategy) GetResetFields() map[fieldpath.APIVersion]*fieldpath.Set { 64 fields := map[fieldpath.APIVersion]*fieldpath.Set{ 65 "apps/v1": fieldpath.NewSet( 66 fieldpath.MakePathOrDie("status"), 67 ), 68 } 69 70 return fields 71 } 72 73 // PrepareForCreate clears the status of a daemon set before creation. 74 func (daemonSetStrategy) PrepareForCreate(ctx context.Context, obj runtime.Object) { 75 daemonSet := obj.(*apps.DaemonSet) 76 daemonSet.Status = apps.DaemonSetStatus{} 77 78 daemonSet.Generation = 1 79 if daemonSet.Spec.TemplateGeneration < 1 { 80 daemonSet.Spec.TemplateGeneration = 1 81 } 82 83 pod.DropDisabledTemplateFields(&daemonSet.Spec.Template, nil) 84 } 85 86 // PrepareForUpdate clears fields that are not allowed to be set by end users on update. 87 func (daemonSetStrategy) PrepareForUpdate(ctx context.Context, obj, old runtime.Object) { 88 newDaemonSet := obj.(*apps.DaemonSet) 89 oldDaemonSet := old.(*apps.DaemonSet) 90 91 pod.DropDisabledTemplateFields(&newDaemonSet.Spec.Template, &oldDaemonSet.Spec.Template) 92 93 // update is not allowed to set status 94 newDaemonSet.Status = oldDaemonSet.Status 95 96 // update is not allowed to set TemplateGeneration 97 newDaemonSet.Spec.TemplateGeneration = oldDaemonSet.Spec.TemplateGeneration 98 99 // Any changes to the spec increment the generation number, any changes to the 100 // status should reflect the generation number of the corresponding object. We push 101 // the burden of managing the status onto the clients because we can't (in general) 102 // know here what version of spec the writer of the status has seen. It may seem like 103 // we can at first -- since obj contains spec -- but in the future we will probably make 104 // status its own object, and even if we don't, writes may be the result of a 105 // read-update-write loop, so the contents of spec may not actually be the spec that 106 // the manager has *seen*. 107 // 108 // TODO: Any changes to a part of the object that represents desired state (labels, 109 // annotations etc) should also increment the generation. 110 if !apiequality.Semantic.DeepEqual(oldDaemonSet.Spec.Template, newDaemonSet.Spec.Template) { 111 newDaemonSet.Spec.TemplateGeneration = oldDaemonSet.Spec.TemplateGeneration + 1 112 newDaemonSet.Generation = oldDaemonSet.Generation + 1 113 return 114 } 115 if !apiequality.Semantic.DeepEqual(oldDaemonSet.Spec, newDaemonSet.Spec) { 116 newDaemonSet.Generation = oldDaemonSet.Generation + 1 117 } 118 } 119 120 // Validate validates a new daemon set. 121 func (daemonSetStrategy) Validate(ctx context.Context, obj runtime.Object) field.ErrorList { 122 daemonSet := obj.(*apps.DaemonSet) 123 opts := pod.GetValidationOptionsFromPodTemplate(&daemonSet.Spec.Template, nil) 124 return validation.ValidateDaemonSet(daemonSet, opts) 125 } 126 127 // WarningsOnCreate returns warnings for the creation of the given object. 128 func (daemonSetStrategy) WarningsOnCreate(ctx context.Context, obj runtime.Object) []string { 129 newDaemonSet := obj.(*apps.DaemonSet) 130 return pod.GetWarningsForPodTemplate(ctx, field.NewPath("spec", "template"), &newDaemonSet.Spec.Template, nil) 131 } 132 133 // Canonicalize normalizes the object after validation. 134 func (daemonSetStrategy) Canonicalize(obj runtime.Object) { 135 } 136 137 // AllowCreateOnUpdate is false for daemon set; this means a POST is 138 // needed to create one 139 func (daemonSetStrategy) AllowCreateOnUpdate() bool { 140 return false 141 } 142 143 // ValidateUpdate is the default update validation for an end user. 144 func (daemonSetStrategy) ValidateUpdate(ctx context.Context, obj, old runtime.Object) field.ErrorList { 145 newDaemonSet := obj.(*apps.DaemonSet) 146 oldDaemonSet := old.(*apps.DaemonSet) 147 148 opts := pod.GetValidationOptionsFromPodTemplate(&newDaemonSet.Spec.Template, &oldDaemonSet.Spec.Template) 149 opts.AllowInvalidLabelValueInSelector = opts.AllowInvalidLabelValueInSelector || metav1validation.LabelSelectorHasInvalidLabelValue(oldDaemonSet.Spec.Selector) 150 151 allErrs := validation.ValidateDaemonSet(obj.(*apps.DaemonSet), opts) 152 allErrs = append(allErrs, validation.ValidateDaemonSetUpdate(newDaemonSet, oldDaemonSet, opts)...) 153 154 // Update is not allowed to set Spec.Selector for apps/v1 and apps/v1beta2 (allowed for extensions/v1beta1). 155 // If RequestInfo is nil, it is better to revert to old behavior (i.e. allow update to set Spec.Selector) 156 // to prevent unintentionally breaking users who may rely on the old behavior. 157 // TODO(#50791): after extensions/v1beta1 is removed, move selector immutability check inside ValidateDaemonSetUpdate(). 158 if requestInfo, found := genericapirequest.RequestInfoFrom(ctx); found { 159 groupVersion := schema.GroupVersion{Group: requestInfo.APIGroup, Version: requestInfo.APIVersion} 160 switch groupVersion { 161 case extensionsv1beta1.SchemeGroupVersion: 162 // no-op for compatibility 163 default: 164 // disallow mutation of selector 165 allErrs = append(allErrs, apivalidation.ValidateImmutableField(newDaemonSet.Spec.Selector, oldDaemonSet.Spec.Selector, field.NewPath("spec").Child("selector"))...) 166 } 167 } 168 169 return allErrs 170 } 171 172 // WarningsOnUpdate returns warnings for the given update. 173 func (daemonSetStrategy) WarningsOnUpdate(ctx context.Context, obj, old runtime.Object) []string { 174 var warnings []string 175 newDaemonSet := obj.(*apps.DaemonSet) 176 oldDaemonSet := old.(*apps.DaemonSet) 177 if newDaemonSet.Spec.TemplateGeneration != oldDaemonSet.Spec.TemplateGeneration { 178 warnings = pod.GetWarningsForPodTemplate(ctx, field.NewPath("spec", "template"), &newDaemonSet.Spec.Template, &oldDaemonSet.Spec.Template) 179 } 180 return warnings 181 } 182 183 // AllowUnconditionalUpdate is the default update policy for daemon set objects. 184 func (daemonSetStrategy) AllowUnconditionalUpdate() bool { 185 return true 186 } 187 188 type daemonSetStatusStrategy struct { 189 daemonSetStrategy 190 } 191 192 // StatusStrategy is the default logic invoked when updating object status. 193 var StatusStrategy = daemonSetStatusStrategy{Strategy} 194 195 // GetResetFields returns the set of fields that get reset by the strategy 196 // and should not be modified by the user. 197 func (daemonSetStatusStrategy) GetResetFields() map[fieldpath.APIVersion]*fieldpath.Set { 198 return map[fieldpath.APIVersion]*fieldpath.Set{ 199 "apps/v1": fieldpath.NewSet( 200 fieldpath.MakePathOrDie("spec"), 201 ), 202 } 203 } 204 205 func (daemonSetStatusStrategy) PrepareForUpdate(ctx context.Context, obj, old runtime.Object) { 206 newDaemonSet := obj.(*apps.DaemonSet) 207 oldDaemonSet := old.(*apps.DaemonSet) 208 newDaemonSet.Spec = oldDaemonSet.Spec 209 } 210 211 func (daemonSetStatusStrategy) ValidateUpdate(ctx context.Context, obj, old runtime.Object) field.ErrorList { 212 return validation.ValidateDaemonSetStatusUpdate(obj.(*apps.DaemonSet), old.(*apps.DaemonSet)) 213 } 214 215 // WarningsOnUpdate returns warnings for the given update. 216 func (daemonSetStatusStrategy) WarningsOnUpdate(ctx context.Context, obj, old runtime.Object) []string { 217 return nil 218 }