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

refactor: add receiver to iptables and create interface #2421

Merged
merged 13 commits into from
Dec 14, 2023
Merged

Conversation

QxBytes
Copy link
Contributor

@QxBytes QxBytes commented Dec 1, 2023

Reason for Change:

The iptables package is currently a set of functions without a receiver. Because of this, any code that uses the iptables package has a dependency on it, and proper unit tests cannot be written. This PR will add a receiver to the iptables methods and create an interface in the network folder so that in the future, unit tests can mock the iptables interface and methods that use iptables can be tested. This PR should not change the functionality of the codebase (for example, existing tests, if they use iptables, should still use a non-mocked version of the new iptables client).

Issue Fixed:

See above.

Requirements:

Notes:

  • A new iptables interface is created in network_utils.go and used in the network package.
  • Any creation of structs without a New function (ex: Client{ }) has been checked to ensure either an iptablesClient is passed in if the struct now requires an iptablesClient, or is part of testing code.
  • TransparentVlanEndpointClient, Client (from snat_linux), and OVSEndpointClient are the clients that now require an iptablesClient.
  • Functions that use iptables in networkutils linux now have receivers and the iptables client is passed in as the first parameter.

@QxBytes QxBytes added the cni Related to CNI. label Dec 1, 2023
network/networkutils/iptables.go Outdated Show resolved Hide resolved
@QxBytes QxBytes force-pushed the alew/iptables branch 2 times, most recently from 343ceee to f26f229 Compare December 5, 2023 17:42
@QxBytes QxBytes marked this pull request as ready for review December 5, 2023 18:24
@QxBytes QxBytes requested review from a team as code owners December 5, 2023 18:24
@QxBytes QxBytes requested a review from tamilmani1989 December 5, 2023 18:24
cni/network/invoker_cns.go Show resolved Hide resolved
network/endpoint_linux.go Show resolved Hide resolved
network/snat/snat_linux.go Outdated Show resolved Hide resolved
@@ -0,0 +1,9 @@
package network
Copy link
Member

Choose a reason for hiding this comment

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

this should be in iptable package and export interface type

Copy link
Contributor

Choose a reason for hiding this comment

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

No, that's not how we do interfaces in Go. The interface exists to abstract the behavior that your code is using from the backing implementation of that behavior at your usage of that behavior. This package only needs this subset of behavior, so the interface should be defined in this package.

Copy link
Contributor

Choose a reason for hiding this comment

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

https://go.dev/doc/effective_go#interfaces

"If something can do this, it can be used here."

Don't predefine an interface that simply matches a type.

Copy link
Member

Choose a reason for hiding this comment

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

i guess iptables package used in other components like cns i guess.. if its just used in network package, then i agree

network/iptables.go Show resolved Hide resolved
network/network_linux.go Show resolved Hide resolved
network/iptables.go Outdated Show resolved Hide resolved
rbtr
rbtr previously approved these changes Dec 8, 2023
Copy link
Contributor

@rbtr rbtr left a comment

Choose a reason for hiding this comment

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

some nits but mostly lgtm

Comment on lines +90 to +94
type Client struct{}

func NewClient() *Client {
return &Client{}
}
Copy link
Contributor

Choose a reason for hiding this comment

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

there are no fields (at all) being set in this struct, or other initialization magic - we don't need a constructor and can make the struct directly ... := &iptables.Client{}

network/networkutils/networkutils_linux.go Outdated Show resolved Hide resolved
Comment on lines +496 to +499
_, err := client.plClient.ExecuteCommand(enableIPForwardCmd)
if err != nil {
return errors.Wrap(err, "enable ipforwarding command failed")
}
Copy link
Contributor

Choose a reason for hiding this comment

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

hm, I wish we didn't have this in the codebase already... I generally don't think that it's our responsibility to configure the host like this.

vipul-21
vipul-21 previously approved these changes Dec 8, 2023
tamilmani1989
tamilmani1989 previously approved these changes Dec 12, 2023
Copy link
Member

@tamilmani1989 tamilmani1989 left a comment

Choose a reason for hiding this comment

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

talked with @QxBytes offline and clarified standard practice is to define interface in consumer side

@QxBytes QxBytes dismissed stale reviews from tamilmani1989, vipul-21, and rbtr via eaad01c December 12, 2023 23:31
@QxBytes QxBytes force-pushed the alew/iptables branch 2 times, most recently from e640d0a to 43b4726 Compare December 13, 2023 17:03
@QxBytes QxBytes enabled auto-merge (squash) December 14, 2023 16:58
@QxBytes QxBytes merged commit 0c8f78a into master Dec 14, 2023
78 checks passed
@QxBytes QxBytes deleted the alew/iptables branch December 14, 2023 17:27
paulyufan2 pushed a commit that referenced this pull request Dec 18, 2023
* Move network utils functions with iptables to new file

* Add receiver to iptables and create interface

* Resolve conflicts from rebasing

* Add changes for building on windows

* Address linter issues

* Address windows linter issues

* Invert if condition for linter nesting

* Scope iptables interfaces to package

* Rename iptables client to avoid stuttering

* Move EnableIPForwarding to snat linux

* Rename ipTablesClientInterface to ipTablesClient

* Address linter issues from moving enable ip forwarding function

* Rename after rebase
matmerr pushed a commit that referenced this pull request Jan 17, 2024
* Move network utils functions with iptables to new file

* Add receiver to iptables and create interface

* Resolve conflicts from rebasing

* Add changes for building on windows

* Address linter issues

* Address windows linter issues

* Invert if condition for linter nesting

* Scope iptables interfaces to package

* Rename iptables client to avoid stuttering

* Move EnableIPForwarding to snat linux

* Rename ipTablesClientInterface to ipTablesClient

* Address linter issues from moving enable ip forwarding function

* Rename after rebase
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cni Related to CNI.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants