Skip to content

Commit

Permalink
Changes required to use envtest from controller-runtime v0.10
Browse files Browse the repository at this point in the history
Environment.CRDs is now a slice of apiextensionsv1.CustomResourceDefinitions instead of client.Object kubernetes-sigs/controller-runtime#1626 (comment)

Signed-off-by: irbekrm <irbekrm@gmail.com>
  • Loading branch information
irbekrm committed Sep 27, 2021
1 parent 6d99dcd commit ef016c2
Showing 1 changed file with 19 additions and 22 deletions.
41 changes: 19 additions & 22 deletions test/integration/framework/apiserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,20 +27,20 @@ import (
admissionregistrationv1 "k8s.io/api/admissionregistration/v1"
"k8s.io/apiextensions-apiserver/pkg/apis/apiextensions"
apiextensionsinstall "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/install"
v1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
jsonserializer "k8s.io/apimachinery/pkg/runtime/serializer/json"
"k8s.io/apimachinery/pkg/runtime/serializer/versioning"
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
"k8s.io/client-go/rest"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/envtest"

webhooktesting "github.com/jetstack/cert-manager/cmd/webhook/app/testing"
"github.com/jetstack/cert-manager/pkg/api"
apitesting "github.com/jetstack/cert-manager/pkg/api/testing"
"github.com/jetstack/cert-manager/test/internal/apiserver"
"sigs.k8s.io/controller-runtime/pkg/envtest"
)

type StopFunc func()
Expand All @@ -56,10 +56,10 @@ func RunControlPlane(t *testing.T, ctx context.Context) (*rest.Config, StopFunc)
for _, crd := range crds {
t.Logf("Found CRD with name %q", crd.Name)
}
patchCRDConversion(crds, webhookOpts.URL, webhookOpts.CAPEM)
crds = patchCRDConversion(crds, webhookOpts.URL, webhookOpts.CAPEM)

if _, err := envtest.InstallCRDs(config, envtest.CRDInstallOptions{
CRDs: crdsToRuntimeObjects(crds),
CRDs: crds,
}); err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -96,18 +96,23 @@ func init() {
apiextensionsinstall.Install(internalScheme)
}

func patchCRDConversion(crds []*v1.CustomResourceDefinition, url string, caPEM []byte) {
func patchCRDConversion(crds []apiextensionsv1.CustomResourceDefinition, url string, caPEM []byte) []apiextensionsv1.CustomResourceDefinition {
out := []apiextensionsv1.CustomResourceDefinition{}
for _, crd := range crds {
if crd.Spec.Conversion == nil {
out = append(out, crd)
continue
}
if crd.Spec.Conversion.Webhook == nil {
out = append(out, crd)
continue
}
if crd.Spec.Conversion.Webhook.ClientConfig == nil {
out = append(out, crd)
continue
}
if crd.Spec.Conversion.Webhook.ClientConfig.Service == nil {
out = append(out, crd)
continue
}
path := ""
Expand All @@ -118,17 +123,19 @@ func patchCRDConversion(crds []*v1.CustomResourceDefinition, url string, caPEM [
crd.Spec.Conversion.Webhook.ClientConfig.URL = &url
crd.Spec.Conversion.Webhook.ClientConfig.CABundle = caPEM
crd.Spec.Conversion.Webhook.ClientConfig.Service = nil
out = append(out, crd)
}
return out
}

func readCustomResourcesAtPath(t *testing.T, path string) []*v1.CustomResourceDefinition {
func readCustomResourcesAtPath(t *testing.T, path string) []apiextensionsv1.CustomResourceDefinition {
serializer := jsonserializer.NewSerializerWithOptions(jsonserializer.DefaultMetaFactory, internalScheme, internalScheme, jsonserializer.SerializerOptions{
Yaml: true,
})
converter := runtime.UnsafeObjectConvertor(internalScheme)
codec := versioning.NewCodec(serializer, serializer, converter, internalScheme, internalScheme, internalScheme, runtime.InternalGroupVersioner, runtime.InternalGroupVersioner, internalScheme.Name())

var crds []*v1.CustomResourceDefinition
var crds []apiextensionsv1.CustomResourceDefinition
if err := filepath.Walk(path, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
Expand All @@ -138,7 +145,7 @@ func readCustomResourcesAtPath(t *testing.T, path string) []*v1.CustomResourceDe
}
crd, err := readCRDsAtPath(codec, converter, path)
if err != nil {
return err
return fmt.Errorf("failed reading CRDs at path %s: %w", path, err)
}
crds = append(crds, crd...)
return nil
Expand All @@ -148,13 +155,13 @@ func readCustomResourcesAtPath(t *testing.T, path string) []*v1.CustomResourceDe
return crds
}

func readCRDsAtPath(codec runtime.Codec, converter runtime.ObjectConvertor, path string) ([]*v1.CustomResourceDefinition, error) {
func readCRDsAtPath(codec runtime.Codec, converter runtime.ObjectConvertor, path string) ([]apiextensionsv1.CustomResourceDefinition, error) {
data, err := os.ReadFile(path)
if err != nil {
return nil, err
}

var crds []*v1.CustomResourceDefinition
var crds []apiextensionsv1.CustomResourceDefinition
for _, d := range strings.Split(string(data), "\n---\n") {
// skip empty YAML documents
if strings.TrimSpace(d) == "" {
Expand All @@ -166,8 +173,8 @@ func readCRDsAtPath(codec runtime.Codec, converter runtime.ObjectConvertor, path
return nil, err
}

out := &v1.CustomResourceDefinition{}
if err := converter.Convert(internalCRD, out, nil); err != nil {
out := apiextensionsv1.CustomResourceDefinition{}
if err := converter.Convert(internalCRD, &out, nil); err != nil {
return nil, err
}

Expand All @@ -177,16 +184,6 @@ func readCRDsAtPath(codec runtime.Codec, converter runtime.ObjectConvertor, path
return crds, nil
}

func crdsToRuntimeObjects(in []*v1.CustomResourceDefinition) []client.Object {
out := make([]client.Object, len(in))

for i, crd := range in {
out[i] = client.Object(crd)
}

return out
}

func getValidatingWebhookConfig(url string, caPEM []byte) client.Object {
failurePolicy := admissionregistrationv1.Fail
sideEffects := admissionregistrationv1.SideEffectClassNone
Expand Down

0 comments on commit ef016c2

Please sign in to comment.