@@ -21,9 +21,9 @@ import (
21
21
"bytes"
22
22
"flag"
23
23
"fmt"
24
+ "io/fs"
24
25
"log"
25
26
"os"
26
- "path"
27
27
"path/filepath"
28
28
"regexp"
29
29
"sort"
@@ -145,12 +145,6 @@ func TestDocumentation(t *testing.T) {
145
145
func getLabelsDocumentation () (map [string ][]string , error ) {
146
146
documentedMetrics := map [string ][]string {}
147
147
148
- docPath := "../../docs/"
149
- docFiles , err := os .ReadDir (docPath )
150
- if err != nil {
151
- return nil , fmt .Errorf ("failed to read documentation directory: %w" , err )
152
- }
153
-
154
148
// Match file names such as daemonset-metrics.md
155
149
fileRe := regexp .MustCompile (`^([a-z]*)-metrics.md$` )
156
150
// Match doc lines such as | kube_node_created | Gauge | `node`=<node-address>| STABLE |
@@ -160,15 +154,16 @@ func getLabelsDocumentation() (map[string][]string, error) {
160
154
// Match wildcard patterns for dynamic labels such as label_CRONJOB_LABEL
161
155
patternRe := regexp .MustCompile (`_[A-Z_]+` )
162
156
163
- for _ , file := range docFiles {
164
- if file .IsDir () || ! fileRe .MatchString (file .Name ()) {
165
- continue
157
+ err := filepath .WalkDir ("../../docs" , func (p string , d fs.DirEntry , err error ) error {
158
+
159
+ if d .IsDir () || ! fileRe .MatchString (d .Name ()) {
160
+ // Ignore the entry
161
+ return nil
166
162
}
167
163
168
- filePath := path .Join (docPath , file .Name ())
169
- f , err := os .Open (filepath .Clean (filePath ))
170
- if err != nil {
171
- return nil , fmt .Errorf ("cannot read file %s: %w" , filePath , err )
164
+ f , e := os .Open (filepath .Clean (p ))
165
+ if e != nil {
166
+ return fmt .Errorf ("cannot read file %s: %w" , p , e )
172
167
}
173
168
scanner := bufio .NewScanner (f )
174
169
for scanner .Scan () {
@@ -183,14 +178,19 @@ func getLabelsDocumentation() (map[string][]string, error) {
183
178
labelPatterns := make ([]string , len (labels ))
184
179
for i , l := range labels {
185
180
if len (l ) <= 1 {
186
- return nil , fmt .Errorf ("Label documentation %s did not match regex" , labelsDoc )
181
+ return fmt .Errorf ("label documentation %s did not match regex" , labelsDoc )
187
182
}
188
183
labelPatterns [i ] = patternRe .ReplaceAllString (l [1 ], "_.*" )
189
184
}
190
185
191
186
documentedMetrics [metric ] = labelPatterns
192
187
}
188
+ return nil
189
+ })
190
+ if err != nil {
191
+ log .Fatalf ("impossible to walk directories: %s" , err )
193
192
}
193
+
194
194
return documentedMetrics , nil
195
195
}
196
196
0 commit comments