From 70141d0daa838312b9104bb9af9ae8c44c643fef Mon Sep 17 00:00:00 2001 From: Vince Prignano Date: Wed, 18 Oct 2023 17:03:13 -0700 Subject: [PATCH] envtest: allow to ignore scheme.Convertible check for CRDs Signed-off-by: Vince Prignano --- pkg/envtest/crd.go | 24 +++++++++++++----------- pkg/envtest/webhook.go | 10 ++++++++-- 2 files changed, 21 insertions(+), 13 deletions(-) diff --git a/pkg/envtest/crd.go b/pkg/envtest/crd.go index b93a18a530..43abea35f6 100644 --- a/pkg/envtest/crd.go +++ b/pkg/envtest/crd.go @@ -371,17 +371,19 @@ func modifyConversionWebhooks(crds []*apiextensionsv1.CustomResourceDefinition, if crds[i].Spec.PreserveUnknownFields { continue } - // Continue if the GroupKind isn't registered as being convertible, - // and remove any existing conversion webhooks if they exist. - // This is to prevent the CRD from being rejected by the apiserver, usually - // manifests that are generated by controller-gen will have a conversion - // webhook set, but we don't want to enable it if the type isn't registered. - if _, ok := convertibles[schema.GroupKind{ - Group: crds[i].Spec.Group, - Kind: crds[i].Spec.Names.Kind, - }]; !ok { - crds[i].Spec.Conversion = nil - continue + if !webhookOptions.IgnoreSchemeConvertible { + // Continue if the GroupKind isn't registered as being convertible, + // and remove any existing conversion webhooks if they exist. + // This is to prevent the CRD from being rejected by the apiserver, usually + // manifests that are generated by controller-gen will have a conversion + // webhook set, but we don't want to enable it if the type isn't registered. + if _, ok := convertibles[schema.GroupKind{ + Group: crds[i].Spec.Group, + Kind: crds[i].Spec.Names.Kind, + }]; !ok { + crds[i].Spec.Conversion = nil + continue + } } if crds[i].Spec.Conversion == nil { crds[i].Spec.Conversion = &apiextensionsv1.CustomResourceConversion{ diff --git a/pkg/envtest/webhook.go b/pkg/envtest/webhook.go index f7e43a1480..e4e54e472c 100644 --- a/pkg/envtest/webhook.go +++ b/pkg/envtest/webhook.go @@ -49,6 +49,11 @@ type WebhookInstallOptions struct { // ValidatingWebhooks is a list of ValidatingWebhookConfigurations to install ValidatingWebhooks []*admissionv1.ValidatingWebhookConfiguration + // IgnoreSchemeConvertible, will modify any CRD conversion webhook to use the local serving host and port, + // bypassing the need to have the types registered in the Scheme. This is useful for testing CRD conversion webhooks + // with unregistered or unstructured types. + IgnoreSchemeConvertible bool + // IgnoreErrorIfPathMissing will ignore an error if a DirectoryPath does not exist when set to true IgnoreErrorIfPathMissing bool @@ -184,7 +189,8 @@ func defaultWebhookOptions(o *WebhookInstallOptions) { func WaitForWebhooks(config *rest.Config, mutatingWebhooks []*admissionv1.MutatingWebhookConfiguration, validatingWebhooks []*admissionv1.ValidatingWebhookConfiguration, - options WebhookInstallOptions) error { + options WebhookInstallOptions, +) error { waitingFor := map[schema.GroupVersionKind]*sets.Set[string]{} for _, hook := range mutatingWebhooks { @@ -242,7 +248,7 @@ func (p *webhookPoller) poll(ctx context.Context) (done bool, err error) { continue } for _, name := range names.UnsortedList() { - var obj = &unstructured.Unstructured{} + obj := &unstructured.Unstructured{} obj.SetGroupVersionKind(gvk) err := c.Get(context.Background(), client.ObjectKey{ Namespace: "",