Skip to content

Commit e73e64a

Browse files
committed
initialize ssa tags linter
Signed-off-by: sivchari <shibuuuu5@gmail.com>
1 parent 25b7231 commit e73e64a

File tree

8 files changed

+151
-0
lines changed

8 files changed

+151
-0
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -328,6 +328,10 @@ It will suggest to remove the pointer from the field, and update the `json` tag
328328
If you prefer not to suggest fixes for pointers in required fields, you can change the `pointerPolicy` to `Warn`.
329329
The linter will then only suggest to remove the `omitempty` value from the `json` tag.
330330

331+
## SSATags
332+
333+
TODO
334+
331335
## StatusOptional
332336

333337
The `statusoptional` linter checks that all first-level children fields within a status struct are marked as optional.

pkg/analysis/registry.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ import (
3131
"sigs.k8s.io/kube-api-linter/pkg/analysis/nophase"
3232
"sigs.k8s.io/kube-api-linter/pkg/analysis/optionalorrequired"
3333
"sigs.k8s.io/kube-api-linter/pkg/analysis/requiredfields"
34+
"sigs.k8s.io/kube-api-linter/pkg/analysis/ssatags"
3435
"sigs.k8s.io/kube-api-linter/pkg/analysis/statusoptional"
3536
"sigs.k8s.io/kube-api-linter/pkg/analysis/statussubresource"
3637
"sigs.k8s.io/kube-api-linter/pkg/config"
@@ -86,6 +87,7 @@ func NewRegistry() Registry {
8687
nophase.Initializer(),
8788
optionalorrequired.Initializer(),
8889
requiredfields.Initializer(),
90+
ssatags.Initializer(),
8991
statusoptional.Initializer(),
9092
statussubresource.Initializer(),
9193
},

pkg/analysis/registry_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ var _ = Describe("Registry", func() {
4141
"nophase",
4242
"optionalorrequired",
4343
"requiredfields",
44+
"ssatags",
4445
"statusoptional",
4546
))
4647
})
@@ -62,6 +63,7 @@ var _ = Describe("Registry", func() {
6263
"nophase",
6364
"optionalorrequired",
6465
"requiredfields",
66+
"ssatags",
6567
"statusoptional",
6668
"statussubresource",
6769
))

pkg/analysis/ssatags/analyzer.go

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
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 ssatags
17+
18+
import (
19+
"go/ast"
20+
21+
"golang.org/x/tools/go/analysis"
22+
"sigs.k8s.io/kube-api-linter/pkg/analysis/helpers/extractjsontags"
23+
"sigs.k8s.io/kube-api-linter/pkg/analysis/helpers/inspector"
24+
"sigs.k8s.io/kube-api-linter/pkg/analysis/helpers/markers"
25+
)
26+
27+
const name = "ssatags"
28+
29+
// Analyzer is the analyzer for the ssatags package.
30+
// It checks whether the array types in the API have the SSA tags and the usage of the tags is correct.
31+
var Analyzer = &analysis.Analyzer{
32+
Name: name,
33+
Doc: "Check that all array types in the API have the SSA tags and the usage of the tags is correct",
34+
Run: run,
35+
Requires: []*analysis.Analyzer{inspector.Analyzer},
36+
}
37+
38+
func run(pass *analysis.Pass) (any, error) {
39+
inspect, ok := pass.ResultOf[inspector.Analyzer].(inspector.Inspector)
40+
if !ok {
41+
return nil, nil
42+
}
43+
44+
inspect.InspectFields(func(field *ast.Field, stack []ast.Node, jsonTagInfo extractjsontags.FieldTagInfo, markersAccess markers.Markers) {
45+
})
46+
47+
inspect.InspectTypeSpec(func(typeSpec *ast.TypeSpec, markersAccess markers.Markers) {
48+
})
49+
50+
return nil, nil //nolint:nilnil
51+
}

pkg/analysis/ssatags/analyzer_test.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
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 ssatags_test
17+
18+
import (
19+
"testing"
20+
21+
"golang.org/x/tools/go/analysis/analysistest"
22+
"sigs.k8s.io/kube-api-linter/pkg/analysis/ssatags"
23+
)
24+
25+
func Test(t *testing.T) {
26+
testdata := analysistest.TestData()
27+
analysistest.RunWithSuggestedFixes(t, testdata, ssatags.Analyzer, "a")
28+
}

pkg/analysis/ssatags/doc.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
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 ssatags

pkg/analysis/ssatags/initializer.go

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
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 ssatags
17+
18+
import (
19+
"golang.org/x/tools/go/analysis"
20+
"sigs.k8s.io/kube-api-linter/pkg/config"
21+
)
22+
23+
// Initializer returns the AnalyzerInitializer for this
24+
// Analyzer so that it can be added to the registry.
25+
func Initializer() initializer {
26+
return initializer{}
27+
}
28+
29+
// intializer implements the AnalyzerInitializer interface.
30+
type initializer struct{}
31+
32+
// Name returns the name of the Analyzer.
33+
func (initializer) Name() string {
34+
return name
35+
}
36+
37+
// Init returns the intialized Analyzer.
38+
func (initializer) Init(cfg config.LintersConfig) (*analysis.Analyzer, error) {
39+
return Analyzer, nil
40+
}
41+
42+
// Default determines whether this Analyzer is on by default, or not.
43+
func (initializer) Default() bool {
44+
// Duplicated markers are a sign of bad code, and should be avoided.
45+
// This is a good rule to have on by default.
46+
return true
47+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
package a

0 commit comments

Comments
 (0)