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

fixed using reference to loop iterator #105433

Merged
merged 2 commits into from
Oct 19, 2021

Conversation

shivanshuraj1333
Copy link
Contributor

@shivanshuraj1333 shivanshuraj1333 commented Oct 3, 2021

What type of PR is this?

/kind cleanup

What this PR does / why we need it:

Current code incorrectly uses for loop and passes wrong value as reference
Guide to refer https://github.com/golang/go/wiki/CommonMistakes#using-reference-to-loop-iterator-variable

Which issue(s) this PR fixes:

Fixes #105427

Special notes for your reviewer:

Please refer to sample code to understand the bug https://goplay.space/#TIT_IZEAEud

package main

import "fmt"

type st struct {
	it []string
}

func main() {
	rp := &st{[]string{"value1", "value2"}}
	ownedReplicaSets1 := make([]*string, 0, len(rp.it))
	ownedReplicaSets2 := make([]*string, 0, len(rp.it))

	// similar to current implementation
	for _, t := range rp.it {
		ownedReplicaSets1 = append(ownedReplicaSets1, &t)
	}

	fmt.Println(ownedReplicaSets1)
	fmt.Println(*ownedReplicaSets1[0], *ownedReplicaSets1[1])

	// similar new change
	for i := range rp.it {
		ownedReplicaSets2 = append(ownedReplicaSets2, &rp.it[i])
	}

	fmt.Println(ownedReplicaSets2)
	fmt.Println(*ownedReplicaSets2[0], *ownedReplicaSets2[1])
}
output:
value2 value2
[0xc000054020 0xc000054030]
value1 value2

Does this PR introduce a user-facing change?

NA

Additional documentation e.g., KEPs (Kubernetes Enhancement Proposals), usage docs, etc.:


@k8s-ci-robot k8s-ci-robot added release-note Denotes a PR that will be considered when it comes time to generate release notes. size/XS Denotes a PR that changes 0-9 lines, ignoring generated files. cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. do-not-merge/needs-kind Indicates a PR lacks a `kind/foo` label and requires one. do-not-merge/needs-sig Indicates an issue or PR lacks a `sig/foo` label and requires one. labels Oct 3, 2021
@k8s-ci-robot
Copy link
Contributor

@shivanshu1333: This issue is currently awaiting triage.

If a SIG or subproject determines this is a relevant issue, they will accept it by applying the triage/accepted label and provide further guidance.

The triage/accepted label can be added by org members by writing /triage accepted in a comment.

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-triage Indicates an issue or PR lacks a `triage/foo` label and requires one. needs-priority Indicates a PR lacks a `priority/foo` label and requires one. labels Oct 3, 2021
@k8s-ci-robot k8s-ci-robot added area/e2e-test-framework Issues or PRs related to refactoring the kubernetes e2e test framework area/test sig/testing Categorizes an issue or PR as relevant to SIG Testing. and removed do-not-merge/needs-sig Indicates an issue or PR lacks a `sig/foo` label and requires one. labels Oct 3, 2021
@shivanshuraj1333
Copy link
Contributor Author

/test pull-kubernetes-node-e2e-containerd

@shivanshuraj1333
Copy link
Contributor Author

/kind cleanup

@k8s-ci-robot k8s-ci-robot added kind/cleanup Categorizes issue or PR as related to cleaning up code, process, or technical debt. and removed do-not-merge/needs-kind Indicates a PR lacks a `kind/foo` label and requires one. labels Oct 4, 2021
@aojea
Copy link
Member

aojea commented Oct 6, 2021

/lgtm
/approve
Thanks

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

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: aojea, shivanshu1333

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 Oct 6, 2021
@aojea
Copy link
Member

aojea commented Oct 6, 2021

/hold
it has the same issue in

ownedPods := &v1.PodList{Items: make([]v1.Pod, 0, len(allPods.Items))}
for _, pod := range allPods.Items {
controllerRef := metav1.GetControllerOf(&pod)
if controllerRef != nil && controllerRef.UID == replicaSetUID {
ownedPods.Items = append(ownedPods.Items, pod)
}
}

and

for _, rs := range ownedReplicaSets {
if !podTemplatesEqualsIgnoringHash(&rs.Spec.Template, &deployment.Spec.Template) {
continue
}
replicaSet = rs
break

@k8s-ci-robot k8s-ci-robot added the do-not-merge/hold Indicates that a PR should not merge because someone has issued a /hold command. label Oct 6, 2021
@aojea
Copy link
Member

aojea commented Oct 7, 2021

/lgtm cancel
can we replace the loops using references in the same function too?

@k8s-ci-robot k8s-ci-robot removed the lgtm "Looks good to me", indicates that a PR is ready to be merged. label Oct 7, 2021
@k8s-ci-robot k8s-ci-robot removed the size/XS Denotes a PR that changes 0-9 lines, ignoring generated files. label Oct 7, 2021
@k8s-ci-robot k8s-ci-robot added the size/S Denotes a PR that changes 10-29 lines, ignoring generated files. label Oct 7, 2021
@shivanshuraj1333
Copy link
Contributor Author

Missed it previously, my bad
Fixed other for loops

for _, pod := range allPods.Items {
controllerRef := metav1.GetControllerOf(&pod)
for i, pod := range allPods.Items {
controllerRef := metav1.GetControllerOf(&allPods.Items[i])
if controllerRef != nil && controllerRef.UID == replicaSetUID {
ownedPods.Items = append(ownedPods.Items, pod)
Copy link
Member

Choose a reason for hiding this comment

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

should we use the same pattern everywhere?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

same as above

for _, rs := range ownedReplicaSets {
if !podTemplatesEqualsIgnoringHash(&rs.Spec.Template, &deployment.Spec.Template) {
for i, rs := range ownedReplicaSets {
if !podTemplatesEqualsIgnoringHash(&ownedReplicaSets[i].Spec.Template, &deployment.Spec.Template) {
Copy link
Member

Choose a reason for hiding this comment

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

should we use the same patter in line 134?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

In line number 134 since we're not passing the address of rs, we can use both rs and ownedReplicaSets[i].
The current code is more readable and it also highlights the reason behind why we are explicitly passing &ownedReplicaSets[i] instead of &rs

@shivanshuraj1333
Copy link
Contributor Author

@aojea if you think we still need to make the changes to the above review comments, kindly let me know. As far as I understand we can probably keep it like that.
Over to you xP

@aojea
Copy link
Member

aojea commented Oct 17, 2021

@aojea if you think we still need to make the changes to the above review comments, kindly let me know. As far as I understand we can probably keep it like that. Over to you xP

it is ok, but better if you can squash the commits

@shivanshuraj1333
Copy link
Contributor Author

using label to squash
/label tide/merge-method-squash

@k8s-ci-robot k8s-ci-robot added the tide/merge-method-squash Denotes a PR that should be squashed by tide when it merges. label Oct 17, 2021
@aojea
Copy link
Member

aojea commented Oct 17, 2021

/lgtm
Thanks

@k8s-ci-robot k8s-ci-robot added the lgtm "Looks good to me", indicates that a PR is ready to be merged. label Oct 17, 2021
@shivanshuraj1333
Copy link
Contributor Author

/hold cancel

@k8s-ci-robot k8s-ci-robot removed the do-not-merge/hold Indicates that a PR should not merge because someone has issued a /hold command. label Oct 19, 2021
@shivanshuraj1333
Copy link
Contributor Author

/retest

@k8s-ci-robot k8s-ci-robot merged commit 3e6d122 into kubernetes:master Oct 19, 2021
@k8s-ci-robot k8s-ci-robot added this to the v1.23 milestone Oct 19, 2021
tallclair pushed a commit to tallclair/kubernetes that referenced this pull request Oct 26, 2021
* fixed using reference to loop iterator

* fixed other for loops
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. area/e2e-test-framework Issues or PRs related to refactoring the kubernetes e2e test framework area/test 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. needs-priority Indicates a PR lacks a `priority/foo` label and requires one. needs-triage Indicates an issue or PR lacks a `triage/foo` label and requires one. release-note Denotes a PR that will be considered when it comes time to generate release notes. sig/testing Categorizes an issue or PR as relevant to SIG Testing. size/S Denotes a PR that changes 10-29 lines, ignoring generated files. tide/merge-method-squash Denotes a PR that should be squashed by tide when it merges.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Report a coding issue that uses reference to loop iterator variable
3 participants