Skip to content

Commit add96eb

Browse files
committed
fix lint
Signed-off-by: Zhonghu Xu <xuzhonghu@huawei.com>
1 parent a46ba94 commit add96eb

File tree

14 files changed

+52
-194
lines changed

14 files changed

+52
-194
lines changed

.golangci.yml

Lines changed: 18 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -6,122 +6,50 @@ version: 2
66
run:
77
# Timeout for analysis
88
timeout: 5m
9-
9+
1010
# Exit code when at least one issue was found
1111
issues-exit-code: 1
12-
12+
1313
# Include test files
1414
tests: true
15-
16-
# Which dirs to skip: issues from them won't be reported
17-
skip-dirs:
18-
- bin
19-
- vendor
20-
- client-go # Generated client code
21-
22-
# Which files to skip: they will be analyzed, but issues from them won't be reported
23-
skip-files:
24-
- ".*\\.pb\\.go$"
25-
- ".*_generated\\.go$"
26-
- "zz_generated.*\\.go$"
2715

2816
# output configuration options
2917
output:
30-
format: colored-line-number
18+
formats:
19+
- format: colored-line-number
3120
print-issued-lines: true
3221
print-linter-name: true
33-
uniq-by-line: true
3422
sort-results: true
3523

3624
# all available settings of specific linters
3725
linters-settings:
3826
errcheck:
39-
# Report about not checking of errors in type assertions: `a := b.(MyStruct)`
4027
check-type-assertions: true
41-
# Report about assignment of errors to blank identifier: `num, _ := strconv.Atoi(numStr)`
4228
check-blank: false
43-
44-
govet:
45-
# Enable all analyzers
46-
enable-all: true
47-
# Disable shadow checking (can be too strict)
48-
disable:
49-
- shadow
50-
5129
gocyclo:
52-
# Minimal code complexity to report
5330
min-complexity: 15
54-
5531
misspell:
56-
# Correct spellings using locale preferences for US or UK
5732
locale: US
58-
5933
unused:
60-
# Treat code as a program (not a library)
6134
check-exported: false
62-
6335
unparam:
64-
# Report unused function parameters
6536
check-exported: false
66-
6737
staticcheck:
68-
# Select the Go version to target
6938
checks: ["all"]
7039

7140
linters:
72-
# Disable all linters by default
73-
disable-all: true
74-
75-
# Enable specific linters
41+
disable:
42+
- gosimple
7643
enable:
77-
- errcheck # Checks for unchecked errors
78-
- govet # Reports suspicious constructs
79-
- ineffassign # Detects ineffectual assignments
80-
- staticcheck # Go static analysis
81-
- unused # Checks for unused constants, variables, functions and types
82-
- misspell # Finds commonly misspelled English words
83-
- revive # Fast, configurable, extensible, flexible, and beautiful linter for Go
84-
- gosec # Inspects source code for security problems
85-
- unconvert # Removes unnecessary type conversions
86-
- unparam # Reports unused function parameters
87-
- goconst # Finds repeated strings that could be replaced by a constant
88-
- gocyclo # Computes and checks the cyclomatic complexity of functions
89-
90-
issues:
91-
# Maximum issues count per one linter
92-
max-issues-per-linter: 0
93-
94-
# Maximum count of issues with the same text
95-
max-same-issues: 0
96-
97-
# Show only new issues
98-
new: false
99-
100-
# Exclude some linters from running on tests files
101-
exclude-rules:
102-
# Exclude some linters from running on tests files
103-
- path: _test\.go
104-
linters:
105-
- errcheck
106-
- gosec
107-
- goconst
108-
109-
# Exclude generated files
110-
- path: "zz_generated.*\\.go"
111-
linters:
112-
- all
113-
114-
# Exclude client-go generated code
115-
- path: client-go/
116-
linters:
117-
- all
118-
119-
# Exclude some issues from staticcheck
120-
- linters:
121-
- staticcheck
122-
text: "SA1019:" # Ignore deprecation warnings
123-
124-
# Exclude some common issues
125-
- text: "G404:" # Ignore weak random number generator in non-security contexts
126-
linters:
127-
- gosec
44+
- errcheck
45+
- govet
46+
- ineffassign
47+
- staticcheck
48+
- unused
49+
- misspell
50+
- revive
51+
- gosec
52+
- unconvert
53+
- unparam
54+
- goconst
55+
- gocyclo

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{

pkg/workloadmanager/handlers.go

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ func (s *Server) handleHealth(c *gin.Context) {
2525
}
2626

2727
// handleCreateSandbox handles sandbox creation requests
28+
// nolint :gocyclo
2829
func (s *Server) handleCreateSandbox(c *gin.Context) {
2930
createAgentRequest := &types.CreateSandboxRequest{}
3031
if err := c.ShouldBindJSON(createAgentRequest); err != nil {
@@ -162,12 +163,7 @@ func (s *Server) handleCreateSandbox(c *gin.Context) {
162163
return
163164
}
164165

165-
redisCacheInfo, err := convertSandboxToRedisCache(createdSandbox, podIP, externalInfo)
166-
if err != nil {
167-
log.Printf("convert to redis cache info failed: %v", err)
168-
respondError(c, http.StatusInternalServerError, "SANDBOX_BUILD_FAILED", err.Error())
169-
return
170-
}
166+
redisCacheInfo := convertSandboxToRedisCache(createdSandbox, podIP, externalInfo)
171167

172168
response := &types.CreateSandboxResponse{
173169
SessionID: externalInfo.SessionID,

pkg/workloadmanager/k8s_client.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ const (
2828

2929
var (
3030
// SessionIdLabelKey labels key for session id
31-
SessionIdLabelKey = "runtime.agentcube.io/session-id"
31+
SessionIdLabelKey = "runtime.agentcube.io/session-id" // revive:disable-line:var-naming - keep label backward compatible
3232
// WorkloadNameLabelKey labels key for workload name
3333
WorkloadNameLabelKey = "runtime.agentcube.io/workload-name"
3434
// Annotation key for last activity time
@@ -142,7 +142,7 @@ func (c *K8sClient) GetOrCreateUserK8sClient(userToken, namespace, serviceAccoun
142142
cacheKey := makeCacheKey(namespace, serviceAccountName)
143143

144144
// Try to get from cache
145-
if cachedClient := c.clientCache.Get(cacheKey, userToken); cachedClient != nil {
145+
if cachedClient := c.clientCache.Get(cacheKey); cachedClient != nil {
146146
return cachedClient, nil
147147
}
148148

0 commit comments

Comments
 (0)