Skip to content

Commit ce9bf3b

Browse files
committedMay 18, 2021
🐛 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 <vincepri@vmware.com>
1 parent fddf978 commit ce9bf3b

File tree

2 files changed

+51
-1
lines changed

2 files changed

+51
-1
lines changed
 

‎pkg/envtest/crd.go

+50-1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"bufio"
2121
"bytes"
2222
"context"
23+
"encoding/base64"
2324
"fmt"
2425
"io"
2526
"io/ioutil"
@@ -370,13 +371,22 @@ func modifyConversionWebhooks(crds []client.Object, webhookOptions WebhookInstal
370371
for _, crd := range crds {
371372
switch c := crd.(type) {
372373
case *apiextensionsv1beta1.CustomResourceDefinition:
374+
// preserveUnknownFields defaults to true if `nil` in v1beta1.
375+
if c.Spec.PreserveUnknownFields == nil || *c.Spec.PreserveUnknownFields {
376+
continue
377+
}
378+
c.Spec.Conversion.Strategy = apiextensionsv1beta1.WebhookConverter
373379
c.Spec.Conversion.WebhookClientConfig.Service = nil
374380
c.Spec.Conversion.WebhookClientConfig = &apiextensionsv1beta1.WebhookClientConfig{
375381
Service: nil,
376382
URL: url,
377383
CABundle: webhookOptions.LocalServingCAData,
378384
}
379385
case *apiextensionsv1.CustomResourceDefinition:
386+
if c.Spec.PreserveUnknownFields {
387+
continue
388+
}
389+
c.Spec.Conversion.Strategy = apiextensionsv1.WebhookConverter
380390
c.Spec.Conversion.Webhook.ClientConfig.Service = nil
381391
c.Spec.Conversion.Webhook.ClientConfig = &apiextensionsv1.WebhookClientConfig{
382392
Service: nil,
@@ -386,18 +396,57 @@ func modifyConversionWebhooks(crds []client.Object, webhookOptions WebhookInstal
386396
case *unstructured.Unstructured:
387397
webhookClientConfig := map[string]interface{}{
388398
"url": *url,
389-
"caBundle": webhookOptions.LocalServingCAData,
399+
"caBundle": base64.StdEncoding.EncodeToString(webhookOptions.LocalServingCAData),
390400
}
391401

392402
switch c.GroupVersionKind().Version {
393403
case "v1beta1":
404+
// preserveUnknownFields defaults to true if `nil` in v1beta1.
405+
if preserve, found, _ := unstructured.NestedBool(c.Object, "spec", "preserveUnknownFields"); preserve || !found {
406+
continue
407+
}
408+
409+
// Set the strategy.
410+
if err := unstructured.SetNestedField(
411+
c.Object,
412+
string(apiextensionsv1beta1.WebhookConverter),
413+
"spec", "conversion", "strategy"); err != nil {
414+
return err
415+
}
416+
// Set the conversion review versions.
417+
if err := unstructured.SetNestedStringSlice(
418+
c.Object,
419+
[]string{"v1beta1"},
420+
"spec", "conversion", "webhook", "clientConfig"); err != nil {
421+
return err
422+
}
423+
// Set the client configuration.
394424
if err := unstructured.SetNestedMap(
395425
c.Object,
396426
webhookClientConfig,
397427
"spec", "conversion", "webhookClientConfig"); err != nil {
398428
return err
399429
}
400430
case "v1":
431+
if preserve, _, _ := unstructured.NestedBool(c.Object, "spec", "preserveUnknownFields"); preserve {
432+
continue
433+
}
434+
435+
// Set the strategy.
436+
if err := unstructured.SetNestedField(
437+
c.Object,
438+
string(apiextensionsv1.WebhookConverter),
439+
"spec", "conversion", "strategy"); err != nil {
440+
return err
441+
}
442+
// Set the conversion review versions.
443+
if err := unstructured.SetNestedStringSlice(
444+
c.Object,
445+
[]string{"v1", "v1beta1"},
446+
"spec", "conversion", "webhook", "conversionReviewVersions"); err != nil {
447+
return err
448+
}
449+
// Set the client configuration.
401450
if err := unstructured.SetNestedMap(
402451
c.Object,
403452
webhookClientConfig,

‎pkg/envtest/server.go

+1
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,7 @@ func (te *Environment) Start() (*rest.Config, error) {
273273
te.CRDInstallOptions.CRDs = mergeCRDs(te.CRDInstallOptions.CRDs, te.CRDs)
274274
te.CRDInstallOptions.Paths = mergePaths(te.CRDInstallOptions.Paths, te.CRDDirectoryPaths)
275275
te.CRDInstallOptions.ErrorIfPathMissing = te.ErrorIfCRDPathMissing
276+
te.CRDInstallOptions.WebhookOptions = te.WebhookInstallOptions
276277
crds, err := InstallCRDs(te.Config, te.CRDInstallOptions)
277278
if err != nil {
278279
return te.Config, err

0 commit comments

Comments
 (0)
Please sign in to comment.