forked from kubernetes-sigs/reference-docs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.go
670 lines (592 loc) · 17.9 KB
/
config.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
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
/*
Copyright 2016 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 api
import (
"flag"
"fmt"
"html"
"log"
"os"
"path/filepath"
"sort"
"strings"
"unicode"
"gopkg.in/yaml.v2"
"github.com/go-openapi/loads"
"github.com/go-openapi/spec"
)
var AllowErrors = flag.Bool("allow-errors", false, "If true, don't fail on errors.")
var WorkDir = flag.String("work-dir", "", "Working directory for the generator.")
var UseTags = flag.Bool("use-tags", false, "If true, use the openapi tags instead of the config yaml.")
var KubernetesRelease = flag.String("kubernetes-release", "", "Kubernetes release version.")
// Directory for output files
var BuildDir string
// Directory for configuration and data files
var ConfigDir string
// Directory for static sections
var SectionsDir string
// Directory for temporary files that will eventually get merged into the HTML output file.
var IncludesDir string
// Directory for versioned configuration file and swagger.json
var VersionedConfigDir string
func NewConfig() (*Config, error) {
// Initialize global directories
BuildDir = filepath.Join(*WorkDir, "build")
ConfigDir = filepath.Join(*WorkDir, "config")
IncludesDir = filepath.Join(BuildDir, "includes")
SectionsDir = filepath.Join(ConfigDir, "sections")
var k8sRelease = fmt.Sprintf("v%s", strings.ReplaceAll(*KubernetesRelease, ".", "_"))
VersionedConfigDir = filepath.Join(ConfigDir, k8sRelease)
config, err := LoadConfigFromYAML()
if err != nil {
return nil, fmt.Errorf("failed to load config yaml: %w", err)
}
specs, err := LoadOpenApiSpec()
if err != nil {
return nil, fmt.Errorf("failed to load openapi spec: %w", err)
}
// Parse spec version
ParseSpecInfo(specs, config)
// Set the spec version
config.SpecVersion = fmt.Sprintf("v%s.%s", *KubernetesRelease, "0")
// Initialize all of the operations
defs, err := NewDefinitions(config, specs)
if err != nil {
return nil, fmt.Errorf("failed to init definitions: %w", err)
}
config.Definitions = *defs
if *UseTags {
// Initialize the config and ToC from the tags on definitions
if err := config.genConfigFromTags(specs); err != nil {
return nil, fmt.Errorf("failed to generate config from tags: %w", err)
}
} else {
// Initialization for ToC resources only
if err := config.visitResourcesInToc(); err != nil {
return nil, fmt.Errorf("failed to visit resources in TOC: %w", err)
}
}
if err := config.initOperations(specs); err != nil {
return nil, fmt.Errorf("failed to init operations: %w", err)
}
// replace unicode escape sequences with HTML entities.
config.escapeDescriptions()
config.CleanUp()
// Prune anything that shouldn't be in the ToC
if *UseTags {
categories := []ResourceCategory{}
for _, c := range config.ResourceCategories {
resources := Resources{}
for _, r := range c.Resources {
if d, f := config.Definitions.GetByVersionKind(r.Group, r.Version, r.Name); f {
if d.InToc {
resources = append(resources, r)
}
}
}
c.Resources = resources
if len(resources) > 0 {
categories = append(categories, c)
}
}
config.ResourceCategories = categories
}
return config, nil
}
func (c *Config) genConfigFromTags(specs []*loads.Document) error {
log.Printf("Using OpenAPI extension tags to configure.")
// build the apis from the observed groups
groupsMap := map[ApiGroup]DefinitionList{}
for _, d := range c.Definitions.All {
if strings.HasSuffix(d.Name, "List") {
continue
}
if strings.HasSuffix(d.Name, "Status") {
continue
}
if strings.HasPrefix(d.Description(), "Deprecated. Please use") {
// Don't look at deprecated types
continue
}
if err := d.initExample(c); err != nil {
return fmt.Errorf("failed to init example: %w", err)
}
g := d.Group
groupsMap[g] = append(groupsMap[g], d)
}
groupsList := ApiGroups{}
for g := range groupsMap {
groupsList = append(groupsList, g)
}
sort.Sort(groupsList)
for _, g := range groupsList {
groupName := strings.Title(string(g))
c.ApiGroups = append(c.ApiGroups, ApiGroup(groupName))
rc := ResourceCategory{
Include: string(g),
Name: groupName,
}
defList := groupsMap[g]
sort.Sort(defList)
for _, d := range defList {
r := &Resource{
Name: d.Name,
Group: string(d.Group),
Version: string(d.Version),
Definition: d,
}
rc.Resources = append(rc.Resources, r)
}
c.ResourceCategories = append(c.ResourceCategories, rc)
}
return nil
}
func (config *Config) initOperationsFromTags(specs []*loads.Document) error {
if *UseTags {
ops := map[string]map[string][]*Operation{}
defs := map[string]*Definition{}
for _, d := range config.Definitions.All {
name := fmt.Sprintf("%s.%s.%s", d.Group, d.Version, d.GetResourceName())
defs[name] = d
}
VisitOperations(specs, func(operation Operation) {
if o, found := config.Operations[operation.ID]; found && o.Definition != nil {
return
}
op := operation
o := &op
config.Operations[operation.ID] = o
group, version, kind, sub := o.GetGroupVersionKindSub()
if sub == "status" {
return
}
if len(group) == 0 {
return
}
key := fmt.Sprintf("%s.%s.%s", group, version, kind)
o.Definition = defs[key]
// Index by group and subresource
if _, f := ops[key]; !f {
ops[key] = map[string][]*Operation{}
}
ops[key][sub] = append(ops[key][sub], o)
})
for key, subMap := range ops {
def := defs[key]
if def == nil {
return fmt.Errorf("Unable to locate resource %s in resource map\n%v\n", key, defs)
}
subs := []string{}
for s := range subMap {
subs = append(subs, s)
}
sort.Strings(subs)
for _, s := range subs {
cat := &OperationCategory{}
cat.Name = strings.Title(s) + " Operations"
for _, op := range subMap[s] {
ot := OperationType{}
ot.Name = op.GetMethod() + " " + strings.Title(s)
op.Type = ot
cat.Operations = append(cat.Operations, op)
}
def.OperationCategories = append(def.OperationCategories, cat)
}
}
}
return nil
}
// initOperations returns all Operations found in the Documents
func (c *Config) initOperations(specs []*loads.Document) error {
c.Operations = Operations{}
VisitOperations(specs, func(op Operation) {
c.Operations[op.ID] = &op
})
if err := c.mapOperationsToDefinitions(); err != nil {
return err
}
if err := c.initOperationsFromTags(specs); err != nil {
return err
}
VisitOperations(specs, func(target Operation) {
if op, ok := c.Operations[target.ID]; !ok || op.Definition == nil {
if !c.opExcluded(op.ID) {
fmt.Printf("\033[31mNo Definition found for %s [%s].\033[0m\n", op.ID, op.Path)
}
}
})
if err := c.initOperationParameters(specs); err != nil {
return err
}
// Clear the operations. We still have to calculate the operations because that is how we determine
// the API Group for each definition.
if !*BuildOps {
c.Operations = Operations{}
c.OperationCategories = []OperationCategory{}
for _, d := range c.Definitions.All {
d.OperationCategories = []*OperationCategory{}
}
}
return nil
}
func (c *Config) opExcluded(op string) bool {
for _, pattern := range c.ExcludedOperations {
if strings.Contains(op, pattern) {
return true
}
}
return false
}
// CleanUp sorts and dedups fields
func (c *Config) CleanUp() {
for _, d := range c.Definitions.All {
sort.Sort(d.AppearsIn)
sort.Sort(d.Fields)
dedup := SortDefinitionsByName{}
var last *Definition
for _, i := range d.AppearsIn {
if last != nil &&
i.Name == last.Name &&
i.Group.String() == last.Group.String() &&
i.Version.String() == last.Version.String() {
continue
}
last = i
dedup = append(dedup, i)
}
d.AppearsIn = dedup
}
}
// LoadConfigFromYAML reads the config yaml file into a struct
func LoadConfigFromYAML() (*Config, error) {
config := &Config{}
f := filepath.Join(VersionedConfigDir, "config.yaml")
contents, err := os.ReadFile(f)
if err != nil {
if !*UseTags {
return nil, fmt.Errorf("failed to read yaml file %s: %w", f, err)
}
} else if err = yaml.Unmarshal(contents, config); err != nil {
return nil, err
}
writeCategory := OperationCategory{
Name: "Write Operations",
OperationTypes: []OperationType{
{
Name: "Create",
Match: "create${group}${version}(Namespaced)?${resource}",
},
{
Name: "Create Eviction",
Match: "create${group}${version}(Namespaced)?${resource}Eviction",
},
{
Name: "Patch",
Match: "patch${group}${version}(Namespaced)?${resource}",
},
{
Name: "Replace",
Match: "replace${group}${version}(Namespaced)?${resource}",
},
{
Name: "Delete",
Match: "delete${group}${version}(Namespaced)?${resource}",
},
{
Name: "Delete Collection",
Match: "delete${group}${version}Collection(Namespaced)?${resource}",
},
},
}
readCategory := OperationCategory{
Name: "Read Operations",
OperationTypes: []OperationType{
{
Name: "Read",
Match: "read${group}${version}(Namespaced)?${resource}",
},
{
Name: "List",
Match: "list${group}${version}(Namespaced)?${resource}",
},
{
Name: "List All Namespaces",
Match: "list${group}${version}(Namespaced)?${resource}ForAllNamespaces",
},
{
Name: "Watch",
Match: "watch${group}${version}(Namespaced)?${resource}",
},
{
Name: "Watch List",
Match: "watch${group}${version}(Namespaced)?${resource}List",
},
{
Name: "Watch List All Namespaces",
Match: "watch${group}${version}(Namespaced)?${resource}ListForAllNamespaces",
},
},
}
statusCategory := OperationCategory{
Name: "Status Operations",
OperationTypes: []OperationType{
{
Name: "Patch Status",
Match: "patch${group}${version}(Namespaced)?${resource}Status",
},
{
Name: "Read Status",
Match: "read${group}${version}(Namespaced)?${resource}Status",
},
{
Name: "Replace Status",
Match: "replace${group}${version}(Namespaced)?${resource}Status",
},
},
}
ephemaralCategory := OperationCategory{
Name: "EphemeralContainers Operations",
OperationTypes: []OperationType{
{
Name: "Patch EphemeralContainers",
Match: "patch${group}${version}(Namespaced)?${resource}Ephemeralcontainers",
},
{
Name: "Read EphemeralContainers",
Match: "read${group}${version}(Namespaced)?${resource}Ephemeralcontainers",
},
{
Name: "Replace EphemeralContainers",
Match: "replace${group}${version}(Namespaced)?${resource}Ephemeralcontainers",
},
},
}
config.OperationCategories = append([]OperationCategory{writeCategory, readCategory, statusCategory, ephemaralCategory}, config.OperationCategories...)
return config, nil
}
const (
PATH = "path"
QUERY = "query"
BODY = "body"
)
func (c *Config) initOperationParameters(specs []*loads.Document) error {
s := c.Definitions
for _, op := range c.Operations {
pathItem := op.item
location := ""
var param spec.Parameter
var found bool
// Path parameters
for _, p := range pathItem.Parameters {
if p.In == "" {
paramID := strings.Split(p.Ref.String(), "/")[2]
swagger := specs[0].Spec()
if param, found = swagger.Parameters[paramID]; found {
location = param.In
}
} else {
location = p.In
param = p
}
switch location {
case PATH:
op.PathParams = append(op.PathParams, s.parameterToField(param))
case QUERY:
op.QueryParams = append(op.QueryParams, s.parameterToField(param))
case BODY:
op.BodyParams = append(op.BodyParams, s.parameterToField(param))
default:
return fmt.Errorf("unknown location %q", location)
}
}
// Query parameters
location = ""
for _, p := range op.op.Parameters {
if p.In == "" {
paramID := strings.Split(p.Ref.String(), "/")[2]
swagger := specs[0].Spec()
if param, found = swagger.Parameters[paramID]; found {
location = param.In
}
} else {
location = p.In
param = p
}
switch location {
case PATH:
op.PathParams = append(op.PathParams, s.parameterToField(param))
case QUERY:
op.QueryParams = append(op.QueryParams, s.parameterToField(param))
case BODY:
op.BodyParams = append(op.BodyParams, s.parameterToField(param))
default:
return fmt.Errorf("unknown location %q", location)
}
}
for code, response := range op.op.Responses.StatusCodeResponses {
if response.Schema == nil {
continue
}
r := &HttpResponse{
Field: Field{
Description: strings.ReplaceAll(response.Description, "\n", " "),
Type: GetTypeName(*response.Schema),
Name: fmt.Sprintf("%d", code),
},
Code: fmt.Sprintf("%d", code),
}
if IsComplex(*response.Schema) {
r.Definition, _ = s.GetForSchema(*response.Schema)
if r.Definition != nil {
r.Definition.FoundInOperation = true
}
}
op.HttpResponses = append(op.HttpResponses, r)
}
}
return nil
}
func (c *Config) getOperationGroupName(group string) string {
for k, v := range c.OperationGroupMap {
if strings.ToLower(group) == k {
return v
}
}
return strings.Title(group)
}
func (c *Config) getOperationId(match string, group string, version ApiVersion, kind string) string {
ver := []rune(string(version))
ver[0] = unicode.ToUpper(ver[0])
match = strings.ReplaceAll(match, "${group}", group)
match = strings.ReplaceAll(match, "${version}", string(ver))
match = strings.ReplaceAll(match, "${resource}", kind)
return match
}
func (c *Config) setOperation(match, namespace string, ot *OperationType, oc *OperationCategory, d *Definition) error {
key := strings.ReplaceAll(match, "(Namespaced)?", namespace)
if o, ok := c.Operations[key]; ok {
// Each operation should have exactly 1 definition
if o.Definition != nil {
return fmt.Errorf(
"Found multiple matching definitions [%s/%s/%s, %s/%s/%s] for operation key: %s",
d.Group, d.Version, d.Name, o.Definition.Group, o.Definition.Version, o.Definition.Name, key)
}
o.Type = *ot
o.Definition = d
if err := o.initExample(c); err != nil {
return fmt.Errorf("failed to init example: %w", err)
}
oc.Operations = append(oc.Operations, o)
// When using tags for the configuration, everything with an operation goes in the ToC
if *UseTags && !o.Definition.IsOldVersion {
o.Definition.InToc = true
}
}
return nil
}
// mapOperationsToDefinitions adds operations to the definitions they operate
func (c *Config) mapOperationsToDefinitions() error {
for _, d := range c.Definitions.All {
if d.IsInlined {
continue
}
// XXX: The TokenRequest definition has operation defined as "createCoreV1NamespacedServiceAccountToken"!
if d.Name == "TokenRequest" && d.Group.String() == "authentication" && d.Version == "v1" {
operationId := "createCoreV1NamespacedServiceAccountToken"
if o, ok := c.Operations[operationId]; ok {
ot := OperationType{
Name: "Create",
Match: "createCoreV1NamespacedServiceAccountToken",
}
oc := OperationCategory{
Name: "Write Operations",
OperationTypes: []OperationType{ot},
}
o.Definition = d
o.Definition.InToc = true
if err := o.initExample(c); err != nil {
return fmt.Errorf("failed to init example: %w", err)
}
oc.Operations = append(oc.Operations, o)
}
continue
}
for i := range c.OperationCategories {
oc := c.OperationCategories[i]
for j := range oc.OperationTypes {
ot := oc.OperationTypes[j]
groupName := c.getOperationGroupName(d.Group.String())
operationId := c.getOperationId(ot.Match, groupName, d.Version, d.Name)
if err := c.setOperation(operationId, "Namespaced", &ot, &oc, d); err != nil {
return err
}
if err := c.setOperation(operationId, "", &ot, &oc, d); err != nil {
return err
}
}
if len(oc.Operations) > 0 {
d.OperationCategories = append(d.OperationCategories, &oc)
}
}
}
return nil
}
// The OpenAPI spec has escape sequences like \u003c. When the spec is unmarshaled,
// the escape sequences get converted to ordinary characters. For example,
// \u003c gets converted to a regular < character. But we can't use regular <
// and > characters in our HTML document. This function replaces these regular
// characters with HTML entities: <, >, &, ', and ".
func (c *Config) escapeDescriptions() {
for _, d := range c.Definitions.All {
d.DescriptionWithEntities = html.EscapeString(d.Description())
for _, f := range d.Fields {
f.DescriptionWithEntities = html.EscapeString(f.Description)
}
}
for _, op := range c.Operations {
for _, p := range op.BodyParams {
p.DescriptionWithEntities = html.EscapeString(p.Description)
}
for _, p := range op.QueryParams {
p.DescriptionWithEntities = html.EscapeString(p.Description)
}
for _, p := range op.PathParams {
p.DescriptionWithEntities = html.EscapeString(p.Description)
}
for _, r := range op.HttpResponses {
r.DescriptionWithEntities = html.EscapeString(r.Description)
}
}
}
// For each resource in the ToC, look up its definition and visit it.
func (c *Config) visitResourcesInToc() error {
missing := false
for _, cat := range c.ResourceCategories {
for _, r := range cat.Resources {
if d, ok := c.Definitions.GetByVersionKind(r.Group, r.Version, r.Name); ok {
d.InToc = true // Mark as in Toc
if err := d.initExample(c); err != nil {
return fmt.Errorf("failed to init example: %w", err)
}
r.Definition = d
} else {
fmt.Printf("\033[31mCould not find definition for resource in TOC: %s %s %s.\033[0m\n", r.Group, r.Version, r.Name)
missing = true
}
}
}
if missing {
fmt.Printf("\033[36mAll known definitions: %v\033[0m\n", c.Definitions.All)
}
return nil
}