-
Notifications
You must be signed in to change notification settings - Fork 5k
refactor: use slices.Contains to simplify code #21381
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
base: master
Are you sure you want to change the base?
Conversation
|
Welcome @deepdring! |
Hi @deepdring. Thanks for your PR. I'm waiting for a kubernetes member to verify that this patch is reasonable to test. If it is, they should reply with Once the patch is verified, the new status will be reflected by the 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. |
Can one of the admins verify this patch? |
thank you @deepdring was that the only place we could replace it ? |
/ok-to-test |
This comment has been minimized.
This comment has been minimized.
Thank you for your response. So far, I have only checked and modified the changes in the cmd directory, but I just noticed that there are still many modifications under the pkg directory. Should I continue to submit on this PR, or would it be better for the review if I create a separate PR for the changes under the pkg directory? |
Looking at the CI errors, they should not be related to this change. FAIL k8s.io/minikube/cmd/minikube/cmd 0.761s
# k8s.io/minikube/pkg/util [k8s.io/minikube/pkg/util.test]
Error: pkg\util\utils_test.go:118:92: undefined: syscall.Stat_t
make: *** [Makefile:421: test] Interrupt
Error: The action 'unit test' has timed out after 5 minutes. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice!
Modified. Please review again. 😄 |
This comment has been minimized.
This comment has been minimized.
The changes have been made; please review again. |
This comment has been minimized.
This comment has been minimized.
[APPROVALNOTIFIER] This PR is NOT APPROVED This pull-request has been approved by: deepdring, nirs The full list of commands accepted by this bot can be found here.
Needs approval from an approver in each of these files:
Approvers can indicate their approval by writing |
Finding all code that can be replaced with slices.Contains is not easy. We have more than 1000 for loops: % git grep -E 'for .+ := range' | wc -l
1048 Searching for found = true: % git grep -A5 -B5 'found = true'
cmd/minikube/cmd/root.go- // Check whether this is a windows binary (.exe) running inisde WSL.
cmd/minikube/cmd/root.go- if runtime.GOOS == "windows" && detect.IsMicrosoftWSL() {
cmd/minikube/cmd/root.go- var found = false
cmd/minikube/cmd/root.go- for _, a := range os.Args {
cmd/minikube/cmd/root.go- if a == "--force" {
cmd/minikube/cmd/root.go: found = true
cmd/minikube/cmd/root.go- break
cmd/minikube/cmd/root.go- }
cmd/minikube/cmd/root.go- }
cmd/minikube/cmd/root.go- if !found {
cmd/minikube/cmd/root.go- exit.Message(reason.WrongBinaryWSL, "You are trying to run a windows .exe binary inside WSL. For better integration please use a Linux binary instead (Download at https://minikube.sigs.k8s.io/docs/start/.). Otherwise if you still want to do this, you can do it using --force")
--
cmd/minikube/cmd/start_test.go- } else {
cmd/minikube/cmd/start_test.go- // proxy must in config
cmd/minikube/cmd/start_test.go- found := false
cmd/minikube/cmd/start_test.go- for _, v := range config.DockerEnv {
cmd/minikube/cmd/start_test.go- if v == proxyEnv {
cmd/minikube/cmd/start_test.go: found = true
cmd/minikube/cmd/start_test.go- break
cmd/minikube/cmd/start_test.go- }
cmd/minikube/cmd/start_test.go- }
cmd/minikube/cmd/start_test.go- if !found {
cmd/minikube/cmd/start_test.go- t.Fatalf("Value %s expected in dockerEnv but not occurred", test.proxy)
--
pkg/drivers/kvm/gpu.go- }
pkg/drivers/kvm/gpu.go-
pkg/drivers/kvm/gpu.go- // Check if this is an NVIDIA device
pkg/drivers/kvm/gpu.go- if strings.EqualFold(strings.TrimSpace(string(content)), nvidiaVendorID) {
pkg/drivers/kvm/gpu.go- log.Infof("Found device %v with NVIDIA's vendorId %v", device.Name(), nvidiaVendorID)
pkg/drivers/kvm/gpu.go: found = true
pkg/drivers/kvm/gpu.go-
pkg/drivers/kvm/gpu.go- // Check whether it's unbound. We don't want the device to be bound to nvidia/nouveau etc.
pkg/drivers/kvm/gpu.go- if isUnbound(device.Name()) {
pkg/drivers/kvm/gpu.go- // Add the unbound device to the map. The value is set to false initially,
pkg/drivers/kvm/gpu.go- // it will be set to true later if the device is also isolated.
--
pkg/drivers/kvm/kvm.go- }
pkg/drivers/kvm/kvm.go-
pkg/drivers/kvm/kvm.go- var found bool
pkg/drivers/kvm/kvm.go- for _, domain := range definedDomains {
pkg/drivers/kvm/kvm.go- if domain == d.MachineName {
pkg/drivers/kvm/kvm.go: found = true
pkg/drivers/kvm/kvm.go- break
pkg/drivers/kvm/kvm.go- }
pkg/drivers/kvm/kvm.go- }
pkg/drivers/kvm/kvm.go-
pkg/drivers/kvm/kvm.go- if !found {
--
pkg/minikube/cruntime/containerd.go- found := false
pkg/minikube/cruntime/containerd.go- for _, ji := range jsonImages.Images {
pkg/minikube/cruntime/containerd.go- for _, rt := range ji.RepoTags {
pkg/minikube/cruntime/containerd.go- i = addRepoTagToImageName(i)
pkg/minikube/cruntime/containerd.go- if i == rt {
pkg/minikube/cruntime/containerd.go: found = true
pkg/minikube/cruntime/containerd.go- break
pkg/minikube/cruntime/containerd.go- }
pkg/minikube/cruntime/containerd.go- }
pkg/minikube/cruntime/containerd.go- if found {
pkg/minikube/cruntime/containerd.go- break
--
pkg/minikube/cruntime/crio.go- found := false
pkg/minikube/cruntime/crio.go- for _, ji := range jsonImages.Images {
pkg/minikube/cruntime/crio.go- for _, rt := range ji.RepoTags {
pkg/minikube/cruntime/crio.go- i = addRepoTagToImageName(i)
pkg/minikube/cruntime/crio.go- if i == rt {
pkg/minikube/cruntime/crio.go: found = true
pkg/minikube/cruntime/crio.go- break
pkg/minikube/cruntime/crio.go- }
pkg/minikube/cruntime/crio.go- }
pkg/minikube/cruntime/crio.go- if found {
pkg/minikube/cruntime/crio.go- break
--
pkg/minikube/driver/driver_test.go-func TestSupportedDrivers(t *testing.T) {
pkg/minikube/driver/driver_test.go- got := SupportedDrivers()
pkg/minikube/driver/driver_test.go- found := false
pkg/minikube/driver/driver_test.go- for _, s := range SupportedDrivers() {
pkg/minikube/driver/driver_test.go- if s == SSH {
pkg/minikube/driver/driver_test.go: found = true
pkg/minikube/driver/driver_test.go- }
pkg/minikube/driver/driver_test.go- }
pkg/minikube/driver/driver_test.go-
pkg/minikube/driver/driver_test.go- if found == false {
pkg/minikube/driver/driver_test.go- t.Errorf("%s not in supported drivers: %v", SSH, got)
--
pkg/minikube/logs/logs_test.go- expectedPods := []string{"kubernetes-dashboard", "gcp-auth", "controller_ingress", "storage-provisioner"}
pkg/minikube/logs/logs_test.go- for _, expectedPod := range expectedPods {
pkg/minikube/logs/logs_test.go- found := false
pkg/minikube/logs/logs_test.go- for _, pod := range got {
pkg/minikube/logs/logs_test.go- if expectedPod == pod {
pkg/minikube/logs/logs_test.go: found = true
pkg/minikube/logs/logs_test.go- break
pkg/minikube/logs/logs_test.go- }
pkg/minikube/logs/logs_test.go- }
pkg/minikube/logs/logs_test.go- if !found {
pkg/minikube/logs/logs_test.go- t.Errorf("%q was not found; got = %s", expectedPod, got)
--
pkg/minikube/machine/cache_images.go- // if the image is loaded with a tarball without a registry specified in tag
pkg/minikube/machine/cache_images.go- // see https://github.com/containers/podman/issues/15974
pkg/minikube/machine/cache_images.go- tryImageExist := []string{imgName, cruntime.AddDockerIO(imgName), cruntime.AddLocalhostPrefix(imgName)}
pkg/minikube/machine/cache_images.go- for _, imgName = range tryImageExist {
pkg/minikube/machine/cache_images.go- if r.ImageExists(imgName, "") {
pkg/minikube/machine/cache_images.go: found = true
pkg/minikube/machine/cache_images.go- break
pkg/minikube/machine/cache_images.go- }
pkg/minikube/machine/cache_images.go- }
pkg/minikube/machine/cache_images.go-
pkg/minikube/machine/cache_images.go- if !found {
--
pkg/minikube/machine/cluster_test.go- }
pkg/minikube/machine/cluster_test.go-
pkg/minikube/machine/cluster_test.go- found := false
pkg/minikube/machine/cluster_test.go- for _, def := range registry.List() {
pkg/minikube/machine/cluster_test.go- if h.DriverName == def.Name {
pkg/minikube/machine/cluster_test.go: found = true
pkg/minikube/machine/cluster_test.go- break
pkg/minikube/machine/cluster_test.go- }
pkg/minikube/machine/cluster_test.go- }
pkg/minikube/machine/cluster_test.go-
pkg/minikube/machine/cluster_test.go- if !found {
--
pkg/minikube/reason/match_test.go- }
pkg/minikube/reason/match_test.go-
pkg/minikube/reason/match_test.go- found := false
pkg/minikube/reason/match_test.go- for _, i := range got.Issues {
pkg/minikube/reason/match_test.go- if i == tc.issue {
pkg/minikube/reason/match_test.go: found = true
pkg/minikube/reason/match_test.go- }
pkg/minikube/reason/match_test.go- }
pkg/minikube/reason/match_test.go- if !found {
pkg/minikube/reason/match_test.go- t.Errorf("Issue %d is not listed in %+v", tc.issue, got.Issues)
pkg/minikube/reason/match_test.go- }
--
test/integration/functional_test.go- }
test/integration/functional_test.go- expectedDir := "cloud.google.com"
test/integration/functional_test.go- found := false
test/integration/functional_test.go- for _, file := range files {
test/integration/functional_test.go- if file.Name() == expectedDir {
test/integration/functional_test.go: found = true
test/integration/functional_test.go- break
test/integration/functional_test.go- }
test/integration/functional_test.go- }
test/integration/functional_test.go- if !found {
test/integration/functional_test.go- t.Fatalf("expected licenses dir to contain %s dir, but was not found", expectedDir) Maybe AI can help here? |
Searching with claude-sonnet-4 with this PR reports:
I'll help you find code that can be replaced with Let me search more specifically for the patterns I found and get the actual function implementations: Now let me search for more patterns that could be replaced with Let me search for more specific patterns of contains functions: Let me check the Let me check the Based on my analysis of the codebase, I found several functions that can be replaced with Functions That Can Be Replaced with
|
@deepdring check #21381 (comment) for other places that can use slices.Contains. |
@deepdring see #21428 you want to more work on modernizing minikube. |
Thank you for your help. I have found more and made the changes accordingly. Please review it again. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ContainsFunc has a bug
This comment has been minimized.
This comment has been minimized.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
deepdring can you please rebase?
kvm2 driver with docker runtime
Times for minikube start: 48.5s 44.8s 47.4s 44.8s 46.6s Times for minikube ingress: 15.7s 15.7s 15.2s 14.7s 15.2s docker driver with docker runtime
Times for minikube start: 22.0s 22.8s 26.2s 24.2s 22.6s Times for minikube ingress: 13.6s 13.6s 11.6s 13.6s 13.6s docker driver with containerd runtime
Times for minikube start: 21.0s 20.4s 22.2s 23.5s 21.0s Times for minikube ingress: 23.1s 24.1s 24.1s 23.1s 24.1s |
Signed-off-by: deepdring <[email protected]>
Of course. Rebased. |
There is a new function added in the go1.21 standard library, which can make the code more concise and easy to read.