Skip to content

Commit 4e2482c

Browse files
authored
Merge pull request #56 from sivchari/integrate-common-kal-error
integrate error variable of KAL
2 parents b21bc67 + f709389 commit 4e2482c

File tree

16 files changed

+74
-105
lines changed

16 files changed

+74
-105
lines changed

pkg/analysis/commentstart/analyzer.go

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,25 +16,21 @@ limitations under the License.
1616
package commentstart
1717

1818
import (
19-
"errors"
2019
"fmt"
2120
"go/ast"
2221
"go/token"
2322
"go/types"
2423
"strings"
2524

2625
"golang.org/x/tools/go/analysis"
26+
kalerrors "sigs.k8s.io/kube-api-linter/pkg/analysis/errors"
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"
3030
)
3131

3232
const name = "commentstart"
3333

34-
var (
35-
errCouldNotGetInspector = errors.New("could not get inspector")
36-
)
37-
3834
// Analyzer is the analyzer for the commentstart package.
3935
// It checks that all struct fields in an API have a godoc, and that the godoc starts with the serialised field name.
4036
var Analyzer = &analysis.Analyzer{
@@ -47,7 +43,7 @@ var Analyzer = &analysis.Analyzer{
4743
func run(pass *analysis.Pass) (interface{}, error) {
4844
inspect, ok := pass.ResultOf[inspector.Analyzer].(inspector.Inspector)
4945
if !ok {
50-
return nil, errCouldNotGetInspector
46+
return nil, kalerrors.ErrCouldNotGetInspector
5147
}
5248

5349
inspect.InspectFields(func(field *ast.Field, stack []ast.Node, jsonTagInfo extractjsontags.FieldTagInfo, markersAccess markers.Markers) {

pkg/analysis/conditions/analyzer.go

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ limitations under the License.
1616
package conditions
1717

1818
import (
19-
"errors"
2019
"fmt"
2120
"go/ast"
2221
"go/token"
@@ -26,6 +25,7 @@ import (
2625
"golang.org/x/tools/go/analysis"
2726
"golang.org/x/tools/go/analysis/passes/inspect"
2827
"golang.org/x/tools/go/ast/inspector"
28+
kalerrors "sigs.k8s.io/kube-api-linter/pkg/analysis/errors"
2929
"sigs.k8s.io/kube-api-linter/pkg/analysis/helpers/extractjsontags"
3030
"sigs.k8s.io/kube-api-linter/pkg/analysis/helpers/markers"
3131
"sigs.k8s.io/kube-api-linter/pkg/config"
@@ -62,11 +62,6 @@ func init() {
6262
)
6363
}
6464

65-
var (
66-
errCouldNotGetInspector = errors.New("could not get inspector")
67-
errCouldNotGetMarkers = errors.New("could not get markers")
68-
)
69-
7065
type analyzer struct {
7166
isFirstField config.ConditionsFirstField
7267
useProtobuf config.ConditionsUseProtobuf
@@ -94,12 +89,12 @@ func newAnalyzer(cfg config.ConditionsConfig) *analysis.Analyzer {
9489
func (a *analyzer) run(pass *analysis.Pass) (interface{}, error) {
9590
inspect, ok := pass.ResultOf[inspect.Analyzer].(*inspector.Inspector)
9691
if !ok {
97-
return nil, errCouldNotGetInspector
92+
return nil, kalerrors.ErrCouldNotGetInspector
9893
}
9994

10095
markersAccess, ok := pass.ResultOf[markers.Analyzer].(markers.Markers)
10196
if !ok {
102-
return nil, errCouldNotGetMarkers
97+
return nil, kalerrors.ErrCouldNotGetMarkers
10398
}
10499

105100
// Filter to structs so that we can iterate over fields in a struct.

pkg/analysis/errors/errors.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
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+
package errors
17+
18+
import "errors"
19+
20+
var (
21+
// ErrCouldNotCreateMarkers is returned when the markers could not be created.
22+
ErrCouldNotCreateMarkers = errors.New("could not create markers")
23+
24+
// ErrCouldNotCreateStructFieldTags is returned when the struct field tags could not be created.
25+
ErrCouldNotCreateStructFieldTags = errors.New("could not create new structFieldTags")
26+
27+
// ErrCouldNotGetInspector is returned when the inspector could not be retrieved.
28+
ErrCouldNotGetInspector = errors.New("could not get inspector")
29+
30+
// ErrCouldNotGetMarkers is returned when the markers analyzer could not be retrieved.
31+
ErrCouldNotGetMarkers = errors.New("could not get markers")
32+
33+
// ErrCouldNotGetJSONTags is returned when the JSON tags could not be retrieved.
34+
ErrCouldNotGetJSONTags = errors.New("could not get json tags")
35+
)

pkg/analysis/helpers/extractjsontags/analyzer.go

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ limitations under the License.
1616
package extractjsontags
1717

1818
import (
19-
"errors"
2019
"go/ast"
2120
"go/token"
2221
"reflect"
@@ -26,11 +25,8 @@ import (
2625
"golang.org/x/tools/go/analysis"
2726
"golang.org/x/tools/go/analysis/passes/inspect"
2827
"golang.org/x/tools/go/ast/inspector"
29-
)
3028

31-
var (
32-
errCouldNotGetInspector = errors.New("could not get inspector")
33-
errCouldNotCreateStructFieldTags = errors.New("could not create new structFieldTags")
29+
kalerrors "sigs.k8s.io/kube-api-linter/pkg/analysis/errors"
3430
)
3531

3632
// StructFieldTags is used to find information about
@@ -71,7 +67,7 @@ var Analyzer = &analysis.Analyzer{
7167
func run(pass *analysis.Pass) (interface{}, error) {
7268
inspect, ok := pass.ResultOf[inspect.Analyzer].(*inspector.Inspector)
7369
if !ok {
74-
return nil, errCouldNotGetInspector
70+
return nil, kalerrors.ErrCouldNotGetInspector
7571
}
7672

7773
// Filter to structs so that we can iterate over fields in a struct.
@@ -81,7 +77,7 @@ func run(pass *analysis.Pass) (interface{}, error) {
8177

8278
results, ok := newStructFieldTags().(*structFieldTags)
8379
if !ok {
84-
return nil, errCouldNotCreateStructFieldTags
80+
return nil, kalerrors.ErrCouldNotCreateStructFieldTags
8581
}
8682

8783
inspect.Preorder(nodeFilter, func(n ast.Node) {

pkg/analysis/helpers/inspector/analyzer.go

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,25 +16,19 @@ limitations under the License.
1616
package inspector
1717

1818
import (
19-
"errors"
2019
"reflect"
2120

2221
"golang.org/x/tools/go/analysis"
2322
"golang.org/x/tools/go/analysis/passes/inspect"
2423
astinspector "golang.org/x/tools/go/ast/inspector"
2524

25+
kalerrors "sigs.k8s.io/kube-api-linter/pkg/analysis/errors"
2626
"sigs.k8s.io/kube-api-linter/pkg/analysis/helpers/extractjsontags"
2727
"sigs.k8s.io/kube-api-linter/pkg/analysis/helpers/markers"
2828
)
2929

3030
const name = "inspector"
3131

32-
var (
33-
errCouldNotGetInspector = errors.New("could not get inspector")
34-
errCouldNotGetJSONTags = errors.New("could not get json tags")
35-
errCouldNotGetMarkers = errors.New("could not get markers")
36-
)
37-
3832
// Analyzer is the analyzer for the inspector package.
3933
// It provides common functionality for analyzers that need to inspect fields and struct.
4034
// Abstracting away filtering of fields that the analyzers should and shouldn't be worrying about.
@@ -49,17 +43,17 @@ var Analyzer = &analysis.Analyzer{
4943
func run(pass *analysis.Pass) (interface{}, error) {
5044
astinspector, ok := pass.ResultOf[inspect.Analyzer].(*astinspector.Inspector)
5145
if !ok {
52-
return nil, errCouldNotGetInspector
46+
return nil, kalerrors.ErrCouldNotGetInspector
5347
}
5448

5549
jsonTags, ok := pass.ResultOf[extractjsontags.Analyzer].(extractjsontags.StructFieldTags)
5650
if !ok {
57-
return nil, errCouldNotGetJSONTags
51+
return nil, kalerrors.ErrCouldNotGetJSONTags
5852
}
5953

6054
markersAccess, ok := pass.ResultOf[markers.Analyzer].(markers.Markers)
6155
if !ok {
62-
return nil, errCouldNotGetMarkers
56+
return nil, kalerrors.ErrCouldNotGetMarkers
6357
}
6458

6559
return newInspector(astinspector, jsonTags, markersAccess), nil

pkg/analysis/helpers/markers/analyzer.go

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ limitations under the License.
1616
package markers
1717

1818
import (
19-
"errors"
2019
"go/ast"
2120
"go/token"
2221
"reflect"
@@ -25,6 +24,8 @@ import (
2524
"golang.org/x/tools/go/analysis"
2625
"golang.org/x/tools/go/analysis/passes/inspect"
2726
"golang.org/x/tools/go/ast/inspector"
27+
28+
kalerrors "sigs.k8s.io/kube-api-linter/pkg/analysis/errors"
2829
)
2930

3031
// UnnamedExpression is the expression key used
@@ -38,11 +39,6 @@ import (
3839
// is "kubebuilder:validation:XValidation:rule='...',message='...'".
3940
const UnnamedExpression = ""
4041

41-
var (
42-
errCouldNotGetInspector = errors.New("could not get inspector")
43-
errCouldNotCreateMarkers = errors.New("could not create markers")
44-
)
45-
4642
// Markers allows access to markers extracted from the
4743
// go types.
4844
type Markers interface {
@@ -120,7 +116,7 @@ var Analyzer = &analysis.Analyzer{
120116
func run(pass *analysis.Pass) (interface{}, error) {
121117
inspect, ok := pass.ResultOf[inspect.Analyzer].(*inspector.Inspector)
122118
if !ok {
123-
return nil, errCouldNotGetInspector
119+
return nil, kalerrors.ErrCouldNotGetInspector
124120
}
125121

126122
nodeFilter := []ast.Node{
@@ -146,7 +142,7 @@ func run(pass *analysis.Pass) (interface{}, error) {
146142

147143
results, ok := newMarkers().(*markers)
148144
if !ok {
149-
return nil, errCouldNotCreateMarkers
145+
return nil, kalerrors.ErrCouldNotCreateMarkers
150146
}
151147

152148
inspect.Preorder(nodeFilter, func(n ast.Node) {

pkg/analysis/integers/analyzer.go

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,21 +16,17 @@ limitations under the License.
1616
package integers
1717

1818
import (
19-
"errors"
2019
"go/ast"
2120

2221
"golang.org/x/tools/go/analysis"
2322
"golang.org/x/tools/go/analysis/passes/inspect"
2423
"golang.org/x/tools/go/ast/inspector"
24+
kalerrors "sigs.k8s.io/kube-api-linter/pkg/analysis/errors"
2525
"sigs.k8s.io/kube-api-linter/pkg/analysis/utils"
2626
)
2727

2828
const name = "integers"
2929

30-
var (
31-
errCouldNotGetInspector = errors.New("could not get inspector")
32-
)
33-
3430
// Analyzer is the analyzer for the integers package.
3531
// It checks that no struct fields or type aliases are `int`, or unsigned integers.
3632
var Analyzer = &analysis.Analyzer{
@@ -43,7 +39,7 @@ var Analyzer = &analysis.Analyzer{
4339
func run(pass *analysis.Pass) (interface{}, error) {
4440
inspect, ok := pass.ResultOf[inspect.Analyzer].(*inspector.Inspector)
4541
if !ok {
46-
return nil, errCouldNotGetInspector
42+
return nil, kalerrors.ErrCouldNotGetInspector
4743
}
4844

4945
// Filter to fields so that we can look at fields within structs.

pkg/analysis/jsontags/analyzer.go

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,11 @@ limitations under the License.
1616
package jsontags
1717

1818
import (
19-
"errors"
2019
"fmt"
2120
"go/ast"
2221
"regexp"
2322

23+
kalerrors "sigs.k8s.io/kube-api-linter/pkg/analysis/errors"
2424
"sigs.k8s.io/kube-api-linter/pkg/analysis/helpers/extractjsontags"
2525
"sigs.k8s.io/kube-api-linter/pkg/analysis/helpers/inspector"
2626
"sigs.k8s.io/kube-api-linter/pkg/analysis/helpers/markers"
@@ -36,10 +36,6 @@ const (
3636
name = "jsontags"
3737
)
3838

39-
var (
40-
errCouldNotGetInspector = errors.New("could not get inspector")
41-
)
42-
4339
type analyzer struct {
4440
jsonTagRegex *regexp.Regexp
4541
}
@@ -68,7 +64,7 @@ func newAnalyzer(cfg config.JSONTagsConfig) (*analysis.Analyzer, error) {
6864
func (a *analyzer) run(pass *analysis.Pass) (interface{}, error) {
6965
inspect, ok := pass.ResultOf[inspector.Analyzer].(inspector.Inspector)
7066
if !ok {
71-
return nil, errCouldNotGetInspector
67+
return nil, kalerrors.ErrCouldNotGetInspector
7268
}
7369

7470
inspect.InspectFields(func(field *ast.Field, stack []ast.Node, jsonTagInfo extractjsontags.FieldTagInfo, markersAccess markers.Markers) {

pkg/analysis/maxlength/analyzer.go

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,11 @@ limitations under the License.
1616
package maxlength
1717

1818
import (
19-
"errors"
2019
"fmt"
2120
"go/ast"
2221

2322
"golang.org/x/tools/go/analysis"
23+
kalerrors "sigs.k8s.io/kube-api-linter/pkg/analysis/errors"
2424
"sigs.k8s.io/kube-api-linter/pkg/analysis/helpers/extractjsontags"
2525
"sigs.k8s.io/kube-api-linter/pkg/analysis/helpers/inspector"
2626
"sigs.k8s.io/kube-api-linter/pkg/analysis/helpers/markers"
@@ -40,10 +40,6 @@ const (
4040
kubebuilderMaxItems = "kubebuilder:validation:MaxItems"
4141
)
4242

43-
var (
44-
errCouldNotGetInspector = errors.New("could not get inspector")
45-
)
46-
4743
// Analyzer is the analyzer for the maxlength package.
4844
// It checks that strings and arrays have maximum lengths and maximum items respectively.
4945
var Analyzer = &analysis.Analyzer{
@@ -56,7 +52,7 @@ var Analyzer = &analysis.Analyzer{
5652
func run(pass *analysis.Pass) (interface{}, error) {
5753
inspect, ok := pass.ResultOf[inspector.Analyzer].(inspector.Inspector)
5854
if !ok {
59-
return nil, errCouldNotGetInspector
55+
return nil, kalerrors.ErrCouldNotGetInspector
6056
}
6157

6258
inspect.InspectFields(func(field *ast.Field, stack []ast.Node, jsonTagInfo extractjsontags.FieldTagInfo, markersAccess markers.Markers) {

pkg/analysis/nobools/analyzer.go

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,21 +16,17 @@ limitations under the License.
1616
package nobools
1717

1818
import (
19-
"errors"
2019
"go/ast"
2120

2221
"golang.org/x/tools/go/analysis"
2322
"golang.org/x/tools/go/analysis/passes/inspect"
2423
"golang.org/x/tools/go/ast/inspector"
24+
kalerrors "sigs.k8s.io/kube-api-linter/pkg/analysis/errors"
2525
"sigs.k8s.io/kube-api-linter/pkg/analysis/utils"
2626
)
2727

2828
const name = "nobools"
2929

30-
var (
31-
errCouldNotGetInspector = errors.New("could not get inspector")
32-
)
33-
3430
// Analyzer is the analyzer for the nobools package.
3531
// It checks that no struct fields are `bool`.
3632
var Analyzer = &analysis.Analyzer{
@@ -43,7 +39,7 @@ var Analyzer = &analysis.Analyzer{
4339
func run(pass *analysis.Pass) (interface{}, error) {
4440
inspect, ok := pass.ResultOf[inspect.Analyzer].(*inspector.Inspector)
4541
if !ok {
46-
return nil, errCouldNotGetInspector
42+
return nil, kalerrors.ErrCouldNotGetInspector
4743
}
4844

4945
// Filter to fields so that we can look at fields within structs.

0 commit comments

Comments
 (0)