forked from gravitational/teleport
/
schemagen.go
540 lines (486 loc) · 17.3 KB
/
schemagen.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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
/*
* Teleport
* Copyright (C) 2023 Gravitational, Inc.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package main
import (
"fmt"
"regexp"
"slices"
"strings"
"github.com/dustin/go-humanize/english"
"github.com/gravitational/trace"
apiextv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
crdtools "sigs.k8s.io/controller-tools/pkg/crd"
crdmarkers "sigs.k8s.io/controller-tools/pkg/crd/markers"
"sigs.k8s.io/controller-tools/pkg/loader"
"sigs.k8s.io/controller-tools/pkg/markers"
)
const (
k8sKindPrefix = "Teleport"
statusPackagePath = "github.com/gravitational/teleport/integrations/operator/apis"
statusPackageName = "resources"
statusPackage = statusPackagePath + "/" + statusPackageName
statusTypeName = "Status"
)
// Add names to this array when adding support to new Teleport resources that could conflict with Kubernetes
var (
kubernetesReservedNames = []string{"role"}
regexpResourceName = regexp.MustCompile(`^([A-Za-z]+)(V[0-9]+)?$`)
)
// SchemaGenerator generates the OpenAPI v3 schema from a proto file.
type SchemaGenerator struct {
groupName string
memo map[string]*Schema
roots map[string]*RootSchema
}
// RootSchema is a wrapper for a message we are generating a schema for.
type RootSchema struct {
groupName string
versions []SchemaVersion
name string
pluralName string
// teleportKind is the kind of the Teleport resource
teleportKind string
// kubernetesKind is the kind of the Kubernetes resource. This is the
// teleportKind, prefixed by "Teleport" and potentially suffixed by the
// version. Since v15, resources with multiple versions are exposed through
// different kinds. At some point we will suffix all kinds by the version
// and deprecate the old resources.
kubernetesKind string
}
type SchemaVersion struct {
// Version is the Kubernetes CR API version. For single-version
// Teleport resource, this is equal to the Teleport resource Version for
// compatibility purposes. For multi-version resource, the value is always
// "v1" as the version is already in the CR kind.
Version string
Schema *Schema
}
// Schema is a set of object properties.
type Schema struct {
apiextv1.JSONSchemaProps
built bool
}
func (s *Schema) DeepCopy() *Schema {
return &Schema{
JSONSchemaProps: *s.JSONSchemaProps.DeepCopy(),
built: s.built,
}
}
func NewSchemaGenerator(groupName string) *SchemaGenerator {
return &SchemaGenerator{
groupName: groupName,
memo: make(map[string]*Schema),
roots: make(map[string]*RootSchema),
}
}
func NewSchema() *Schema {
return &Schema{JSONSchemaProps: apiextv1.JSONSchemaProps{
Type: "object",
Properties: make(map[string]apiextv1.JSONSchemaProps),
}}
}
type resourceSchemaConfig struct {
versionOverride string
customSpecFields []string
kindContainsVersion bool
}
type resourceSchemaOption func(*resourceSchemaConfig)
func withVersionOverride(version string) resourceSchemaOption {
return func(cfg *resourceSchemaConfig) {
cfg.versionOverride = version
}
}
// set this onlt on new multi-version resources
func withVersionInKindOverride() resourceSchemaOption {
return func(cfg *resourceSchemaConfig) {
cfg.kindContainsVersion = true
}
}
func withCustomSpecFields(customSpecFields []string) resourceSchemaOption {
return func(cfg *resourceSchemaConfig) {
cfg.customSpecFields = customSpecFields
}
}
func (generator *SchemaGenerator) addResource(file *File, name string, opts ...resourceSchemaOption) error {
var cfg resourceSchemaConfig
for _, opt := range opts {
opt(&cfg)
}
rootMsg, ok := file.messageByName[name]
if !ok {
return trace.NotFound("resource %q is not found", name)
}
var schema *Schema
if len(cfg.customSpecFields) > 0 {
schema = NewSchema()
for _, fieldName := range cfg.customSpecFields {
field, ok := rootMsg.GetField(fieldName)
if !ok {
return trace.NotFound("field %q not found", fieldName)
}
var err error
schema.Properties[fieldName], err = generator.prop(field)
if err != nil {
return trace.Wrap(err)
}
}
} else {
// We check both "Spec" with a capital S, and "spec" in lower case.
specField, ok := rootMsg.GetField("Spec")
if !ok {
specField, ok = rootMsg.GetField("spec")
if !ok {
return trace.NotFound("message %q does not have Spec field", name)
}
}
specMsg := specField.TypeMessage()
if specMsg == nil {
return trace.NotFound("message %q Spec type is not a message", name)
}
var err error
schema, err = generator.traverseInner(specMsg)
if err != nil {
return trace.Wrap(err)
}
}
schema = schema.DeepCopy()
resourceKind, resourceVersion, err := parseKindAndVersion(rootMsg)
if err != nil {
return trace.Wrap(err)
}
if cfg.versionOverride != "" {
resourceVersion = cfg.versionOverride
}
kubernetesKind := resourceKind
if cfg.kindContainsVersion {
kubernetesKind = resourceKind + strings.ToUpper(resourceVersion)
}
schema.Description = fmt.Sprintf("%s resource definition %s from Teleport", resourceKind, resourceVersion)
root, ok := generator.roots[kubernetesKind]
if !ok {
pluralName := strings.ToLower(english.PluralWord(2, resourceKind, ""))
if cfg.kindContainsVersion {
pluralName = pluralName + resourceVersion
}
root = &RootSchema{
groupName: generator.groupName,
teleportKind: resourceKind,
kubernetesKind: kubernetesKind,
name: strings.ToLower(kubernetesKind),
pluralName: pluralName,
}
generator.roots[kubernetesKind] = root
}
// For legacy CRs with a single version, we use the Teleport version as the
// Kubernetes API version
kubernetesVersion := resourceVersion
if cfg.kindContainsVersion {
// For new multi-version resources we always set the version to "v1" as
// the Teleport version is also in the CR kind.
kubernetesVersion = "v1"
}
root.versions = append(root.versions, SchemaVersion{
Version: kubernetesVersion,
Schema: schema,
})
return nil
}
func parseKindAndVersion(message *Message) (string, string, error) {
msgName := message.Name()
res := regexpResourceName.FindStringSubmatch(msgName)
if len(res) == 0 {
return "", "", trace.Errorf("failed to parse resource name and version from %s message name", msgName)
}
return res[1], strings.ToLower(res[2]), nil
}
func (generator *SchemaGenerator) traverseInner(message *Message) (*Schema, error) {
name := message.Name()
if schema, ok := generator.memo[name]; ok {
if !schema.built {
return nil, trace.Errorf("circular dependency in the %s", message.Name())
}
return schema, nil
}
schema := NewSchema()
generator.memo[name] = schema
for _, field := range message.Fields {
if _, ok := ignoredFields[message.Name()][field.Name()]; ok {
continue
}
jsonName := field.JSONName()
if jsonName == "" {
handled := handleEmptyJSONTag(schema, message, field)
if !handled {
return nil, trace.Errorf("empty json tag for %s.%s", message.Name(), field.Name())
}
continue
}
if jsonName == "-" {
continue
}
var err error
schema.Properties[jsonName], err = generator.prop(field)
if err != nil {
return nil, trace.Wrap(err)
}
}
schema.built = true
return schema, nil
}
// handleEmptyJSONTag attempts to handle special case fields that have
// an empty JSON tag. True is returned if the field was handled and a
// new schema property was created.
func handleEmptyJSONTag(schema *Schema, message *Message, field *Field) bool {
if field.Name() != "MaxAge" && message.Name() != "OIDCConnectorSpecV3" {
return false
}
// Handle MaxAge as a special case. It's type is a message that is embedded.
// Because the message is embedded, MaxAge itself explicitly sets its json
// name to an empty string, but the embedded message type has a single field
// with a json name, so use that instead.
schema.Properties["max_age"] = apiextv1.JSONSchemaProps{
Description: field.LeadingComments(),
Type: "string",
Format: "duration",
}
return true
}
func (generator *SchemaGenerator) prop(field *Field) (apiextv1.JSONSchemaProps, error) {
prop := apiextv1.JSONSchemaProps{Description: field.LeadingComments()}
// Known overrides: we broke the link between the go struct and the protobuf message.
// As we have no guarantee they're identical anymore (they are not) we need
// to manually maintain a list of mappings. This is not maintainable on the
// long term and this defeats the purpose of the generators, but we didn't
// have the time yet to revamp this.
// Traits are represented as map[string][]string in go,
// and as []struct{key string, values []string} in protobuf.
if field.IsRepeated() && field.TypeName() == ".teleport.trait.v1.Trait" {
prop.Type = "object"
prop.AdditionalProperties = &apiextv1.JSONSchemaPropsOrBool{
Schema: &apiextv1.JSONSchemaProps{
Type: "array",
Items: &apiextv1.JSONSchemaPropsOrArray{Schema: &apiextv1.JSONSchemaProps{Type: "string"}},
},
}
return prop, nil
}
// Labels are relying on `utils.Strings`, which can either marshall as an array of strings or a single string
// This does not pass Schema validation from the apiserver, to workaround we don't specify type for those fields
// and ask Kubernetes to preserve unknown fields.
if field.CustomType() == "Labels" {
prop.Type = "object"
preserveUnknownFields := true
prop.AdditionalProperties = &apiextv1.JSONSchemaPropsOrBool{
Schema: &apiextv1.JSONSchemaProps{
XPreserveUnknownFields: &preserveUnknownFields,
},
}
return prop, nil
}
// Regular treatment
if field.IsRepeated() && !field.IsMap() {
prop.Type = "array"
prop.Items = &apiextv1.JSONSchemaPropsOrArray{
Schema: &apiextv1.JSONSchemaProps{},
}
if err := generator.singularProp(field, prop.Items.Schema); err != nil {
return prop, trace.Wrap(err)
}
} else {
if err := generator.singularProp(field, &prop); err != nil {
return prop, trace.Wrap(err)
}
}
if field.IsNullable() && (prop.Type == "array" || prop.Type == "object") {
prop.Nullable = true
}
return prop, nil
}
func (generator *SchemaGenerator) singularProp(field *Field, prop *apiextv1.JSONSchemaProps) error {
switch {
case field.IsBool():
prop.Type = "boolean"
case field.IsString():
prop.Type = "string"
case field.IsDuration():
prop.Type = "string"
prop.Format = "duration"
case field.IsTime():
prop.Type = "string"
prop.Format = "date-time"
case field.IsInt32() || field.IsUint32():
prop.Type = "integer"
prop.Format = "int32"
case field.desc.IsEnum():
prop.XIntOrString = true
case field.IsInt64() || field.IsUint64():
prop.Type = "integer"
prop.Format = "int64"
case field.TypeName() == ".wrappers.LabelValues":
prop.Type = "object"
prop.AdditionalProperties = &apiextv1.JSONSchemaPropsOrBool{
Schema: &apiextv1.JSONSchemaProps{
Type: "array",
Items: &apiextv1.JSONSchemaPropsOrArray{Schema: &apiextv1.JSONSchemaProps{Type: "string"}},
},
}
case field.TypeName() == ".wrappers.StringValues":
prop.Type = "array"
prop.Items = &apiextv1.JSONSchemaPropsOrArray{
Schema: &apiextv1.JSONSchemaProps{Type: "string"},
}
case field.TypeName() == ".types.CertExtensionType" || field.TypeName() == ".types.CertExtensionMode":
prop.Type = "integer"
prop.Format = "int32"
case strings.HasSuffix(field.TypeName(), ".v1.LoginRule.TraitsMapEntry"):
prop.Type = "object"
prop.AdditionalProperties = &apiextv1.JSONSchemaPropsOrBool{
Schema: &apiextv1.JSONSchemaProps{
Type: "array",
Items: &apiextv1.JSONSchemaPropsOrArray{Schema: &apiextv1.JSONSchemaProps{Type: "string"}},
},
}
case field.IsMessage():
inner := field.TypeMessage()
if inner == nil {
return trace.Errorf("failed to get type for %s.%s", field.Message().Name(), field.Name())
}
schema, err := generator.traverseInner(inner)
if err != nil {
return trace.Wrap(err)
}
prop.Type = "object"
prop.Properties = schema.Properties
case field.CastType() != "":
return trace.Errorf("unsupported casttype %s.%s", field.Message().Name(), field.Name())
case field.CustomType() != "":
return trace.Errorf("unsupported customtype %s.%s", field.Message().Name(), field.Name())
default:
return trace.Errorf("unsupported %s.%s", field.Message().Name(), field.Name())
}
return nil
}
func (root RootSchema) CustomResourceDefinition() (apiextv1.CustomResourceDefinition, error) {
crd := apiextv1.CustomResourceDefinition{
TypeMeta: metav1.TypeMeta{
APIVersion: apiextv1.SchemeGroupVersion.String(),
Kind: "CustomResourceDefinition",
},
ObjectMeta: metav1.ObjectMeta{
Name: fmt.Sprintf("%s.%s", strings.ToLower(k8sKindPrefix+root.pluralName), root.groupName),
},
Spec: apiextv1.CustomResourceDefinitionSpec{
Group: root.groupName,
Names: apiextv1.CustomResourceDefinitionNames{
Kind: k8sKindPrefix + root.kubernetesKind,
ListKind: k8sKindPrefix + root.kubernetesKind + "List",
Plural: strings.ToLower(k8sKindPrefix + root.pluralName),
Singular: strings.ToLower(k8sKindPrefix + root.name),
ShortNames: root.getShortNames(),
},
Scope: apiextv1.NamespaceScoped,
},
}
// This part parses the types not coming from the protobuf (the status)
// We instantiate a parser, load the relevant packages in it and look for
// the package we need. The package is then loaded to the parser, a schema is
// generated and used in the CRD
registry := &markers.Registry{}
// CRD markers contain special markers used by the parser to discover properties
// e.g. `+kubebuilder:validation:Minimum=0`
err := crdmarkers.Register(registry)
if err != nil {
return apiextv1.CustomResourceDefinition{},
trace.Wrap(err, "adding CRD markers to the registry")
}
parser := &crdtools.Parser{
Collector: &markers.Collector{Registry: registry},
Checker: &loader.TypeChecker{},
}
// Some types are special and require manual overrides, like metav1.Time.
crdtools.AddKnownTypes(parser)
// Status does not exist in Teleport, only in the CR.
// We parse go's AST to find its struct and convert it in a schema.
statusSchema, err := getStatusSchema(parser)
if err != nil {
return apiextv1.CustomResourceDefinition{},
trace.Wrap(err, "getting status schema from go's AST")
}
for i, schemaVersion := range root.versions {
schema := schemaVersion.Schema
crd.Spec.Versions = append(crd.Spec.Versions, apiextv1.CustomResourceDefinitionVersion{
Name: schemaVersion.Version,
Served: true,
// Storage the first version available.
Storage: i == 0,
Subresources: &apiextv1.CustomResourceSubresources{
Status: &apiextv1.CustomResourceSubresourceStatus{},
},
Schema: &apiextv1.CustomResourceValidation{
OpenAPIV3Schema: &apiextv1.JSONSchemaProps{
Type: "object",
Description: fmt.Sprintf("%s is the Schema for the %s API", root.kubernetesKind, root.pluralName),
Properties: map[string]apiextv1.JSONSchemaProps{
"apiVersion": {
Type: "string",
Description: "APIVersion defines the versioned schema of this representation of an object. Servers should convert recognized schemas to the latest internal value, and may reject unrecognized values. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#resources",
},
"kind": {
Type: "string",
Description: "Kind is a string value representing the REST resource this object represents. Servers may infer this from the endpoint the client submits requests to. Cannot be updated. In CamelCase. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#types-kinds",
},
"metadata": {Type: "object"},
"spec": schema.JSONSchemaProps,
"status": statusSchema,
},
},
},
})
}
return crd, nil
}
// getShortNames returns the schema short names while ensuring they won't conflict with existing Kubernetes resources
// See https://github.com/gravitational/teleport/issues/17587 and https://github.com/kubernetes/kubernetes/issues/113227
func (root RootSchema) getShortNames() []string {
if slices.Contains(kubernetesReservedNames, root.name) {
return []string{}
}
return []string{root.name, root.pluralName}
}
func getStatusSchema(parser *crdtools.Parser) (apiextv1.JSONSchemaProps, error) {
pkgs, err := loader.LoadRoots(statusPackage)
if err != nil {
// Loader errors might be non-critical.
// e.g. the loader complains about the unknown "toolchain" directive in our go mod
fmt.Printf("loader error: %s", err)
}
var statusType crdtools.TypeIdent
for _, pkg := range pkgs {
if pkg.Name == "resources" {
parser.NeedPackage(pkg)
statusType = crdtools.TypeIdent{
Package: pkg,
Name: statusTypeName,
}
parser.NeedFlattenedSchemaFor(statusType)
return parser.FlattenedSchemata[statusType], nil
}
}
return apiextv1.JSONSchemaProps{}, trace.NotFound("Package %q not found, cannot generate status JSON Schema", statusPackage)
}