Skip to content

Commit 27408d6

Browse files
authored
Modernize Go idioms and remove outdated patterns (#1086)
* Modernize Go idioms and remove outdated patterns Signed-off-by: Aravindhan Ayyanathan <aravindhan.a@est.tech> * Address review comments Signed-off-by: Aravindhan Ayyanathan <aravindhan.a@est.tech> --------- Signed-off-by: Aravindhan Ayyanathan <aravindhan.a@est.tech>
1 parent 093335b commit 27408d6

45 files changed

Lines changed: 259 additions & 197 deletions

File tree

Some content is hidden

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

api/porch/fuzzer/fuzzer.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2022 The kpt Authors
1+
// Copyright 2022, 2026 The kpt Authors
22
//
33
// Licensed under the Apache License, Version 2.0 (the "License");
44
// you may not use this file except in compliance with the License.
@@ -18,6 +18,6 @@ import (
1818
runtimeserializer "k8s.io/apimachinery/pkg/runtime/serializer"
1919
)
2020

21-
var Funcs = func(codecs runtimeserializer.CodecFactory) []interface{} {
22-
return []interface{}{}
21+
var Funcs = func(codecs runtimeserializer.CodecFactory) []any {
22+
return []any{}
2323
}

controllers/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ import (
2525
"strings"
2626
"time"
2727

28-
"golang.org/x/exp/slices"
28+
"slices"
2929

3030
// Import all Kubernetes client auth plugins (e.g. Azure, GCP, OIDC, etc.)
3131
// to ensure that exec-entrypoint and run can make use of them.

controllers/packagevariants/pkg/controllers/packagevariant/injection-with-workspacename_test.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2023 The kpt Authors
1+
// Copyright 2023, 2026 The kpt Authors
22
//
33
// Licensed under the Apache License, Version 2.0 (the "License");
44
// you may not use this file except in compliance with the License.
@@ -17,7 +17,8 @@ package packagevariant
1717
import (
1818
"context"
1919
"fmt"
20-
"sort"
20+
"slices"
21+
"strings"
2122
"testing"
2223

2324
kptfilev1 "github.com/kptdev/kpt/api/kptfile/v1"
@@ -234,9 +235,9 @@ spec:
234235
require.Equal(t, len(tc.expected), len(actualInjectionPoints))
235236

236237
// ensure a stable ordering
237-
sort.Slice(actualInjectionPoints,
238-
func(i, j int) bool {
239-
return actualInjectionPoints[i].conditionType < actualInjectionPoints[j].conditionType
238+
slices.SortFunc(actualInjectionPoints,
239+
func(a, b *injectionPoint) int {
240+
return strings.Compare(a.conditionType, b.conditionType)
240241
})
241242
for i, ip := range actualInjectionPoints {
242243
require.Equal(t, tc.expected[i].conditionType, ip.conditionType)

controllers/packagevariants/pkg/controllers/packagevariant/injection.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2023-2024 The kpt Authors
1+
// Copyright 2023-2024, 2026 The kpt Authors
22
//
33
// Licensed under the Apache License, Version 2.0 (the "License");
44
// you may not use this file except in compliance with the License.
@@ -18,7 +18,7 @@ import (
1818
"context"
1919
"fmt"
2020
"path/filepath"
21-
"sort"
21+
"slices"
2222
"strings"
2323

2424
api "github.com/kptdev/porch/api/porchconfig/v1alpha1"
@@ -149,7 +149,7 @@ func parseFiles(prr *porchapi.PackageRevisionResources) (map[string]fn.KubeObjec
149149
// Convert to KubeObjects for easier processing
150150
kos, err := fn.ParseKubeObjects([]byte(r))
151151
if err != nil {
152-
return nil, fmt.Errorf("%s: %s", file, err.Error())
152+
return nil, fmt.Errorf("%s: %w", file, err)
153153
}
154154
result[file] = kos
155155
}
@@ -341,7 +341,7 @@ func setInjectionPointConditionsAndGates(kptfileKubeObject *fn.KubeObject, injec
341341
for k := range gateMap {
342342
gates = append(gates, kptfilev1.ReadinessGate{ConditionType: k})
343343
}
344-
sort.SliceStable(gates, func(i, j int) bool { return gates[i].ConditionType < gates[j].ConditionType })
344+
slices.SortStableFunc(gates, func(a, b kptfilev1.ReadinessGate) int { return strings.Compare(a.ConditionType, b.ConditionType) })
345345

346346
if gates != nil {
347347
info.ReadinessGates = gates
@@ -353,7 +353,7 @@ func setInjectionPointConditionsAndGates(kptfileKubeObject *fn.KubeObject, injec
353353

354354
// update the status conditions
355355
if conditions != nil {
356-
sort.SliceStable(conditions, func(i, j int) bool { return conditions[i].Type < conditions[j].Type })
356+
slices.SortStableFunc(conditions, func(a, b metav1.Condition) int { return strings.Compare(a.Type, b.Type) })
357357
status.Conditions = convertConditionsFromMetaToKptfile(conditions)
358358
err = kptfileKubeObject.SetNestedField(status, "status")
359359
if err != nil {

controllers/packagevariants/pkg/controllers/packagevariant/injection_test.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2023, 2025 The kpt Authors
1+
// Copyright 2023, 2025-2026 The kpt Authors
22
//
33
// Licensed under the Apache License, Version 2.0 (the "License");
44
// you may not use this file except in compliance with the License.
@@ -17,7 +17,8 @@ package packagevariant
1717
import (
1818
"context"
1919
"fmt"
20-
"sort"
20+
"slices"
21+
"strings"
2122
"testing"
2223

2324
kptfilev1 "github.com/kptdev/kpt/api/kptfile/v1"
@@ -234,9 +235,9 @@ spec:
234235
require.Equal(t, len(tc.expected), len(actualInjectionPoints))
235236

236237
// ensure a stable ordering
237-
sort.Slice(actualInjectionPoints,
238-
func(i, j int) bool {
239-
return actualInjectionPoints[i].conditionType < actualInjectionPoints[j].conditionType
238+
slices.SortFunc(actualInjectionPoints,
239+
func(a, b *injectionPoint) int {
240+
return strings.Compare(a.conditionType, b.conditionType)
240241
})
241242
for i, ip := range actualInjectionPoints {
242243
require.Equal(t, tc.expected[i].conditionType, ip.conditionType)

controllers/packagevariantsets/pkg/controllers/packagevariantset/packagevariantset_controller.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -276,7 +276,7 @@ func (r *PackageVariantSetReconciler) ensurePackageVariants(ctx context.Context,
276276
client.MatchingLabels{
277277
PackageVariantSetOwnerLabel: string(pvs.UID),
278278
}); err != nil {
279-
return fmt.Errorf("error listing package variants %v", err)
279+
return fmt.Errorf("error listing package variants: %w", err)
280280
}
281281

282282
// existingPackageVariantMap holds the PackageVariant objects that currently exist.

controllers/packagevariantsets/pkg/controllers/packagevariantset/packagevariantset_controller_test.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2023, 2025 The kpt Authors
1+
// Copyright 2023, 2025-2026 The kpt Authors
22
//
33
// Licensed under the Apache License, Version 2.0 (the "License");
44
// you may not use this file except in compliance with the License.
@@ -16,7 +16,8 @@ package packagevariantset
1616

1717
import (
1818
"context"
19-
"sort"
19+
"slices"
20+
"strings"
2021
"testing"
2122

2223
porchapi "github.com/kptdev/porch/api/porch/v1alpha1"
@@ -27,6 +28,7 @@ import (
2728
corev1 "k8s.io/api/core/v1"
2829
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2930
"k8s.io/apimachinery/pkg/runtime/serializer/json"
31+
"sigs.k8s.io/controller-runtime/pkg/client"
3032
"sigs.k8s.io/yaml"
3133
)
3234

@@ -135,8 +137,8 @@ func TestEnsurePackageVariants(t *testing.T) {
135137
require.Equal(t, "my-pvs-dnrepo2-dnpkg2", fc.updated[0].GetName())
136138
require.Equal(t, 2, len(fc.created))
137139
// ordering of calls to create is not stable (map iteration)
138-
sort.Slice(fc.created, func(i, j int) bool {
139-
return fc.created[i].GetName() < fc.created[j].GetName()
140+
slices.SortFunc(fc.created, func(a, b client.Object) int {
141+
return strings.Compare(a.GetName(), b.GetName())
140142
})
141143
require.Equal(t, "my-pvs-dnrepo3-dnpkg3", fc.created[0].GetName())
142144
require.Equal(t, "my-pvs-dnrepo4-supersupersuperlooooooooooooooooooooooo-bec36506", fc.created[1].GetName())

controllers/packagevariantsets/pkg/controllers/packagevariantset/render.go

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2023 The kpt Authors
1+
// Copyright 2023, 2026 The kpt Authors
22
//
33
// Licensed under the Apache License, Version 2.0 (the "License");
44
// you may not use this file except in compliance with the License.
@@ -18,7 +18,7 @@ import (
1818
"context"
1919
"fmt"
2020
"reflect"
21-
"sort"
21+
"slices"
2222

2323
"github.com/google/cel-go/cel"
2424
api "github.com/kptdev/porch/api/porchconfig/v1alpha2"
@@ -73,7 +73,7 @@ func renderPackageVariantSpec(ctx context.Context, pvs *api.PackageVariantSet, r
7373
if pvt.Downstream.RepoExpr != nil && *pvt.Downstream.RepoExpr != "" {
7474
repo, err = evalExpr(*pvt.Downstream.RepoExpr, inputs)
7575
if err != nil {
76-
return nil, fmt.Errorf("template.downstream.repoExpr: %s", err.Error())
76+
return nil, fmt.Errorf("template.downstream.repoExpr: %w", err)
7777
}
7878
}
7979

@@ -103,7 +103,7 @@ func renderPackageVariantSpec(ctx context.Context, pvs *api.PackageVariantSet, r
103103
if pvt.Downstream.PackageExpr != nil && *pvt.Downstream.PackageExpr != "" {
104104
spec.Downstream.Package, err = evalExpr(*pvt.Downstream.PackageExpr, inputs)
105105
if err != nil {
106-
return nil, fmt.Errorf("template.downstream.packageExpr: %s", err.Error())
106+
return nil, fmt.Errorf("template.downstream.packageExpr: %w", err)
107107
}
108108
}
109109
}
@@ -155,7 +155,7 @@ func renderPackageVariantSpec(ctx context.Context, pvs *api.PackageVariantSet, r
155155
if injTemplate.NameExpr != nil && *injTemplate.NameExpr != "" {
156156
injector.Name, err = evalExpr(*injTemplate.NameExpr, inputs)
157157
if err != nil {
158-
return nil, fmt.Errorf("template.injectors[%d].nameExpr: %s", i, err.Error())
158+
return nil, fmt.Errorf("template.injectors[%d].nameExpr: %w", i, err)
159159
}
160160
}
161161

@@ -180,7 +180,7 @@ func renderPackageVariantSpec(ctx context.Context, pvs *api.PackageVariantSet, r
180180
return spec, nil
181181
}
182182

183-
func renderFunctionTemplateList(field string, templateList []api.FunctionTemplate, inputs map[string]interface{}) ([]kptfilev1.Function, error) {
183+
func renderFunctionTemplateList(field string, templateList []api.FunctionTemplate, inputs map[string]any) ([]kptfilev1.Function, error) {
184184
var results []kptfilev1.Function
185185
for i, ft := range templateList {
186186
var err error
@@ -195,8 +195,8 @@ func renderFunctionTemplateList(field string, templateList []api.FunctionTemplat
195195
return results, nil
196196
}
197197

198-
func buildBaseInputs(upstreamPR *porchapi.PackageRevision, downstream pvContext) (map[string]interface{}, error) {
199-
inputs := make(map[string]interface{}, 5)
198+
func buildBaseInputs(upstreamPR *porchapi.PackageRevision, downstream pvContext) (map[string]any, error) {
199+
inputs := make(map[string]any, 5)
200200
inputs[RepoDefaultVarName] = downstream.repoDefault
201201
inputs[PackageDefaultVarName] = downstream.packageDefault
202202

@@ -222,9 +222,9 @@ func buildBaseInputs(upstreamPR *porchapi.PackageRevision, downstream pvContext)
222222
return inputs, nil
223223
}
224224

225-
func objectToInput(obj interface{}) (map[string]interface{}, error) {
225+
func objectToInput(obj any) (map[string]any, error) {
226226

227-
result := make(map[string]interface{})
227+
result := make(map[string]any)
228228

229229
uo, err := runtime.DefaultUnstructuredConverter.ToUnstructured(obj)
230230
if err != nil {
@@ -243,7 +243,7 @@ func objectToInput(obj interface{}) (map[string]interface{}, error) {
243243
return result, nil
244244
}
245245

246-
func copyAndOverlayMapExpr(fieldName string, inMap map[string]string, mapExprs []api.MapExpr, inputs map[string]interface{}) (map[string]string, error) {
246+
func copyAndOverlayMapExpr(fieldName string, inMap map[string]string, mapExprs []api.MapExpr, inputs map[string]any) (map[string]string, error) {
247247
outMap := make(map[string]string, len(inMap))
248248
for k, v := range inMap {
249249
outMap[k] = v
@@ -258,7 +258,7 @@ func copyAndOverlayMapExpr(fieldName string, inMap map[string]string, mapExprs [
258258
if me.KeyExpr != nil {
259259
k, err = evalExpr(*me.KeyExpr, inputs)
260260
if err != nil {
261-
return nil, fmt.Errorf("%s[%d].keyExpr: %s", fieldName, i, err.Error())
261+
return nil, fmt.Errorf("%s[%d].keyExpr: %w", fieldName, i, err)
262262
}
263263
}
264264
if me.Value != nil {
@@ -267,7 +267,7 @@ func copyAndOverlayMapExpr(fieldName string, inMap map[string]string, mapExprs [
267267
if me.ValueExpr != nil {
268268
v, err = evalExpr(*me.ValueExpr, inputs)
269269
if err != nil {
270-
return nil, fmt.Errorf("%s[%d].valueExpr: %s", fieldName, i, err.Error())
270+
return nil, fmt.Errorf("%s[%d].valueExpr: %w", fieldName, i, err)
271271
}
272272
}
273273
outMap[k] = v
@@ -280,7 +280,7 @@ func copyAndOverlayMapExpr(fieldName string, inMap map[string]string, mapExprs [
280280
return outMap, nil
281281
}
282282

283-
func copyAndOverlayStringSlice(fieldName string, in, exprs []string, inputs map[string]interface{}) ([]string, error) {
283+
func copyAndOverlayStringSlice(fieldName string, in, exprs []string, inputs map[string]any) ([]string, error) {
284284
outMap := make(map[string]bool, len(in)+len(exprs))
285285

286286
for _, v := range in {
@@ -289,7 +289,7 @@ func copyAndOverlayStringSlice(fieldName string, in, exprs []string, inputs map[
289289
for i, e := range exprs {
290290
v, err := evalExpr(e, inputs)
291291
if err != nil {
292-
return nil, fmt.Errorf("%s[%d]: %s", fieldName, i, err.Error())
292+
return nil, fmt.Errorf("%s[%d]: %w", fieldName, i, err)
293293
}
294294
outMap[v] = true
295295
}
@@ -302,11 +302,11 @@ func copyAndOverlayStringSlice(fieldName string, in, exprs []string, inputs map[
302302
for k := range outMap {
303303
out = append(out, k)
304304
}
305-
sort.Strings(out)
305+
slices.Sort(out)
306306
return out, nil
307307
}
308308

309-
func evalExpr(expr string, inputs map[string]interface{}) (string, error) {
309+
func evalExpr(expr string, inputs map[string]any) (string, error) {
310310
prog, err := compileExpr(expr)
311311
if err != nil {
312312
return "", err

controllers/packagevariantsets/pkg/controllers/packagevariantset/render_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2023, 2025 The kpt Authors
1+
// Copyright 2023, 2025-2026 The kpt Authors
22
//
33
// Licensed under the Apache License, Version 2.0 (the "License");
44
// you may not use this file except in compliance with the License.
@@ -490,7 +490,7 @@ func TestRenderPackageVariantSpec(t *testing.T) {
490490
}
491491

492492
func TestEvalExpr(t *testing.T) {
493-
baseInputs := map[string]interface{}{
493+
baseInputs := map[string]any{
494494
"repoDefault": "foo-repo",
495495
"packageDefault": "bar-package",
496496
}
@@ -502,7 +502,7 @@ func TestEvalExpr(t *testing.T) {
502502

503503
testCases := map[string]struct {
504504
expr string
505-
target interface{}
505+
target any
506506
expectedResult string
507507
expectedErr string
508508
}{
@@ -575,7 +575,7 @@ func TestEvalExpr(t *testing.T) {
575575
}
576576

577577
func TestCopyAndOverlayMapExpr(t *testing.T) {
578-
baseInputs := map[string]interface{}{
578+
baseInputs := map[string]any{
579579
"repoDefault": "foo-repo",
580580
"packageDefault": "bar-package",
581581
}

controllers/repositories/pkg/controllers/repository/config_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,11 @@ func TestBindFlags(t *testing.T) {
4747
}
4848

4949
type mockLogger struct {
50-
infoCalls [][]interface{}
50+
infoCalls [][]any
5151
}
5252

53-
func (m *mockLogger) Info(msg string, keysAndValues ...interface{}) {
54-
m.infoCalls = append(m.infoCalls, append([]interface{}{msg}, keysAndValues...))
53+
func (m *mockLogger) Info(msg string, keysAndValues ...any) {
54+
m.infoCalls = append(m.infoCalls, append([]any{msg}, keysAndValues...))
5555
}
5656

5757
func TestLogConfig(t *testing.T) {

0 commit comments

Comments
 (0)