Skip to content

modernize: simplify code by using modern constructs#5163

Merged
oilbeater merged 1 commit intomasterfrom
chore/modernize
Apr 19, 2025
Merged

modernize: simplify code by using modern constructs#5163
oilbeater merged 1 commit intomasterfrom
chore/modernize

Conversation

@oilbeater
Copy link
Copy Markdown
Collaborator

Pull Request

What type of this PR

Examples of user facing changes:

  • chore

Which issue(s) this PR fixes

Fixes #(issue-number)

@dosubot dosubot bot added size:L This PR changes 100-499 lines, ignoring generated files. refactor labels Apr 18, 2025
Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

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.Split with strings.SplitSeq for more efficient iteration over split strings.
  • String Concatenation: Replaced fmt.Sprintf with direct string concatenation for simpler string formatting.
  • Looping: Simplified for loop iterations using range for cleaner code.
  • Conditional Logic: Simplified conditional logic to improve code readability.
  • Go Tooling: Added modernize tooling to the linting process.

Changelog

Click here to see the changelog
  • build.mk
    • Added modernize tooling to the linting process.
  • cmd/cni/cni.go
    • Replaced fmt.Sprintf with string concatenation for constructing the about string.
    • Replaced strings.Split with strings.SplitSeq for iterating over CIDR blocks.
    • Replaced strings.Split with strings.SplitSeq and simplified string formatting in parseValueFromArgs.
  • cmd/daemon/cniserver.go
    • Removed unused fmt import.
    • Replaced fmt.Sprintf with string concatenation for FieldSelector.
  • pkg/apis/kubeovn/v1/condition.go
    • Removed omitempty tag from LastUpdateTime and LastTransitionTime fields.
  • pkg/apis/kubeovn/v1/ip.go
    • Removed omitempty tag from ObjectMeta field.
  • pkg/apis/kubeovn/v1/ippool.go
    • Removed omitempty tag from ObjectMeta and Status fields.
  • pkg/apis/kubeovn/v1/iptables-dnat-rule.go
    • Removed omitempty tag from ObjectMeta and Status fields.
  • pkg/apis/kubeovn/v1/iptables-eip.go
    • Removed omitempty tag from ObjectMeta and Status fields.
  • pkg/apis/kubeovn/v1/iptables-fip-rule.go
    • Removed omitempty tag from ObjectMeta and Status fields.
  • pkg/apis/kubeovn/v1/iptables-snat-rule.go
    • Removed omitempty tag from ObjectMeta and Status fields.
  • pkg/apis/kubeovn/v1/ovn-dnat-rule.go
    • Removed omitempty tag from ObjectMeta and Status fields.
  • pkg/apis/kubeovn/v1/ovn-eip.go
    • Removed omitempty tag from ObjectMeta and Status fields.
  • pkg/apis/kubeovn/v1/ovn-fip.go
    • Removed omitempty tag from ObjectMeta and Status fields.
  • pkg/apis/kubeovn/v1/ovn-snat-rule.go
    • Removed omitempty tag from ObjectMeta and Status fields.
  • pkg/apis/kubeovn/v1/provider-network.go
    • Added slices import.
    • Removed omitempty tag from ObjectMeta field.
    • Replaced append with slices.Delete for removing node conditions.
  • pkg/apis/kubeovn/v1/qos-policy.go
    • Removed omitempty tag from ObjectMeta and Status fields.
  • pkg/apis/kubeovn/v1/register_test.go
    • Replaced standard for loop with range based loop.
  • pkg/apis/kubeovn/v1/security-group.go
    • Removed omitempty tag from ObjectMeta field.
  • pkg/apis/kubeovn/v1/subnet.go
    • Removed omitempty tag from ObjectMeta and Status fields.
  • pkg/apis/kubeovn/v1/switch-lb-rule.go
    • Removed omitempty tag from ObjectMeta and Status fields.
  • pkg/apis/kubeovn/v1/vip.go
    • Removed omitempty tag from ObjectMeta and Status fields.
  • pkg/apis/kubeovn/v1/vlan.go
    • Removed omitempty tag from ObjectMeta field.
  • pkg/apis/kubeovn/v1/vpc-dns.go
    • Removed omitempty tag from ObjectMeta and Status fields.
  • pkg/apis/kubeovn/v1/vpc-egress-gateway.go
    • Removed omitempty tag from ObjectMeta, BFD and Status fields.
    • Removed omitempty tag from Workload field.
  • pkg/apis/kubeovn/v1/vpc-nat-gateway.go
    • Removed omitempty tag from ObjectMeta and Status fields.
  • pkg/apis/kubeovn/v1/vpc.go
    • Removed omitempty tag from ObjectMeta and Status fields.
  • pkg/controller/admin_network_policy.go
    • Replaced interface{} with any for function arguments.
    • Replaced strings.Split with strings.SplitSeq for iterating over pod IPs.
  • pkg/controller/baseline_admin_network_policy.go
    • Replaced interface{} with any for function arguments.
  • pkg/controller/config.go
    • Replaced strings.Split with strings.SplitSeq for iterating over node local DNS IPs.
  • pkg/controller/controller.go
    • Simplified calculation of numKeyLocks using max function.
    • Replaced standard for loop with range based loop in startWorkers function.
  • pkg/controller/deployment.go
    • Replaced interface{} with any for function arguments.
  • pkg/controller/endpoint.go
    • Replaced interface{} with any for function arguments.
  • pkg/controller/external_gw.go
    • Replaced strings.Split with strings.SplitSeq for iterating over external gateway nodes.
  • pkg/controller/gc_test.go
    • Removed unused fmt import.
    • Simplified string formatting for peer variable.
  • pkg/controller/ip.go
    • Replaced interface{} with any for function arguments.
    • Replaced strings.Split with strings.SplitSeq for iterating over IPs.
  • pkg/controller/ippool.go
    • Replaced interface{} with any for function arguments.
  • pkg/controller/kubevirt.go
    • Replaced interface{} with any for function arguments.
  • pkg/controller/namespace.go
    • Replaced interface{} with any for function arguments.
    • Replaced standard for loop with slices.Contains for checking namespace inclusion in subnets.
    • Replaced strings.Split with strings.SplitSeq for iterating over subnet names.
  • pkg/controller/network_policy.go
    • Replaced interface{} with any for function arguments.
    • Replaced strings.Split with strings.SplitSeq for iterating over pod IPs.
  • pkg/controller/node.go
    • Replaced interface{} with any for function arguments.
    • Replaced strings.Split with strings.SplitSeq for iterating over IPs and CIDR blocks.
    • Replaced standard for loop with range based loop in startWorkers function.
  • pkg/controller/ovn_dnat.go
    • Replaced interface{} with any for function arguments.
  • pkg/controller/ovn_eip.go
    • Replaced interface{} with any for function arguments.
  • pkg/controller/ovn_fip.go
    • Replaced interface{} with any for function arguments.
  • pkg/controller/ovn_snat.go
    • Replaced interface{} with any for function arguments.
  • pkg/controller/pod.go
    • Replaced interface{} with any for function arguments.
    • Replaced strings.Split with strings.SplitSeq for iterating over IPs and VIPs.
    • Replaced standard for loop with range based loop in startWorkers function.
    • Replaced fmt.Sprintf with string concatenation for constructing policy route matches.
  • pkg/controller/qos_policy.go
    • Replaced interface{} with any for function arguments.
  • pkg/controller/security_group.go
    • Replaced interface{} with any for function arguments.
    • Replaced fmt.Sprintf with string concatenation for external ID keys.
    • Replaced strings.Split with strings.SplitSeq for iterating over security groups.
  • pkg/controller/service.go
    • Replaced interface{} with any for function arguments.
    • Replaced strings.Split with strings.SplitSeq for iterating over load balancer IPs.
  • pkg/controller/service_lb.go
    • Replaced fmt.Sprintf with string concatenation for generating service names.
  • pkg/controller/signer.go
    • Replaced interface{} with any for function arguments.
  • pkg/controller/subnet.go
    • Replaced interface{} with any for function arguments.
    • Replaced strings.Split with strings.SplitSeq for iterating over CIDR blocks and gateway nodes.
    • Replaced append with slices.Contains for checking namespace inclusion in subnets.
    • Replaced fmt.Sprintf with string concatenation for constructing policy route matches.
    • Replaced standard for ... range loop with maps.Copy for copying external IDs.
  • pkg/controller/switch_lb_rule.go
    • Replaced interface{} with any for function arguments.
    • Replaced fmt.Sprintf with string concatenation for generating service names.
  • pkg/controller/vip.go
    • Replaced interface{} with any for function arguments.
  • pkg/controller/vlan.go
    • Replaced interface{} with any for function arguments.
  • pkg/controller/vpc.go
    • Replaced interface{} with any for function arguments.
    • Replaced strings.Split with strings.SplitSeq for iterating over next hop IPs.
  • pkg/controller/vpc_dns.go
    • Replaced interface{} with any for function arguments.
    • Replaced fmt.Sprintf with string concatenation for generating deployment names.
    • Replaced strings.Split with strings.SplitSeq for iterating over subnet gateways.
  • pkg/controller/vpc_egress_gateway.go
    • Replaced interface{} with any for function arguments.
  • pkg/controller/vpc_nat_gateway.go
    • Replaced interface{} with any for function arguments.
    • Replaced fmt.Sprintf with string concatenation for constructing commands and external IDs.
    • Replaced standard for ... range loop with maps.Copy for copying pod annotations.
  • pkg/controller/vpc_nat_gw_eip.go
    • Replaced interface{} with any for function arguments.
    • Replaced strings.Split with strings.SplitSeq for iterating over IPs.
  • pkg/controller/vpc_nat_gw_nat.go
    • Replaced interface{} with any for function arguments.
  • pkg/daemon/config.go
    • Replaced strings.Split with strings.SplitSeq for iterating over IPs.
  • pkg/daemon/controller.go
    • Replaced interface{} with any for function arguments.
  • pkg/daemon/controller_linux.go
    • Replaced strings.Split with strings.SplitSeq for iterating over gateway IPs.
    • Replaced standard for loop with slices.Contains for checking rule existence.
  • pkg/daemon/exporter_metric.go
    • Replaced strings.Split with strings.SplitSeq for iterating over lines in the output.
  • pkg/daemon/gateway.go
    • Replaced strings.Split with strings.SplitSeq for iterating over CIDR blocks and gateway nodes.
  • pkg/daemon/handler.go
    • Replaced interface{} with any for function arguments.
  • pkg/daemon/ipsec.go
    • Replaced fmt.Sprintf with string concatenation for constructing the subject in the CSR.
  • pkg/daemon/ovs.go
    • Replaced fmt.Sprintf with string concatenation for creating mirror names.
    • Replaced strings.Split with strings.SplitSeq for iterating over ports.
  • pkg/daemon/ovs_linux.go
    • Replaced fmt.Sprintf with string concatenation for setting interface options and external IDs.
    • Replaced strings.Split with strings.SplitSeq for iterating over tunnel IPs.
  • pkg/informer/kubevirt.go
    • Replaced interface{} with any for function arguments.
  • pkg/ipam/ip_range.go
    • Replaced standard for loop with range based loop for iterating over IP bytes.
  • pkg/ipam/ip_range_list.go
    • Replaced standard for loop with range based loop for iterating over IP ranges.
    • Replaced append with slices.Delete for removing merged IP ranges.
  • pkg/ovn_ic_controller/ovn_ic_controller.go
    • Replaced fmt.Sprintf with string concatenation for constructing ovn-ctl commands.
  • pkg/ovn_leader_checker/ovn.go
    • Replaced fmt.Sprintf with string concatenation for constructing ovn-ctl commands.
  • pkg/ovnmonitor/util.go
    • Replaced fmt.Sprintf with string concatenation for constructing ovn-appctl commands.
    • Replaced strings.Split with strings.SplitSeq for iterating over lines in the output.
  • pkg/ovs/interface.go
    • Replaced interface{} with any for function arguments.
  • pkg/ovs/ovn-ic-nbctl.go
    • Replaced fmt.Sprintf with string concatenation for constructing ovn-ic-nbctl commands.
  • pkg/ovs/ovn-ic-sbctl.go
    • Replaced fmt.Sprintf with string concatenation for constructing ovn-ic-sbctl commands.
  • pkg/ovs/ovn-nb-acl.go
    • Replaced strings.Split with strings.SplitSeq for iterating over CIDR blocks.
    • Replaced interface{} with any for function arguments.
  • pkg/ovs/ovn-nb-address_set.go
    • Replaced interface{} with any for function arguments.
  • pkg/ovs/ovn-nb-bfd.go
    • Replaced interface{} with any for function arguments.
  • pkg/ovs/ovn-nb-dhcp_options.go
    • Replaced interface{} with any for function arguments.
  • pkg/ovs/ovn-nb-gateway_chassis.go
    • Replaced interface{} with any for function arguments.
  • pkg/ovs/ovn-nb-load_balancer.go
    • Replaced interface{} with any for function arguments.
    • Replaced standard for ... range loop with maps.Copy for copying options.
  • pkg/ovs/ovn-nb-load_balancer_health_check.go
    • Replaced interface{} with any for function arguments.
  • pkg/ovs/ovn-nb-logical_router.go
    • Replaced interface{} with any for function arguments.
  • pkg/ovs/ovn-nb-logical_router_policy.go
    • Replaced interface{} with any for function arguments.
  • pkg/ovs/ovn-nb-logical_router_port.go
    • Replaced interface{} with any for function arguments.
  • pkg/ovs/ovn-nb-logical_router_route.go
    • Replaced interface{} with any for function arguments.
  • pkg/ovs/ovn-sb-chassis.go
    • Replaced interface{} with any for function arguments.
    • Replaced standard for ... range loop with maps.Copy for copying external IDs.
  • pkg/ovs/ovn.go
    • Replaced interface{} with any for function arguments.
  • pkg/pinger/config.go
    • Replaced standard for loop with range based loop for retrying pod retrieval.
  • pkg/pinger/ovn.go
    • Replaced strings.Split with strings.SplitSeq for iterating over lines in the output.
  • pkg/pinger/ping.go
    • Replaced strings.Split with strings.SplitSeq for iterating over external addresses and target IP ports.
  • pkg/pinger/util.go
    • Replaced strings.Split with strings.SplitSeq for iterating over lines in the output.
  • pkg/tproxy/tproxy_tcp_linux.go
    • Simplified string formatting for fdFile name.
  • pkg/util/arp.go
    • Replaced standard for loop with range based loop for probing and announcing ARP addresses.
  • pkg/util/klog.go
    • Replaced interface{} with any for function arguments.
  • pkg/util/net.go
    • Replaced strings.Split with strings.SplitSeq for iterating over CIDR blocks, gateway IPs, and IPs.
    • Replaced standard for ... range loop with slices.Contains for checking if a node is a gateway.
  • pkg/util/patch.go
    • Replaced interface{} with any for function arguments.
  • pkg/util/slice.go
    • Replaced standard for loop with slices.Contains for checking string slice overlap.
  • pkg/util/validator.go
    • Replaced strings.Split with strings.SplitSeq for iterating over CIDR blocks.
    • Replaced fmt.Sprintf with string concatenation for constructing error messages.
  • pkg/util/version.go
    • Replaced standard for loop with range based loop for comparing version components.
  • pkg/webhook/static_ip.go
    • Replaced strings.Split with strings.SplitSeq for iterating over IPs.
  • pkg/webhook/vpc_nat_gateway.go
    • Replaced strings.Split with strings.SplitSeq for iterating over NAT types.
  • test/e2e/framework/cni.go
    • Removed omitempty tag from RuntimeConfig field.
  • test/e2e/framework/daemonset.go
    • Replaced interface{} with any for function arguments.
  • test/e2e/framework/deployment.go
    • Replaced interface{} with any for function arguments.
  • test/e2e/framework/expect.go
    • Replaced interface{} with any for function arguments.
  • test/e2e/framework/iproute/iproute.go
    • Replaced standard for loop with slices.Contains for checking error types.
  • test/e2e/framework/log.go
    • Replaced interface{} with any for function arguments.
  • test/e2e/kube-ovn/kubectl-ko/kubectl-ko.go
    • Replaced fmt.Sprintf with string concatenation for constructing command strings.
  • test/e2e/kube-ovn/network-policy/network-policy.go
    • Replaced fmt.Sprintf with string concatenation for constructing curl commands.
  • test/e2e/kube-ovn/node/node.go
    • Replaced strings.Split with strings.SplitSeq for iterating over CIDR blocks.
  • test/e2e/ovn-ic/e2e_test.go
    • Replaced standard for loop with range based loop for retrying command execution.
  • test/e2e/ovn-vpc-nat-gw/e2e_test.go
    • Replaced fmt.Sprintf with string concatenation for constructing pod names and commands.
  • test/unittest/ipam/ip_range.go
    • Replaced standard for loop with range based loop for iterating over IP bytes.
  • test/unittest/ipam/ip_range_list.go
    • Replaced standard for loop with range based loop for iterating over IP ranges.
    • Replaced append with slices.Delete for removing merged IP ranges.
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

  1. 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.

@coveralls
Copy link
Copy Markdown

Pull Request Test Coverage Report for Build 14533560450

Details

  • 87 of 376 (23.14%) changed or added relevant lines in 94 files are covered.
  • 3 unchanged lines in 2 files lost coverage.
  • Overall coverage decreased (-0.02%) to 21.721%

Changes Missing Coverage Covered Lines Changed/Added Lines %
pkg/apis/kubeovn/v1/provider-network.go 0 1 0.0%
pkg/controller/config.go 0 1 0.0%
pkg/controller/service_lb.go 0 1 0.0%
pkg/daemon/config.go 0 1 0.0%
pkg/daemon/ipsec.go 0 1 0.0%
pkg/informer/kubevirt.go 0 1 0.0%
pkg/ovs/ovn-nb-load_balancer.go 2 3 66.67%
pkg/pinger/config.go 0 1 0.0%
pkg/tproxy/tproxy_tcp_linux.go 0 1 0.0%
pkg/webhook/static_ip.go 0 1 0.0%
Files with Coverage Reduction New Missed Lines %
pkg/controller/namespace.go 1 0.0%
pkg/ovs/ovn-nb-logical_router_route.go 2 74.93%
Totals Coverage Status
Change from base Build 14530610990: -0.02%
Covered Lines: 10247
Relevant Lines: 47175

💛 - Coveralls

@gemini-code-assist
Copy link
Copy Markdown
Contributor

Warning

Gemini encountered an error creating the review. You can try again by commenting /gemini review.

Signed-off-by: Mengxin Liu <liumengxinfly@gmail.com>
@oilbeater oilbeater merged commit 3177e93 into master Apr 19, 2025
136 of 140 checks passed
@oilbeater oilbeater deleted the chore/modernize branch April 19, 2025 08:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

refactor size:L This PR changes 100-499 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants