Skip to content

Commit 5a5ed81

Browse files
jpbetzthockinaaron-prindleyongruilindeads2k
committed
ReplicationController: Enable declarative validation
After declarative validation is enabled in the ReplicationController strategy in this way, the generated declarative validation code in pkg/apis/core/v1/zz.generated.validations.go will be run when the strategy validates ReplicationController. Co-authored-by: Tim Hockin <[email protected]> Co-authored-by: Aaron Prindle <[email protected]> Co-authored-by: Yongrui Lin <[email protected]> Co-authored-by: David Eads <[email protected]>
1 parent e856356 commit 5a5ed81

File tree

1 file changed

+42
-1
lines changed

1 file changed

+42
-1
lines changed

pkg/registry/core/replicationcontroller/strategy.go

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,13 @@ import (
3737
"k8s.io/apiserver/pkg/registry/rest"
3838
apistorage "k8s.io/apiserver/pkg/storage"
3939
"k8s.io/apiserver/pkg/storage/names"
40+
utilfeature "k8s.io/apiserver/pkg/util/feature"
4041
"k8s.io/kubernetes/pkg/api/legacyscheme"
4142
"k8s.io/kubernetes/pkg/api/pod"
4243
api "k8s.io/kubernetes/pkg/apis/core"
4344
"k8s.io/kubernetes/pkg/apis/core/helper"
4445
corevalidation "k8s.io/kubernetes/pkg/apis/core/validation"
46+
"k8s.io/kubernetes/pkg/features"
4547
"sigs.k8s.io/structured-merge-diff/v4/fieldpath"
4648
)
4749

@@ -123,7 +125,27 @@ func (rcStrategy) PrepareForUpdate(ctx context.Context, obj, old runtime.Object)
123125
func (rcStrategy) Validate(ctx context.Context, obj runtime.Object) field.ErrorList {
124126
controller := obj.(*api.ReplicationController)
125127
opts := pod.GetValidationOptionsFromPodTemplate(controller.Spec.Template, nil)
126-
return corevalidation.ValidateReplicationController(controller, opts)
128+
129+
// Run imperative validation
130+
allErrs := corevalidation.ValidateReplicationController(controller, opts)
131+
132+
// If DeclarativeValidation feature gate is enabled, also run declarative validation
133+
if utilfeature.DefaultFeatureGate.Enabled(features.DeclarativeValidation) {
134+
// Determine if takeover is enabled
135+
takeover := utilfeature.DefaultFeatureGate.Enabled(features.DeclarativeValidationTakeover)
136+
137+
// Run declarative validation with panic recovery
138+
declarativeErrs := rest.ValidateDeclarativelyWithRecovery(ctx, nil, legacyscheme.Scheme, controller, takeover)
139+
140+
// Compare imperative and declarative errors and log + emit metric if there's a mismatch
141+
rest.CompareDeclarativeErrorsAndEmitMismatches(ctx, allErrs, declarativeErrs, takeover)
142+
143+
// Only apply declarative errors if takeover is enabled
144+
if takeover {
145+
allErrs = append(allErrs.RemoveCoveredByDeclarative(), declarativeErrs...)
146+
}
147+
}
148+
return allErrs
127149
}
128150

129151
// WarningsOnCreate returns warnings for the creation of the given object.
@@ -153,6 +175,8 @@ func (rcStrategy) ValidateUpdate(ctx context.Context, obj, old runtime.Object) f
153175
newRc := obj.(*api.ReplicationController)
154176

155177
opts := pod.GetValidationOptionsFromPodTemplate(newRc.Spec.Template, oldRc.Spec.Template)
178+
// FIXME: Calling both validator functions here results in redundant calls to ValidateReplicationControllerSpec.
179+
// This should be fixed to avoid the redundant calls, but carefully.
156180
validationErrorList := corevalidation.ValidateReplicationController(newRc, opts)
157181
updateErrorList := corevalidation.ValidateReplicationControllerUpdate(newRc, oldRc, opts)
158182
errs := append(validationErrorList, updateErrorList...)
@@ -174,6 +198,23 @@ func (rcStrategy) ValidateUpdate(ctx context.Context, obj, old runtime.Object) f
174198
}
175199
}
176200

201+
// If DeclarativeValidation feature gate is enabled, also run declarative validation
202+
if utilfeature.DefaultFeatureGate.Enabled(features.DeclarativeValidation) {
203+
// Determine if takeover is enabled
204+
takeover := utilfeature.DefaultFeatureGate.Enabled(features.DeclarativeValidationTakeover)
205+
206+
// Run declarative update validation with panic recovery
207+
declarativeErrs := rest.ValidateUpdateDeclarativelyWithRecovery(ctx, nil, legacyscheme.Scheme, newRc, oldRc, takeover)
208+
209+
// Compare imperative and declarative errors and emit metric if there's a mismatch
210+
rest.CompareDeclarativeErrorsAndEmitMismatches(ctx, errs, declarativeErrs, takeover)
211+
212+
// Only apply declarative errors if takeover is enabled
213+
if takeover {
214+
errs = append(errs.RemoveCoveredByDeclarative(), declarativeErrs...)
215+
}
216+
}
217+
177218
return errs
178219
}
179220

0 commit comments

Comments
 (0)