From 1db5ea0cdb3cfeb9ee8f2a7c56971e41a997b056 Mon Sep 17 00:00:00 2001 From: Vince Prignano Date: Tue, 18 May 2021 13:43:05 -0700 Subject: [PATCH] :bug: Envtest conversion mods should set strategy and review versions Conversion webhooks require the strategy to be set and all the supported review versions. This change also fixes an issue where the CABundle wasn't previously base64 encoded. Signed-off-by: Vince Prignano --- pkg/envtest/crd.go | 35 ++++++++++++++++++++++++++++++++++- pkg/envtest/server.go | 1 + 2 files changed, 35 insertions(+), 1 deletion(-) diff --git a/pkg/envtest/crd.go b/pkg/envtest/crd.go index 923fa6da0b..eb5ef2b2ab 100644 --- a/pkg/envtest/crd.go +++ b/pkg/envtest/crd.go @@ -20,6 +20,7 @@ import ( "bufio" "bytes" "context" + "encoding/base64" "fmt" "io" "io/ioutil" @@ -370,6 +371,7 @@ func modifyConversionWebhooks(crds []client.Object, webhookOptions WebhookInstal for _, crd := range crds { switch c := crd.(type) { case *apiextensionsv1beta1.CustomResourceDefinition: + c.Spec.Conversion.Strategy = apiextensionsv1beta1.WebhookConverter c.Spec.Conversion.WebhookClientConfig.Service = nil c.Spec.Conversion.WebhookClientConfig = &apiextensionsv1beta1.WebhookClientConfig{ Service: nil, @@ -377,6 +379,7 @@ func modifyConversionWebhooks(crds []client.Object, webhookOptions WebhookInstal CABundle: webhookOptions.LocalServingCAData, } case *apiextensionsv1.CustomResourceDefinition: + c.Spec.Conversion.Strategy = apiextensionsv1.WebhookConverter c.Spec.Conversion.Webhook.ClientConfig.Service = nil c.Spec.Conversion.Webhook.ClientConfig = &apiextensionsv1.WebhookClientConfig{ Service: nil, @@ -386,11 +389,26 @@ func modifyConversionWebhooks(crds []client.Object, webhookOptions WebhookInstal case *unstructured.Unstructured: webhookClientConfig := map[string]interface{}{ "url": *url, - "caBundle": webhookOptions.LocalServingCAData, + "caBundle": base64.StdEncoding.EncodeToString(webhookOptions.LocalServingCAData), } switch c.GroupVersionKind().Version { case "v1beta1": + // Set the strategy. + if err := unstructured.SetNestedField( + c.Object, + string(apiextensionsv1beta1.WebhookConverter), + "spec", "conversion", "strategy"); err != nil { + return err + } + // Set the conversion review versions. + if err := unstructured.SetNestedStringSlice( + c.Object, + []string{"v1beta1"}, + "spec", "conversion", "webhook", "clientConfig"); err != nil { + return err + } + // Set the client configuration. if err := unstructured.SetNestedMap( c.Object, webhookClientConfig, @@ -398,6 +416,21 @@ func modifyConversionWebhooks(crds []client.Object, webhookOptions WebhookInstal return err } case "v1": + // Set the strategy. + if err := unstructured.SetNestedField( + c.Object, + string(apiextensionsv1.WebhookConverter), + "spec", "conversion", "strategy"); err != nil { + return err + } + // Set the conversion review versions. + if err := unstructured.SetNestedStringSlice( + c.Object, + []string{"v1", "v1beta1"}, + "spec", "conversion", "webhook", "conversionReviewVersions"); err != nil { + return err + } + // Set the client configuration. if err := unstructured.SetNestedMap( c.Object, webhookClientConfig, diff --git a/pkg/envtest/server.go b/pkg/envtest/server.go index b23e81fee9..776e435bf2 100644 --- a/pkg/envtest/server.go +++ b/pkg/envtest/server.go @@ -273,6 +273,7 @@ func (te *Environment) Start() (*rest.Config, error) { te.CRDInstallOptions.CRDs = mergeCRDs(te.CRDInstallOptions.CRDs, te.CRDs) te.CRDInstallOptions.Paths = mergePaths(te.CRDInstallOptions.Paths, te.CRDDirectoryPaths) te.CRDInstallOptions.ErrorIfPathMissing = te.ErrorIfCRDPathMissing + te.CRDInstallOptions.WebhookOptions = te.WebhookInstallOptions crds, err := InstallCRDs(te.Config, te.CRDInstallOptions) if err != nil { return te.Config, err