Skip to content

Commit 8081d1b

Browse files
committed
Add warning for setting enableCommonBootImageImport
setting or changing the enableCommonBootImageImport FG triggers warning from the validation webhook. Signed-off-by: Nahshon Unna-Tsameret <nunnatsa@redhat.com>
1 parent 0c05825 commit 8081d1b

File tree

2 files changed

+86
-8
lines changed

2 files changed

+86
-8
lines changed

pkg/webhooks/validator/validator.go

+46-6
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ func (wh *WebhookHandler) ValidateCreate(_ context.Context, dryrun bool, hc *v1b
154154
return err
155155
}
156156

157-
if err := wh.validateFeatureGates(hc); err != nil {
157+
if err := wh.validateFeatureGatesOnCreate(hc); err != nil {
158158
return err
159159
}
160160

@@ -221,7 +221,7 @@ func (wh *WebhookHandler) ValidateUpdate(ctx context.Context, dryrun bool, reque
221221
return err
222222
}
223223

224-
if err := wh.validateFeatureGates(requested); err != nil {
224+
if err := wh.validateFeatureGatesOnUpdate(requested, exists); err != nil {
225225
return err
226226
}
227227

@@ -433,7 +433,29 @@ const (
433433
fgDeprecationWarning = "spec.featureGates.%s is deprecated and ignored. It will be removed in a future version;"
434434
)
435435

436-
func (wh *WebhookHandler) validateFeatureGates(hc *v1beta1.HyperConverged) error {
436+
func (wh *WebhookHandler) validateFeatureGatesOnCreate(hc *v1beta1.HyperConverged) error {
437+
warnings := wh.validateDeprecatedFeatureGates(hc)
438+
warnings = validateOldFGOnCreate(warnings, hc)
439+
440+
if len(warnings) > 0 {
441+
return newValidationWarning(warnings)
442+
}
443+
444+
return nil
445+
}
446+
447+
func (wh *WebhookHandler) validateFeatureGatesOnUpdate(requested, exists *v1beta1.HyperConverged) error {
448+
warnings := wh.validateDeprecatedFeatureGates(requested)
449+
warnings = validateOldFGOnUpdate(warnings, requested, exists)
450+
451+
if len(warnings) > 0 {
452+
return newValidationWarning(warnings)
453+
}
454+
455+
return nil
456+
}
457+
458+
func (wh *WebhookHandler) validateDeprecatedFeatureGates(hc *v1beta1.HyperConverged) []string {
437459
var warnings []string
438460

439461
//nolint:staticcheck
@@ -456,11 +478,29 @@ func (wh *WebhookHandler) validateFeatureGates(hc *v1beta1.HyperConverged) error
456478
warnings = append(warnings, fmt.Sprintf(fgDeprecationWarning, "enableManagedTenantQuota"))
457479
}
458480

459-
if len(warnings) > 0 {
460-
return newValidationWarning(warnings)
481+
return warnings
482+
}
483+
484+
func validateOldFGOnCreate(warnings []string, hc *v1beta1.HyperConverged) []string {
485+
//nolint:staticcheck
486+
if hc.Spec.FeatureGates.EnableCommonBootImageImport != nil {
487+
warnings = append(warnings, fmt.Sprintf(fgMovedWarning, "enableCommonBootImageImport"))
461488
}
462489

463-
return nil
490+
return warnings
491+
}
492+
493+
func validateOldFGOnUpdate(warnings []string, hc, prevHC *v1beta1.HyperConverged) []string {
494+
//nolint:staticcheck
495+
if oldFGChanged(hc.Spec.FeatureGates.EnableCommonBootImageImport, prevHC.Spec.FeatureGates.EnableCommonBootImageImport) {
496+
warnings = append(warnings, fmt.Sprintf(fgMovedWarning, "enableCommonBootImageImport"))
497+
}
498+
499+
return warnings
500+
}
501+
502+
func oldFGChanged(newFG, prevFG *bool) bool {
503+
return newFG != nil && (prevFG == nil || *newFG != *prevFG)
464504
}
465505

466506
func hasRequiredHTTP2Ciphers(ciphers []string) bool {

pkg/webhooks/validator/validator_test.go

+40-2
Original file line numberDiff line numberDiff line change
@@ -514,7 +514,7 @@ var _ = Describe("webhooks validator", func() {
514514
NonRoot: ptr.To(false),
515515
EnableCommonBootImageImport: ptr.To(true),
516516
EnableManagedTenantQuota: ptr.To(false),
517-
}, "enableManagedTenantQuota", "nonRoot"),
517+
}, "enableManagedTenantQuota", "nonRoot", "enableCommonBootImageImport"),
518518
)
519519
})
520520
})
@@ -1181,9 +1181,47 @@ var _ = Describe("webhooks validator", func() {
11811181
NonRoot: ptr.To(false),
11821182
EnableCommonBootImageImport: ptr.To(true),
11831183
EnableManagedTenantQuota: ptr.To(false),
1184-
}, "enableManagedTenantQuota", "nonRoot"),
1184+
}, "enableManagedTenantQuota", "nonRoot", "enableCommonBootImageImport"),
1185+
)
1186+
})
1187+
1188+
Context("validate moved FG on update", func() {
1189+
DescribeTable("should return warning for enableCommonBootImageImport on update", func(newFG, oldFG *bool) {
1190+
newHCO := hco.DeepCopy()
1191+
hco.Spec.FeatureGates.EnableCommonBootImageImport = newFG
1192+
newHCO.Spec.FeatureGates.EnableCommonBootImageImport = oldFG
1193+
1194+
err := wh.ValidateUpdate(ctx, dryRun, newHCO, hco)
1195+
1196+
Expect(err).To(HaveOccurred())
1197+
expected := &ValidationWarning{}
1198+
Expect(errors.As(err, &expected)).To(BeTrue())
1199+
1200+
Expect(expected.warnings).To(HaveLen(1))
1201+
Expect(expected.warnings).To(ContainElements(ContainSubstring("enableCommonBootImageImport")))
1202+
},
1203+
Entry("should trigger warning if enableCommonBootImageImport appeared as true", nil, ptr.To(true)),
1204+
Entry("should trigger warning if enableCommonBootImageImport appeared as false", nil, ptr.To(false)),
1205+
Entry("should trigger warning if enableCommonBootImageImport has changed from true to false", ptr.To(true), ptr.To(false)),
1206+
Entry("should trigger warning if enableCommonBootImageImport has changed from false to true", ptr.To(false), ptr.To(true)),
1207+
)
1208+
1209+
DescribeTable("should not return warning for enableCommonBootImageImport if not change", func(newFG, oldFG *bool) {
1210+
cli := getFakeClient(hco)
1211+
wh := NewWebhookHandler(logger, cli, decoder, HcoValidNamespace, true, nil)
1212+
newHCO := hco.DeepCopy()
1213+
hco.Spec.FeatureGates.EnableCommonBootImageImport = newFG
1214+
newHCO.Spec.FeatureGates.EnableCommonBootImageImport = oldFG
1215+
1216+
Expect(wh.ValidateUpdate(ctx, dryRun, newHCO, hco)).To(Succeed())
1217+
},
1218+
Entry("should not trigger warning if enableCommonBootImageImport (true) disappeared", ptr.To(true), nil),
1219+
Entry("should not trigger warning if enableCommonBootImageImport (false) disappeared", ptr.To(false), nil),
1220+
Entry("should not trigger warning if enableCommonBootImageImport (true) wasn't changed", ptr.To(true), ptr.To(true)),
1221+
Entry("should not trigger warning if enableCommonBootImageImport (false) wasn't changed", ptr.To(false), ptr.To(false)),
11851222
)
11861223
})
1224+
11871225
})
11881226

11891227
Context("validate delete validation webhook", func() {

0 commit comments

Comments
 (0)