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

all: fix setupEmbedCfg functions to set cfg.InitialCluster more safely #18009

Closed
wants to merge 1 commit into from

Conversation

callthingsoff
Copy link
Contributor

When the slice is empty, it can panic with "index 1 out of bounds". This patch rewrites these functions to set cfg.InitialCluster more safely.

Please read https://github.com/etcd-io/etcd/blob/main/CONTRIBUTING.md#contribution-flow.

@k8s-ci-robot
Copy link

Hi @callthingsoff. Thanks for your PR.

I'm waiting for a etcd-io 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-sigs/prow repository.

@callthingsoff
Copy link
Contributor Author

cc @ahrtr @serathius @jmhbnz

@jmhbnz
Copy link
Member

jmhbnz commented May 15, 2024

/ok-to-test

Copy link
Member

@ivanvc ivanvc left a comment

Choose a reason for hiding this comment

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

Thanks for your PR @callthingsoff. I left a possible improvement for your code ✌️

tests/integration/embed/embed_test.go Outdated Show resolved Hide resolved
tools/etcd-dump-metrics/etcd.go Outdated Show resolved Hide resolved
tests/integration/embed/embed_test.go Outdated Show resolved Hide resolved
tools/etcd-dump-metrics/etcd.go Outdated Show resolved Hide resolved
@serathius
Copy link
Member

QQ, if you are touching this code. Have you double checked if it has any tests? If not please start from adding tests.

Copy link
Member

@ivanvc ivanvc left a comment

Choose a reason for hiding this comment

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

This is a valid solution, and it works. My only concern is that I feel the readability could be improved. I feel that the original solution was intricate. i.e., I think something like this would work too:

initialCluster := make([]string, len(ics))
for i, ic := range ics {
	initialCluster[i] = fmt.Sprintf("%d=%s", i, ic.String())
}
cfg.InitialCluster = strings.Join(initialCluster, ",")

Ultimately, this code doesn't need to be highly efficient as it's used in the tests and the etcd-dump-metrics tool. So, I may weigh readability over performance. But readability could also be something subjective 😅. So, I'm just leaving this as food for thought.

Thanks, @callthingsoff.

tests/integration/embed/join_urls_test.go Outdated Show resolved Hide resolved
When the slice is empty, it can panic with "index 1 out of bounds".
This patch rewrites these functions to set cfg.InitialCluster more safely.

Signed-off-by: Jes Cok <xigua67damn@gmail.com>
@ivanvc
Copy link
Member

ivanvc commented May 16, 2024

/retest

},
}
for i, tt := range tests {
if got := joinURLs(tt.urls); got != tt.want {
Copy link
Member

Choose a reason for hiding this comment

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

joinURLs is already part of the test, we are adding test for test. Not necessary to me.

Comment on lines -56 to -58
cfg.InitialCluster = ""
for i := range ics {
cfg.InitialCluster += fmt.Sprintf(",%d=%s", i, ics[i].String())
Copy link
Member

Choose a reason for hiding this comment

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

It seems an unnecessary change,

  • The function setupEmbedCfg is already very simple.
  • The new function joinURLs isn't used by multiple users (it has only one user).

@callthingsoff
Copy link
Contributor Author

Thanks, closing this.

@callthingsoff callthingsoff deleted the fix_setupEmbedCfg branch May 17, 2024 04:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

Successfully merging this pull request may close these issues.

None yet

7 participants