Skip to content

Commit 37102c8

Browse files
authored
Merge pull request kubernetes-sigs#84 from JoelSpeed/types-from-other-files-duplicates-statusoptional
Account for types from other files in statusoptional/duplicatemarkers
2 parents 25b7231 + f00a96a commit 37102c8

File tree

10 files changed

+33
-114
lines changed

10 files changed

+33
-114
lines changed

pkg/analysis/duplicatemarkers/analyzer.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import (
2727
"sigs.k8s.io/kube-api-linter/pkg/analysis/helpers/extractjsontags"
2828
"sigs.k8s.io/kube-api-linter/pkg/analysis/helpers/inspector"
2929
"sigs.k8s.io/kube-api-linter/pkg/analysis/helpers/markers"
30+
"sigs.k8s.io/kube-api-linter/pkg/analysis/utils"
3031
)
3132

3233
const (
@@ -90,7 +91,7 @@ func collectMarkers(pass *analysis.Pass, markersAccess markers.Markers, field *a
9091
return markers
9192
}
9293

93-
typeSpec, ok := ident.Obj.Decl.(*ast.TypeSpec)
94+
typeSpec, ok := utils.LookupTypeSpec(pass, ident)
9495
if !ok {
9596
return markers
9697
}

pkg/analysis/duplicatemarkers/testdata/src/a/a.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,11 @@ type DuplicateMarkerSpec struct { // want "DuplicateMarkerSpec has duplicated ma
7676
// +kubebuilder:validation:XValidation:rule="self == oldSelf",message="replicas must be immutable"
7777
// +kubebuilder:validation:XValidation:message="must be 1 to 5",rule="self >= 1 && self <= 3"
7878
DuplicatedUnorderedValidationReplicas *int `json:"duplicatedUnorderedValidationReplicas"` // want "DuplicatedUnorderedValidationReplicas has duplicated markers kubebuilder:validation:XValidation:message=\"must be 1 to 5\",rule=\"self >= 1 && self <= 3\""
79+
80+
StringFromAnotherFile StringFromAnotherFile `json:"stringFromAnotherFile"`
81+
82+
// +kubebuilder:validation:MaxLength=10
83+
StringFromAnotherFileWithMaxLength StringFromAnotherFile `json:"stringFromAnotherFileWithMaxLength"` // want "StringFromAnotherFileWithMaxLength has duplicated markers kubebuilder:validation:MaxLength=10"
7984
}
8085

8186
type Map struct {

pkg/analysis/duplicatemarkers/testdata/src/a/a.go.golden

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,10 @@ type DuplicateMarkerSpec struct { // want "DuplicateMarkerSpec has duplicated ma
6666
// +kubebuilder:validation:XValidation:rule="self >= 1 && self <= 3",message="must be 1 to 5"
6767
// +kubebuilder:validation:XValidation:rule="self == oldSelf",message="replicas must be immutable"
6868
DuplicatedUnorderedValidationReplicas *int `json:"duplicatedUnorderedValidationReplicas"` // want "DuplicatedUnorderedValidationReplicas has duplicated markers kubebuilder:validation:XValidation:message=\"must be 1 to 5\",rule=\"self >= 1 && self <= 3\""
69+
70+
StringFromAnotherFile StringFromAnotherFile `json:"stringFromAnotherFile"`
71+
72+
StringFromAnotherFileWithMaxLength StringFromAnotherFile `json:"stringFromAnotherFileWithMaxLength"` // want "StringFromAnotherFileWithMaxLength has duplicated markers kubebuilder:validation:MaxLength=10"
6973
}
7074

7175
type Map struct {
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
package a
2+
3+
// +kubebuilder:validation:MaxLength=10
4+
type StringFromAnotherFile string

pkg/analysis/duplicatemarkers/testdata/src/a/go.mod

Lines changed: 0 additions & 24 deletions
This file was deleted.

pkg/analysis/duplicatemarkers/testdata/src/a/go.sum

Lines changed: 0 additions & 85 deletions
This file was deleted.

pkg/analysis/statusoptional/analyzer.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ func (a *analyzer) run(pass *analysis.Pass) (any, error) {
8484
return
8585
}
8686

87-
statusStructType := getStructFromField(field)
87+
statusStructType := getStructFromField(pass, field)
8888
if statusStructType == nil {
8989
return
9090
}
@@ -114,7 +114,7 @@ func (a *analyzer) checkStatusStruct(pass *analysis.Pass, statusType *ast.Struct
114114
continue
115115
}
116116
// Check embedded structs recursively
117-
a.checkStatusStruct(pass, getStructFromField(childField), markersAccess, jsonTags)
117+
a.checkStatusStruct(pass, getStructFromField(pass, childField), markersAccess, jsonTags)
118118
default:
119119
// Check if the field has the required optional markers
120120
a.checkFieldOptionalMarker(pass, childField, fieldName, markersAccess)
@@ -240,13 +240,13 @@ func createMarkerRemovalEdits(fieldMarkers markershelper.MarkerSet) []analysis.T
240240
}
241241

242242
// getStructFromField extracts the struct type from an AST Field.
243-
func getStructFromField(field *ast.Field) *ast.StructType {
243+
func getStructFromField(pass *analysis.Pass, field *ast.Field) *ast.StructType {
244244
ident, ok := field.Type.(*ast.Ident)
245245
if !ok {
246246
return nil
247247
}
248248

249-
typeSpec, ok := ident.Obj.Decl.(*ast.TypeSpec)
249+
typeSpec, ok := utils.LookupTypeSpec(pass, ident)
250250
if !ok {
251251
return nil
252252
}

pkg/analysis/statusoptional/testdata/src/a/a.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,11 @@ type StatusWithEmbeddings struct {
2020

2121
// Pointer to non-inlined embed with omitempty
2222
*PointerOmitEmptyEmbed `json:"pointerOmitEmpty,omitempty"` // want "status field \"PointerOmitEmptyEmbed\" must be marked as optional"
23+
24+
// NonInlinedStructFromAnotherFile imports a type from another file
25+
NonInlinedStructFromAnotherFile StructFromAnotherFile `json:"nonInlinedStructFromAnotherFile"` // want "status field \"NonInlinedStructFromAnotherFile\" must be marked as optional"
26+
27+
StructFromAnotherFile `json:",inline"`
2328
}
2429

2530
type InlineEmbed struct {

pkg/analysis/statusoptional/testdata/src/a/a.go.golden

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,12 @@ type StatusWithEmbeddings struct {
2424
// Pointer to non-inlined embed with omitempty
2525
// +optional
2626
*PointerOmitEmptyEmbed `json:"pointerOmitEmpty,omitempty"` // want "status field \"PointerOmitEmptyEmbed\" must be marked as optional"
27+
28+
// NonInlinedStructFromAnotherFile imports a type from another file
29+
// +optional
30+
NonInlinedStructFromAnotherFile StructFromAnotherFile `json:"nonInlinedStructFromAnotherFile"` // want "status field \"NonInlinedStructFromAnotherFile\" must be marked as optional"
31+
32+
StructFromAnotherFile `json:",inline"`
2733
}
2834

2935
type InlineEmbed struct {
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
package a
2+
3+
type StructFromAnotherFile struct{}

0 commit comments

Comments
 (0)