Skip to content

Commit 516b15c

Browse files
authored
add modernize linter (#12842)
Signed-off-by: Jenny Shu <[email protected]>
1 parent d5b245a commit 516b15c

File tree

62 files changed

+239
-248
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

62 files changed

+239
-248
lines changed

.custom-gcl.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
version: v2.5.0
1+
version: v2.6.1
22
name: golangci-lint-custom
33
destination: _output
44
plugins:

.golangci.yaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ linters:
2222
- krtequals
2323
- kubeapilinter
2424
- misspell
25+
- modernize
2526
- nakedret
2627
- predeclared
2728
- promlinter
@@ -106,6 +107,10 @@ linters:
106107
misspell:
107108
ignore-rules:
108109
- kgateway
110+
modernize:
111+
disable:
112+
# disabled because modernize gets confused with conflicting edits. we can probably rely on the kube api linter instead for this one.
113+
- omitzero
109114
nakedret:
110115
# The team consensus is that naked returns hinder the readability of the code.
111116
# However, named return values can still be useful as documentation for certain scenarios.

api/settings/settings_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -287,9 +287,9 @@ var (
287287

288288
// expectedEnvVars returns a map of all the env vars that should be set for the given Settings value.
289289
// The value of the map is the default value of the field.
290-
func expectedEnvVars(settingsValue reflect.Value) map[string]interface{} {
290+
func expectedEnvVars(settingsValue reflect.Value) map[string]any {
291291
// This is a modified version of the code in https://github.com/kelseyhightower/envconfig/blob/7834011875d613aec60c606b52c2b0fe8949fe91/envconfig.go#L102-L128
292-
expectedEnvVars := make(map[string]interface{}, settingsValue.NumField())
292+
expectedEnvVars := make(map[string]any, settingsValue.NumField())
293293
for i := 0; i < settingsValue.NumField(); i++ {
294294
fieldType := settingsValue.Type().Field(i)
295295
splitWords := fieldType.Tag.Get("split_words") == "true"

internal/envoyinit/pkg/downward/downward.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -131,8 +131,8 @@ func (di *downwardInjectable) PodAnnotations() map[string]string { return di.pod
131131
func parse(data []byte) map[string]string {
132132
m := map[string]string{}
133133

134-
lines := strings.Split(string(data), "\n")
135-
for _, l := range lines {
134+
lines := strings.SplitSeq(string(data), "\n")
135+
for l := range lines {
136136
l = strings.TrimSpace(l)
137137
parts := strings.SplitN(l, "=", 2)
138138
if len(parts) != 2 {

internal/kgateway/admin/logging.go

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ func getLoggingDescription() string {
2020
componentLevels := logging.GetComponentLevels()
2121

2222
// Build component selector
23-
componentSelector := `<select id="componentselector">`
23+
var componentSelector strings.Builder
24+
componentSelector.WriteString(`<select id="componentselector">`)
2425
// Ensure consistent order for the selector
2526
components := make([]string, 0, len(componentLevels))
2627
for comp := range componentLevels {
@@ -29,29 +30,31 @@ func getLoggingDescription() string {
2930
sort.Strings(components) // Sort alphabetically
3031

3132
for _, comp := range components {
32-
componentSelector += fmt.Sprintf(`<option value="%s">%s (Current: %s)</option>`, comp, comp, logging.LevelToString(componentLevels[comp]))
33+
componentSelector.WriteString(fmt.Sprintf(`<option value="%s">%s (Current: %s)</option>`, comp, comp, logging.LevelToString(componentLevels[comp])))
3334
}
34-
componentSelector += `</select>`
35+
componentSelector.WriteString(`</select>`)
3536

3637
// Build level selector
37-
levelSelector := `<select id="loglevelselector">`
38+
var levelSelector strings.Builder
39+
levelSelector.WriteString(`<select id="loglevelselector">`)
3840
supportedLogLevels := []string{"trace", "debug", "info", "warn", "error"}
3941
for _, level := range supportedLogLevels {
40-
levelSelector += fmt.Sprintf(`<option value="%s">%s</option>`, level, strings.ToUpper(level))
42+
levelSelector.WriteString(fmt.Sprintf(`<option value="%s">%s</option>`, level, strings.ToUpper(level)))
4143
}
42-
levelSelector += `</select>`
44+
levelSelector.WriteString(`</select>`)
4345

4446
// Display current levels
45-
currentLevelsDisplay := "<h4>Current Levels:</h4><ul>"
47+
var currentLevelsDisplay strings.Builder
48+
currentLevelsDisplay.WriteString("<h4>Current Levels:</h4><ul>")
4649
for _, comp := range components {
47-
currentLevelsDisplay += fmt.Sprintf("<li>%s: %s</li>", comp, logging.LevelToString(componentLevels[comp]))
50+
currentLevelsDisplay.WriteString(fmt.Sprintf("<li>%s: %s</li>", comp, logging.LevelToString(componentLevels[comp])))
4851
}
49-
currentLevelsDisplay += "</ul><hr/>"
52+
currentLevelsDisplay.WriteString("</ul><hr/>")
5053

51-
return currentLevelsDisplay + `Set log level for a specific component.<br/>
54+
return currentLevelsDisplay.String() + `Set log level for a specific component.<br/>
5255
53-
Component: ` + componentSelector + `
54-
Level: ` + levelSelector + `
56+
Component: ` + componentSelector.String() + `
57+
Level: ` + levelSelector.String() + `
5558
5659
<button onclick="setlevel()">Set Component Level</button>
5760
<button onclick="setAllLevels()">Set All Levels</button>

internal/kgateway/admin/response.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ import (
99
// It allows us to encapsulate data and errors together, so that if an issue occurs during the request,
1010
// we can get access to all the relevant information
1111
type SnapshotResponseData struct {
12-
Data interface{} `json:"data"`
13-
Error error `json:"error"`
12+
Data any `json:"data"`
13+
Error error `json:"error"`
1414
}
1515

1616
// OutputFormat identifies the format to output an object
@@ -38,8 +38,8 @@ func (r SnapshotResponseData) Format(format OutputFormat) ([]byte, error) {
3838
errorMsg = r.Error.Error()
3939
}
4040
anon := struct {
41-
Data interface{} `json:"data"`
42-
Error string `json:"error"`
41+
Data any `json:"data"`
42+
Error string `json:"error"`
4343
}{
4444
Data: r.Data,
4545
Error: errorMsg,
@@ -61,7 +61,7 @@ func (r SnapshotResponseData) MarshalJSONString() string {
6161
}
6262

6363
// formatOutput formats a generic object into the specified output format
64-
func formatOutput(format OutputFormat, genericOutput interface{}) ([]byte, error) {
64+
func formatOutput(format OutputFormat, genericOutput any) ([]byte, error) {
6565
switch format {
6666
case Json:
6767
return json.MarshalIndent(genericOutput, "", " ")
@@ -76,7 +76,7 @@ func formatOutput(format OutputFormat, genericOutput interface{}) ([]byte, error
7676
}
7777
}
7878

79-
func completeSnapshotResponse(data interface{}) SnapshotResponseData {
79+
func completeSnapshotResponse(data any) SnapshotResponseData {
8080
return SnapshotResponseData{
8181
Data: data,
8282
Error: nil,

internal/kgateway/admin/xds_snapshot.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ func addXdsSnapshotHandler(path string, mux *http.ServeMux, profiles map[string]
1919

2020
func getXdsSnapshotDataFromCache(xdsCache cache.SnapshotCache) SnapshotResponseData {
2121
cacheKeys := xdsCache.GetStatusKeys()
22-
cacheEntries := make(map[string]interface{}, len(cacheKeys))
22+
cacheEntries := make(map[string]any, len(cacheKeys))
2323

2424
for _, k := range cacheKeys {
2525
xdsSnapshot, err := getXdsSnapshot(xdsCache, k)

internal/kgateway/agentgatewaysyncer/agentgateway_testutils.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ func (tr *translationResult) MarshalJSON() ([]byte, error) {
7676
}
7777

7878
// Create a map to hold the marshaled fields
79-
result := make(map[string]interface{})
79+
result := make(map[string]any)
8080

8181
// Marshal each field using protojson
8282
if len(tr.Routes) > 0 {
@@ -257,14 +257,14 @@ func (tr *translationResult) UnmarshalJSON(data []byte) error {
257257
return nil
258258
}
259259

260-
func marshalProtoMessages[T proto.Message](messages []T, m protojson.MarshalOptions) ([]interface{}, error) {
261-
var result []interface{}
260+
func marshalProtoMessages[T proto.Message](messages []T, m protojson.MarshalOptions) ([]any, error) {
261+
var result []any
262262
for _, msg := range messages {
263263
data, err := m.Marshal(msg)
264264
if err != nil {
265265
return nil, err
266266
}
267-
var jsonObj interface{}
267+
var jsonObj any
268268
if err := json.Unmarshal(data, &jsonObj); err != nil {
269269
return nil, err
270270
}
@@ -444,7 +444,7 @@ func sortTranslationResult(tr *translationResult) *translationResult {
444444
return tr
445445
}
446446

447-
func ReadYamlFile(file string, out interface{}) error {
447+
func ReadYamlFile(file string, out any) error {
448448
data, err := os.ReadFile(file)
449449
if err != nil {
450450
return err

internal/kgateway/agentgatewaysyncer/krtxds/pushqueue_test.go

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ func ExpectDequeue(t *testing.T, p *PushQueue, expected *Connection) {
6161

6262
func TestProxyQueue(t *testing.T) {
6363
proxies := make([]*Connection, 0, 100)
64-
for p := 0; p < 100; p++ {
64+
for p := range 100 {
6565
conn := &Connection{}
6666
conn.SetID(fmt.Sprintf("proxy-%d", p))
6767
proxies = append(proxies, conn)
@@ -137,11 +137,9 @@ func TestProxyQueue(t *testing.T) {
137137
defer p.ShutDown()
138138

139139
wg := &sync.WaitGroup{}
140-
wg.Add(1)
141-
go func() {
140+
wg.Go(func() {
142141
ExpectDequeue(t, p, proxies[0])
143-
wg.Done()
144-
}()
142+
})
145143
time.Sleep(time.Millisecond * 50)
146144
p.Enqueue(proxies[0], &PushRequest{})
147145
wg.Wait()
@@ -214,13 +212,13 @@ func TestProxyQueue(t *testing.T) {
214212
// We will trigger many pushes for eds services to each proxy. In the end we will expect
215213
// all of these to be dequeue, but order is not deterministic.
216214
expected := sets.String{}
217-
for eds := 0; eds < 100; eds++ {
215+
for eds := range 100 {
218216
for _, pr := range proxies {
219217
expected.Insert(key(pr, fmt.Sprintf("%d", eds)))
220218
}
221219
}
222220
go func() {
223-
for eds := 0; eds < 100; eds++ {
221+
for eds := range 100 {
224222
for _, pr := range proxies {
225223
p.Enqueue(pr, &PushRequest{
226224
ConfigsUpdated: map[TypeUrl]sets.String{
@@ -271,12 +269,12 @@ func TestProxyQueue(t *testing.T) {
271269
// We will trigger many pushes for eds services to the proxy. In the end we will expect
272270
// all of these to be dequeue, but order is deterministic.
273271
expected := make([]string, 100)
274-
for eds := 0; eds < 100; eds++ {
272+
for eds := range 100 {
275273
expected[eds] = fmt.Sprintf("%d", eds)
276274
}
277275
go func() {
278276
// send to pushQueue
279-
for eds := 0; eds < 100; eds++ {
277+
for eds := range 100 {
280278
p.Enqueue(con, &PushRequest{
281279
ConfigsUpdated: map[TypeUrl]sets.String{
282280
"type1": sets.New(fmt.Sprintf("%d", eds)),

internal/kgateway/agentgatewaysyncer/krtxds/xds.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1037,20 +1037,20 @@ func debounce(ch chan *PushRequest, stopCh <-chan struct{}, opts DebounceOptions
10371037
}
10381038

10391039
func configsUpdated(req *PushRequest) string {
1040-
configs := ""
1040+
var configs strings.Builder
10411041
count := 0
10421042
for _, keys := range req.ConfigsUpdated {
10431043
count += len(keys)
10441044
for key := range keys {
1045-
configs += key
1045+
configs.WriteString(key)
10461046
break
10471047
}
10481048
}
10491049
if count > 1 {
10501050
more := " and " + strconv.Itoa(count-1) + " more configs"
1051-
configs += more
1051+
configs.WriteString(more)
10521052
}
1053-
return configs
1053+
return configs.String()
10541054
}
10551055

10561056
func nonce(noncePrefix string) string {

0 commit comments

Comments
 (0)