Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove unneeded loop variable copying #1946

Merged

Conversation

alexandear
Copy link
Contributor

What type of PR is this?

/kind cleanup

What this PR does / why we need it:

Clean up unnecessary loop variable copying and enable the copyloopvar linter for detecting this redundant variable copying.

Which issue(s) this PR fixes:

Special notes for your reviewer:

After the project upgraded to Go version 1.22 in #1895, copying variables inside a for loop became unnecessary. See this blog post for a detailed explanation.

The copyloopvar linter is only available from golangci-lint v1.57 onwards, so we also need to update this tool.

Does this PR introduce a user-facing change?

NONE

@k8s-ci-robot k8s-ci-robot added release-note-none Denotes a PR that doesn't merit a release note. kind/cleanup Categorizes issue or PR as related to cleaning up code, process, or technical debt. cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. labels Apr 4, 2024
@k8s-ci-robot
Copy link
Contributor

Hi @alexandear. Thanks for your PR.

I'm waiting for a kubernetes-sigs member to verify that this patch is reasonable to test. If it is, they should reply with /ok-to-test on its own line. Until that is done, I will not automatically test new commits in this PR, but the usual testing commands by org members will still work. Regular contributors should join the org to skip this step.

Once the patch is verified, the new status will be reflected by the ok-to-test label.

I understand the commands that are listed here.

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository.

@k8s-ci-robot k8s-ci-robot added needs-ok-to-test Indicates a PR that requires an org member to verify it is safe to test. size/M Denotes a PR that changes 30-99 lines, ignoring generated files. labels Apr 4, 2024
Copy link

netlify bot commented Apr 4, 2024

Deploy Preview for kubernetes-sigs-kueue ready!

Name Link
🔨 Latest commit 88a5db8
🔍 Latest deploy log https://app.netlify.com/sites/kubernetes-sigs-kueue/deploys/660ebd24357e9000082a1c34
😎 Deploy Preview https://deploy-preview-1946--kubernetes-sigs-kueue.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

Copy link
Member

@tenzen-y tenzen-y left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@alculquicondor @alexandear I understand that we don't use loop variables in the jobframework, and admissionchecks. But, let us confirm if no issues happen on the external job manager and admission controller implemented by Go 1.21 when we implement concurrent processes without loop variables for the future.

Comment on lines +9 to +17
disabled-checks: # temporarily disabled checks, will fix them later
- appendAssign
- assignOp
- captLocal
- commentFormatting
- deprecatedComment
- elseif
- exitAfterDefer
- ifElseChain
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we need these?

Copy link
Contributor Author

@alexandear alexandear Apr 5, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Without these lines, a lot of the gocritic issues are reported, which I don't want to fix in this PR to avoid mixing things.

Running make ci-lint with removed disabled-checks section
pkg/controller/constants/constants.go:25:2: deprecatedComment: use `Deprecated: ` (note the casing) instead of `DEPRECATED: ` (gocritic)
        // DEPRECATED: Use QueueLabel as a label key.
        ^
pkg/util/resource/resource.go:35:10: elseif: can replace 'else {if cond {}}' with 'else if cond {}' (gocritic)
                } else {
                       ^
pkg/util/testing/client.go:143:72: captLocal: `SubResourceName' should not be capitalized (gocritic)
func TreatSSAAsStrategicMerge(ctx context.Context, clnt client.Client, SubResourceName string, obj client.Object, patch client.Patch, opts ...client.SubResourcePatchOption) error {
                                                                       ^
pkg/util/testing/wrappers.go:883:49: captLocal: `LocationType' should not be capitalized (gocritic)
func (mkc *MultiKueueClusterWrapper) KubeConfig(LocationType kueuealpha.LocationType, location string) *MultiKueueClusterWrapper {
                                                ^
pkg/util/testing/wrappers.go:745:2: commentFormatting: put a space between `//` and comment text (gocritic)
        //nothing
        ^
pkg/util/admissioncheck/admissioncheck.go:118:30: captLocal: `ControllerName' should not be capitalized (gocritic)
func IndexerByConfigFunction(ControllerName string, gvk schema.GroupVersionKind) client.IndexerFunc {
                             ^
pkg/util/admissioncheck/admissioncheck.go:133:100: captLocal: `ControllerName' should not be capitalized (gocritic)
func FilterForController(ctx context.Context, c client.Client, states []kueue.AdmissionCheckState, ControllerName string) ([]string, error) {
                                                                                                   ^
pkg/controller/admissionchecks/provisioning/controller.go:798:3: assignOp: replace `backoffDuration = backoffDuration * 2` with `backoffDuration *= 2` (gocritic)
                backoffDuration = backoffDuration * 2
                ^
pkg/controller/admissionchecks/provisioning/controller.go:122:3: commentFormatting: put a space between `//` and comment text (gocritic)
                //1.2 workload has no reservation or is finished
                ^
pkg/controller/admissionchecks/provisioning/controller.go:225:3: commentFormatting: put a space between `//` and comment text (gocritic)
                //get the config
                ^
pkg/cache/snapshot_test.go:991:13: appendAssign: append result not assigned to the same slice (gocritic)
        cmpOpts := append(snapCmpOpts,
                   ^
pkg/cache/snapshot_test.go:1498:13: appendAssign: append result not assigned to the same slice (gocritic)
        cmpOpts := append(snapCmpOpts,
                   ^
pkg/queue/manager_test.go:237:3: commentFormatting: put a space between `//` and comment text (gocritic)
                //nothing
                ^
pkg/workload/workload.go:397:2: commentFormatting: put a space between `//` and comment text (gocritic)
        //reset Evicted condition if present.
        ^
pkg/controller/jobs/mpijob/mpijob_controller_test.go:294:12: appendAssign: append result not assigned to the same slice (gocritic)
                        objs := append(tc.priorityClasses, tc.job)
                                ^
pkg/controller/jobs/raycluster/raycluster_controller_test.go:334:12: appendAssign: append result not assigned to the same slice (gocritic)
                        objs := append(tc.priorityClasses, &tc.job)
                                ^
pkg/scheduler/flavorassigner/flavorassigner.go:450:10: elseif: can replace 'else {if cond {}}' with 'else if cond {}' (gocritic)
                } else {
                       ^
pkg/controller/jobs/jobset/jobset_controller_test.go:389:12: appendAssign: append result not assigned to the same slice (gocritic)
                        objs := append(tc.priorityClasses, tc.job)
                                ^
pkg/visibility/api/rest/pending_workloads_cq_test.go:343:4: ifElseChain: rewrite if-else to switch statement (gocritic)
                        if tc.wantErrMatch != nil {
                        ^
pkg/visibility/api/rest/pending_workloads_lq_test.go:460:4: ifElseChain: rewrite if-else to switch statement (gocritic)
                        if tc.wantErrMatch != nil {
                        ^
pkg/controller/admissionchecks/multikueue/admissioncheck.go:98:11: elseif: can replace 'else {if cond {}}' with 'else if cond {}' (gocritic)
                        } else {
                               ^
pkg/controller/admissionchecks/multikueue/multikueuecluster.go:221:4: commentFormatting: put a space between `//` and comment text (gocritic)
                        //reconnect if this is the first watch failing.
                        ^
pkg/controller/admissionchecks/multikueue/multikueuecluster.go:241:9: elseif: can replace 'else {if cond {}}' with 'else if cond {}' (gocritic)
        } else {
               ^
pkg/controller/admissionchecks/multikueue/workload_test.go:455:89: commentFormatting: put a space between `//` and comment text (gocritic)
                                                LastTransitionTime: metav1.NewTime(time.Now().Add(-defaulWorkerLostTimeout / 2)), //50% of the timeout
                                                                                                                                  ^
pkg/controller/admissionchecks/multikueue/workload_test.go:593:55: commentFormatting: put a space between `//` and comment text (gocritic)
                                        QuotaReservedTime(time.Now().Add(-time.Minute)). //one minute ago
                                                                                         ^
cmd/importer/pod/check.go:66:10: elseif: can replace 'else {if cond {}}' with 'else if cond {}' (gocritic)
                } else {
                       ^
cmd/importer/pod/import.go:89:3: commentFormatting: put a space between `//` and comment text (gocritic)
                //make its admission and update its status
                ^
pkg/controller/jobs/job/job_webhook.go:101:10: elseif: can replace 'else {if cond {}}' with 'else if cond {}' (gocritic)
                } else {
                       ^
pkg/controller/jobs/job/job_controller_test.go:2205:12: appendAssign: append result not assigned to the same slice (gocritic)
                        objs := append(tc.priorityClasses, &tc.job, utiltesting.MakeResourceFlavor("default").Obj())
                                ^
test/util/util.go:231:30: appendAssign: append result not assigned to the same slice (gocritic)
                        newWL.Status.Conditions = append(w.Status.Conditions, metav1.Condition{
                                                  ^
pkg/scheduler/scheduler_test.go:1418:17: appendAssign: append result not assigned to the same slice (gocritic)
                        allQueues := append(queues, tc.additionalLocalQueues...)
                                     ^
pkg/scheduler/scheduler_test.go:1419:24: appendAssign: append result not assigned to the same slice (gocritic)
                        allClusterQueues := append(clusterQueues, tc.additionalClusterQueues...)
                                            ^
pkg/scheduler/scheduler_test.go:1500:6: ifElseChain: rewrite if-else to switch statement (gocritic)
                                        if !workload.HasQuotaReservation(w.Obj) {
                                        ^
pkg/scheduler/scheduler_test.go:2089:6: ifElseChain: rewrite if-else to switch statement (gocritic)
                                        if !workload.IsAdmitted(w.Obj) {
                                        ^
pkg/controller/jobs/pod/expectations.go:44:83: captLocal: `UIDs' should not be capitalized (gocritic)
func (e *expectationsStore) ExpectUIDs(log logr.Logger, key types.NamespacedName, UIDs []types.UID) {
                                                                                  ^
test/integration/controller/jobs/jobset/jobset_controller_test.go:161:3: commentFormatting: put a space between `//` and comment text (gocritic)
                //gomega.Expect(k8sClient.Get(ctx, wlLookupKey, createdWorkload)).Should(gomega.Succeed())
                ^
cmd/kueue/main.go:286:3: exitAfterDefer: os.Exit will exit, and `defer setupLog.Info("Probe endpoints are configured on healthz and readyz")` will not run (gocritic)
                os.Exit(1)
                ^
make: *** [Makefile:217: ci-lint] Error 1

It's because gocritic behavior in golangci-lint v1.57.2 slightly changes (see golangci/golangci-lint#4335)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should we use disable-all instead?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can't use disable-all as it will disable all checks, including the defaults.

You can view the enabled gocritic checks by setting the environment variable GL_DEBUG=gocritic:

❯ GL_DEBUG=gocritic golangci-lint run
...
DEBU [gocritic] Enabled by config checks (1): [dupImport] 
DEBU [gocritic] Disabled by config checks (8): [appendAssign assignOp captLocal commentFormatting deprecatedComment elseif exitAfterDefer ifElseChain] 
DEBU [gocritic] Final used checks (27): [argOrder badCall badCond caseOrder codegenComment defaultCaseOrder dupArg dupBranchBody dupCase dupImport dupSubExpr flagDeref flagName mapKey newDeref offBy1 regexpMust singleCaseSwitch sloppyLen sloppyTypeAssert switchTrue typeSwitchVar underef unlambda unslice valSwap wrapperFunc] 

@@ -21,10 +24,21 @@ linters-settings:
# Settings for enabling and disabling linters
linters:
enable:
- copyloopvar
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I found that this is implemented by my co-worker :)

@alculquicondor
Copy link
Contributor

/ok-to-test

@k8s-ci-robot k8s-ci-robot added ok-to-test Indicates a non-member PR verified by an org member that is safe to test. and removed needs-ok-to-test Indicates a PR that requires an org member to verify it is safe to test. labels Apr 5, 2024
@@ -1,17 +1,20 @@
# golangci-lint configuration file
# see: https://golangci-lint.run/usage/configuration/

# Options for analysis running
run:
# Which dirs to skip: they won't be analyzed;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why remove this?

IIUC, issues.exclude-dirs would skip the reporting for this folder, but it would still analyze it. Or not?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

run.skip-dirs has been deprecated since golangci-lint v1.57.0 and replaced by issues.exclude-dirs. These options are technically identical. For a detailed explanation, see golangci/golangci-lint#4509.

Unfortunately, golangci-lint does not provide a way to skip directory analysis, run.skip-dirs lies. See golangci/golangci-lint#1832

Comment on lines +9 to +17
disabled-checks: # temporarily disabled checks, will fix them later
- appendAssign
- assignOp
- captLocal
- commentFormatting
- deprecatedComment
- elseif
- exitAfterDefer
- ifElseChain
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should we use disable-all instead?

Copy link
Contributor

@alculquicondor alculquicondor left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

/lgtm
/approve

@k8s-ci-robot k8s-ci-robot added the lgtm "Looks good to me", indicates that a PR is ready to be merged. label Apr 11, 2024
@k8s-ci-robot
Copy link
Contributor

LGTM label has been added.

Git tree hash: a6e952bfc18aa52b24ed0f55b3661445df0147e3

@k8s-ci-robot
Copy link
Contributor

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: alculquicondor, alexandear

The full list of commands accepted by this bot can be found here.

The pull request process is described here

Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@k8s-ci-robot k8s-ci-robot added the approved Indicates a PR has been approved by an approver from all required OWNERS files. label Apr 11, 2024
@k8s-ci-robot k8s-ci-robot merged commit 3347afb into kubernetes-sigs:main Apr 11, 2024
14 checks passed
@k8s-ci-robot k8s-ci-robot added this to the v0.7 milestone Apr 11, 2024
vsoch pushed a commit to researchapps/kueue that referenced this pull request Apr 18, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
approved Indicates a PR has been approved by an approver from all required OWNERS files. cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. kind/cleanup Categorizes issue or PR as related to cleaning up code, process, or technical debt. lgtm "Looks good to me", indicates that a PR is ready to be merged. ok-to-test Indicates a non-member PR verified by an org member that is safe to test. release-note-none Denotes a PR that doesn't merit a release note. size/M Denotes a PR that changes 30-99 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants