Skip to content

Commit 70141d0

Browse files
committed
envtest: allow to ignore scheme.Convertible check for CRDs
Signed-off-by: Vince Prignano <[email protected]>
1 parent c033388 commit 70141d0

File tree

2 files changed

+21
-13
lines changed

2 files changed

+21
-13
lines changed

pkg/envtest/crd.go

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -371,17 +371,19 @@ func modifyConversionWebhooks(crds []*apiextensionsv1.CustomResourceDefinition,
371371
if crds[i].Spec.PreserveUnknownFields {
372372
continue
373373
}
374-
// Continue if the GroupKind isn't registered as being convertible,
375-
// and remove any existing conversion webhooks if they exist.
376-
// This is to prevent the CRD from being rejected by the apiserver, usually
377-
// manifests that are generated by controller-gen will have a conversion
378-
// webhook set, but we don't want to enable it if the type isn't registered.
379-
if _, ok := convertibles[schema.GroupKind{
380-
Group: crds[i].Spec.Group,
381-
Kind: crds[i].Spec.Names.Kind,
382-
}]; !ok {
383-
crds[i].Spec.Conversion = nil
384-
continue
374+
if !webhookOptions.IgnoreSchemeConvertible {
375+
// Continue if the GroupKind isn't registered as being convertible,
376+
// and remove any existing conversion webhooks if they exist.
377+
// This is to prevent the CRD from being rejected by the apiserver, usually
378+
// manifests that are generated by controller-gen will have a conversion
379+
// webhook set, but we don't want to enable it if the type isn't registered.
380+
if _, ok := convertibles[schema.GroupKind{
381+
Group: crds[i].Spec.Group,
382+
Kind: crds[i].Spec.Names.Kind,
383+
}]; !ok {
384+
crds[i].Spec.Conversion = nil
385+
continue
386+
}
385387
}
386388
if crds[i].Spec.Conversion == nil {
387389
crds[i].Spec.Conversion = &apiextensionsv1.CustomResourceConversion{

pkg/envtest/webhook.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,11 @@ type WebhookInstallOptions struct {
4949
// ValidatingWebhooks is a list of ValidatingWebhookConfigurations to install
5050
ValidatingWebhooks []*admissionv1.ValidatingWebhookConfiguration
5151

52+
// IgnoreSchemeConvertible, will modify any CRD conversion webhook to use the local serving host and port,
53+
// bypassing the need to have the types registered in the Scheme. This is useful for testing CRD conversion webhooks
54+
// with unregistered or unstructured types.
55+
IgnoreSchemeConvertible bool
56+
5257
// IgnoreErrorIfPathMissing will ignore an error if a DirectoryPath does not exist when set to true
5358
IgnoreErrorIfPathMissing bool
5459

@@ -184,7 +189,8 @@ func defaultWebhookOptions(o *WebhookInstallOptions) {
184189
func WaitForWebhooks(config *rest.Config,
185190
mutatingWebhooks []*admissionv1.MutatingWebhookConfiguration,
186191
validatingWebhooks []*admissionv1.ValidatingWebhookConfiguration,
187-
options WebhookInstallOptions) error {
192+
options WebhookInstallOptions,
193+
) error {
188194
waitingFor := map[schema.GroupVersionKind]*sets.Set[string]{}
189195

190196
for _, hook := range mutatingWebhooks {
@@ -242,7 +248,7 @@ func (p *webhookPoller) poll(ctx context.Context) (done bool, err error) {
242248
continue
243249
}
244250
for _, name := range names.UnsortedList() {
245-
var obj = &unstructured.Unstructured{}
251+
obj := &unstructured.Unstructured{}
246252
obj.SetGroupVersionKind(gvk)
247253
err := c.Get(context.Background(), client.ObjectKey{
248254
Namespace: "",

0 commit comments

Comments
 (0)