Skip to content

Commit 88eef88

Browse files
authored
Merge pull request #16 from pmady/fix/lint-issues-pr12
fix(lint): address lint issues from PR #12
2 parents b531522 + 53d968c commit 88eef88

File tree

7 files changed

+71
-65
lines changed

7 files changed

+71
-65
lines changed

.golangci.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,11 @@ linters:
2020
- gosec
2121
exclusions:
2222
generated: strict
23+
rules:
24+
- path: 'pkg/scaling/algorithm\.go'
25+
text: 'type name will be used as scaling\.(ScalingAlgorithm|ScalingInput|ScalingResult)'
26+
linters:
27+
- revive
2328

2429
formatters:
2530
enable:

pkg/controller/reconciler_test.go

Lines changed: 56 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,14 @@ import (
3030

3131
func TestCalculateDesiredReplicas(t *testing.T) {
3232
tests := []struct {
33-
name string
34-
policy *kubeaiv1alpha1.AIInferenceAutoscalerPolicy
35-
currentReplicas int32
36-
currentMetrics *kubeaiv1alpha1.CurrentMetrics
37-
expected int32
38-
expectedAlgorithm string
39-
expectedRequestedAlgoNotFound bool
40-
expectedRequestedName string
33+
name string
34+
policy *kubeaiv1alpha1.AIInferenceAutoscalerPolicy
35+
currentReplicas int32
36+
currentMetrics *kubeaiv1alpha1.CurrentMetrics
37+
expected int32
38+
expectedAlgorithm string
39+
expectedRequestedAlgoNotFound bool
40+
expectedRequestedName string
4141
}{
4242
{
4343
name: "scale up based on latency",
@@ -53,12 +53,12 @@ func TestCalculateDesiredReplicas(t *testing.T) {
5353
},
5454
},
5555
},
56-
currentReplicas: 2,
57-
currentMetrics: &kubeaiv1alpha1.CurrentMetrics{LatencyP99Ms: 200},
58-
expected: 4,
59-
expectedAlgorithm: "MaxRatio",
60-
expectedRequestedAlgoNotFound: false,
61-
expectedRequestedName: "",
56+
currentReplicas: 2,
57+
currentMetrics: &kubeaiv1alpha1.CurrentMetrics{LatencyP99Ms: 200},
58+
expected: 4,
59+
expectedAlgorithm: "MaxRatio",
60+
expectedRequestedAlgoNotFound: false,
61+
expectedRequestedName: "",
6262
},
6363
{
6464
name: "scale up based on GPU utilization",
@@ -74,12 +74,12 @@ func TestCalculateDesiredReplicas(t *testing.T) {
7474
},
7575
},
7676
},
77-
currentReplicas: 2,
78-
currentMetrics: &kubeaiv1alpha1.CurrentMetrics{GPUUtilizationPercent: 100},
79-
expected: 4,
80-
expectedAlgorithm: "MaxRatio",
81-
expectedRequestedAlgoNotFound: false,
82-
expectedRequestedName: "",
77+
currentReplicas: 2,
78+
currentMetrics: &kubeaiv1alpha1.CurrentMetrics{GPUUtilizationPercent: 100},
79+
expected: 4,
80+
expectedAlgorithm: "MaxRatio",
81+
expectedRequestedAlgoNotFound: false,
82+
expectedRequestedName: "",
8383
},
8484
{
8585
name: "respect max replicas",
@@ -95,12 +95,12 @@ func TestCalculateDesiredReplicas(t *testing.T) {
9595
},
9696
},
9797
},
98-
currentReplicas: 3,
99-
currentMetrics: &kubeaiv1alpha1.CurrentMetrics{LatencyP99Ms: 500},
100-
expected: 5,
101-
expectedAlgorithm: "MaxRatio",
102-
expectedRequestedAlgoNotFound: false,
103-
expectedRequestedName: "",
98+
currentReplicas: 3,
99+
currentMetrics: &kubeaiv1alpha1.CurrentMetrics{LatencyP99Ms: 500},
100+
expected: 5,
101+
expectedAlgorithm: "MaxRatio",
102+
expectedRequestedAlgoNotFound: false,
103+
expectedRequestedName: "",
104104
},
105105
{
106106
name: "respect min replicas",
@@ -116,12 +116,12 @@ func TestCalculateDesiredReplicas(t *testing.T) {
116116
},
117117
},
118118
},
119-
currentReplicas: 1,
120-
currentMetrics: &kubeaiv1alpha1.CurrentMetrics{LatencyP99Ms: 100},
121-
expected: 2,
122-
expectedAlgorithm: "MaxRatio",
123-
expectedRequestedAlgoNotFound: false,
124-
expectedRequestedName: "",
119+
currentReplicas: 1,
120+
currentMetrics: &kubeaiv1alpha1.CurrentMetrics{LatencyP99Ms: 100},
121+
expected: 2,
122+
expectedAlgorithm: "MaxRatio",
123+
expectedRequestedAlgoNotFound: false,
124+
expectedRequestedName: "",
125125
},
126126
{
127127
name: "no scaling when at target",
@@ -137,12 +137,12 @@ func TestCalculateDesiredReplicas(t *testing.T) {
137137
},
138138
},
139139
},
140-
currentReplicas: 3,
141-
currentMetrics: &kubeaiv1alpha1.CurrentMetrics{LatencyP99Ms: 100},
142-
expected: 3,
143-
expectedAlgorithm: "MaxRatio",
144-
expectedRequestedAlgoNotFound: false,
145-
expectedRequestedName: "",
140+
currentReplicas: 3,
141+
currentMetrics: &kubeaiv1alpha1.CurrentMetrics{LatencyP99Ms: 100},
142+
expected: 3,
143+
expectedAlgorithm: "MaxRatio",
144+
expectedRequestedAlgoNotFound: false,
145+
expectedRequestedName: "",
146146
},
147147
{
148148
name: "use highest ratio from multiple metrics",
@@ -162,12 +162,12 @@ func TestCalculateDesiredReplicas(t *testing.T) {
162162
},
163163
},
164164
},
165-
currentReplicas: 2,
166-
currentMetrics: &kubeaiv1alpha1.CurrentMetrics{LatencyP99Ms: 150, GPUUtilizationPercent: 150},
167-
expected: 6,
168-
expectedAlgorithm: "MaxRatio",
169-
expectedRequestedAlgoNotFound: false,
170-
expectedRequestedName: "",
165+
currentReplicas: 2,
166+
currentMetrics: &kubeaiv1alpha1.CurrentMetrics{LatencyP99Ms: 150, GPUUtilizationPercent: 150},
167+
expected: 6,
168+
expectedAlgorithm: "MaxRatio",
169+
expectedRequestedAlgoNotFound: false,
170+
expectedRequestedName: "",
171171
},
172172
{
173173
name: "use AverageRatio algorithm",
@@ -191,12 +191,12 @@ func TestCalculateDesiredReplicas(t *testing.T) {
191191
},
192192
},
193193
},
194-
currentReplicas: 2,
195-
currentMetrics: &kubeaiv1alpha1.CurrentMetrics{LatencyP99Ms: 200, GPUUtilizationPercent: 100},
196-
expected: 4, // avg of 2.0 and 2.0 = 2.0, 2 * 2.0 = 4
197-
expectedAlgorithm: "AverageRatio",
198-
expectedRequestedAlgoNotFound: false,
199-
expectedRequestedName: "AverageRatio",
194+
currentReplicas: 2,
195+
currentMetrics: &kubeaiv1alpha1.CurrentMetrics{LatencyP99Ms: 200, GPUUtilizationPercent: 100},
196+
expected: 4, // avg of 2.0 and 2.0 = 2.0, 2 * 2.0 = 4
197+
expectedAlgorithm: "AverageRatio",
198+
expectedRequestedAlgoNotFound: false,
199+
expectedRequestedName: "AverageRatio",
200200
},
201201
{
202202
name: "fallback to MaxRatio for unknown algorithm",
@@ -215,12 +215,12 @@ func TestCalculateDesiredReplicas(t *testing.T) {
215215
},
216216
},
217217
},
218-
currentReplicas: 2,
219-
currentMetrics: &kubeaiv1alpha1.CurrentMetrics{LatencyP99Ms: 200},
220-
expected: 4,
221-
expectedAlgorithm: "MaxRatio", // Falls back
222-
expectedRequestedAlgoNotFound: true,
223-
expectedRequestedName: "NonExistentAlgorithm",
218+
currentReplicas: 2,
219+
currentMetrics: &kubeaiv1alpha1.CurrentMetrics{LatencyP99Ms: 200},
220+
expected: 4,
221+
expectedAlgorithm: "MaxRatio", // Falls back
222+
expectedRequestedAlgoNotFound: true,
223+
expectedRequestedName: "NonExistentAlgorithm",
224224
},
225225
}
226226

pkg/scaling/algorithm.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ func (a *MaxRatioAlgorithm) Name() string {
8282
}
8383

8484
// ComputeScale implements the ScalingAlgorithm interface
85-
func (a *MaxRatioAlgorithm) ComputeScale(ctx context.Context, input ScalingInput) (ScalingResult, error) {
85+
func (a *MaxRatioAlgorithm) ComputeScale(_ context.Context, input ScalingInput) (ScalingResult, error) {
8686
tolerance := input.Tolerance
8787

8888
if len(input.MetricRatios) == 0 {
@@ -192,7 +192,7 @@ func (a *AverageRatioAlgorithm) Name() string {
192192
}
193193

194194
// ComputeScale implements the ScalingAlgorithm interface
195-
func (a *AverageRatioAlgorithm) ComputeScale(ctx context.Context, input ScalingInput) (ScalingResult, error) {
195+
func (a *AverageRatioAlgorithm) ComputeScale(_ context.Context, input ScalingInput) (ScalingResult, error) {
196196
tolerance := input.Tolerance
197197

198198
if len(input.MetricRatios) == 0 {
@@ -307,7 +307,7 @@ func (a *WeightedRatioAlgorithm) SetWeights(weights []float64) {
307307
}
308308

309309
// ComputeScale implements the ScalingAlgorithm interface
310-
func (a *WeightedRatioAlgorithm) ComputeScale(ctx context.Context, input ScalingInput) (ScalingResult, error) {
310+
func (a *WeightedRatioAlgorithm) ComputeScale(_ context.Context, input ScalingInput) (ScalingResult, error) {
311311
tolerance := input.Tolerance
312312

313313
if len(input.MetricRatios) == 0 {

pkg/scaling/algorithm_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -320,7 +320,7 @@ func TestScalingAlgorithm_ToleranceFromInput(t *testing.T) {
320320
assert.Equal(t, int32(3), result.DesiredReplicas)
321321
}
322322

323-
func TestScalingAlgorithm_ImplementsInterface(t *testing.T) {
323+
func TestScalingAlgorithm_ImplementsInterface(_ *testing.T) {
324324
// Verify all algorithms implement ScalingAlgorithm
325325
var _ ScalingAlgorithm = (*MaxRatioAlgorithm)(nil)
326326
var _ ScalingAlgorithm = (*AverageRatioAlgorithm)(nil)

pkg/scaling/plugin_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ func TestLoadPlugin_InvalidFile(t *testing.T) {
4040
tmpDir := t.TempDir()
4141
invalidPlugin := filepath.Join(tmpDir, "invalid.so")
4242

43-
err := os.WriteFile(invalidPlugin, []byte("not a plugin"), 0644)
43+
err := os.WriteFile(invalidPlugin, []byte("not a plugin"), 0600) // #nosec G306
4444
assert.NoError(t, err)
4545

4646
_, err = LoadPlugin(invalidPlugin)
@@ -62,7 +62,7 @@ func TestLoadPlugins_NotADirectory(t *testing.T) {
6262
tmpDir := t.TempDir()
6363
tmpFile := filepath.Join(tmpDir, "notadir")
6464

65-
err := os.WriteFile(tmpFile, []byte("file"), 0644)
65+
err := os.WriteFile(tmpFile, []byte("file"), 0600) // #nosec G306
6666
assert.NoError(t, err)
6767

6868
_, err = LoadPlugins(tmpFile)

pkg/scaling/registry.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ func (e ErrAlgorithmAlreadyRegistered) Error() string {
4141
return fmt.Sprintf("algorithm already registered: name=%q", e.Name)
4242
}
4343

44+
// ErrInvalidAlgorithmName is returned when an algorithm name is empty
4445
type ErrInvalidAlgorithmName struct{}
4546

4647
func (e ErrInvalidAlgorithmName) Error() string {

pkg/scaling/registry_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ func (m *mockAlgorithm) Name() string {
3434
return m.name
3535
}
3636

37-
func (m *mockAlgorithm) ComputeScale(ctx context.Context, input ScalingInput) (ScalingResult, error) {
37+
func (m *mockAlgorithm) ComputeScale(_ context.Context, input ScalingInput) (ScalingResult, error) {
3838
return ScalingResult{
3939
DesiredReplicas: input.CurrentReplicas,
4040
Reason: "mock",
@@ -110,9 +110,9 @@ func TestRegistry_Get(t *testing.T) {
110110
require.NoError(t, r.Register(algo))
111111

112112
tests := []struct {
113-
name string
113+
name string
114114
algoName string
115-
wantErr bool
115+
wantErr bool
116116
}{
117117
{
118118
name: "get existing algorithm",

0 commit comments

Comments
 (0)