Skip to content

Commit d39233c

Browse files
feat: Exclude comments from CRD generation but not from go struct definition (#3321)
* implemented logic for excluding comments from CRD generation * edited regex pattern * added a comment for excludeFromCRD tag * added excludeFromCRD tag to AbsoluteURI
1 parent 2d7bb82 commit d39233c

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

apis/v1/shared_types.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -542,7 +542,7 @@ type PreciseHostname string
542542
// scheme (e.g., "http" or "spiffe") and a scheme-specific-part. URIs that
543543
// include an authority MUST include a fully qualified domain name or
544544
// IP address as the host.
545-
545+
// <gateway:util:excludeFromCRD> The below regex is taken from the regex section in RFC 3986 with a slight modification to enforce a full URI and not relative. </gateway:util:excludeFromCRD>
546546
// +kubebuilder:validation:MinLength=1
547547
// +kubebuilder:validation:MaxLength=253
548548
// +kubebuilder:validation:Pattern=`^(([^:/?#]+):)(//([^/?#]*))([^?#]*)(\?([^#]*))?(#(.*))?`

pkg/generator/main.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,21 @@ func gatewayTweaks(channel string, props map[string]apiext.JSONSchemaProps) map[
222222
jsonProps.Description = strings.ReplaceAll(jsonProps.Description, endTag, "")
223223
}
224224

225+
// Comments within "gateway:util:excludeFromCRD" tag is not included in the generated CRD and all trailing \n operators before
226+
// and after the tags are removed and replaced with three \n operators.
227+
startTag = "<gateway:util:excludeFromCRD>"
228+
endTag = "</gateway:util:excludeFromCRD>"
229+
regexPattern = `\n*` + regexp.QuoteMeta(startTag) + `(?s:(.*?))` + regexp.QuoteMeta(endTag) + `\n*`
230+
if strings.Contains(jsonProps.Description, "<gateway:util:excludeFromCRD>") {
231+
re := regexp.MustCompile(regexPattern)
232+
match := re.FindStringSubmatch(jsonProps.Description)
233+
if len(match) != 2 {
234+
log.Fatalf("Invalid <gateway:util:excludeFromCRD> tag for %s", name)
235+
}
236+
modifiedDescription := re.ReplaceAllString(jsonProps.Description, "\n\n\n")
237+
jsonProps.Description = modifiedDescription
238+
}
239+
225240
if numValid < numExpressions {
226241
fmt.Printf("Description: %s\n", jsonProps.Description)
227242
log.Fatalf("Found %d Gateway validation expressions, but only %d were valid", numExpressions, numValid)

0 commit comments

Comments
 (0)