Skip to content

Commit 6455626

Browse files
Merge pull request #58 from volcano-sh/copilot/add-golangci-lint-check
Add golangci-lint CI check
2 parents f3b93f1 + add96eb commit 6455626

File tree

16 files changed

+133
-107
lines changed

16 files changed

+133
-107
lines changed

.github/workflows/lint.yml

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
name: Lint
2+
3+
on:
4+
pull_request:
5+
branches:
6+
- main
7+
- 'release-*'
8+
paths:
9+
- '**.go'
10+
- 'go.mod'
11+
- 'go.sum'
12+
- '.golangci.yml'
13+
- '.github/workflows/lint.yml'
14+
15+
permissions:
16+
contents: read
17+
# Optional: allow read access to pull request. Use with `only-new-issues` option.
18+
pull-requests: read
19+
20+
jobs:
21+
golangci:
22+
name: golangci-lint
23+
runs-on: ubuntu-24.04
24+
steps:
25+
- name: Checkout code
26+
uses: actions/checkout@v4
27+
28+
- name: Set up Go
29+
uses: actions/setup-go@v5
30+
with:
31+
go-version: '1.24'
32+
33+
- name: Run golangci-lint
34+
run: make lint

.golangci.yml

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
# golangci-lint configuration file
2+
# Documentation: https://golangci-lint.run/usage/configuration/
3+
4+
version: 2
5+
6+
run:
7+
# Timeout for analysis
8+
timeout: 5m
9+
10+
# Exit code when at least one issue was found
11+
issues-exit-code: 1
12+
13+
# Include test files
14+
tests: true
15+
16+
# output configuration options
17+
output:
18+
formats:
19+
- format: colored-line-number
20+
print-issued-lines: true
21+
print-linter-name: true
22+
sort-results: true
23+
24+
# all available settings of specific linters
25+
linters-settings:
26+
errcheck:
27+
check-type-assertions: true
28+
check-blank: false
29+
gocyclo:
30+
min-complexity: 15
31+
misspell:
32+
locale: US
33+
unused:
34+
check-exported: false
35+
unparam:
36+
check-exported: false
37+
staticcheck:
38+
checks: ["all"]
39+
40+
linters:
41+
disable:
42+
- gosimple
43+
enable:
44+
- errcheck
45+
- govet
46+
- ineffassign
47+
- staticcheck
48+
- unused
49+
- misspell
50+
- revive
51+
- gosec
52+
- unconvert
53+
- unparam
54+
- goconst
55+
- gocyclo

Makefile

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -132,9 +132,9 @@ vet: ## Run go vet against code.
132132
go vet ./...
133133

134134
# Run linter
135-
lint:
136-
@echo "Running linter..."
137-
golangci-lint run ./...
135+
.PHONY: lint
136+
lint: golangci-lint ## Run golangci-lint
137+
$(GOLANGCI_LINT) run ./...
138138

139139
# Install to system
140140
install: build
@@ -273,15 +273,22 @@ $(LOCALBIN):
273273

274274
## Tool Binaries
275275
CONTROLLER_GEN ?= $(LOCALBIN)/controller-gen
276+
GOLANGCI_LINT ?= $(LOCALBIN)/golangci-lint
276277

277278
## Tool Versions
278279
CONTROLLER_TOOLS_VERSION ?= v0.17.2
280+
GOLANGCI_LINT_VERSION ?= v1.64.1
279281

280282
.PHONY: controller-gen
281283
controller-gen: $(CONTROLLER_GEN) ## Download controller-gen locally if necessary.
282284
$(CONTROLLER_GEN): $(LOCALBIN)
283285
$(call go-install-tool,$(CONTROLLER_GEN),sigs.k8s.io/controller-tools/cmd/controller-gen,$(CONTROLLER_TOOLS_VERSION))
284286

287+
.PHONY: golangci-lint
288+
golangci-lint: $(GOLANGCI_LINT) ## Download golangci-lint locally if necessary.
289+
$(GOLANGCI_LINT): $(LOCALBIN)
290+
$(call go-install-tool,$(GOLANGCI_LINT),github.com/golangci/golangci-lint/cmd/golangci-lint,$(GOLANGCI_LINT_VERSION))
291+
285292
# go-install-tool will 'go install' any package with custom target and name of binary, if it doesn't exist
286293
# $1 - target path with name of binary
287294
# $2 - package url which can be installed

cmd/agentd/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ func main() {
3939

4040
err = ctrl.NewControllerManagedBy(mgr).
4141
For(&sandboxv1alpha1.Sandbox{}).
42-
Complete(&agentd.AgentdReconciler{
42+
Complete(&agentd.Reconciler{
4343
Client: mgr.GetClient(),
4444
Scheme: mgr.GetScheme(),
4545
})

pkg/agentd/agentd.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,13 @@ var (
1717
SessionExpirationTimeout = 15 * time.Minute
1818
)
1919

20-
// AgentdReconciler reconciles a Sandbox object
21-
type AgentdReconciler struct {
20+
// Reconciler reconciles a Sandbox object
21+
type Reconciler struct {
2222
client.Client
2323
Scheme *runtime.Scheme
2424
}
2525

26-
func (r *AgentdReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
26+
func (r *Reconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
2727
sandbox := &sandboxv1alpha1.Sandbox{}
2828
err := r.Get(ctx, req.NamespacedName, sandbox)
2929
if err != nil {
@@ -58,7 +58,7 @@ func (r *AgentdReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctr
5858
}
5959

6060
// SetupWithManager sets up the controller with the Manager.
61-
func (r *AgentdReconciler) SetupWithManager(mgr ctrl.Manager) error {
61+
func (r *Reconciler) SetupWithManager(mgr ctrl.Manager) error {
6262
return ctrl.NewControllerManagedBy(mgr).
6363
For(&sandboxv1alpha1.Sandbox{}).
6464
Complete(r)

pkg/agentd/agentd_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import (
1717
"sigs.k8s.io/controller-runtime/pkg/reconcile"
1818
)
1919

20-
func TestAgentdReconciler_Reconcile_WithLastActivity(t *testing.T) {
20+
func TestReconciler_Reconcile_WithLastActivity(t *testing.T) {
2121
testScheme := runtime.NewScheme()
2222
utilruntime.Must(scheme.AddToScheme(testScheme))
2323
utilruntime.Must(sandboxv1alpha1.AddToScheme(testScheme))
@@ -107,7 +107,7 @@ func TestAgentdReconciler_Reconcile_WithLastActivity(t *testing.T) {
107107
WithObjects(tt.sandbox).
108108
Build()
109109

110-
reconciler := &AgentdReconciler{
110+
reconciler := &Reconciler{
111111
Client: fakeClient,
112112
Scheme: testScheme,
113113
}

pkg/redis/client.go

Lines changed: 0 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -325,71 +325,6 @@ func (c *client) ListInactiveSandboxes(ctx context.Context, before time.Time, li
325325
return c.loadSandboxesBySessionIDs(ctx, ids)
326326
}
327327

328-
// loadSandboxesByIDs loads sandbox objects for the given sandbox IDs.
329-
func (c *client) loadSandboxesByIDs(ctx context.Context, sandboxIDs []string) ([]*types.SandboxRedis, error) {
330-
if len(sandboxIDs) == 0 {
331-
return nil, nil
332-
}
333-
334-
sessionIDCmds := make([]*redisv9.StringCmd, len(sandboxIDs))
335-
pipe := c.rdb.Pipeline()
336-
for i, id := range sandboxIDs {
337-
sessionKey := c.sandboxKey(id)
338-
sessionIDCmds[i] = pipe.Get(ctx, sessionKey)
339-
}
340-
_, _ = pipe.Exec(ctx)
341-
342-
type pair struct {
343-
sandboxID string
344-
sessionID string
345-
}
346-
pairs := make([]pair, 0, len(sandboxIDs))
347-
348-
for i, cmd := range sessionIDCmds {
349-
sessionID, err := cmd.Result()
350-
if errors.Is(err, redisv9.Nil) {
351-
continue
352-
}
353-
if err != nil {
354-
return nil, fmt.Errorf("loadSandboxesByIDs: get sessionID for sandbox %s: %w", sandboxIDs[i], err)
355-
}
356-
pairs = append(pairs, pair{
357-
sandboxID: sandboxIDs[i],
358-
sessionID: sessionID,
359-
})
360-
}
361-
362-
if len(pairs) == 0 {
363-
return nil, nil
364-
}
365-
366-
sandboxCmds := make([]*redisv9.StringCmd, len(pairs))
367-
pipe = c.rdb.Pipeline()
368-
for i, p := range pairs {
369-
sessionKey := c.sessionKey(p.sessionID)
370-
sandboxCmds[i] = pipe.Get(ctx, sessionKey)
371-
}
372-
_, _ = pipe.Exec(ctx)
373-
374-
result := make([]*types.SandboxRedis, 0, len(pairs))
375-
for i, cmd := range sandboxCmds {
376-
data, err := cmd.Bytes()
377-
if errors.Is(err, redisv9.Nil) {
378-
continue
379-
}
380-
if err != nil {
381-
return nil, fmt.Errorf("loadSandboxesByIDs: get sandbox JSON for session %s: %w", pairs[i].sessionID, err)
382-
}
383-
var sandboxRedis types.SandboxRedis
384-
if err := json.Unmarshal(data, &sandboxRedis); err != nil {
385-
return nil, fmt.Errorf("loadSandboxesByIDs: unmarshal sandbox for session %s: %w", pairs[i].sessionID, err)
386-
}
387-
result = append(result, &sandboxRedis)
388-
}
389-
390-
return result, nil
391-
}
392-
393328
// loadSandboxesBySessionIDs loads sandbox objects for the given session IDs.
394329
func (c *client) loadSandboxesBySessionIDs(ctx context.Context, sessionIDs []string) ([]*types.SandboxRedis, error) {
395330
if len(sessionIDs) == 0 {

pkg/workloadmanager/auth.go

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -176,12 +176,6 @@ func (s *Server) validateServiceAccountToken(ctx context.Context, token string)
176176
return true, username, nil
177177
}
178178

179-
// checkSandboxAccess checks if the current user has access to the sandbox
180-
// Returns true if the user is the creator of the sandbox
181-
func (s *Server) checkSandboxAccess(sandbox *Sandbox, serviceAccountName string) bool {
182-
return sandbox.CreatorServiceAccount == serviceAccountName
183-
}
184-
185179
// extractUserInfo extracts user information from request context
186180
// Returns userToken, userNamespace, serviceAccount, serviceAccountName
187181
// If extraction fails, returns empty strings

pkg/workloadmanager/client_cache.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ func NewClientCache(maxSize int) *ClientCache {
8181
// Get retrieves a client from cache based on key (service account)
8282
// Returns the client if found and cached token is not expired, nil otherwise
8383
// Different tokens for the same service account can share the same client
84-
func (c *ClientCache) Get(key, token string) *UserK8sClient {
84+
func (c *ClientCache) Get(key string) *UserK8sClient {
8585
c.mu.Lock()
8686
defer c.mu.Unlock()
8787

@@ -158,6 +158,7 @@ func (c *ClientCache) evictOldest() {
158158
return
159159
}
160160

161+
// nolint:errcheck
161162
entry := back.Value.(*clientCacheEntry)
162163
c.lruList.Remove(back)
163164
delete(c.cache, entry.key)
@@ -280,7 +281,7 @@ func (c *TokenCache) evictOldest() {
280281
if back == nil {
281282
return
282283
}
283-
284+
// nolint:errcheck
284285
entry := back.Value.(*tokenCacheEntry)
285286
c.lruList.Remove(back)
286287
delete(c.cache, entry.token)

pkg/workloadmanager/codeinterpreter_controller.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,7 @@ func (r *CodeInterpreterReconciler) deleteSandboxTemplate(ctx context.Context, c
278278
}
279279

280280
// convertToPodTemplate converts CodeInterpreterSandboxTemplate to sandboxv1alpha1.PodTemplate
281-
func (r *CodeInterpreterReconciler) convertToPodTemplate(template *runtimev1alpha1.CodeInterpreterSandboxTemplate, ci *runtimev1alpha1.CodeInterpreter) sandboxv1alpha1.PodTemplate {
281+
func (r *CodeInterpreterReconciler) convertToPodTemplate(template *runtimev1alpha1.CodeInterpreterSandboxTemplate, _ *runtimev1alpha1.CodeInterpreter) sandboxv1alpha1.PodTemplate {
282282
// Build pod spec
283283
podSpec := corev1.PodSpec{
284284
Containers: []corev1.Container{

0 commit comments

Comments
 (0)