k8s.io/kubernetes@v1.29.3/pkg/registry/networking/servicecidr/strategy.go (about) 1 /* 2 Copyright 2023 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 servicecidr 18 19 import ( 20 "context" 21 22 "k8s.io/apimachinery/pkg/runtime" 23 "k8s.io/apimachinery/pkg/util/validation/field" 24 "k8s.io/apiserver/pkg/registry/rest" 25 "k8s.io/apiserver/pkg/storage/names" 26 "k8s.io/kubernetes/pkg/api/legacyscheme" 27 "k8s.io/kubernetes/pkg/apis/networking" 28 "k8s.io/kubernetes/pkg/apis/networking/validation" 29 "sigs.k8s.io/structured-merge-diff/v4/fieldpath" 30 ) 31 32 // serviceCIDRStrategy implements verification logic for ServiceCIDR allocators. 33 type serviceCIDRStrategy struct { 34 runtime.ObjectTyper 35 names.NameGenerator 36 } 37 38 // Strategy is the default logic that applies when creating and updating Replication ServiceCIDR objects. 39 var Strategy = serviceCIDRStrategy{legacyscheme.Scheme, names.SimpleNameGenerator} 40 41 // Strategy should implement rest.RESTCreateStrategy 42 var _ rest.RESTCreateStrategy = Strategy 43 44 // Strategy should implement rest.RESTUpdateStrategy 45 var _ rest.RESTUpdateStrategy = Strategy 46 47 // NamespaceScoped returns false because all ServiceCIDRes is cluster scoped. 48 func (serviceCIDRStrategy) NamespaceScoped() bool { 49 return false 50 } 51 52 // GetResetFields returns the set of fields that get reset by the strategy 53 // and should not be modified by the user. 54 func (serviceCIDRStrategy) GetResetFields() map[fieldpath.APIVersion]*fieldpath.Set { 55 fields := map[fieldpath.APIVersion]*fieldpath.Set{ 56 "networking/v1alpha1": fieldpath.NewSet( 57 fieldpath.MakePathOrDie("status"), 58 ), 59 } 60 return fields 61 } 62 63 // PrepareForCreate clears the status of an ServiceCIDR before creation. 64 func (serviceCIDRStrategy) PrepareForCreate(ctx context.Context, obj runtime.Object) { 65 _ = obj.(*networking.ServiceCIDR) 66 67 } 68 69 // PrepareForUpdate clears fields that are not allowed to be set by end users on update. 70 func (serviceCIDRStrategy) PrepareForUpdate(ctx context.Context, obj, old runtime.Object) { 71 newServiceCIDR := obj.(*networking.ServiceCIDR) 72 oldServiceCIDR := old.(*networking.ServiceCIDR) 73 74 _, _ = newServiceCIDR, oldServiceCIDR 75 } 76 77 // Validate validates a new ServiceCIDR. 78 func (serviceCIDRStrategy) Validate(ctx context.Context, obj runtime.Object) field.ErrorList { 79 cidrConfig := obj.(*networking.ServiceCIDR) 80 err := validation.ValidateServiceCIDR(cidrConfig) 81 return err 82 } 83 84 // Canonicalize normalizes the object after validation. 85 func (serviceCIDRStrategy) Canonicalize(obj runtime.Object) { 86 } 87 88 // AllowCreateOnUpdate is false for ServiceCIDR; this means POST is needed to create one. 89 func (serviceCIDRStrategy) AllowCreateOnUpdate() bool { 90 return false 91 } 92 93 // WarningsOnCreate returns warnings for the creation of the given object. 94 func (serviceCIDRStrategy) WarningsOnCreate(ctx context.Context, obj runtime.Object) []string { 95 return nil 96 } 97 98 // ValidateUpdate is the default update validation for an end user. 99 func (serviceCIDRStrategy) ValidateUpdate(ctx context.Context, new, old runtime.Object) field.ErrorList { 100 newServiceCIDR := new.(*networking.ServiceCIDR) 101 oldServiceCIDR := old.(*networking.ServiceCIDR) 102 errList := validation.ValidateServiceCIDR(newServiceCIDR) 103 errList = append(errList, validation.ValidateServiceCIDRUpdate(newServiceCIDR, oldServiceCIDR)...) 104 return errList 105 } 106 107 // AllowUnconditionalUpdate is the default update policy for ServiceCIDR objects. 108 func (serviceCIDRStrategy) AllowUnconditionalUpdate() bool { 109 return true 110 } 111 112 // WarningsOnUpdate returns warnings for the given update. 113 func (serviceCIDRStrategy) WarningsOnUpdate(ctx context.Context, obj, old runtime.Object) []string { 114 return nil 115 } 116 117 type serviceCIDRStatusStrategy struct { 118 serviceCIDRStrategy 119 } 120 121 // StatusStrategy implements logic used to validate and prepare for updates of the status subresource 122 var StatusStrategy = serviceCIDRStatusStrategy{Strategy} 123 124 // GetResetFields returns the set of fields that get reset by the strategy 125 // and should not be modified by the user. 126 func (serviceCIDRStatusStrategy) GetResetFields() map[fieldpath.APIVersion]*fieldpath.Set { 127 fields := map[fieldpath.APIVersion]*fieldpath.Set{ 128 "networking/v1alpha1": fieldpath.NewSet( 129 fieldpath.MakePathOrDie("spec"), 130 ), 131 } 132 return fields 133 } 134 135 // PrepareForUpdate clears fields that are not allowed to be set by end users on update of status 136 func (serviceCIDRStatusStrategy) PrepareForUpdate(ctx context.Context, obj, old runtime.Object) { 137 newServiceCIDR := obj.(*networking.ServiceCIDR) 138 oldServiceCIDR := old.(*networking.ServiceCIDR) 139 // status changes are not allowed to update spec 140 newServiceCIDR.Spec = oldServiceCIDR.Spec 141 } 142 143 // ValidateUpdate is the default update validation for an end user updating status 144 func (serviceCIDRStatusStrategy) ValidateUpdate(ctx context.Context, obj, old runtime.Object) field.ErrorList { 145 return validation.ValidateServiceCIDRStatusUpdate(obj.(*networking.ServiceCIDR), old.(*networking.ServiceCIDR)) 146 } 147 148 // WarningsOnUpdate returns warnings for the given update. 149 func (serviceCIDRStatusStrategy) WarningsOnUpdate(ctx context.Context, obj, old runtime.Object) []string { 150 return nil 151 }