modernize: simplify code by using modern constructs#5163
Conversation
There was a problem hiding this comment.
Hello @oilbeater, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!
Summary of Changes
This pull request focuses on modernizing the codebase by employing more concise and contemporary Go constructs. The changes include using strings.SplitSeq instead of strings.Split in several files, which avoids unnecessary memory allocation when iterating over the results of the split. It also replaces fmt.Sprintf with string concatenation for improved readability and performance. Additionally, it updates loop iterations and simplifies conditional logic for better code flow. Finally, it updates the go tooling to use modernize.
Highlights
- String Splitting: Replaced
strings.Splitwithstrings.SplitSeqfor more efficient iteration over split strings. - String Concatenation: Replaced
fmt.Sprintfwith direct string concatenation for simpler string formatting. - Looping: Simplified
forloop iterations usingrangefor cleaner code. - Conditional Logic: Simplified conditional logic to improve code readability.
- Go Tooling: Added
modernizetooling to the linting process.
Changelog
Click here to see the changelog
- build.mk
- Added
modernizetooling to the linting process.
- Added
- cmd/cni/cni.go
- Replaced
fmt.Sprintfwith string concatenation for constructing theaboutstring. - Replaced
strings.Splitwithstrings.SplitSeqfor iterating over CIDR blocks. - Replaced
strings.Splitwithstrings.SplitSeqand simplified string formatting inparseValueFromArgs.
- Replaced
- cmd/daemon/cniserver.go
- Removed unused
fmtimport. - Replaced
fmt.Sprintfwith string concatenation forFieldSelector.
- Removed unused
- pkg/apis/kubeovn/v1/condition.go
- Removed
omitemptytag fromLastUpdateTimeandLastTransitionTimefields.
- Removed
- pkg/apis/kubeovn/v1/ip.go
- Removed
omitemptytag fromObjectMetafield.
- Removed
- pkg/apis/kubeovn/v1/ippool.go
- Removed
omitemptytag fromObjectMetaandStatusfields.
- Removed
- pkg/apis/kubeovn/v1/iptables-dnat-rule.go
- Removed
omitemptytag fromObjectMetaandStatusfields.
- Removed
- pkg/apis/kubeovn/v1/iptables-eip.go
- Removed
omitemptytag fromObjectMetaandStatusfields.
- Removed
- pkg/apis/kubeovn/v1/iptables-fip-rule.go
- Removed
omitemptytag fromObjectMetaandStatusfields.
- Removed
- pkg/apis/kubeovn/v1/iptables-snat-rule.go
- Removed
omitemptytag fromObjectMetaandStatusfields.
- Removed
- pkg/apis/kubeovn/v1/ovn-dnat-rule.go
- Removed
omitemptytag fromObjectMetaandStatusfields.
- Removed
- pkg/apis/kubeovn/v1/ovn-eip.go
- Removed
omitemptytag fromObjectMetaandStatusfields.
- Removed
- pkg/apis/kubeovn/v1/ovn-fip.go
- Removed
omitemptytag fromObjectMetaandStatusfields.
- Removed
- pkg/apis/kubeovn/v1/ovn-snat-rule.go
- Removed
omitemptytag fromObjectMetaandStatusfields.
- Removed
- pkg/apis/kubeovn/v1/provider-network.go
- Added
slicesimport. - Removed
omitemptytag fromObjectMetafield. - Replaced
appendwithslices.Deletefor removing node conditions.
- Added
- pkg/apis/kubeovn/v1/qos-policy.go
- Removed
omitemptytag fromObjectMetaandStatusfields.
- Removed
- pkg/apis/kubeovn/v1/register_test.go
- Replaced standard
forloop withrangebased loop.
- Replaced standard
- pkg/apis/kubeovn/v1/security-group.go
- Removed
omitemptytag fromObjectMetafield.
- Removed
- pkg/apis/kubeovn/v1/subnet.go
- Removed
omitemptytag fromObjectMetaandStatusfields.
- Removed
- pkg/apis/kubeovn/v1/switch-lb-rule.go
- Removed
omitemptytag fromObjectMetaandStatusfields.
- Removed
- pkg/apis/kubeovn/v1/vip.go
- Removed
omitemptytag fromObjectMetaandStatusfields.
- Removed
- pkg/apis/kubeovn/v1/vlan.go
- Removed
omitemptytag fromObjectMetafield.
- Removed
- pkg/apis/kubeovn/v1/vpc-dns.go
- Removed
omitemptytag fromObjectMetaandStatusfields.
- Removed
- pkg/apis/kubeovn/v1/vpc-egress-gateway.go
- Removed
omitemptytag fromObjectMeta,BFDandStatusfields. - Removed
omitemptytag fromWorkloadfield.
- Removed
- pkg/apis/kubeovn/v1/vpc-nat-gateway.go
- Removed
omitemptytag fromObjectMetaandStatusfields.
- Removed
- pkg/apis/kubeovn/v1/vpc.go
- Removed
omitemptytag fromObjectMetaandStatusfields.
- Removed
- pkg/controller/admin_network_policy.go
- Replaced
interface{}withanyfor function arguments. - Replaced
strings.Splitwithstrings.SplitSeqfor iterating over pod IPs.
- Replaced
- pkg/controller/baseline_admin_network_policy.go
- Replaced
interface{}withanyfor function arguments.
- Replaced
- pkg/controller/config.go
- Replaced
strings.Splitwithstrings.SplitSeqfor iterating over node local DNS IPs.
- Replaced
- pkg/controller/controller.go
- Simplified calculation of
numKeyLocksusingmaxfunction. - Replaced standard
forloop withrangebased loop instartWorkersfunction.
- Simplified calculation of
- pkg/controller/deployment.go
- Replaced
interface{}withanyfor function arguments.
- Replaced
- pkg/controller/endpoint.go
- Replaced
interface{}withanyfor function arguments.
- Replaced
- pkg/controller/external_gw.go
- Replaced
strings.Splitwithstrings.SplitSeqfor iterating over external gateway nodes.
- Replaced
- pkg/controller/gc_test.go
- Removed unused
fmtimport. - Simplified string formatting for
peervariable.
- Removed unused
- pkg/controller/ip.go
- Replaced
interface{}withanyfor function arguments. - Replaced
strings.Splitwithstrings.SplitSeqfor iterating over IPs.
- Replaced
- pkg/controller/ippool.go
- Replaced
interface{}withanyfor function arguments.
- Replaced
- pkg/controller/kubevirt.go
- Replaced
interface{}withanyfor function arguments.
- Replaced
- pkg/controller/namespace.go
- Replaced
interface{}withanyfor function arguments. - Replaced standard
forloop withslices.Containsfor checking namespace inclusion in subnets. - Replaced
strings.Splitwithstrings.SplitSeqfor iterating over subnet names.
- Replaced
- pkg/controller/network_policy.go
- Replaced
interface{}withanyfor function arguments. - Replaced
strings.Splitwithstrings.SplitSeqfor iterating over pod IPs.
- Replaced
- pkg/controller/node.go
- Replaced
interface{}withanyfor function arguments. - Replaced
strings.Splitwithstrings.SplitSeqfor iterating over IPs and CIDR blocks. - Replaced standard
forloop withrangebased loop instartWorkersfunction.
- Replaced
- pkg/controller/ovn_dnat.go
- Replaced
interface{}withanyfor function arguments.
- Replaced
- pkg/controller/ovn_eip.go
- Replaced
interface{}withanyfor function arguments.
- Replaced
- pkg/controller/ovn_fip.go
- Replaced
interface{}withanyfor function arguments.
- Replaced
- pkg/controller/ovn_snat.go
- Replaced
interface{}withanyfor function arguments.
- Replaced
- pkg/controller/pod.go
- Replaced
interface{}withanyfor function arguments. - Replaced
strings.Splitwithstrings.SplitSeqfor iterating over IPs and VIPs. - Replaced standard
forloop withrangebased loop instartWorkersfunction. - Replaced
fmt.Sprintfwith string concatenation for constructing policy route matches.
- Replaced
- pkg/controller/qos_policy.go
- Replaced
interface{}withanyfor function arguments.
- Replaced
- pkg/controller/security_group.go
- Replaced
interface{}withanyfor function arguments. - Replaced
fmt.Sprintfwith string concatenation for external ID keys. - Replaced
strings.Splitwithstrings.SplitSeqfor iterating over security groups.
- Replaced
- pkg/controller/service.go
- Replaced
interface{}withanyfor function arguments. - Replaced
strings.Splitwithstrings.SplitSeqfor iterating over load balancer IPs.
- Replaced
- pkg/controller/service_lb.go
- Replaced
fmt.Sprintfwith string concatenation for generating service names.
- Replaced
- pkg/controller/signer.go
- Replaced
interface{}withanyfor function arguments.
- Replaced
- pkg/controller/subnet.go
- Replaced
interface{}withanyfor function arguments. - Replaced
strings.Splitwithstrings.SplitSeqfor iterating over CIDR blocks and gateway nodes. - Replaced
appendwithslices.Containsfor checking namespace inclusion in subnets. - Replaced
fmt.Sprintfwith string concatenation for constructing policy route matches. - Replaced standard
for ... rangeloop withmaps.Copyfor copying external IDs.
- Replaced
- pkg/controller/switch_lb_rule.go
- Replaced
interface{}withanyfor function arguments. - Replaced
fmt.Sprintfwith string concatenation for generating service names.
- Replaced
- pkg/controller/vip.go
- Replaced
interface{}withanyfor function arguments.
- Replaced
- pkg/controller/vlan.go
- Replaced
interface{}withanyfor function arguments.
- Replaced
- pkg/controller/vpc.go
- Replaced
interface{}withanyfor function arguments. - Replaced
strings.Splitwithstrings.SplitSeqfor iterating over next hop IPs.
- Replaced
- pkg/controller/vpc_dns.go
- Replaced
interface{}withanyfor function arguments. - Replaced
fmt.Sprintfwith string concatenation for generating deployment names. - Replaced
strings.Splitwithstrings.SplitSeqfor iterating over subnet gateways.
- Replaced
- pkg/controller/vpc_egress_gateway.go
- Replaced
interface{}withanyfor function arguments.
- Replaced
- pkg/controller/vpc_nat_gateway.go
- Replaced
interface{}withanyfor function arguments. - Replaced
fmt.Sprintfwith string concatenation for constructing commands and external IDs. - Replaced standard
for ... rangeloop withmaps.Copyfor copying pod annotations.
- Replaced
- pkg/controller/vpc_nat_gw_eip.go
- Replaced
interface{}withanyfor function arguments. - Replaced
strings.Splitwithstrings.SplitSeqfor iterating over IPs.
- Replaced
- pkg/controller/vpc_nat_gw_nat.go
- Replaced
interface{}withanyfor function arguments.
- Replaced
- pkg/daemon/config.go
- Replaced
strings.Splitwithstrings.SplitSeqfor iterating over IPs.
- Replaced
- pkg/daemon/controller.go
- Replaced
interface{}withanyfor function arguments.
- Replaced
- pkg/daemon/controller_linux.go
- Replaced
strings.Splitwithstrings.SplitSeqfor iterating over gateway IPs. - Replaced standard
forloop withslices.Containsfor checking rule existence.
- Replaced
- pkg/daemon/exporter_metric.go
- Replaced
strings.Splitwithstrings.SplitSeqfor iterating over lines in the output.
- Replaced
- pkg/daemon/gateway.go
- Replaced
strings.Splitwithstrings.SplitSeqfor iterating over CIDR blocks and gateway nodes.
- Replaced
- pkg/daemon/handler.go
- Replaced
interface{}withanyfor function arguments.
- Replaced
- pkg/daemon/ipsec.go
- Replaced
fmt.Sprintfwith string concatenation for constructing the subject in the CSR.
- Replaced
- pkg/daemon/ovs.go
- Replaced
fmt.Sprintfwith string concatenation for creating mirror names. - Replaced
strings.Splitwithstrings.SplitSeqfor iterating over ports.
- Replaced
- pkg/daemon/ovs_linux.go
- Replaced
fmt.Sprintfwith string concatenation for setting interface options and external IDs. - Replaced
strings.Splitwithstrings.SplitSeqfor iterating over tunnel IPs.
- Replaced
- pkg/informer/kubevirt.go
- Replaced
interface{}withanyfor function arguments.
- Replaced
- pkg/ipam/ip_range.go
- Replaced standard
forloop withrangebased loop for iterating over IP bytes.
- Replaced standard
- pkg/ipam/ip_range_list.go
- Replaced standard
forloop withrangebased loop for iterating over IP ranges. - Replaced
appendwithslices.Deletefor removing merged IP ranges.
- Replaced standard
- pkg/ovn_ic_controller/ovn_ic_controller.go
- Replaced
fmt.Sprintfwith string concatenation for constructing ovn-ctl commands.
- Replaced
- pkg/ovn_leader_checker/ovn.go
- Replaced
fmt.Sprintfwith string concatenation for constructing ovn-ctl commands.
- Replaced
- pkg/ovnmonitor/util.go
- Replaced
fmt.Sprintfwith string concatenation for constructing ovn-appctl commands. - Replaced
strings.Splitwithstrings.SplitSeqfor iterating over lines in the output.
- Replaced
- pkg/ovs/interface.go
- Replaced
interface{}withanyfor function arguments.
- Replaced
- pkg/ovs/ovn-ic-nbctl.go
- Replaced
fmt.Sprintfwith string concatenation for constructing ovn-ic-nbctl commands.
- Replaced
- pkg/ovs/ovn-ic-sbctl.go
- Replaced
fmt.Sprintfwith string concatenation for constructing ovn-ic-sbctl commands.
- Replaced
- pkg/ovs/ovn-nb-acl.go
- Replaced
strings.Splitwithstrings.SplitSeqfor iterating over CIDR blocks. - Replaced
interface{}withanyfor function arguments.
- Replaced
- pkg/ovs/ovn-nb-address_set.go
- Replaced
interface{}withanyfor function arguments.
- Replaced
- pkg/ovs/ovn-nb-bfd.go
- Replaced
interface{}withanyfor function arguments.
- Replaced
- pkg/ovs/ovn-nb-dhcp_options.go
- Replaced
interface{}withanyfor function arguments.
- Replaced
- pkg/ovs/ovn-nb-gateway_chassis.go
- Replaced
interface{}withanyfor function arguments.
- Replaced
- pkg/ovs/ovn-nb-load_balancer.go
- Replaced
interface{}withanyfor function arguments. - Replaced standard
for ... rangeloop withmaps.Copyfor copying options.
- Replaced
- pkg/ovs/ovn-nb-load_balancer_health_check.go
- Replaced
interface{}withanyfor function arguments.
- Replaced
- pkg/ovs/ovn-nb-logical_router.go
- Replaced
interface{}withanyfor function arguments.
- Replaced
- pkg/ovs/ovn-nb-logical_router_policy.go
- Replaced
interface{}withanyfor function arguments.
- Replaced
- pkg/ovs/ovn-nb-logical_router_port.go
- Replaced
interface{}withanyfor function arguments.
- Replaced
- pkg/ovs/ovn-nb-logical_router_route.go
- Replaced
interface{}withanyfor function arguments.
- Replaced
- pkg/ovs/ovn-sb-chassis.go
- Replaced
interface{}withanyfor function arguments. - Replaced standard
for ... rangeloop withmaps.Copyfor copying external IDs.
- Replaced
- pkg/ovs/ovn.go
- Replaced
interface{}withanyfor function arguments.
- Replaced
- pkg/pinger/config.go
- Replaced standard
forloop withrangebased loop for retrying pod retrieval.
- Replaced standard
- pkg/pinger/ovn.go
- Replaced
strings.Splitwithstrings.SplitSeqfor iterating over lines in the output.
- Replaced
- pkg/pinger/ping.go
- Replaced
strings.Splitwithstrings.SplitSeqfor iterating over external addresses and target IP ports.
- Replaced
- pkg/pinger/util.go
- Replaced
strings.Splitwithstrings.SplitSeqfor iterating over lines in the output.
- Replaced
- pkg/tproxy/tproxy_tcp_linux.go
- Simplified string formatting for
fdFilename.
- Simplified string formatting for
- pkg/util/arp.go
- Replaced standard
forloop withrangebased loop for probing and announcing ARP addresses.
- Replaced standard
- pkg/util/klog.go
- Replaced
interface{}withanyfor function arguments.
- Replaced
- pkg/util/net.go
- Replaced
strings.Splitwithstrings.SplitSeqfor iterating over CIDR blocks, gateway IPs, and IPs. - Replaced standard
for ... rangeloop withslices.Containsfor checking if a node is a gateway.
- Replaced
- pkg/util/patch.go
- Replaced
interface{}withanyfor function arguments.
- Replaced
- pkg/util/slice.go
- Replaced standard
forloop withslices.Containsfor checking string slice overlap.
- Replaced standard
- pkg/util/validator.go
- Replaced
strings.Splitwithstrings.SplitSeqfor iterating over CIDR blocks. - Replaced
fmt.Sprintfwith string concatenation for constructing error messages.
- Replaced
- pkg/util/version.go
- Replaced standard
forloop withrangebased loop for comparing version components.
- Replaced standard
- pkg/webhook/static_ip.go
- Replaced
strings.Splitwithstrings.SplitSeqfor iterating over IPs.
- Replaced
- pkg/webhook/vpc_nat_gateway.go
- Replaced
strings.Splitwithstrings.SplitSeqfor iterating over NAT types.
- Replaced
- test/e2e/framework/cni.go
- Removed
omitemptytag fromRuntimeConfigfield.
- Removed
- test/e2e/framework/daemonset.go
- Replaced
interface{}withanyfor function arguments.
- Replaced
- test/e2e/framework/deployment.go
- Replaced
interface{}withanyfor function arguments.
- Replaced
- test/e2e/framework/expect.go
- Replaced
interface{}withanyfor function arguments.
- Replaced
- test/e2e/framework/iproute/iproute.go
- Replaced standard
forloop withslices.Containsfor checking error types.
- Replaced standard
- test/e2e/framework/log.go
- Replaced
interface{}withanyfor function arguments.
- Replaced
- test/e2e/kube-ovn/kubectl-ko/kubectl-ko.go
- Replaced
fmt.Sprintfwith string concatenation for constructing command strings.
- Replaced
- test/e2e/kube-ovn/network-policy/network-policy.go
- Replaced
fmt.Sprintfwith string concatenation for constructing curl commands.
- Replaced
- test/e2e/kube-ovn/node/node.go
- Replaced
strings.Splitwithstrings.SplitSeqfor iterating over CIDR blocks.
- Replaced
- test/e2e/ovn-ic/e2e_test.go
- Replaced standard
forloop withrangebased loop for retrying command execution.
- Replaced standard
- test/e2e/ovn-vpc-nat-gw/e2e_test.go
- Replaced
fmt.Sprintfwith string concatenation for constructing pod names and commands.
- Replaced
- test/unittest/ipam/ip_range.go
- Replaced standard
forloop withrangebased loop for iterating over IP bytes.
- Replaced standard
- test/unittest/ipam/ip_range_list.go
- Replaced standard
forloop withrangebased loop for iterating over IP ranges. - Replaced
appendwithslices.Deletefor removing merged IP ranges.
- Replaced standard
Using Gemini Code Assist
The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.
Invoking Gemini
You can request assistance from Gemini at any point in your pull request via creating an issue comment (i.e. comment on the pull request page) using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands.
| Feature | Command | Description |
|---|---|---|
| Code Review | /gemini review |
Performs a code review for the current pull request in its current state. |
| Pull Request Summary | /gemini summary |
Provides a summary of the current pull request in its current state. |
| Comment | @gemini-code-assist | Responds in comments when explicitly tagged, both in issue comments and review comments. |
| Help | /gemini help |
Displays a list of available commands. |
Customization
To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.
Limitations & Feedback
Gemini Code Assist is currently in preview and may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments to provide feedback.
You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.
In realms of code, where patterns reside,
Modern constructs help us stride.
With simpler forms,
Averts all the storms,
And clarity becomes our guide.
Footnotes
-
Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution. ↩
Pull Request Test Coverage Report for Build 14533560450Details
💛 - Coveralls |
|
Warning Gemini encountered an error creating the review. You can try again by commenting |
Signed-off-by: Mengxin Liu <liumengxinfly@gmail.com>
27a03e0 to
962b36d
Compare
Pull Request
What type of this PR
Examples of user facing changes:
Which issue(s) this PR fixes
Fixes #(issue-number)