diff --git a/charts/gha-runner-scale-set-controller/tests/metrics_test.go b/charts/gha-runner-scale-set-controller/tests/metrics_test.go index e2eae100..bb958efd 100644 --- a/charts/gha-runner-scale-set-controller/tests/metrics_test.go +++ b/charts/gha-runner-scale-set-controller/tests/metrics_test.go @@ -66,7 +66,7 @@ func TestControllerMetricsAddress(t *testing.T) { func indentMetricsValues(values string) string { result := "" - for _, line := range strings.Split(strings.TrimSuffix(values, "\n"), "\n") { + for line := range strings.SplitSeq(strings.TrimSuffix(values, "\n"), "\n") { result += " " + line + "\n" } return result diff --git a/charts/gha-runner-scale-set/tests/template_test.go b/charts/gha-runner-scale-set/tests/template_test.go index 25ee902e..3d7bba66 100644 --- a/charts/gha-runner-scale-set/tests/template_test.go +++ b/charts/gha-runner-scale-set/tests/template_test.go @@ -2,6 +2,7 @@ package tests import ( "fmt" + "maps" "path/filepath" "strings" "testing" @@ -210,12 +211,8 @@ func TestTemplateListenerScalerValidation(t *testing.T) { for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { setValues := make(map[string]string, len(baseValues)+len(tt.setValues)) - for key, value := range baseValues { - setValues[key] = value - } - for key, value := range tt.setValues { - setValues[key] = value - } + maps.Copy(setValues, baseValues) + maps.Copy(setValues, tt.setValues) options := &helm.Options{ Logger: logger.Discard, @@ -276,12 +273,8 @@ func TestTemplateListenerScalerConfig(t *testing.T) { for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { setValues := make(map[string]string, len(baseValues)+len(tt.setValues)) - for key, value := range baseValues { - setValues[key] = value - } - for key, value := range tt.setValues { - setValues[key] = value - } + maps.Copy(setValues, baseValues) + maps.Copy(setValues, tt.setValues) options := &helm.Options{ Logger: logger.Discard, diff --git a/controllers/actions.github.com/ephemeralrunnerset_controller_test.go b/controllers/actions.github.com/ephemeralrunnerset_controller_test.go index 9b9e07bd..e77c0db7 100644 --- a/controllers/actions.github.com/ephemeralrunnerset_controller_test.go +++ b/controllers/actions.github.com/ephemeralrunnerset_controller_test.go @@ -1454,7 +1454,7 @@ var _ = Describe("Test EphemeralRunnerSet controller", func() { ).Should(BeEquivalentTo(3), "3 EphemeralRunner should be created") idleRunnerNames := map[string]struct{}{} - for i := 0; i < 2; i++ { + for i := range 2 { idleRunner := runnerList.Items[i].DeepCopy() idleRunner.Status.Phase = v1alpha1.EphemeralRunnerPhaseRunning idleRunner.Status.RunnerID = i + 101 diff --git a/controllers/actions.github.com/predicates_test.go b/controllers/actions.github.com/predicates_test.go index 4b2f7288..703adb65 100644 --- a/controllers/actions.github.com/predicates_test.go +++ b/controllers/actions.github.com/predicates_test.go @@ -470,8 +470,8 @@ func TestPredicateProjectionsCoverEveryStatusField(t *testing.T) { fieldNames := func(v any) []string { typ := reflect.TypeOf(v) names := make([]string, 0, typ.NumField()) - for i := 0; i < typ.NumField(); i++ { - names = append(names, typ.Field(i).Name) + for field := range typ.Fields() { + names = append(names, field.Name) } sort.Strings(names) return names diff --git a/go.mod b/go.mod index 4d7f64c3..192150ad 100644 --- a/go.mod +++ b/go.mod @@ -28,6 +28,7 @@ require ( github.com/teambition/rrule-go v1.8.2 go.uber.org/multierr v1.11.0 go.uber.org/zap v1.28.0 + golang.org/x/mod v0.41.0 golang.org/x/net v0.59.0 golang.org/x/oauth2 v0.37.0 golang.org/x/sync v0.23.0 @@ -211,7 +212,6 @@ require ( go.yaml.in/yaml/v4 v4.0.0-rc.6 // indirect golang.org/x/crypto v0.57.0 // indirect golang.org/x/exp v0.0.0-20260908205506-85c1c2202aba // indirect - golang.org/x/mod v0.41.0 // indirect golang.org/x/sys v0.48.0 // indirect golang.org/x/term v0.46.0 // indirect golang.org/x/text v0.42.0 // indirect diff --git a/hash/fnv.go b/hash/fnv.go index 20507d4a..bc372e24 100644 --- a/hash/fnv.go +++ b/hash/fnv.go @@ -7,7 +7,7 @@ import ( "k8s.io/apimachinery/pkg/util/rand" ) -func FNVHashStringObjects(objs ...interface{}) string { +func FNVHashStringObjects(objs ...any) string { hash := fnv.New32a() for _, obj := range objs { diff --git a/hash/hash.go b/hash/hash.go index 06537314..6ecd5d08 100644 --- a/hash/hash.go +++ b/hash/hash.go @@ -16,7 +16,7 @@ import ( // DeepHashObject writes specified object to hash using the spew library // which follows pointers and prints actual values of the nested objects // ensuring the hash does not change when a pointer changes. -func DeepHashObject(hasher hash.Hash, objectToWrite interface{}) { +func DeepHashObject(hasher hash.Hash, objectToWrite any) { hasher.Reset() printer := spew.ConfigState{ Indent: " ", @@ -34,7 +34,7 @@ func DeepHashObject(hasher hash.Hash, objectToWrite interface{}) { // // Proudly modified and adopted from k8s.io/kubernetes/pkg/util/hash.DeepHashObject and // k8s.io/kubernetes/pkg/controller.ComputeHash. -func ComputeTemplateHash(template interface{}) string { +func ComputeTemplateHash(template any) string { hasher := fnv.New32a() hasher.Reset() diff --git a/logging/logger.go b/logging/logger.go index 6da21954..6f7a4e41 100644 --- a/logging/logger.go +++ b/logging/logger.go @@ -4,6 +4,7 @@ import ( "errors" "fmt" "os" + "slices" "strconv" "time" @@ -78,10 +79,5 @@ func NewLogger(logLevel string, logFormat string) (logr.Logger, error) { func validLogFormat(logFormat string) bool { validFormat := []string{"text", "json"} - for _, v := range validFormat { - if v == logFormat { - return true - } - } - return false + return slices.Contains(validFormat, logFormat) } diff --git a/logging/transport.go b/logging/transport.go index 50564b7b..c5cb2288 100644 --- a/logging/transport.go +++ b/logging/transport.go @@ -35,7 +35,7 @@ func (t Transport) log(req *http.Request, resp *http.Response) { return } - var args []interface{} + var args []any marked := resp.Header.Get(httpcache.XFromCache) == "1" diff --git a/test/e2e/e2e_test.go b/test/e2e/e2e_test.go index 83d7451a..662cbef9 100644 --- a/test/e2e/e2e_test.go +++ b/test/e2e/e2e_test.go @@ -15,6 +15,7 @@ import ( "github.com/google/go-github/v52/github" "github.com/onsi/gomega" "github.com/stretchr/testify/require" + "golang.org/x/mod/modfile" "golang.org/x/oauth2" "sigs.k8s.io/yaml" ) @@ -1018,7 +1019,7 @@ type job struct { func createTestJobs(id, testResultCMNamePrefix string, numJobs int) []job { var testJobs []job - for i := 0; i < numJobs; i++ { + for i := range numJobs { name := fmt.Sprintf("test%d", i) testArg := fmt.Sprintf("%s%d", id, i) configMapName := testResultCMNamePrefix + testArg @@ -1050,10 +1051,11 @@ func installActionsWorkflow(t *testing.T, testName, runnerLabel, testResultCMNam } kubernetesContainerMode := os.Getenv("TEST_CONTAINER_MODE") == "kubernetes" + goVersion := goVersionFromGoMod(t) var container string if kubernetesContainerMode { - container = "golang:1.27.1" + container = "golang:" + goVersion } for _, j := range testJobs { @@ -1118,7 +1120,7 @@ func installActionsWorkflow(t *testing.T, testName, runnerLabel, testResultCMNam testing.Step{ Uses: "actions/setup-go@v3", With: &testing.With{ - GoVersion: "1.27.1", + GoVersion: goVersion, }, }, ) @@ -1300,6 +1302,33 @@ kubectl create cm %s$id --from-literal=status=ok } } +// goVersionFromGoMod keeps the runner-side Go toolchain in sync with go.mod so +// the version is pinned in exactly one place. Like setup-go's go-version-file, +// the toolchain directive wins over the go directive when both are present. +func goVersionFromGoMod(t *testing.T) string { + t.Helper() + + data, err := os.ReadFile(filepath.Join("..", "..", "go.mod")) + if err != nil { + t.Fatal(err) + } + + f, err := modfile.ParseLax("go.mod", data, nil) + if err != nil { + t.Fatal(err) + } + + if f.Toolchain != nil { + return strings.TrimPrefix(f.Toolchain.Name, "go") + } + + if f.Go == nil { + t.Fatal("go.mod has no go directive") + } + + return f.Go.Version +} + func verifyActionsWorkflowRun(t *testing.T, ctx context.Context, env *testing.Env, testJobs []job, timeout time.Duration, cmCfg testing.KubectlConfig) { t.Helper() diff --git a/testing/kubectl.go b/testing/kubectl.go index 09785b17..2118b775 100644 --- a/testing/kubectl.go +++ b/testing/kubectl.go @@ -122,7 +122,7 @@ func (k *Kubectl) FindPods(ctx context.Context, label string, cfg KubectlConfig) } var pods []string - for _, l := range strings.Split(out, "\n") { + for l := range strings.SplitSeq(out, "\n") { if l != "" { pods = append(pods, l) } diff --git a/testing/runtime/runtime.go b/testing/runtime/runtime.go index 946f2fd6..c885cdf9 100644 --- a/testing/runtime/runtime.go +++ b/testing/runtime/runtime.go @@ -26,6 +26,6 @@ func (k Cmdr) CombinedOutput(cmd *exec.Cmd) (string, error) { return string(o), nil } -func (k Cmdr) Errorf(f string, args ...interface{}) { +func (k Cmdr) Errorf(f string, args ...any) { fmt.Fprintf(os.Stderr, f+"\n", args...) }