Skip to content

Commit 2f93a69

Browse files
committed
review updates
Signed-off-by: grokspawn <jordan@nimblewidget.com>
1 parent 1e671c5 commit 2f93a69

2 files changed

Lines changed: 26 additions & 21 deletions

File tree

‎hack/tools/crd-generator/main.go‎

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ func runGenerator(args ...string) {
137137
if channel == StandardChannel && strings.Contains(version.Name, "alpha") {
138138
channelCrd.Spec.Versions[i].Served = false
139139
}
140-
version.Schema.OpenAPIV3Schema.Properties, version.Schema.OpenAPIV3Schema.Required = opconTweaksMap(channel, version.Schema.OpenAPIV3Schema.Properties, version.Schema.OpenAPIV3Schema.Required)
140+
channelCrd.Spec.Versions[i].Schema.OpenAPIV3Schema.Properties = opconTweaksMap(channel, channelCrd.Spec.Versions[i].Schema.OpenAPIV3Schema)
141141
}
142142

143143
conv, err := crd.AsVersion(*channelCrd, apiextensionsv1.SchemeGroupVersion)
@@ -180,10 +180,10 @@ func runGenerator(args ...string) {
180180
}
181181
}
182182

183-
// Apply Opcon specific tweaks to all properties in a map, and return a list of required fields according to opcon tags.
184-
// For opcon validation optional/required tags, the required list is updated accordingly.
185-
func opconTweaksMap(channel string, props map[string]apiextensionsv1.JSONSchemaProps, existingRequired []string) (map[string]apiextensionsv1.JSONSchemaProps, []string) {
186-
newRequired := slices.Clone(existingRequired)
183+
// Apply Opcon specific tweaks to all properties in a map, and update the parent schema's required list according to opcon tags.
184+
// For opcon validation optional/required tags, the parent schema's required list is mutated directly.
185+
func opconTweaksMap(channel string, parentSchema *apiextensionsv1.JSONSchemaProps) map[string]apiextensionsv1.JSONSchemaProps {
186+
props := parentSchema.Properties
187187

188188
for name := range props {
189189
jsonProps := props[name]
@@ -195,17 +195,17 @@ func opconTweaksMap(channel string, props map[string]apiextensionsv1.JSONSchemaP
195195
// Update required list based on tag
196196
switch reqStatus {
197197
case statusRequired:
198-
if !slices.Contains(newRequired, name) {
199-
newRequired = append(newRequired, name)
198+
if !slices.Contains(parentSchema.Required, name) {
199+
parentSchema.Required = append(parentSchema.Required, name)
200200
}
201201
case statusOptional:
202-
newRequired = slices.DeleteFunc(newRequired, func(s string) bool { return s == name })
202+
parentSchema.Required = slices.DeleteFunc(parentSchema.Required, func(s string) bool { return s == name })
203203
default:
204204
// "" (unspecified) means keep existing status
205205
}
206206
}
207207
}
208-
return props, newRequired
208+
return props
209209
}
210210

211211
// Custom Opcon API Tweaks for tags prefixed with `<opcon:` that get past
@@ -288,7 +288,7 @@ func opconTweaks(channel string, name string, jsonProps apiextensionsv1.JSONSche
288288
jsonProps.Description = formatDescription(jsonProps.Description, channel, name)
289289

290290
if len(jsonProps.Properties) > 0 {
291-
jsonProps.Properties, jsonProps.Required = opconTweaksMap(channel, jsonProps.Properties, jsonProps.Required)
291+
jsonProps.Properties = opconTweaksMap(channel, &jsonProps)
292292
} else if jsonProps.Items != nil && jsonProps.Items.Schema != nil {
293293
jsonProps.Items.Schema, _ = opconTweaks(channel, name, *jsonProps.Items.Schema)
294294
}
@@ -299,24 +299,25 @@ func opconTweaks(channel string, name string, jsonProps apiextensionsv1.JSONSche
299299
func formatDescription(description string, channel string, name string) string {
300300
tagset := []struct {
301301
channel string
302-
start string
303-
end string
302+
tag string
304303
}{
305-
{channel: ExperimentalChannel, start: "<opcon:standard:description>", end: "</opcon:standard:description>"},
306-
{channel: StandardChannel, start: "<opcon:experimental:description>", end: "</opcon:experimental:description>"},
304+
{channel: ExperimentalChannel, tag: "opcon:standard:description"},
305+
{channel: StandardChannel, tag: "opcon:experimental:description"},
307306
}
308307
for _, ts := range tagset {
309-
if channel == ts.channel && strings.Contains(description, ts.start) {
310-
regexPattern := `\n*` + regexp.QuoteMeta(ts.start) + `(?s:(.*?))` + regexp.QuoteMeta(ts.end) + `\n*`
308+
startTag := fmt.Sprintf("<%s>", ts.tag)
309+
endTag := fmt.Sprintf("</%s>", ts.tag)
310+
if channel == ts.channel && strings.Contains(description, ts.tag) {
311+
regexPattern := `\n*` + regexp.QuoteMeta(startTag) + `(?s:(.*?))` + regexp.QuoteMeta(endTag) + `\n*`
311312
re := regexp.MustCompile(regexPattern)
312313
match := re.FindStringSubmatch(description)
313314
if len(match) != 2 {
314-
log.Fatalf("Invalid <opcon:experimental:description> tag for %s", name)
315+
log.Fatalf("Invalid %s tag for %s", startTag, name)
315316
}
316317
description = re.ReplaceAllString(description, "\n\n")
317318
} else {
318-
description = strings.ReplaceAll(description, ts.start, "")
319-
description = strings.ReplaceAll(description, ts.end, "")
319+
description = strings.ReplaceAll(description, startTag, "")
320+
description = strings.ReplaceAll(description, endTag, "")
320321
}
321322
}
322323

‎hack/tools/crd-generator/main_test.go‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -312,8 +312,12 @@ func TestOpconTweaksMapRequiredList(t *testing.T) {
312312

313313
for _, tt := range tests {
314314
t.Run(tt.name, func(t *testing.T) {
315-
_, required := opconTweaksMap(tt.channel, tt.props, tt.existingRequired)
316-
require.ElementsMatch(t, tt.expectedRequired, required)
315+
parentSchema := &apiextensionsv1.JSONSchemaProps{
316+
Properties: tt.props,
317+
Required: tt.existingRequired,
318+
}
319+
opconTweaksMap(tt.channel, parentSchema)
320+
require.ElementsMatch(t, tt.expectedRequired, parentSchema.Required)
317321
})
318322
}
319323
}

0 commit comments

Comments
 (0)