diff --git a/pkg/envtest/crd.go b/pkg/envtest/crd.go index ed54e3da57..0b002115a8 100644 --- a/pkg/envtest/crd.go +++ b/pkg/envtest/crd.go @@ -127,14 +127,17 @@ func WaitForCRDs(config *rest.Config, crds []client.Object, options CRDInstallOp if err != nil { return err } - if crdVersion != "" { - gvs = append(gvs, schema.GroupVersion{Group: crdGroup, Version: crdVersion}) - } - - versions, _, err := unstructured.NestedSlice(crd.Object, "spec", "versions") + versions, found, err := unstructured.NestedSlice(crd.Object, "spec", "versions") if err != nil { return err } + + // gvs should be added here only if single version is found. If multiple version is found we will add those version + // based on the version is served or not. + if crdVersion != "" && !found { + gvs = append(gvs, schema.GroupVersion{Group: crdGroup, Version: crdVersion}) + } + for _, version := range versions { versionMap, ok := version.(map[string]interface{}) if !ok { diff --git a/pkg/envtest/envtest_test.go b/pkg/envtest/envtest_test.go index 7f1885f88b..7caa9e75de 100644 --- a/pkg/envtest/envtest_test.go +++ b/pkg/envtest/envtest_test.go @@ -81,6 +81,44 @@ var _ = Describe("Test", func() { }, teardownTimeoutSeconds) Describe("InstallCRDs", func() { + It("should install the unserved CRDs into the cluster", func() { + crds, err = InstallCRDs(env.Config, CRDInstallOptions{ + Paths: []string{filepath.Join(".", "testdata", "crds", "examplecrd_unserved.yaml")}, + }) + Expect(err).NotTo(HaveOccurred()) + + // Expect to find the CRDs + + crdv1 := &apiextensionsv1.CustomResourceDefinition{} + err = c.Get(context.TODO(), types.NamespacedName{Name: "frigates.ship.example.com"}, crdv1) + Expect(err).NotTo(HaveOccurred()) + Expect(crdv1.Spec.Names.Kind).To(Equal("Frigate")) + + err = WaitForCRDs(env.Config, []client.Object{ + &v1beta1.CustomResourceDefinition{ + Spec: v1beta1.CustomResourceDefinitionSpec{ + Group: "ship.example.com", + Names: v1beta1.CustomResourceDefinitionNames{ + Plural: "frigates", + }, + Versions: []v1beta1.CustomResourceDefinitionVersion{ + { + Name: "v1", + Storage: true, + Served: false, + }, + { + Name: "v1beta1", + Storage: false, + Served: false, + }, + }}, + }, + }, + CRDInstallOptions{MaxTime: 50 * time.Millisecond, PollInterval: 15 * time.Millisecond}, + ) + Expect(err).NotTo(HaveOccurred()) + }) It("should install the CRDs into the cluster using directory", func(done Done) { crds, err = InstallCRDs(env.Config, CRDInstallOptions{ Paths: []string{validDirectory}, diff --git a/pkg/envtest/testdata/crds/examplecrd_unserved.yaml b/pkg/envtest/testdata/crds/examplecrd_unserved.yaml new file mode 100644 index 0000000000..cf759009a0 --- /dev/null +++ b/pkg/envtest/testdata/crds/examplecrd_unserved.yaml @@ -0,0 +1,61 @@ + +--- +apiVersion: apiextensions.k8s.io/v1beta1 +kind: CustomResourceDefinition +metadata: + annotations: + controller-gen.kubebuilder.io/version: v0.3.0 + creationTimestamp: null + name: frigates.ship.example.com +spec: + group: ship.example.com + names: + kind: Frigate + listKind: FrigateList + plural: frigates + singular: frigate + scope: Namespaced + subresources: + status: {} + validation: + openAPIV3Schema: + description: Frigate is the Schema for the frigates API + properties: + apiVersion: + description: 'APIVersion defines the versioned schema of this representation + of an object. Servers should convert recognized schemas to the latest + internal value, and may reject unrecognized values. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#resources' + type: string + kind: + description: 'Kind is a string value representing the REST resource this + object represents. Servers may infer this from the endpoint the client + submits requests to. Cannot be updated. In CamelCase. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#types-kinds' + type: string + metadata: + type: object + spec: + description: FrigateSpec defines the desired state of Frigate + properties: + foo: + description: Foo is an example field of Frigate. Edit Frigate_types.go + to remove/update + type: string + type: object + status: + description: FrigateStatus defines the observed state of Frigate + type: object + type: object + version: v1 + versions: + - name: v1 + served: false + storage: true + - name: v1beta1 + served: false + storage: false +status: + acceptedNames: + kind: "" + plural: "" + conditions: [] + storedVersions: []