...
1
16
17 package main
18
19 import (
20 "fmt"
21 "os"
22 "regexp"
23 "strings"
24
25 flag "github.com/spf13/pflag"
26 kruntime "k8s.io/apimachinery/pkg/runtime"
27 "k8s.io/apimachinery/pkg/util/sets"
28 "k8s.io/klog/v2"
29 )
30
31 var (
32 typeSrc = flag.StringP("type-src", "s", "", "From where we are going to read the types")
33 re = regexp.MustCompile("`(\\b\\w+\\b)`")
34 )
35
36
37 type kubeTypesMap map[string]kruntime.Pair
38
39 func main() {
40 flag.Parse()
41
42 if *typeSrc == "" {
43 klog.Fatalf("Please define -s flag as it is the api type file")
44 }
45
46 docsForTypes := kruntime.ParseDocumentationFrom(*typeSrc)
47 rc := false
48
49 for _, ks := range docsForTypes {
50 typesMap := make(kubeTypesMap)
51
52 for _, p := range ks[1:] {
53
54 if p.Name != "" {
55 typesMap[strings.ToLower(p.Name)] = p
56 }
57 }
58
59 structName := ks[0].Name
60
61 rc = checkFieldNameAndDoc(structName, "", ks[0].Doc, typesMap) || rc
62 for _, p := range ks[1:] {
63 rc = checkFieldNameAndDoc(structName, p.Name, p.Doc, typesMap) || rc
64 }
65 }
66
67 if rc {
68 os.Exit(1)
69 }
70 }
71
72 func checkFieldNameAndDoc(structName, fieldName, doc string, typesMap kubeTypesMap) bool {
73 rc := false
74 visited := sets.Set[string]{}
75
76
77
78
79
80
81
82
83
84
85 nameGroups := re.FindAllStringSubmatch(doc, -1)
86 for _, nameGroup := range nameGroups {
87 name := nameGroup[1]
88 if visited.Has(name) {
89 continue
90 }
91 if p, ok := typesMap[strings.ToLower(name)]; ok && p.Name != name {
92 rc = true
93 visited.Insert(name)
94
95 fmt.Fprintf(os.Stderr, "Error: doc for %s", structName)
96 if fieldName != "" {
97 fmt.Fprintf(os.Stderr, ".%s", fieldName)
98 }
99
100 fmt.Fprintf(os.Stderr, " contains: %s, which should be: %s\n", name, p.Name)
101 }
102 }
103
104 return rc
105 }
106
View as plain text