@@ -19,14 +19,17 @@ package crd
19
19
import (
20
20
"context"
21
21
"fmt"
22
+ "time"
22
23
23
24
"k8s.io/utils/pointer"
24
25
25
26
apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
26
27
crdclientset "k8s.io/apiextensions-apiserver/pkg/client/clientset/clientset"
27
28
"k8s.io/apiextensions-apiserver/test/integration/fixtures"
29
+ apierrors "k8s.io/apimachinery/pkg/api/errors"
28
30
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
29
31
"k8s.io/apimachinery/pkg/runtime/schema"
32
+ "k8s.io/apimachinery/pkg/util/wait"
30
33
"k8s.io/client-go/dynamic"
31
34
"k8s.io/kubernetes/test/e2e/framework"
32
35
)
@@ -47,9 +50,6 @@ type Option func(crd *apiextensionsv1.CustomResourceDefinition)
47
50
48
51
// CreateMultiVersionTestCRD creates a new CRD specifically for the calling test.
49
52
func CreateMultiVersionTestCRD (f * framework.Framework , group string , opts ... Option ) (* TestCrd , error ) {
50
- suffix := framework .RandomSuffix ()
51
- name := fmt .Sprintf ("e2e-test-%s-%s-crd" , f .BaseName , suffix )
52
- kind := fmt .Sprintf ("e2e-test-%s-%s-crd" , f .BaseName , suffix )
53
53
testcrd := & TestCrd {}
54
54
55
55
// Creating a custom resource definition for use by assorted tests.
@@ -69,6 +69,56 @@ func CreateMultiVersionTestCRD(f *framework.Framework, group string, opts ...Opt
69
69
return nil , err
70
70
}
71
71
72
+ crd := genRandomCRD (f , group , opts ... )
73
+ // Be robust about making the crd creation call.
74
+ var got * apiextensionsv1.CustomResourceDefinition
75
+ if err := wait .PollUntilContextTimeout (context .TODO (), f .Timeouts .Poll , 30 * time .Second , true , func (ctx context.Context ) (bool , error ) {
76
+ // Create CRD and waits for the resource to be recognized and available.
77
+ got , err = fixtures .CreateNewV1CustomResourceDefinitionWatchUnsafe (crd , apiExtensionClient )
78
+ if err != nil {
79
+ if apierrors .IsAlreadyExists (err ) {
80
+ // regenerate on conflict
81
+ framework .Logf ("CustomResourceDefinition name %q was already taken, generate a new name and retry" , crd .Name )
82
+ crd = genRandomCRD (f , group , opts ... )
83
+ } else {
84
+ framework .Logf ("Unexpected error while creating CustomResourceDefinition: %v" , err )
85
+ }
86
+ return false , nil
87
+ }
88
+ return true , nil
89
+ }); err != nil {
90
+ return nil , err
91
+ }
92
+
93
+ resourceClients := map [string ]dynamic.ResourceInterface {}
94
+ for _ , v := range got .Spec .Versions {
95
+ if v .Served {
96
+ gvr := schema.GroupVersionResource {Group : got .Spec .Group , Version : v .Name , Resource : got .Spec .Names .Plural }
97
+ resourceClients [v .Name ] = dynamicClient .Resource (gvr ).Namespace (f .Namespace .Name )
98
+ }
99
+ }
100
+
101
+ testcrd .APIExtensionClient = apiExtensionClient
102
+ testcrd .Crd = got
103
+ testcrd .DynamicClients = resourceClients
104
+ testcrd .CleanUp = func (ctx context.Context ) error {
105
+ err := fixtures .DeleteV1CustomResourceDefinition (got , apiExtensionClient )
106
+ if err != nil {
107
+ framework .Failf ("failed to delete CustomResourceDefinition(%s): %v" , got .Name , err )
108
+ }
109
+ return err
110
+ }
111
+ return testcrd , nil
112
+ }
113
+
114
+ // genRandomCRD generates a random CRD name and kind based on the framework's base name and a random suffix.
115
+ // It also sets the group to the provided value and sets the scope to NamespaceScoped. If no versions are provided via
116
+ // the opts, it will create a default version "v1" with an allow-all schema.
117
+ func genRandomCRD (f * framework.Framework , group string , opts ... Option ) * apiextensionsv1.CustomResourceDefinition {
118
+ suffix := framework .RandomSuffix ()
119
+ name := fmt .Sprintf ("e2e-test-%s-%s-crd" , f .UniqueName , suffix )
120
+ kind := fmt .Sprintf ("e2e-test-%s-%s-crd" , f .UniqueName , suffix )
121
+
72
122
crd := & apiextensionsv1.CustomResourceDefinition {
73
123
ObjectMeta : metav1.ObjectMeta {Name : name + "s." + group },
74
124
Spec : apiextensionsv1.CustomResourceDefinitionSpec {
@@ -93,33 +143,7 @@ func CreateMultiVersionTestCRD(f *framework.Framework, group string, opts ...Opt
93
143
Schema : fixtures .AllowAllSchema (),
94
144
}}
95
145
}
96
-
97
- //create CRD and waits for the resource to be recognized and available.
98
- crd , err = fixtures .CreateNewV1CustomResourceDefinitionWatchUnsafe (crd , apiExtensionClient )
99
- if err != nil {
100
- framework .Failf ("failed to create CustomResourceDefinition: %v" , err )
101
- return nil , err
102
- }
103
-
104
- resourceClients := map [string ]dynamic.ResourceInterface {}
105
- for _ , v := range crd .Spec .Versions {
106
- if v .Served {
107
- gvr := schema.GroupVersionResource {Group : crd .Spec .Group , Version : v .Name , Resource : crd .Spec .Names .Plural }
108
- resourceClients [v .Name ] = dynamicClient .Resource (gvr ).Namespace (f .Namespace .Name )
109
- }
110
- }
111
-
112
- testcrd .APIExtensionClient = apiExtensionClient
113
- testcrd .Crd = crd
114
- testcrd .DynamicClients = resourceClients
115
- testcrd .CleanUp = func (ctx context.Context ) error {
116
- err := fixtures .DeleteV1CustomResourceDefinition (crd , apiExtensionClient )
117
- if err != nil {
118
- framework .Failf ("failed to delete CustomResourceDefinition(%s): %v" , name , err )
119
- }
120
- return err
121
- }
122
- return testcrd , nil
146
+ return crd
123
147
}
124
148
125
149
// CreateTestCRD creates a new CRD specifically for the calling test.
0 commit comments