generated from kubernetes/kubernetes-template-project
-
Notifications
You must be signed in to change notification settings - Fork 528
/
Copy pathmain.go
278 lines (237 loc) · 8.84 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
/*
Copyright 2021 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package main
import (
"fmt"
"log"
"os"
"regexp"
"strings"
apiext "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
"sigs.k8s.io/controller-tools/pkg/crd"
"sigs.k8s.io/controller-tools/pkg/loader"
"sigs.k8s.io/controller-tools/pkg/markers"
"sigs.k8s.io/yaml"
"sigs.k8s.io/gateway-api/pkg/consts"
)
var standardKinds = map[string]bool{
"GatewayClass": true,
"Gateway": true,
"GRPCRoute": true,
"HTTPRoute": true,
"ReferenceGrant": true,
}
// This generation code is largely copied from
// github.com/kubernetes-sigs/controller-tools/blob/ab52f76cc7d167925b2d5942f24bf22e30f49a02/pkg/crd/gen.go
func main() {
roots, err := loader.LoadRoots(
"k8s.io/apimachinery/pkg/runtime/schema", // Needed to parse generated register functions.
"sigs.k8s.io/gateway-api/apis/v1alpha3",
"sigs.k8s.io/gateway-api/apis/v1alpha2",
"sigs.k8s.io/gateway-api/apis/v1beta1",
"sigs.k8s.io/gateway-api/apis/v1",
"sigs.k8s.io/gateway-api/apisx/v1alpha1",
)
if err != nil {
log.Fatalf("failed to load package roots: %s", err)
}
generator := &crd.Generator{}
parser := &crd.Parser{
Collector: &markers.Collector{Registry: &markers.Registry{}},
Checker: &loader.TypeChecker{
NodeFilters: []loader.NodeFilter{generator.CheckFilter()},
},
}
err = generator.RegisterMarkers(parser.Collector.Registry)
if err != nil {
log.Fatalf("failed to register markers: %s", err)
}
crd.AddKnownTypes(parser)
for _, r := range roots {
parser.NeedPackage(r)
}
metav1Pkg := crd.FindMetav1(roots)
if metav1Pkg == nil {
log.Fatalf("no objects in the roots, since nothing imported metav1")
}
kubeKinds := crd.FindKubeKinds(parser, metav1Pkg)
if len(kubeKinds) == 0 {
log.Fatalf("no objects in the roots")
}
channels := []string{"standard", "experimental"}
for _, channel := range channels {
for _, groupKind := range kubeKinds {
if channel == "standard" && !standardKinds[groupKind.Kind] {
continue
}
log.Printf("generating %s CRD for %v\n", channel, groupKind)
parser.NeedCRDFor(groupKind, nil)
crdRaw := parser.CustomResourceDefinitions[groupKind]
// Inline version of "addAttribution(&crdRaw)" ...
if crdRaw.ObjectMeta.Annotations == nil {
crdRaw.ObjectMeta.Annotations = map[string]string{}
}
crdRaw.ObjectMeta.Annotations[consts.BundleVersionAnnotation] = consts.BundleVersion
crdRaw.ObjectMeta.Annotations[consts.ChannelAnnotation] = channel
crdRaw.ObjectMeta.Annotations[apiext.KubeAPIApprovedAnnotation] = consts.ApprovalLink
// Prevent the top level metadata for the CRD to be generated regardless of the intention in the arguments
crd.FixTopLevelMetadata(crdRaw)
channelCrd := crdRaw.DeepCopy()
for i, version := range channelCrd.Spec.Versions {
if channel == "standard" && strings.Contains(version.Name, "alpha") {
channelCrd.Spec.Versions[i].Served = false
}
version.Schema.OpenAPIV3Schema.Properties = gatewayTweaksMap(channel, version.Schema.OpenAPIV3Schema.Properties)
}
conv, err := crd.AsVersion(*channelCrd, apiext.SchemeGroupVersion)
if err != nil {
log.Fatalf("failed to convert CRD: %s", err)
}
out, err := yaml.Marshal(conv)
if err != nil {
log.Fatalf("failed to marshal CRD: %s", err)
}
fileName := fmt.Sprintf("config/crd/%s/%s_%s.yaml", channel, crdRaw.Spec.Group, crdRaw.Spec.Names.Plural)
err = os.WriteFile(fileName, out, 0o600)
if err != nil {
log.Fatalf("failed to write CRD: %s", err)
}
}
}
}
func gatewayTweaksMap(channel string, props map[string]apiext.JSONSchemaProps) map[string]apiext.JSONSchemaProps {
for name := range props {
jsonProps, _ := props[name]
p := gatewayTweaks(channel, name, jsonProps)
if p == nil {
delete(props, name)
} else {
props[name] = *p
}
}
return props
}
// Custom Gateway API Tweaks for tags prefixed with `<gateway:` that get past
// the limitations of Kubebuilder annotations.
func gatewayTweaks(channel string, name string, jsonProps apiext.JSONSchemaProps) *apiext.JSONSchemaProps {
if strings.Contains(jsonProps.Description, "<gateway:validateIPAddress>") {
jsonProps.Items.Schema.OneOf = []apiext.JSONSchemaProps{{
Properties: map[string]apiext.JSONSchemaProps{
"type": {
Enum: []apiext.JSON{{Raw: []byte("\"IPAddress\"")}},
},
"value": {
AnyOf: []apiext.JSONSchemaProps{{
Format: "ipv4",
}, {
Format: "ipv6",
}},
},
},
}, {
Properties: map[string]apiext.JSONSchemaProps{
"type": {
Not: &apiext.JSONSchemaProps{
Enum: []apiext.JSON{{Raw: []byte("\"IPAddress\"")}},
},
},
},
}}
}
if channel == "standard" {
if strings.Contains(jsonProps.Description, "<gateway:experimental>") {
return nil
}
}
// TODO(robscott): Figure out why crdgen switched this to "object"
if jsonProps.Format == "date-time" {
jsonProps.Type = "string"
}
validationPrefix := fmt.Sprintf("<gateway:%s:validation:", channel)
numExpressions := strings.Count(jsonProps.Description, validationPrefix)
numValid := 0
if numExpressions > 0 {
enumRe := regexp.MustCompile(validationPrefix + "Enum=([A-Za-z;]*)>")
enumMatches := enumRe.FindAllStringSubmatch(jsonProps.Description, 64)
for _, enumMatch := range enumMatches {
if len(enumMatch) != 2 {
log.Fatalf("Invalid %s Enum tag for %s", validationPrefix, name)
}
numValid++
jsonProps.Enum = []apiext.JSON{}
for _, val := range strings.Split(enumMatch[1], ";") {
jsonProps.Enum = append(jsonProps.Enum, apiext.JSON{Raw: []byte("\"" + val + "\"")})
}
}
celRe := regexp.MustCompile(validationPrefix + "XValidation:message=\"([^\"]*)\",rule=\"([^\"]*)\">")
celMatches := celRe.FindAllStringSubmatch(jsonProps.Description, 64)
for _, celMatch := range celMatches {
if len(celMatch) != 3 {
log.Fatalf("Invalid %s CEL tag for %s", validationPrefix, name)
}
numValid++
jsonProps.XValidations = append(jsonProps.XValidations, apiext.ValidationRule{
Message: celMatch[1],
Rule: celMatch[2],
})
}
}
if numValid < numExpressions {
fmt.Printf("Description: %s\n", jsonProps.Description)
log.Fatalf("Found %d Gateway validation expressions, but only %d were valid", numExpressions, numValid)
}
jsonProps.Description = formatDescription(jsonProps.Description, channel, name)
if len(jsonProps.Properties) > 0 {
jsonProps.Properties = gatewayTweaksMap(channel, jsonProps.Properties)
} else if jsonProps.Items != nil && jsonProps.Items.Schema != nil {
jsonProps.Items.Schema = gatewayTweaks(channel, name, *jsonProps.Items.Schema)
}
return &jsonProps
}
func formatDescription(description string, channel string, name string) string {
startTag := "<gateway:experimental:description>"
endTag := "</gateway:experimental:description>"
if channel == "standard" && strings.Contains(description, "<gateway:experimental:description>") {
regexPattern := `\n*` + regexp.QuoteMeta(startTag) + `(?s:(.*?))` + regexp.QuoteMeta(endTag) + `\n*`
re := regexp.MustCompile(regexPattern)
match := re.FindStringSubmatch(description)
if len(match) != 2 {
log.Fatalf("Invalid <gateway:experimental:description> tag for %s", name)
}
description = re.ReplaceAllString(description, "\n\n")
} else {
description = strings.ReplaceAll(description, startTag, "")
description = strings.ReplaceAll(description, endTag, "")
}
// Comments within "gateway:util:excludeFromCRD" tag is not included in the generated CRD and all trailing \n operators before
// and after the tags are removed and replaced with three \n operators.
startTag = "<gateway:util:excludeFromCRD>"
endTag = "</gateway:util:excludeFromCRD>"
if strings.Contains(description, "<gateway:util:excludeFromCRD>") {
regexPattern := `\n*` + regexp.QuoteMeta(startTag) + `(?s:(.*?))` + regexp.QuoteMeta(endTag) + `\n*`
re := regexp.MustCompile(regexPattern)
match := re.FindStringSubmatch(description)
if len(match) != 2 {
log.Fatalf("Invalid <gateway:util:excludeFromCRD> tag for %s", name)
}
description = re.ReplaceAllString(description, "\n\n\n")
}
gatewayRe := regexp.MustCompile(`<gateway:.*>`)
description = gatewayRe.ReplaceAllLiteralString(description, "")
// Remove any extra \n (more than 3 and all trailing at the end)
regexPattern := `\n\n\n+`
re := regexp.MustCompile(regexPattern)
description = re.ReplaceAllString(description, "\n\n\n")
description = strings.Trim(description, "\n")
return description
}