Skip to content

Commit 7ddd54c

Browse files
authored
Merge pull request #150 from JoelSpeed/buildable-cmd
Add buildable cmd for golangci-lint-kube-api-linter
2 parents 502783c + e48529f commit 7ddd54c

File tree

6 files changed

+1344
-44
lines changed

6 files changed

+1344
-44
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ unit: ## Run unit tests.
6767

6868
.PHONY: build
6969
build: ## Build the golangci-lint custom plugin binary.
70-
${GOLANGCI_LINT} custom
70+
go build -o ./bin ./cmd/golangci-lint-kube-api-linter
7171

7272
.PHONY: verify-%
7373
verify-%:

README.md

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,18 @@ Kube API Linter is aimed at being an assistant to API review, by catching the me
1010

1111
## Installation
1212

13-
Kube API Linter ships as a golangci-lint plugin, and a golangci-lint module.
13+
Kube API Linter ships as a standalone binary, golangci-lint plugin, and a golangci-lint module.
14+
15+
### Standalone binary
16+
17+
The binary version of Kube API Linter can be built with `make build` or a standard `go build` command.
18+
```bash
19+
go build -o ./bin ./cmd/golangci-lint-kube-api-linter
20+
```
21+
22+
The binary builds a custom version of `golangci-lint` with Kube API Linter included as a module.
23+
See [Golangci-lint Moduule](#golangci-lint-module) for details on configuration of the module
24+
under `linter-settings`.
1425

1526
### Golangci-lint Module
1627

Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
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 main
17+
18+
import (
19+
"cmp"
20+
"fmt"
21+
"os"
22+
"regexp"
23+
"runtime/debug"
24+
"strings"
25+
26+
"github.com/golangci/golangci-lint/v2/pkg/commands"
27+
"github.com/golangci/golangci-lint/v2/pkg/exitcodes"
28+
)
29+
30+
//nolint:gochecknoglobals
31+
var (
32+
goVersion = "unknown"
33+
34+
// Populated by goreleaser during build.
35+
version = "unknown"
36+
commit = "?"
37+
date = ""
38+
)
39+
40+
func main() {
41+
info := createBuildInfo()
42+
43+
// set a KAL specific build version.
44+
setKALBuildVersion(&info)
45+
46+
if err := commands.Execute(info); err != nil {
47+
_, _ = fmt.Fprintf(os.Stderr, "Failed executing command with error: %v\n", err)
48+
os.Exit(exitcodes.Failure)
49+
}
50+
}
51+
52+
func createBuildInfo() commands.BuildInfo {
53+
info := commands.BuildInfo{
54+
Commit: commit,
55+
Version: version,
56+
GoVersion: goVersion,
57+
Date: date,
58+
}
59+
60+
buildInfo, available := debug.ReadBuildInfo()
61+
if !available {
62+
return info
63+
}
64+
65+
info.GoVersion = buildInfo.GoVersion
66+
67+
if date != "" {
68+
return info
69+
}
70+
71+
info.Version = buildInfo.Main.Version
72+
73+
matched, _ := regexp.MatchString(`v\d+\.\d+\.\d+`, buildInfo.Main.Version)
74+
if matched {
75+
info.Version = strings.TrimPrefix(buildInfo.Main.Version, "v")
76+
}
77+
78+
var revision, modified string
79+
80+
for _, setting := range buildInfo.Settings {
81+
// The `vcs.xxx` information is only available with `go build`.
82+
// This information is not available with `go install` or `go run`.
83+
switch setting.Key {
84+
case "vcs.time":
85+
info.Date = setting.Value
86+
case "vcs.revision":
87+
revision = setting.Value
88+
case "vcs.modified":
89+
modified = setting.Value
90+
}
91+
}
92+
93+
info.Date = cmp.Or(info.Date, "(unknown)")
94+
95+
info.Commit = fmt.Sprintf("(%s, modified: %s, mod sum: %q)",
96+
cmp.Or(revision, "unknown"), cmp.Or(modified, "?"), buildInfo.Main.Sum)
97+
98+
return info
99+
}
100+
101+
// Import path of golangci-lint.
102+
const golangciPath = "github.com/golangci/golangci-lint/v2"
103+
104+
// setKALBuildVersion sets a KAL specific build version message.
105+
// It fetches the golangci-lint version from the build dependencies.
106+
// Then uses the local version for KAL.
107+
func setKALBuildVersion(info *commands.BuildInfo) {
108+
golangciVersion := "unknown"
109+
110+
buildInfo, available := debug.ReadBuildInfo()
111+
if available {
112+
for _, dep := range buildInfo.Deps {
113+
if dep.Path == golangciPath {
114+
golangciVersion = dep.Version
115+
break
116+
}
117+
}
118+
}
119+
120+
matched, _ := regexp.MatchString(`v\d+\.\d+\.\d+`, golangciVersion)
121+
if matched {
122+
golangciVersion = strings.TrimPrefix(golangciVersion, "v")
123+
}
124+
125+
info.Version = fmt.Sprintf("%s, kube-api-linter has version %s", golangciVersion, info.Version)
126+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
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 main
17+
18+
// This file is used to declare module plugins.
19+
20+
import (
21+
_ "sigs.k8s.io/kube-api-linter"
22+
)

0 commit comments

Comments
 (0)