-
Notifications
You must be signed in to change notification settings - Fork 198
/
generate-types-file.go
575 lines (523 loc) · 18.4 KB
/
generate-types-file.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
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
// Copyright 2022 Google LLC
//
// 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"
"html/template"
"io/ioutil"
"log"
"os"
"os/exec"
"path"
"path/filepath"
"regexp"
"sort"
"strings"
iamv1beta1 "github.com/GoogleCloudPlatform/k8s-config-connector/pkg/apis/iam/v1beta1"
"github.com/GoogleCloudPlatform/k8s-config-connector/pkg/crd/crdloader"
"github.com/GoogleCloudPlatform/k8s-config-connector/pkg/crd/fielddesc"
"github.com/GoogleCloudPlatform/k8s-config-connector/pkg/k8s"
"github.com/GoogleCloudPlatform/k8s-config-connector/pkg/util/repo"
apiextensions "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
"k8s.io/klog/v2"
)
var handwrittenIAMTypes = []string{
iamv1beta1.IAMPolicyGVK.Kind,
iamv1beta1.IAMPartialPolicyGVK.Kind,
iamv1beta1.IAMPolicyMemberGVK.Kind,
iamv1beta1.IAMAuditConfigGVK.Kind,
}
type fieldProperties struct {
Name string
Type string
Description string
JSONName string
FullName string
Optional bool
UsePointer bool
}
type resourceDefinition struct {
Name string
Service string
Kind string
SpecFields []*fieldProperties
StatusFields []*fieldProperties
SpecNestedStructs map[string][]*fieldProperties
StatusNestedStructs map[string][]*fieldProperties
CRD *apiextensions.CustomResourceDefinition
Version *apiextensions.CustomResourceDefinitionVersion
}
type svkMap struct {
Service string
Version string
Kinds []string
Defs []resourceDefinition
}
func main() {
resources := make(map[string]*resourceDefinition)
registerKinds := make(map[string]*svkMap)
crdsDir := repo.GetCRDsPath()
crdsPath, err := filepath.Abs(crdsDir)
if err != nil {
log.Fatalf("error getting the absolute representation of path for directory '%v': %v", crdsDir, err)
}
crdFiles, err := ioutil.ReadDir(crdsPath)
if err != nil {
log.Fatalf("error reading directory '%v': %v", crdsPath, err)
}
for _, crdFile := range crdFiles {
fileName := strings.TrimSuffix(crdFile.Name(), ".yaml")
resources[fileName] = constructResourceDefinition(crdsPath, crdFile.Name())
}
for _, rd := range resources {
// organize resources by service/version, create folder if not present
serviceVersionString := fmt.Sprintf("%v/%v", rd.Service, rd.Version.Name)
if v, ok := registerKinds[serviceVersionString]; ok {
v.Kinds = append(v.Kinds, rd.Name)
v.Defs = append(v.Defs, *rd)
} else {
registerKinds[serviceVersionString] = &svkMap{
Service: rd.Service,
Version: rd.Version.Name,
Kinds: []string{rd.Name},
Defs: []resourceDefinition{*rd},
}
}
}
makeStructNamesUniquePerKind(registerKinds)
// clear out all generated types files
typesDir := repo.GetTypesGeneratedApisPath()
if err := os.RemoveAll(typesDir); err != nil {
log.Fatalf("error deleting dir %v: %v", typesDir, err)
}
if err := os.MkdirAll(typesDir, 0700); err != nil {
log.Fatalf("error recreating dir %v: %v", typesDir, err)
}
// template execution
typesTemplateDir := repo.GetTypesTemplatePath()
// typesTemplateFile := path.Join(typesTemplateDir, "general_types.go.tmpl")
for _, rd := range resources {
serviceDir := path.Join(typesDir, rd.Service)
checkAndCreateFolder(serviceDir)
serviceVersionDir := path.Join(serviceDir, rd.Version.Name)
checkAndCreateFolder(serviceVersionDir)
serviceVersionString := fmt.Sprintf("%v/%v", rd.Service, rd.Version.Name)
// create new file for generated types file
typesFileName := fmt.Sprintf("%s_types.go", rd.Kind)
{
var gen GeneralTypes
gen.resourceDefinition = rd
gen.Generate()
if err := gen.WriteToFile(path.Join(serviceVersionDir, typesFileName)); err != nil {
log.Fatalf("error creating %s_types.go file: %v", rd.Kind, err)
}
}
// executeTemplateWithResourceDefinition(f, typesTemplateFile, rd)
// create doc.go file per service/version directory
f, err := os.Create(path.Join(serviceVersionDir, "doc.go"))
if err != nil {
log.Fatalf("error creating %v doc.go file: %v", serviceVersionString, err)
}
docTemplateFile := path.Join(typesTemplateDir, "doc.go.tmpl")
executeTemplateWithResourceDefinition(f, docTemplateFile, rd)
// create group.go file per service directory
f, err = os.Create(path.Join(serviceDir, "group.go"))
if err != nil {
log.Fatalf("error creating %v group.go file: %v", rd.Service, err)
}
groupTemplateFile := path.Join(typesTemplateDir, "group.go.tmpl")
executeTemplateWithResourceDefinition(f, groupTemplateFile, rd)
}
// create register.go file per service directory
for _, registerInfo := range registerKinds {
sort.Strings(registerInfo.Kinds)
serviceVersionDir := path.Join(typesDir, registerInfo.Service, registerInfo.Version)
f, err := os.Create(path.Join(serviceVersionDir, "register.go"))
if err != nil {
log.Fatalf("error creating %v register.go file: %v", registerInfo.Service, err)
}
registerTemplateFile := path.Join(typesTemplateDir, "register.go.tmpl")
executeTemplateWithResourceDefinition(f, registerTemplateFile, registerInfo)
}
// Copy over common package `k8s` to generated/pkg/apis/ folder
// The `k8s` package has the shared `conditions` type definition
k8sDir := path.Join(repo.GetClientGenerationPath(), "k8s")
cmd := exec.Command("cp", "-r", k8sDir, typesDir)
if err := cmd.Run(); err != nil {
log.Fatalf("Error executing 'cp' command: %v", err)
}
}
func makeStructNamesUniquePerKind(kindMap map[string]*svkMap) {
for _, m := range kindMap { // Loop through each service/version entry
for _, r := range m.Defs { // Loop through each resource kind entry
// Because we will be modifying the nestedFields map, we need to decouple
// the existing field names from the map pointer so that fields aren't
// being renamed multiple times
nestedSpecFields := getArrayOfNestedFieldKeys(r.SpecNestedStructs)
nestedStatusFields := getArrayOfNestedFieldKeys(r.StatusNestedStructs)
// Remove Service from Kind because Kinds will be unique in each service's packages
resourceKind := strings.TrimPrefix(r.Kind, r.Service)
for _, s := range nestedSpecFields {
newStructName := fmt.Sprintf("%v%v", strings.Title(resourceKind), strings.Title(s))
findAndReplaceInStructField(s, newStructName, r.SpecFields)
findAndReplaceInNestedFields(s, newStructName, r.SpecNestedStructs)
}
for _, s := range nestedStatusFields {
newStructName := fmt.Sprintf("%v%v%v", strings.Title(resourceKind), strings.Title(s), "Status")
findAndReplaceInStructField(s, newStructName, r.StatusFields)
findAndReplaceInNestedFields(s, newStructName, r.StatusNestedStructs)
}
}
}
}
func getArrayOfNestedFieldKeys(m map[string][]*fieldProperties) []string {
arr := make([]string, 0)
for k := range m {
arr = append(arr, k)
}
return arr
}
func findAndReplaceInStructField(old, new string, fields []*fieldProperties) {
for i, f := range fields {
if f.Name == old {
switch f.Type {
case "string", "bool", "int": // is literal, don't replace
continue
default:
if strings.HasPrefix(f.Type, "[]") { // Type is list of object
f.Type = fmt.Sprintf("[]%v", new)
} else if strings.HasPrefix(f.Type, "map[string]") {
f.Type = fmt.Sprintf("map[string]%v", new)
} else {
f.Type = new
}
}
}
fields[i] = f
}
}
func findAndReplaceInNestedFields(old, new string, fieldMap map[string][]*fieldProperties) {
for name, children := range fieldMap {
if name == old {
fieldMap[new] = children
delete(fieldMap, old)
}
// Replace in field type in nested struct
findAndReplaceInStructField(old, new, children)
}
}
func executeTemplateWithResourceDefinition(file *os.File, filePath string, r interface{}) {
tmpl, err := template.ParseFiles(filePath)
if err != nil {
log.Fatalf("parsing template file failed: %v", err)
}
if err := tmpl.Execute(file, r); err != nil {
log.Fatalf("template execution failed: %v", err)
}
}
func checkAndCreateFolder(dir string) {
if _, err := os.Stat(dir); err != nil {
if err := os.Mkdir(dir, 0700); err != nil {
log.Fatalf("error creating folder %v: %v", dir, err)
}
}
}
func constructResourceDefinition(crdsPath, crdFile string) *resourceDefinition {
r := &resourceDefinition{}
crdFilePath, err := filepath.Abs(path.Join(crdsPath, crdFile))
if err != nil {
log.Fatalf("error getting the absolute representation of path for directory '%v': %v", crdFile, err)
}
crd, err := crdloader.FileToCRD(crdFilePath)
if err != nil {
log.Fatalf("error loading crd from filepath %v: %v", crdFilePath, err)
}
r.CRD = crd
r.Name = crd.Spec.Names.Kind
if err = buildFieldProperties(r, crd); err != nil {
log.Fatalf("error building field properties for %v: %v", r.Name, err)
}
r.Service = strings.TrimSuffix(crd.Spec.Group, k8s.APIDomainSuffix)
r.Kind = strings.ToLower(crd.Spec.Names.Kind)
// TODO: Should we handle multiple versions?
r.Version = &crd.Spec.Versions[0]
return r
}
func buildFieldProperties(r *resourceDefinition, crd *apiextensions.CustomResourceDefinition) error {
specDesc := fielddesc.GetSpecDescription(crd)
specDescriptions := dropRootAndFlattenChildrenDescriptions(specDesc)
r.SpecNestedStructs = make(map[string][]*fieldProperties)
organizeSpecFieldDescriptions(specDescriptions, r)
statusDesc, err := fielddesc.GetStatusDescription(crd)
if err != nil {
return fmt.Errorf("error getting status descriptions: %w", err)
}
statusDescriptions := dropRootAndFlattenChildrenDescriptions(statusDesc)
r.StatusNestedStructs = make(map[string][]*fieldProperties)
organizeStatusFieldDescriptions(statusDescriptions, r)
return nil
}
func organizeSpecFieldDescriptions(descriptions []fielddesc.FieldDescription, r *resourceDefinition) {
for _, d := range descriptions {
if d.ShortName == "[]" { // Field is just flatted from an array, ignore
continue
}
isRef := isResourceReference(d)
isSec := isSecretReference(d)
if !isRef && !isSec { // Field is NOT a resourceRef, add children to nested structs
if d.Type == "object" { // Field most likely has nested fields
children := getChildrenFromDescription(d, r)
r.SpecNestedStructs[strings.Title(d.ShortName)] = children
}
if d.Type == "list (object)" {
children := getChildrenFromDescription(d.Children[0], r)
r.SpecNestedStructs[strings.Title(d.ShortName)] = children
}
if d.Type == "map (key: string, value: object)" {
additionalProperties := getAdditionalPropertiesFromDescription(d, r)
r.SpecNestedStructs[strings.Title(d.ShortName)] = additionalProperties
}
}
if len(d.FullName) > 2 {
continue //field is nested & should not be listed in first-layer spec
}
r.SpecFields = append(r.SpecFields, fieldDescriptionToFieldProperties(d, isRef, isSec, r))
}
}
func isResourceReference(d fielddesc.FieldDescription) bool {
if d.ShortName == "secretKeyRef" {
return false
}
if strings.HasSuffix(d.ShortName, "Ref") {
return true
}
if len(d.Children) == 3 {
for _, c := range d.Children {
r := regexp.MustCompile("external|name|namespace")
if r.MatchString(c.ShortName) {
continue
}
return false
}
return true
}
if len(d.Children) == 1 && d.Children[0].ShortName == "[]" {
return isResourceReference(d.Children[0])
}
return false
}
func isSecretReference(d fielddesc.FieldDescription) bool {
if len(d.Children) == 2 {
for _, c := range d.Children {
if c.ShortName == "name" || c.ShortName == "key" {
continue
}
return false
}
return d.ShortName == "secretKeyRef"
}
return false
}
func getChildrenFromDescription(d fielddesc.FieldDescription, r *resourceDefinition) []*fieldProperties {
children := make([]*fieldProperties, 0)
for _, c := range d.Children {
children = append(children, fieldDescriptionToFieldProperties(c, isResourceReference(c), isSecretReference(c), r))
}
return children
}
func getAdditionalPropertiesFromDescription(d fielddesc.FieldDescription, r *resourceDefinition) []*fieldProperties {
additionalProperties := make([]*fieldProperties, 0)
for _, c := range d.AdditionalProperties {
additionalProperties = append(additionalProperties, fieldDescriptionToFieldProperties(c, isResourceReference(c), isSecretReference(c), r))
}
return additionalProperties
}
func organizeStatusFieldDescriptions(descriptions []fielddesc.FieldDescription, r *resourceDefinition) {
for _, d := range descriptions {
if d.ShortName == "conditions" {
continue // not defined in types file
}
if d.ShortName == "[]" {
continue
}
isRef := isResourceReference(d)
isSec := isSecretReference(d)
if !isRef && !isSec {
if d.Type == "object" {
children := getChildrenFromDescription(d, r)
r.StatusNestedStructs[strings.Title(d.ShortName)] = children
}
if d.Type == "list (object)" {
children := getChildrenFromDescription(d.Children[0], r)
r.StatusNestedStructs[strings.Title(d.ShortName)] = children
}
if d.Type == "map (key: string, value: object)" {
additionalProperties := getAdditionalPropertiesFromDescription(d, r)
r.StatusNestedStructs[strings.Title(d.ShortName)] = additionalProperties
}
}
if len(d.FullName) > 2 {
continue // field is nested
}
r.StatusFields = append(r.StatusFields, fieldDescriptionToFieldProperties(d, isRef, isSec, r))
}
}
func fieldDescriptionToFieldProperties(desc fielddesc.FieldDescription, isRef bool, isSec bool, r *resourceDefinition) *fieldProperties {
var isIAMRef bool
if isRef {
// Check if resource is IAMPolicy/PolicyMember/AuditConfig and modify ref to use IAMRef struct
for _, v := range handwrittenIAMTypes {
if r.Name == v {
isIAMRef = true
break
}
}
}
fp := &fieldProperties{
FullName: formatName(desc),
Type: formatType(desc, isRef, isSec, isIAMRef),
Description: desc.Description,
Name: strings.Title(desc.ShortName), // Field name UpperCamelCase
JSONName: fmt.Sprintf("%v", desc.ShortName), // ShortName is default lowerCamelCase, exclude omitempty unless the field is optional
}
// If fields are optional, they should use a pointer type in the Go definition (e.g. AwesomeFlag *SomeFlag) or have a built-in nil value (e.g. maps and slices).
// See https://github.com/kubernetes/community/blob/master/contributors/devel/sig-architecture/api-conventions.md#optional-vs-required for details.
requirementLevel := formatRequirementLevel(desc)
if requirementLevel == "Optional" {
fp.Optional = true
fp.JSONName = fmt.Sprintf("%v,omitempty", desc.ShortName)
if !isMapOrSliceType(fp.Type) {
fp.UsePointer = true
}
}
fixFieldDescriptionContainsClosingTags(fp)
return fp
}
func fixFieldDescriptionContainsClosingTags(fp *fieldProperties) {
// If the field description contains "*/" substring, it will break golang comment syntax when placing in block comments /* ${field_description} */
// TODO(b/191413779): As a temporary hacky workaround, we insert a space into "*/" and need to find a more proper way to address this.
fp.Description = strings.ReplaceAll(fp.Description, "*/", "* /")
}
func isMapOrSliceType(t string) bool {
if strings.HasPrefix(t, "[]") {
return true
}
if strings.HasPrefix(t, "map[string]") {
return true
}
return false
}
func formatName(desc fielddesc.FieldDescription) string {
name := strings.Join(desc.FullName, ".")
name = strings.TrimPrefix(name, "spec.")
name = strings.TrimPrefix(name, "status.")
return name
}
func formatType(desc fielddesc.FieldDescription, isRef, isSec, isIAMRef bool) string {
switch desc.Type {
case "boolean":
return "bool"
case "integer":
switch desc.Format {
case "int64":
return "int64"
case "":
// The default is int64 (and not int, we don't want the schema to vary across architectures)
return "int64"
default:
klog.Fatalf("unhandled case in formatType: %+v", desc)
return ""
}
case "float", "number":
return "float64"
case "object":
if isSec {
return "v1alpha1.SecretKeyRef"
}
if isRef {
if isIAMRef {
return "v1alpha1.IAMResourceRef"
}
return "v1alpha1.ResourceRef"
}
return strings.Title(desc.ShortName)
default:
if strings.HasPrefix(desc.Type, "list (") {
listType := strings.TrimSuffix(strings.TrimPrefix(desc.Type, "list ("), ")")
switch listType {
case "boolean", "string", "number", "integer":
return fmt.Sprintf("[]%v", formatToGoLiteral(listType))
default:
if isRef {
return fmt.Sprintf("[]v1alpha1.ResourceRef")
}
return fmt.Sprintf("[]%v", strings.Title(desc.ShortName))
}
}
if strings.HasPrefix(desc.Type, "map (") {
// map (key: string, value: <literal>) -> map[string]<literal>
literal := regexp.MustCompile("map \\(key: string, value: (.*?)\\)").FindStringSubmatch(desc.Type)
valueType := literal[1]
var goType string
if valueType == "object" {
goType = strings.Title(desc.ShortName)
} else {
goType = formatToGoLiteral(valueType)
}
return fmt.Sprintf("map[string]%v", goType)
}
return desc.Type
}
}
func formatToGoLiteral(t string) string {
switch t {
case "string":
return "string"
case "boolean":
return "bool"
case "integer":
return "int64"
case "float", "number":
return "float64"
default:
panic(fmt.Errorf("expected a JSONLiteral but got %v", t))
}
}
// TODO(kcc-eng): Add feature to expose field requirement level
func formatRequirementLevel(desc fielddesc.FieldDescription) string {
switch desc.RequirementLevel {
case fielddesc.RequiredRequirementLevel:
return "Required"
case fielddesc.RequiredWhenParentPresentRequirementLevel:
return "Required*"
case fielddesc.OptionalRequirementLevel:
return "Optional"
default:
panic(fmt.Errorf("unhandled requirement level: %v", desc.RequirementLevel))
}
}
func dropRootAndFlattenChildrenDescriptions(rootDesc fielddesc.FieldDescription) []fielddesc.FieldDescription {
result := flattenChildrenDescription(nil, rootDesc)
return result[1:]
}
func flattenChildrenDescription(result []fielddesc.FieldDescription, fd fielddesc.FieldDescription) []fielddesc.FieldDescription {
if result == nil {
result = make([]fielddesc.FieldDescription, 0, 1)
}
result = append(result, fd)
for _, child := range fd.Children {
result = flattenChildrenDescription(result, child)
}
return result
}