Skip to content

Commit a7bf805

Browse files
programmer04pmalek
andauthored
chore(ci): add modernize as one of the linter steps (#7603)
* chore(ci): add modernize as one of the linter steps * Update internal/annotations/annotations.go Co-authored-by: Patryk Małek <patryk.malek@konghq.com> --------- Co-authored-by: Patryk Małek <patryk.malek@konghq.com>
1 parent 3b37296 commit a7bf805

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

+352
-354
lines changed

.tools_versions.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ controller-tools: "0.18.0"
44
kustomize: "5.7.0"
55
# renovate: datasource=github-releases depName=golangci/golangci-lint
66
golangci-lint: "2.2.1"
7+
# TODO: Renovate comment is missing. This needs to be added at some point.
8+
modernize: "v0.19.1"
79
# renovate: datasource=github-releases depName=GoogleContainerTools/skaffold
810
skaffold: "2.16.1"
911
# renovate: datasource=github-releases depName=kubernetes-sigs/controller-runtime

Makefile

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,13 @@ download.shellcheck: mise yq ## Download shellcheck locally if necessary.
154154
@$(MISE) plugin install --yes -q shellcheck
155155
@$(MISE) install -q shellcheck@$(SHELLCHECK_VERSION)
156156

157+
MODERNIZE_VERSION = $(shell $(YQ) -r '.modernize' < $(TOOLS_VERSIONS_FILE))
158+
MODERNIZE = $(PROJECT_DIR)/bin/modernize
159+
.PHONY: modernize
160+
modernize: yq
161+
GOBIN=$(PROJECT_DIR)/bin go install -v \
162+
golang.org/x/tools/gopls/internal/analysis/modernize/cmd/modernize@$(MODERNIZE_VERSION)
163+
157164
# ------------------------------------------------------------------------------
158165
# Build
159166
# ------------------------------------------------------------------------------
@@ -203,8 +210,13 @@ _build.template.debug:
203210
fmt:
204211
go fmt ./...
205212

213+
214+
.PHONY: lint.modernize
215+
lint.modernize: modernize
216+
$(MODERNIZE) ./...
217+
206218
.PHONY: lint
207-
lint: verify.tidy golangci-lint staticcheck
219+
lint: verify.tidy golangci-lint lint.modernize staticcheck
208220

209221
.PHONY: lint.actions
210222
lint.actions: download.actionlint download.shellcheck

controllers/license/konglicense_controller.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ func NewLicenseCache() cache.Store {
121121
return cache.NewStore(kongLicenseKeyFunc)
122122
}
123123

124-
func kongLicenseKeyFunc(obj interface{}) (string, error) {
124+
func kongLicenseKeyFunc(obj any) (string, error) {
125125
l, ok := obj.(*configurationv1alpha1.KongLicense)
126126
if !ok {
127127
return "", fmt.Errorf("object is type %T, not KongLicense", obj)

internal/adminapi/client.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ func (c *Client) GetKongVersion(ctx context.Context) (string, error) {
134134
return version, nil
135135
}
136136

137-
func extractStringFromRoot(data map[string]interface{}, key string) (string, error) {
137+
func extractStringFromRoot(data map[string]any, key string) (string, error) {
138138
val, ok := data[key]
139139
if !ok {
140140
return "", fmt.Errorf("%q key not found", key)

internal/admission/validator.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -658,7 +658,7 @@ func (validator KongHTTPValidator) ValidateCustomEntity(ctx context.Context, ent
658658
// The admission webhook needs a placeholder value because the actual value isn't present in the KongCustomEntity spec.
659659
// It will be derived from the resource(s) used by the parent KongPlugin during translation.
660660
if field.Required && field.Type == kongstate.EntityFieldTypeForeign {
661-
fields[fieldName] = map[string]interface{}{
661+
fields[fieldName] = map[string]any{
662662
"id": util.DefaultUUIDGenerator{}.NewString(),
663663
}
664664
}

internal/admission/validator_test.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1518,10 +1518,10 @@ func TestValidator_ValidateCustomEntity(t *testing.T) {
15181518

15191519
for _, tc := range testCases {
15201520
t.Run(tc.name, func(t *testing.T) {
1521-
fieldList := make([]interface{}, 0, len(tc.fields))
1521+
fieldList := make([]any, 0, len(tc.fields))
15221522
for _, field := range tc.fields {
1523-
fieldList = append(fieldList, map[string]interface{}{
1524-
field.Name: map[string]interface{}{
1523+
fieldList = append(fieldList, map[string]any{
1524+
field.Name: map[string]any{
15251525
"type": string(field.Type),
15261526
"required": field.Required,
15271527
"reference": field.Reference,
@@ -1533,7 +1533,7 @@ func TestValidator_ValidateCustomEntity(t *testing.T) {
15331533
AdminAPIServicesProvider: fakeServicesProvider{
15341534
schemaSvc: fakeSchemaSvc{
15351535
entityTypeExist: tc.entityTypeExist,
1536-
schema: map[string]interface{}{
1536+
schema: map[string]any{
15371537
"fields": fieldList,
15381538
},
15391539
shouldFail: tc.validateSvcFail,
@@ -1550,7 +1550,7 @@ func TestValidator_ValidateCustomEntity(t *testing.T) {
15501550
}
15511551

15521552
type fakeSchemaSvc struct {
1553-
schema map[string]interface{}
1553+
schema map[string]any
15541554
entityTypeExist bool
15551555
shouldFail bool
15561556
}
@@ -1564,7 +1564,7 @@ func (s fakeSchemaSvc) Get(_ context.Context, _ string) (kong.Schema, error) {
15641564
return s.schema, nil
15651565
}
15661566

1567-
func (s fakeSchemaSvc) Validate(_ context.Context, _ kong.EntityType, _ interface{}) (bool, string, error) {
1567+
func (s fakeSchemaSvc) Validate(_ context.Context, _ kong.EntityType, _ any) (bool, string, error) {
15681568
if s.shouldFail {
15691569
return false, "something is wrong in the entity", nil
15701570
}

internal/annotations/annotations.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -293,8 +293,7 @@ func ExtractHeaders(anns map[string]string) (map[string][]string, bool) {
293293
separator = ","
294294
}
295295
for key, val := range anns {
296-
if strings.HasPrefix(key, prefix) {
297-
header := strings.TrimPrefix(key, prefix)
296+
if header, ok0 := strings.CutPrefix(key, prefix); ok0 {
298297
if len(header) == 0 || len(val) == 0 {
299298
continue
300299
}

internal/annotations/annotations_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ func TestIngressClassValidatorFunc(t *testing.T) {
7676

7777
data := map[string]string{}
7878
ing.SetAnnotations(data)
79-
for i := 0; i < len(tests); i++ {
79+
for i := range tests {
8080
test := tests[i]
8181
ing.Annotations[IngressClassKey] = test.ingress
8282
ingv1.Spec.IngressClassName = &test.ingress

internal/clients/config_status_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,11 @@ func TestChannelConfigNotifier(t *testing.T) {
1919
ch := n.SubscribeGatewayConfigStatus()
2020

2121
// Call NotifyConfigStatus 5 times to make sure that the method is non-blocking.
22-
for i := 0; i < 5; i++ {
22+
for range 5 {
2323
n.NotifyGatewayConfigStatus(ctx, clients.GatewayConfigApplyStatus{})
2424
}
2525

26-
for i := 0; i < 5; i++ {
26+
for i := range 5 {
2727
select {
2828
case <-ch:
2929
case <-time.After(time.Second):

internal/clients/manager_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -354,7 +354,7 @@ func TestAdminAPIClientsManager_ConcurrentNotify(t *testing.T) {
354354
}()
355355

356356
go func() {
357-
for i := 0; i < 100; i++ {
357+
for range 100 {
358358
go m.Notify(ctx, []adminapi.DiscoveredAdminAPI{testDiscoveredAdminAPI(testURL1)})
359359
}
360360
}()

0 commit comments

Comments
 (0)