|
| 1 | +/* |
| 2 | +Copyright 2025 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 nodurations |
| 18 | + |
| 19 | +import ( |
| 20 | + "fmt" |
| 21 | + "go/ast" |
| 22 | + |
| 23 | + "golang.org/x/tools/go/analysis" |
| 24 | + kalerrors "sigs.k8s.io/kube-api-linter/pkg/analysis/errors" |
| 25 | + "sigs.k8s.io/kube-api-linter/pkg/analysis/helpers/extractjsontags" |
| 26 | + "sigs.k8s.io/kube-api-linter/pkg/analysis/helpers/inspector" |
| 27 | + "sigs.k8s.io/kube-api-linter/pkg/analysis/helpers/markers" |
| 28 | + "sigs.k8s.io/kube-api-linter/pkg/analysis/utils" |
| 29 | +) |
| 30 | + |
| 31 | +const name = "nodurations" |
| 32 | + |
| 33 | +// Analyzer is the analyzer for the nodurations package. |
| 34 | +// It checks that no struct field is of a type either time.Duration or metav1.Duration. |
| 35 | +var Analyzer = &analysis.Analyzer{ |
| 36 | + Name: name, |
| 37 | + Doc: "Duration types should not be used, to avoid the need for clients to implement go duration parsing. Instead, use integer based fields with the unit in the field name.", |
| 38 | + Run: run, |
| 39 | + Requires: []*analysis.Analyzer{inspector.Analyzer}, |
| 40 | +} |
| 41 | + |
| 42 | +func run(pass *analysis.Pass) (any, error) { |
| 43 | + inspect, ok := pass.ResultOf[inspector.Analyzer].(inspector.Inspector) |
| 44 | + if !ok { |
| 45 | + return nil, kalerrors.ErrCouldNotGetInspector |
| 46 | + } |
| 47 | + |
| 48 | + inspect.InspectFields(func(field *ast.Field, _ []ast.Node, _ extractjsontags.FieldTagInfo, markersAccess markers.Markers) { |
| 49 | + checkField(pass, field) |
| 50 | + }) |
| 51 | + |
| 52 | + inspect.InspectTypeSpec(func(typeSpec *ast.TypeSpec, markersAccess markers.Markers) { |
| 53 | + checkTypeSpec(pass, typeSpec, typeSpec, "type") |
| 54 | + }) |
| 55 | + |
| 56 | + return nil, nil //nolint:nilnil |
| 57 | +} |
| 58 | + |
| 59 | +func checkField(pass *analysis.Pass, field *ast.Field) { |
| 60 | + fieldName := utils.FieldName(field) |
| 61 | + if fieldName == "" { |
| 62 | + return |
| 63 | + } |
| 64 | + |
| 65 | + prefix := fmt.Sprintf("field %s", fieldName) |
| 66 | + |
| 67 | + checkTypeExpr(pass, field.Type, field, prefix) |
| 68 | +} |
| 69 | + |
| 70 | +//nolint:cyclop |
| 71 | +func checkTypeExpr(pass *analysis.Pass, typeExpr ast.Expr, node ast.Node, prefix string) { |
| 72 | + switch typ := typeExpr.(type) { |
| 73 | + case *ast.SelectorExpr: |
| 74 | + pkg, ok := typ.X.(*ast.Ident) |
| 75 | + if !ok { |
| 76 | + return |
| 77 | + } |
| 78 | + |
| 79 | + if typ.X == nil || (pkg.Name != "time" && pkg.Name != "metav1") { |
| 80 | + return |
| 81 | + } |
| 82 | + |
| 83 | + // Array element is not a metav1.Condition. |
| 84 | + if typ.Sel == nil || typ.Sel.Name != "Duration" { |
| 85 | + return |
| 86 | + } |
| 87 | + |
| 88 | + pass.Reportf(node.Pos(), "%s should not use a Duration. Use an integer type with units in the name to avoid the need for clients to implement Go style duration parsing.", prefix) |
| 89 | + case *ast.Ident: |
| 90 | + checkIdent(pass, typ, node, prefix) |
| 91 | + case *ast.StarExpr: |
| 92 | + checkTypeExpr(pass, typ.X, node, fmt.Sprintf("%s pointer", prefix)) |
| 93 | + case *ast.ArrayType: |
| 94 | + checkTypeExpr(pass, typ.Elt, node, fmt.Sprintf("%s array element", prefix)) |
| 95 | + case *ast.MapType: |
| 96 | + checkTypeExpr(pass, typ.Key, node, fmt.Sprintf("%s map key", prefix)) |
| 97 | + checkTypeExpr(pass, typ.Value, node, fmt.Sprintf("%s map value", prefix)) |
| 98 | + } |
| 99 | +} |
| 100 | + |
| 101 | +// checkIdent calls the checkFunc with the ident, when we have hit a built-in type. |
| 102 | +// If the ident is not a built in, we look at the underlying type until we hit a built-in type. |
| 103 | +func checkIdent(pass *analysis.Pass, ident *ast.Ident, node ast.Node, prefix string) { |
| 104 | + if utils.IsBasicType(pass, ident) { |
| 105 | + // We've hit a built-in type, no need to check further. |
| 106 | + return |
| 107 | + } |
| 108 | + |
| 109 | + tSpec, ok := utils.LookupTypeSpec(pass, ident) |
| 110 | + if !ok { |
| 111 | + return |
| 112 | + } |
| 113 | + |
| 114 | + // The field is using a type alias, check if the alias is an int. |
| 115 | + checkTypeSpec(pass, tSpec, node, fmt.Sprintf("%s type", prefix)) |
| 116 | +} |
| 117 | + |
| 118 | +func checkTypeSpec(pass *analysis.Pass, tSpec *ast.TypeSpec, node ast.Node, prefix string) { |
| 119 | + if tSpec.Name == nil { |
| 120 | + return |
| 121 | + } |
| 122 | + |
| 123 | + typeName := tSpec.Name.Name |
| 124 | + prefix = fmt.Sprintf("%s %s", prefix, typeName) |
| 125 | + |
| 126 | + checkTypeExpr(pass, tSpec.Type, node, prefix) |
| 127 | +} |
0 commit comments