k8s.io/kubernetes@v1.29.3/pkg/registry/apiserverinternal/storageversion/strategy.go (about) 1 /* 2 Copyright 2020 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 storageversion 18 19 import ( 20 "context" 21 22 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" 23 "k8s.io/apimachinery/pkg/runtime" 24 "k8s.io/apimachinery/pkg/util/validation/field" 25 "k8s.io/apiserver/pkg/storage/names" 26 "k8s.io/kubernetes/pkg/api/legacyscheme" 27 "k8s.io/kubernetes/pkg/apis/apiserverinternal" 28 "k8s.io/kubernetes/pkg/apis/apiserverinternal/validation" 29 "sigs.k8s.io/structured-merge-diff/v4/fieldpath" 30 ) 31 32 // storageVersionStrategy implements verification logic for StorageVersion. 33 type storageVersionStrategy struct { 34 runtime.ObjectTyper 35 names.NameGenerator 36 } 37 38 // Strategy is the default logic that applies when creating and updating StorageVersion objects. 39 var Strategy = storageVersionStrategy{legacyscheme.Scheme, names.SimpleNameGenerator} 40 41 // NamespaceScoped returns false because all StorageVersion's need to be cluster scoped 42 func (storageVersionStrategy) NamespaceScoped() bool { 43 return false 44 } 45 46 // GetResetFields returns the set of fields that get reset by the strategy 47 // and should not be modified by the user. 48 func (storageVersionStrategy) GetResetFields() map[fieldpath.APIVersion]*fieldpath.Set { 49 fields := map[fieldpath.APIVersion]*fieldpath.Set{ 50 "internal.apiserver.k8s.io/v1alpha1": fieldpath.NewSet( 51 fieldpath.MakePathOrDie("status"), 52 ), 53 } 54 55 return fields 56 } 57 58 // PrepareForCreate clears the status of an StorageVersion before creation. 59 func (storageVersionStrategy) PrepareForCreate(ctx context.Context, obj runtime.Object) { 60 sv := obj.(*apiserverinternal.StorageVersion) 61 sv.Status = apiserverinternal.StorageVersionStatus{} 62 } 63 64 // PrepareForUpdate clears fields that are not allowed to be set by end users on update. 65 func (storageVersionStrategy) PrepareForUpdate(ctx context.Context, obj, old runtime.Object) { 66 sv := obj.(*apiserverinternal.StorageVersion) 67 sv.Status = old.(*apiserverinternal.StorageVersion).Status 68 } 69 70 // Validate validates a new storageVersion. 71 func (storageVersionStrategy) Validate(ctx context.Context, obj runtime.Object) field.ErrorList { 72 sv := obj.(*apiserverinternal.StorageVersion) 73 return validation.ValidateStorageVersion(sv) 74 } 75 76 // WarningsOnCreate returns warnings for the creation of the given object. 77 func (storageVersionStrategy) WarningsOnCreate(ctx context.Context, obj runtime.Object) []string { 78 return nil 79 } 80 81 // Canonicalize normalizes the object after validation. 82 func (storageVersionStrategy) Canonicalize(obj runtime.Object) { 83 } 84 85 // Does not allow creating a StorageVersion object with a PUT request. 86 func (storageVersionStrategy) AllowCreateOnUpdate() bool { 87 return false 88 } 89 90 // ValidateUpdate is the default update validation for an end user. 91 func (storageVersionStrategy) ValidateUpdate(ctx context.Context, obj, old runtime.Object) field.ErrorList { 92 newStorageVersion := obj.(*apiserverinternal.StorageVersion) 93 oldStorageVersion := old.(*apiserverinternal.StorageVersion) 94 validationErrorList := validation.ValidateStorageVersionUpdate(newStorageVersion, oldStorageVersion) 95 return validationErrorList 96 } 97 98 // WarningsOnUpdate returns warnings for the given update. 99 func (storageVersionStrategy) WarningsOnUpdate(ctx context.Context, obj, old runtime.Object) []string { 100 return nil 101 } 102 103 // AllowUnconditionalUpdate is the default update policy for storageVersion objects. Status update should 104 // only be allowed if version match. 105 func (storageVersionStrategy) AllowUnconditionalUpdate() bool { 106 return false 107 } 108 109 type storageVersionStatusStrategy struct { 110 storageVersionStrategy 111 } 112 113 // StatusStrategy is the default logic invoked when updating object status. 114 var StatusStrategy = storageVersionStatusStrategy{Strategy} 115 116 // GetResetFields returns the set of fields that get reset by the strategy 117 // and should not be modified by the user. 118 func (storageVersionStatusStrategy) GetResetFields() map[fieldpath.APIVersion]*fieldpath.Set { 119 fields := map[fieldpath.APIVersion]*fieldpath.Set{ 120 "internal.apiserver.k8s.io/v1alpha1": fieldpath.NewSet( 121 fieldpath.MakePathOrDie("metadata"), 122 fieldpath.MakePathOrDie("spec"), 123 ), 124 } 125 126 return fields 127 } 128 129 func (storageVersionStatusStrategy) PrepareForUpdate(ctx context.Context, obj, old runtime.Object) { 130 newSV := obj.(*apiserverinternal.StorageVersion) 131 oldSV := old.(*apiserverinternal.StorageVersion) 132 133 newSV.Spec = oldSV.Spec 134 metav1.ResetObjectMetaForStatus(&newSV.ObjectMeta, &oldSV.ObjectMeta) 135 } 136 137 func (storageVersionStatusStrategy) ValidateUpdate(ctx context.Context, obj, old runtime.Object) field.ErrorList { 138 return validation.ValidateStorageVersionStatusUpdate(obj.(*apiserverinternal.StorageVersion), old.(*apiserverinternal.StorageVersion)) 139 } 140 141 // WarningsOnUpdate returns warnings for the given update. 142 func (storageVersionStatusStrategy) WarningsOnUpdate(ctx context.Context, obj, old runtime.Object) []string { 143 return nil 144 }