...

Source file src/k8s.io/kubernetes/cmd/fieldnamedocscheck/field_name_docs_check.go

Documentation: k8s.io/kubernetes/cmd/fieldnamedocscheck

     1  /*
     2  Copyright 2023 The Kubernetes Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    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  // kubeTypesMap is a map from field name to its tag name and doc.
    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  			// skip the field with no tag name
    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  	// The rule is:
    77  	// 1. Get all back-tick quoted names in the doc
    78  	// 2. Skip the name which is already found mismatched.
    79  	// 3. Skip the name whose lowercase is different from the lowercase of tag names,
    80  	//    because some docs use back-tick to quote field value or nil
    81  	// 4. Check if the name is different from its tag name
    82  
    83  	// TODO: a manual pass adding back-ticks to the doc strings, then update the linter to
    84  	// check the existence of back-ticks
    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