Skip to content

Commit 8c38549

Browse files
committed
allow easy access to flattened crds from parser
1 parent 07643d5 commit 8c38549

File tree

2 files changed

+25
-4
lines changed

2 files changed

+25
-4
lines changed

pkg/crd/parser.go

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,10 @@ type Parser struct {
5858
GroupVersions map[*loader.Package]schema.GroupVersion
5959
// CustomResourceDefinitions contains the known CustomResourceDefinitions for types in this parser.
6060
CustomResourceDefinitions map[schema.GroupKind]apiext.CustomResourceDefinition
61+
// FlattenedSchemata contains fully flattened schemata for use in building
62+
// CustomResourceDefinition validation. Each schema has been flattened by the flattener,
63+
// and then embedded fields have been flattened with FlattenEmbedded.
64+
FlattenedSchemata map[TypeIdent]apiext.JSONSchemaProps
6165

6266
// PackageOverrides indicates that the loading of any package with
6367
// the given path should be handled by the given overrider.
@@ -95,6 +99,9 @@ func (p *Parser) init() {
9599
if p.CustomResourceDefinitions == nil {
96100
p.CustomResourceDefinitions = make(map[schema.GroupKind]apiext.CustomResourceDefinition)
97101
}
102+
if p.FlattenedSchemata == nil {
103+
p.FlattenedSchemata = make(map[TypeIdent]apiext.JSONSchemaProps)
104+
}
98105
}
99106

100107
// indexTypes loads all types in the package into Types.
@@ -162,8 +169,20 @@ func (p *Parser) NeedSchemaFor(typ TypeIdent) {
162169
schema := infoToSchema(ctxForInfo)
163170

164171
p.Schemata[typ] = *schema
172+
}
173+
174+
func (p *Parser) NeedFlattenedSchemaFor(typ TypeIdent) {
175+
p.init()
176+
177+
if _, knownSchema := p.FlattenedSchemata[typ]; knownSchema {
178+
return
179+
}
180+
181+
p.NeedSchemaFor(typ)
182+
partialFlattened := p.flattener.FlattenType(typ)
183+
fullyFlattened := FlattenEmbedded(partialFlattened, typ.Package)
165184

166-
return
185+
p.FlattenedSchemata[typ] = *fullyFlattened
167186
}
168187

169188
// NeedCRDFor lives off in spec.go

pkg/crd/spec.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -160,15 +160,17 @@ func (p *Parser) NeedCRDFor(groupKind schema.GroupKind, maxDescLen *int) {
160160
if typeInfo == nil {
161161
continue
162162
}
163-
fullSchema := FlattenEmbedded(p.flattener.FlattenType(typeIdent), pkg)
163+
p.NeedFlattenedSchemaFor(typeIdent)
164+
fullSchema := p.FlattenedSchemata[typeIdent]
165+
fullSchema = *fullSchema.DeepCopy() // don't mutate the cache (we might be truncating description, etc)
164166
if maxDescLen != nil {
165-
TruncateDescription(fullSchema, *maxDescLen)
167+
TruncateDescription(&fullSchema, *maxDescLen)
166168
}
167169
ver := apiext.CustomResourceDefinitionVersion{
168170
Name: p.GroupVersions[pkg].Version,
169171
Served: true,
170172
Schema: &apiext.CustomResourceValidation{
171-
OpenAPIV3Schema: fullSchema,
173+
OpenAPIV3Schema: &fullSchema, // fine to take a reference since we deepcopy above
172174
},
173175
}
174176
crd.Spec.Versions = append(crd.Spec.Versions, ver)

0 commit comments

Comments
 (0)