Skip to content

Commit 29f114c

Browse files
committed
refactor: add linter and various fixes
1 parent 0a64c94 commit 29f114c

File tree

9 files changed

+343
-84
lines changed

9 files changed

+343
-84
lines changed

.github/workflows/lint.yml

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
name: Linting
2+
on:
3+
pull_request:
4+
5+
jobs:
6+
golangci:
7+
name: Lint golang files
8+
runs-on: ubuntu-latest
9+
steps:
10+
- name: Checkout
11+
uses: actions/checkout@v4
12+
with:
13+
ref: ${{ github.event.pull_request.head.sha }}
14+
repository: ${{github.event.pull_request.head.repo.full_name}}
15+
persist-credentials: false
16+
17+
- name: Setup Go
18+
uses: actions/setup-go@v5
19+
with:
20+
go-version: '1.23'
21+
cache: false
22+
23+
- name: golangci-lint
24+
uses: golangci/golangci-lint-action@v6.1.1
25+
with:
26+
only-new-issues: true
27+
version: v1.62.2
28+
args: --timeout=900s
29+
30+
gomodtidy:
31+
name: Enforce go.mod tidiness
32+
runs-on: ubuntu-latest
33+
34+
steps:
35+
- name: Checkout
36+
uses: actions/checkout@v4
37+
with:
38+
ref: "${{ github.event.pull_request.head.sha }}"
39+
repository: ${{github.event.pull_request.head.repo.full_name}}
40+
persist-credentials: false
41+
42+
- name: Setup Go
43+
uses: actions/setup-go@v5
44+
with:
45+
go-version: '1.23'
46+
47+
- name: Execute go mod tidy and check the outcome
48+
working-directory: ./
49+
run: |
50+
go mod tidy
51+
exit_code=$(git diff --exit-code)
52+
exit ${exit_code}
53+
54+
- name: Issue a comment in case the of failure
55+
uses: peter-evans/create-or-update-comment@v4
56+
with:
57+
token: ${{ secrets.GITHUB_TOKEN }}
58+
issue-number: ${{ github.event.pull_request.number }}
59+
body: |
60+
The `go.mod` and/or `go.sum` files appear not to be correctly tidied.
61+
62+
Please, rerun `go mod tidy` to fix the issues.
63+
reactions: confused
64+
if: |
65+
failure() && github.event.pull_request.head.repo.full_name == github.repository
66+
67+
shelllint:
68+
name: Lint bash files
69+
runs-on: ubuntu-latest
70+
steps:
71+
- name: Checkout code
72+
uses: actions/checkout@v4
73+
with:
74+
ref: "${{ github.event.pull_request.head.sha }}"
75+
repository: ${{github.event.pull_request.head.repo.full_name}}
76+
persist-credentials: false
77+
- name: Run Shellcheck
78+
uses: ludeeus/action-shellcheck@2.0.0
79+
env:
80+
# Allow 'source' outside of FILES
81+
SHELLCHECK_OPTS: -x
82+
83+
markdownlint:
84+
name: Lint markdown files
85+
runs-on: ubuntu-latest
86+
steps:
87+
- name: Check out code
88+
uses: actions/checkout@v4
89+
with:
90+
ref: "${{ github.event.pull_request.head.sha }}"
91+
repository: ${{github.event.pull_request.head.repo.full_name}}
92+
persist-credentials: false
93+
- name: Lint markdown files
94+
uses: avto-dev/markdown-lint@v1
95+
with:
96+
config: '.markdownlint.yml'
97+
args: '**/*.md'
98+
ignore: .github/ISSUE_TEMPLATE/*.md

.golangci.yml

Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
run:
2+
timeout: 10m
3+
4+
linters-settings:
5+
exhaustive:
6+
check-generated: false
7+
default-signifies-exhaustive: true
8+
9+
lll:
10+
line-length: 150
11+
gomodguard:
12+
blocked:
13+
modules:
14+
- github.com/go-logr/logr:
15+
recommendations:
16+
- k8s.io/klog/v2
17+
gci:
18+
sections:
19+
- standard # Captures all standard packages if they do not match another section.
20+
- default # Contains all imports that could not be matched to another section type.
21+
- prefix(github.com/liqotech/resource-slice-class-controller-template) # Groups all imports with the specified Prefix.
22+
goconst:
23+
min-len: 2
24+
min-occurrences: 2
25+
gocritic:
26+
enabled-tags:
27+
- diagnostic
28+
- experimental
29+
- opinionated
30+
- performance
31+
- style
32+
disabled-checks:
33+
# Conflicts with govet check-shadowing
34+
- sloppyReassign
35+
goimports:
36+
local-prefixes: github.com/liqotech/resource-slice-class-controller-template
37+
govet:
38+
enable:
39+
- shadow
40+
- nilness
41+
- nilfunc
42+
misspell:
43+
locale: US
44+
nolintlint:
45+
allow-unused: false # report any unused nolint directives
46+
require-explanation: true # require an explanation for nolint directives
47+
require-specific: true # require nolint directives to be specific about which linter is being skipped
48+
dupl:
49+
threshold: 300
50+
51+
linters:
52+
disable-all: true
53+
enable:
54+
- asciicheck
55+
- bodyclose
56+
- copyloopvar
57+
# - depguard
58+
- dogsled
59+
- dupl
60+
- errcheck
61+
- errorlint
62+
- exhaustive
63+
# - funlen
64+
# - gochecknoglobals
65+
# - gochecknoinits
66+
# - gocognit
67+
- gci
68+
- goconst
69+
- gocritic
70+
- gocyclo
71+
- godot
72+
# - godox
73+
# - goerr113
74+
- gofmt
75+
- goheader
76+
- goimports
77+
- gomodguard
78+
# - gomnd
79+
- goprintffuncname
80+
- gosec
81+
- gosimple
82+
- govet
83+
- ineffassign
84+
- lll
85+
# - maligned
86+
- misspell
87+
- nakedret
88+
# - nestif
89+
- noctx
90+
- nolintlint
91+
# - prealloc
92+
- revive
93+
- rowserrcheck
94+
- staticcheck
95+
- stylecheck
96+
# - testpackage
97+
- typecheck
98+
- unconvert
99+
- unparam
100+
- unused
101+
- whitespace
102+
# - wsl
103+
104+
issues:
105+
#fix: true
106+
107+
max-issues-per-linter: 0
108+
max-same-issues: 0
109+
110+
# Disable the default exclude patterns (as they disable the mandatory comments)
111+
exclude-use-default: false
112+
exclude:
113+
# errcheck: Almost all programs ignore errors on these functions and in most cases it's ok
114+
- Error return value of .((os\.)?std(out|err)\..*|.*Close|.*Flush|os\.Remove(All)?|.*printf?|os\.(Un)?Setenv). is not checked
115+
116+
exclude-rules:
117+
- linters:
118+
- govet
119+
text: 'declaration of "(err|ctx)" shadows declaration at'
120+
- linters:
121+
- gosec
122+
# Disable the check to test that HTTP clients are not using an insecure TLS connection.
123+
# We need it to contact the remote authentication services exposing a self-signed certificate
124+
text: TLS InsecureSkipVerify set true.
125+
- linters:
126+
- errorlint
127+
# Disable the check to test errors type assertion on switches.
128+
text: type switch on error will fail on wrapped errors. Use errors.As to check for specific errors
129+
130+
# Exclude the following linters from running on tests files.
131+
- path: _test\.go
132+
linters:
133+
- whitespace
134+
- revive # it does not allow dot imports (e.g. . "github.com/onsi/ginkgo")
135+
136+
exclude-files:
137+
- "zz_generated.*.go"
138+
139+
exclude-dirs:
140+
- "pkg/client"

.markdownlint.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
default: true
2+
line-length: false
3+
no-inline-html: false
4+
no-duplicate-header:
5+
siblings_only: true

README.md

Lines changed: 47 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,14 @@ The controller manages the ResourceSlice status updates and conditions, while th
1919
## Installation
2020

2121
1. Clone the repository:
22+
2223
```bash
2324
git clone https://github.com/liqotech/resource-slice-class-controller-template.git
2425
cd resource-slice-class-controller-template
2526
```
2627

2728
2. Build the controller:
29+
2830
```bash
2931
go build -o bin/manager main.go
3032
```
@@ -40,13 +42,15 @@ The controller requires a class name to be specified:
4042
```
4143

4244
Additional flags:
45+
4346
- `--metrics-bind-address`: The address to bind the metrics endpoint (default: ":8080")
4447
- `--health-probe-bind-address`: The address to bind the health probe endpoint (default: ":8081")
4548
- `--leader-elect`: Enable leader election for controller manager
4649

4750
### Example Implementation
4851

4952
The repository includes an example handler implementation in `example/resourceslice/handler.go` that:
53+
5054
- Generates CPU resources between 1 and 10 cores
5155
- Generates Memory resources between 1 and 5 GB
5256
- Allocates 110 pods
@@ -58,56 +62,56 @@ To implement a custom handler for your ResourceSlice class:
5862

5963
1. Create a new type that implements the `handler.Handler` interface:
6064

61-
```go
62-
package myhandler
65+
```go
66+
package myhandler
6367

64-
import (
65-
"context"
66-
authv1beta1 "github.com/liqotech/liqo/apis/authentication/v1beta1"
67-
"github.com/liqotech/resource-slice-classes/pkg/resourceslice/handler"
68-
ctrl "sigs.k8s.io/controller-runtime"
69-
)
68+
import (
69+
"context"
70+
authv1beta1 "github.com/liqotech/liqo/apis/authentication/v1beta1"
71+
rshandler "github.com/liqotech/resource-slice-class-controller-template/pkg/resourceslice/handler"
72+
ctrl "sigs.k8s.io/controller-runtime"
73+
)
7074

71-
type MyHandler struct {}
75+
type MyHandler struct {}
7276

73-
func NewMyHandler() handler.Handler {
74-
return &MyHandler{}
75-
}
77+
func NewMyHandler() rshandler.Handler {
78+
return &MyHandler{}
79+
}
7680

77-
func (h *MyHandler) Handle(ctx context.Context, resourceSlice *authv1beta1.ResourceSlice) (ctrl.Result, error) {
78-
// Implement your custom resource allocation logic here
79-
// Update resourceSlice.Status.Resources with your allocated resources
80-
81-
return ctrl.Result{}, nil
82-
}
83-
```
81+
func (h *MyHandler) Handle(ctx context.Context, resourceSlice *authv1beta1.ResourceSlice) (ctrl.Result, error) {
82+
// Implement your custom resource allocation logic here
83+
// Update resourceSlice.Status.Resources with your allocated resources
84+
85+
return ctrl.Result{}, nil
86+
}
87+
```
8488

8589
2. Update `main.go` to use your custom handler:
8690

87-
```go
88-
import (
89-
"github.com/your-org/your-module/pkg/myhandler"
90-
)
91-
92-
func main() {
93-
// ...
94-
95-
// Create your custom handler
96-
customHandler := myhandler.NewMyHandler()
97-
98-
if err = controller.NewResourceSliceReconciler(
99-
mgr.GetClient(),
100-
mgr.GetScheme(),
101-
mgr.GetEventRecorderFor("resource-slice-controller"),
102-
className,
103-
customHandler,
104-
).SetupWithManager(mgr); err != nil {
91+
```go
92+
import (
93+
"github.com/your-org/your-module/pkg/myhandler"
94+
)
95+
96+
func main() {
97+
// ...
98+
99+
// Create your custom handler
100+
customHandler := myhandler.NewMyHandler()
101+
102+
if err = controller.NewResourceSliceReconciler(
103+
mgr.GetClient(),
104+
mgr.GetScheme(),
105+
mgr.GetEventRecorderFor("resource-slice-controller"),
106+
className,
107+
customHandler,
108+
).SetupWithManager(mgr); err != nil {
109+
// ...
110+
}
111+
105112
// ...
106113
}
107-
108-
// ...
109-
}
110-
```
114+
```
111115

112116
## Handler Interface
113117

@@ -120,11 +124,13 @@ type Handler interface {
120124
```
121125

122126
Your handler implementation should:
127+
123128
1. Implement your resource allocation strategy
124129
2. Set the allocated resources in `resourceSlice.Status.Resources`
125130
3. Return appropriate reconciliation results and errors
126131

127132
Note: The controller, not the handler, is responsible for:
133+
128134
- Updating the ResourceSlice status in the API server
129135
- Managing ResourceSlice conditions
130136
- Recording events
@@ -150,7 +156,3 @@ Note: The controller, not the handler, is responsible for:
150156
## Contributing
151157

152158
Contributions are welcome! Please feel free to submit a Pull Request.
153-
154-
## License
155-
156-
This project is licensed under the Apache License 2.0 - see the LICENSE file for details.

0 commit comments

Comments
 (0)