Skip to content

Commit ea8d4ea

Browse files
authored
Merge pull request #511 from kevindelgado/pr/386-no-desc-metadata
🐛 Don't generate description on metadata of CRD structural schema
2 parents 557da25 + d7d8fd1 commit ea8d4ea

File tree

5 files changed

+59
-6
lines changed

5 files changed

+59
-6
lines changed

pkg/crd/gen.go

Lines changed: 47 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -161,10 +161,14 @@ func (g Generator) Generate(ctx *genall.GenerationContext) error {
161161
}
162162

163163
for i, crd := range versionedCRDs {
164-
// defaults are not allowed to be specified in v1beta1 CRDs, so strip them
165-
// before writing to a file
164+
// defaults are not allowed to be specified in v1beta1 CRDs and
165+
// decriptions are not allowed on the metadata regardless of version
166+
// strip them before writing to a file
166167
if crdVersions[i] == "v1beta1" {
167168
removeDefaultsFromSchemas(crd.(*apiextlegacy.CustomResourceDefinition))
169+
removeDescriptionFromMetadataLegacy(crd.(*apiextlegacy.CustomResourceDefinition))
170+
} else {
171+
removeDescriptionFromMetadata(crd.(*apiext.CustomResourceDefinition))
168172
}
169173
var fileName string
170174
if i == 0 {
@@ -181,6 +185,47 @@ func (g Generator) Generate(ctx *genall.GenerationContext) error {
181185
return nil
182186
}
183187

188+
func removeDescriptionFromMetadata(crd *apiext.CustomResourceDefinition) {
189+
for _, versionSpec := range crd.Spec.Versions {
190+
if versionSpec.Schema != nil {
191+
removeDescriptionFromMetadataProps(versionSpec.Schema.OpenAPIV3Schema)
192+
}
193+
}
194+
}
195+
196+
func removeDescriptionFromMetadataProps(v *apiext.JSONSchemaProps) {
197+
if m, ok := v.Properties["metadata"]; ok {
198+
meta := &m
199+
if meta.Description != "" {
200+
meta.Description = ""
201+
v.Properties["metadata"] = m
202+
203+
}
204+
}
205+
}
206+
207+
func removeDescriptionFromMetadataLegacy(crd *apiextlegacy.CustomResourceDefinition) {
208+
if crd.Spec.Validation != nil {
209+
removeDescriptionFromMetadataPropsLegacy(crd.Spec.Validation.OpenAPIV3Schema)
210+
}
211+
for _, versionSpec := range crd.Spec.Versions {
212+
if versionSpec.Schema != nil {
213+
removeDescriptionFromMetadataPropsLegacy(versionSpec.Schema.OpenAPIV3Schema)
214+
}
215+
}
216+
}
217+
218+
func removeDescriptionFromMetadataPropsLegacy(v *apiextlegacy.JSONSchemaProps) {
219+
if m, ok := v.Properties["metadata"]; ok {
220+
meta := &m
221+
if meta.Description != "" {
222+
meta.Description = ""
223+
v.Properties["metadata"] = m
224+
225+
}
226+
}
227+
}
228+
184229
// removeDefaultsFromSchemas will remove all instances of default values being
185230
// specified across all defined API versions
186231
func removeDefaultsFromSchemas(crd *apiextlegacy.CustomResourceDefinition) {

pkg/crd/gen_integration_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ var _ = Describe("CRD Generation proper defaulting", func() {
6767
}
6868
})
6969

70-
It("should strip v1beta1 CRDs of default fields", func() {
70+
It("should strip v1beta1 CRDs of default fields and metadata description", func() {
7171
By("calling Generate")
7272
gen := &crd.Generator{
7373
CRDVersions: []string{"v1beta1"},
@@ -84,7 +84,7 @@ var _ = Describe("CRD Generation proper defaulting", func() {
8484

8585
})
8686

87-
It("should not strip v1 CRDs of default fields", func() {
87+
It("should not strip v1 CRDs of default fields and metadata description", func() {
8888
By("calling Generate")
8989
gen := &crd.Generator{
9090
CRDVersions: []string{"v1"},

pkg/crd/testdata/gen/bar.example.com_foos.v1beta1.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ spec:
2727
metadata:
2828
type: object
2929
spec:
30+
description: Spec comments SHOULD appear in the CRD spec
3031
properties:
3132
defaultedString:
3233
description: This tests that defaulted fields are stripped for v1beta1, but not for v1
@@ -35,6 +36,7 @@ spec:
3536
- defaultedString
3637
type: object
3738
status:
39+
description: Status comments SHOULD appear in the CRD spec
3840
type: object
3941
type: object
4042
version: foo

pkg/crd/testdata/gen/bar.example.com_foos.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ spec:
2929
metadata:
3030
type: object
3131
spec:
32+
description: Spec comments SHOULD appear in the CRD spec
3233
properties:
3334
defaultedString:
3435
default: fooDefaultString
@@ -38,6 +39,7 @@ spec:
3839
- defaultedString
3940
type: object
4041
status:
42+
description: Status comments SHOULD appear in the CRD spec
4143
type: object
4244
type: object
4345
served: true

pkg/crd/testdata/gen/foo_types.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,13 @@ type FooSpec struct {
3333
type FooStatus struct{}
3434

3535
type Foo struct {
36-
metav1.TypeMeta `json:",inline"`
36+
// TypeMeta comments should NOT appear in the CRD spec
37+
metav1.TypeMeta `json:",inline"`
38+
// ObjectMeta comments should NOT appear in the CRD spec
3739
metav1.ObjectMeta `json:"metadata,omitempty"`
3840

39-
Spec FooSpec `json:"spec,omitempty"`
41+
// Spec comments SHOULD appear in the CRD spec
42+
Spec FooSpec `json:"spec,omitempty"`
43+
// Status comments SHOULD appear in the CRD spec
4044
Status FooStatus `json:"status,omitempty"`
4145
}

0 commit comments

Comments
 (0)