Skip to content

Commit 76a25b6

Browse files
authored
Merge pull request #322 from joelanford/merge-one-version
🐛 promote version-specific CRD fields when only one version
2 parents e791289 + 6ed4ff3 commit 76a25b6

File tree

3 files changed

+4707
-4781
lines changed

3 files changed

+4707
-4781
lines changed

pkg/crd/crd_spec_test.go

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,36 @@ import (
2727
var _ = Describe("CRD Generation", func() {
2828
Describe("Utilities", func() {
2929
Describe("MergeIdenticalVersionInfo", func() {
30+
It("should replace per-version schemata with a top-level schema if only one version", func() {
31+
spec := &apiext.CustomResourceDefinition{
32+
Spec: apiext.CustomResourceDefinitionSpec{
33+
Versions: []apiext.CustomResourceDefinitionVersion{
34+
{
35+
Name: "v1",
36+
Storage: true,
37+
Schema: &apiext.CustomResourceValidation{
38+
OpenAPIV3Schema: &apiext.JSONSchemaProps{
39+
Required: []string{"foo"},
40+
Type: "object",
41+
Properties: map[string]apiext.JSONSchemaProps{"foo": apiext.JSONSchemaProps{Type: "string"}},
42+
},
43+
},
44+
},
45+
},
46+
},
47+
}
48+
crd.MergeIdenticalVersionInfo(spec)
49+
Expect(spec.Spec.Validation).To(Equal(&apiext.CustomResourceValidation{
50+
OpenAPIV3Schema: &apiext.JSONSchemaProps{
51+
Required: []string{"foo"},
52+
Type: "object",
53+
Properties: map[string]apiext.JSONSchemaProps{"foo": apiext.JSONSchemaProps{Type: "string"}},
54+
},
55+
}))
56+
Expect(spec.Spec.Versions).To(Equal([]apiext.CustomResourceDefinitionVersion{
57+
{Name: "v1", Storage: true},
58+
}))
59+
})
3060
It("should replace per-version schemata with a top-level schema if all are identical", func() {
3161
spec := &apiext.CustomResourceDefinition{
3262
Spec: apiext.CustomResourceDefinitionSpec{
@@ -100,6 +130,30 @@ var _ = Describe("CRD Generation", func() {
100130
Expect(spec).To(Equal(orig))
101131
})
102132

133+
It("should replace per-version subresources with top-level subresources if only one version", func() {
134+
spec := &apiext.CustomResourceDefinition{
135+
Spec: apiext.CustomResourceDefinitionSpec{
136+
Versions: []apiext.CustomResourceDefinitionVersion{
137+
{
138+
Name: "v1",
139+
Storage: true,
140+
Subresources: &apiext.CustomResourceSubresources{
141+
Status: &apiext.CustomResourceSubresourceStatus{},
142+
},
143+
},
144+
},
145+
},
146+
}
147+
148+
crd.MergeIdenticalVersionInfo(spec)
149+
Expect(spec.Spec.Subresources).To(Equal(&apiext.CustomResourceSubresources{
150+
Status: &apiext.CustomResourceSubresourceStatus{},
151+
}))
152+
Expect(spec.Spec.Versions).To(Equal([]apiext.CustomResourceDefinitionVersion{
153+
{Name: "v1", Storage: true},
154+
}))
155+
})
156+
103157
It("should replace per-version subresources with top-level subresources if all are identical", func() {
104158
spec := &apiext.CustomResourceDefinition{
105159
Spec: apiext.CustomResourceDefinitionSpec{
@@ -152,6 +206,32 @@ var _ = Describe("CRD Generation", func() {
152206
Expect(spec).To(Equal(orig))
153207
})
154208

209+
It("should replace per-version printer columns with top-level printer columns if only one version", func() {
210+
spec := &apiext.CustomResourceDefinition{
211+
Spec: apiext.CustomResourceDefinitionSpec{
212+
Versions: []apiext.CustomResourceDefinitionVersion{
213+
{
214+
Name: "v1",
215+
Storage: true,
216+
AdditionalPrinterColumns: []apiext.CustomResourceColumnDefinition{
217+
{Name: "Cheddar", JSONPath: ".spec.cheddar"},
218+
{Name: "Parmesan", JSONPath: ".status.parmesan"},
219+
},
220+
},
221+
},
222+
},
223+
}
224+
225+
crd.MergeIdenticalVersionInfo(spec)
226+
Expect(spec.Spec.AdditionalPrinterColumns).To(Equal([]apiext.CustomResourceColumnDefinition{
227+
{Name: "Cheddar", JSONPath: ".spec.cheddar"},
228+
{Name: "Parmesan", JSONPath: ".status.parmesan"},
229+
}))
230+
Expect(spec.Spec.Versions).To(Equal([]apiext.CustomResourceDefinitionVersion{
231+
{Name: "v1", Storage: true},
232+
}))
233+
})
234+
155235
It("should replace per-version printer columns with top-level printer columns if all are identical", func() {
156236
spec := &apiext.CustomResourceDefinition{
157237
Spec: apiext.CustomResourceDefinitionSpec{

pkg/crd/spec.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ func mergeIdenticalPrinterColumns(crd *apiext.CustomResourceDefinition) {
111111
// this restriction it would be ambiguous how a v1-with-identical-subresources
112112
// converts into a v1beta1).
113113
func MergeIdenticalVersionInfo(crd *apiext.CustomResourceDefinition) {
114-
if len(crd.Spec.Versions) > 1 {
114+
if len(crd.Spec.Versions) > 0 {
115115
mergeIdenticalSubresources(crd)
116116
mergeIdenticalSchemata(crd)
117117
mergeIdenticalPrinterColumns(crd)

0 commit comments

Comments
 (0)