Skip to content

Commit e4ca925

Browse files
committed
🐛 Fix the unserved version CRD install timeout in envtest
1 parent c000ea8 commit e4ca925

File tree

3 files changed

+107
-5
lines changed

3 files changed

+107
-5
lines changed

pkg/envtest/crd.go

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -127,14 +127,17 @@ func WaitForCRDs(config *rest.Config, crds []client.Object, options CRDInstallOp
127127
if err != nil {
128128
return err
129129
}
130-
if crdVersion != "" {
131-
gvs = append(gvs, schema.GroupVersion{Group: crdGroup, Version: crdVersion})
132-
}
133-
134-
versions, _, err := unstructured.NestedSlice(crd.Object, "spec", "versions")
130+
versions, found, err := unstructured.NestedSlice(crd.Object, "spec", "versions")
135131
if err != nil {
136132
return err
137133
}
134+
135+
// gvs should be added here only if single version is found. If multiple version is found we will add those version
136+
// based on the version is served or not.
137+
if crdVersion != "" && !found {
138+
gvs = append(gvs, schema.GroupVersion{Group: crdGroup, Version: crdVersion})
139+
}
140+
138141
for _, version := range versions {
139142
versionMap, ok := version.(map[string]interface{})
140143
if !ok {

pkg/envtest/envtest_test.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,44 @@ var _ = Describe("Test", func() {
8181
}, teardownTimeoutSeconds)
8282

8383
Describe("InstallCRDs", func() {
84+
It("should install the unserved CRDs into the cluster", func() {
85+
crds, err = InstallCRDs(env.Config, CRDInstallOptions{
86+
Paths: []string{filepath.Join(".", "testdata", "crds", "examplecrd_unserved.yaml")},
87+
})
88+
Expect(err).NotTo(HaveOccurred())
89+
90+
// Expect to find the CRDs
91+
92+
crdv1 := &apiextensionsv1.CustomResourceDefinition{}
93+
err = c.Get(context.TODO(), types.NamespacedName{Name: "frigates.ship.example.com"}, crdv1)
94+
Expect(err).NotTo(HaveOccurred())
95+
Expect(crdv1.Spec.Names.Kind).To(Equal("Frigate"))
96+
97+
err = WaitForCRDs(env.Config, []client.Object{
98+
&v1beta1.CustomResourceDefinition{
99+
Spec: v1beta1.CustomResourceDefinitionSpec{
100+
Group: "ship.example.com",
101+
Names: v1beta1.CustomResourceDefinitionNames{
102+
Plural: "frigates",
103+
},
104+
Versions: []v1beta1.CustomResourceDefinitionVersion{
105+
{
106+
Name: "v1",
107+
Storage: true,
108+
Served: false,
109+
},
110+
{
111+
Name: "v1beta1",
112+
Storage: false,
113+
Served: false,
114+
},
115+
}},
116+
},
117+
},
118+
CRDInstallOptions{MaxTime: 50 * time.Millisecond, PollInterval: 15 * time.Millisecond},
119+
)
120+
Expect(err).NotTo(HaveOccurred())
121+
})
84122
It("should install the CRDs into the cluster using directory", func(done Done) {
85123
crds, err = InstallCRDs(env.Config, CRDInstallOptions{
86124
Paths: []string{validDirectory},
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
2+
---
3+
apiVersion: apiextensions.k8s.io/v1beta1
4+
kind: CustomResourceDefinition
5+
metadata:
6+
annotations:
7+
controller-gen.kubebuilder.io/version: v0.3.0
8+
creationTimestamp: null
9+
name: frigates.ship.example.com
10+
spec:
11+
group: ship.example.com
12+
names:
13+
kind: Frigate
14+
listKind: FrigateList
15+
plural: frigates
16+
singular: frigate
17+
scope: Namespaced
18+
subresources:
19+
status: {}
20+
validation:
21+
openAPIV3Schema:
22+
description: Frigate is the Schema for the frigates API
23+
properties:
24+
apiVersion:
25+
description: 'APIVersion defines the versioned schema of this representation
26+
of an object. Servers should convert recognized schemas to the latest
27+
internal value, and may reject unrecognized values. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#resources'
28+
type: string
29+
kind:
30+
description: 'Kind is a string value representing the REST resource this
31+
object represents. Servers may infer this from the endpoint the client
32+
submits requests to. Cannot be updated. In CamelCase. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#types-kinds'
33+
type: string
34+
metadata:
35+
type: object
36+
spec:
37+
description: FrigateSpec defines the desired state of Frigate
38+
properties:
39+
foo:
40+
description: Foo is an example field of Frigate. Edit Frigate_types.go
41+
to remove/update
42+
type: string
43+
type: object
44+
status:
45+
description: FrigateStatus defines the observed state of Frigate
46+
type: object
47+
type: object
48+
version: v1
49+
versions:
50+
- name: v1
51+
served: false
52+
storage: true
53+
- name: v1beta1
54+
served: false
55+
storage: false
56+
status:
57+
acceptedNames:
58+
kind: ""
59+
plural: ""
60+
conditions: []
61+
storedVersions: []

0 commit comments

Comments
 (0)