Skip to content

Commit

Permalink
Rename iptables client to avoid stuttering
Browse files Browse the repository at this point in the history
  • Loading branch information
QxBytes committed Dec 5, 2023
1 parent 152b9ef commit 25ef4c7
Show file tree
Hide file tree
Showing 7 changed files with 18 additions and 18 deletions.
2 changes: 1 addition & 1 deletion cni/network/invoker_cns.go
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ func setHostOptions(ncSubnetPrefix *net.IPNet, options map[string]interface{}, i
// we need to snat IMDS traffic to node IP, this sets up snat '--to'
snatHostIPJump := fmt.Sprintf("%s --to %s", iptables.Snat, info.hostPrimaryIP)

iptablesClient := iptables.NewIPTablesClient()
iptablesClient := iptables.NewClient()
var iptableCmds []iptables.IPTableEntry
if !iptablesClient.ChainExists(iptables.V4, iptables.Nat, iptables.Swift) {
iptableCmds = append(iptableCmds, iptablesClient.GetCreateChainCmd(iptables.V4, iptables.Nat, iptables.Swift))
Expand Down
2 changes: 1 addition & 1 deletion cni/network/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ func NewPlugin(name string,

nl := netlink.NewNetlink()
// Setup network manager.
nm, err := network.NewNetworkManager(nl, platform.NewExecClient(logger), &netio.NetIO{}, network.NewNamespaceClient(), iptables.NewIPTablesClient())
nm, err := network.NewNetworkManager(nl, platform.NewExecClient(logger), &netio.NetIO{}, network.NewNamespaceClient(), iptables.NewClient())
if err != nil {
return nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion cnm/network/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func NewPlugin(config *common.PluginConfig) (NetPlugin, error) {

nl := netlink.NewNetlink()
// Setup network manager.
nm, err := network.NewNetworkManager(nl, platform.NewExecClient(nil), &netio.NetIO{}, network.NewNamespaceClient(), iptables.NewIPTablesClient())
nm, err := network.NewNetworkManager(nl, platform.NewExecClient(nil), &netio.NetIO{}, network.NewNamespaceClient(), iptables.NewClient())
if err != nil {
return nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion cnms/service/networkmonitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ func main() {
}

nl := netlink.NewNetlink()
nm, err := network.NewNetworkManager(nl, platform.NewExecClient(nil), &netio.NetIO{}, network.NewNamespaceClient(), iptables.NewIPTablesClient())
nm, err := network.NewNetworkManager(nl, platform.NewExecClient(nil), &netio.NetIO{}, network.NewNamespaceClient(), iptables.NewClient())
if err != nil {
log.Printf("[monitor] Failed while creating network manager")
return
Expand Down
2 changes: 1 addition & 1 deletion iptables/iptables.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ type IPTableEntry struct {

type Client struct{}

func NewIPTablesClient() *Client {
func NewClient() *Client {
return &Client{}
}

Expand Down
22 changes: 11 additions & 11 deletions network/endpoint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ var _ = Describe("Test Endpoint", func() {
It("Should be added", func() {
// Add endpoint with valid id
ep, err := nw.newEndpointImpl(nil, netlink.NewMockNetlink(false, ""), platform.NewMockExecClient(false),
netio.NewMockNetIO(false, 0), NewMockEndpointClient(nil), NewMockNamespaceClient(), iptables.NewIPTablesClient(), []*EndpointInfo{epInfo})
netio.NewMockNetIO(false, 0), NewMockEndpointClient(nil), NewMockNamespaceClient(), iptables.NewClient(), []*EndpointInfo{epInfo})
Expect(err).NotTo(HaveOccurred())
Expect(ep).NotTo(BeNil())
Expect(ep.Id).To(Equal(epInfo.Id))
Expand All @@ -196,7 +196,7 @@ var _ = Describe("Test Endpoint", func() {
extIf: &externalInterface{IPv4Gateway: net.ParseIP("192.168.0.1")},
}
ep, err := nw2.newEndpointImpl(nil, netlink.NewMockNetlink(false, ""), platform.NewMockExecClient(false),
netio.NewMockNetIO(false, 0), NewMockEndpointClient(nil), NewMockNamespaceClient(), iptables.NewIPTablesClient(), []*EndpointInfo{epInfo})
netio.NewMockNetIO(false, 0), NewMockEndpointClient(nil), NewMockNamespaceClient(), iptables.NewClient(), []*EndpointInfo{epInfo})
Expect(err).NotTo(HaveOccurred())
Expect(ep).NotTo(BeNil())
Expect(ep.Id).To(Equal(epInfo.Id))
Expand All @@ -212,7 +212,7 @@ var _ = Describe("Test Endpoint", func() {
Expect(err).ToNot(HaveOccurred())
// Adding endpoint with same id should fail and delete should cleanup the state
ep2, err := nw.newEndpointImpl(nil, netlink.NewMockNetlink(false, ""), platform.NewMockExecClient(false),
netio.NewMockNetIO(false, 0), mockCli, NewMockNamespaceClient(), iptables.NewIPTablesClient(), []*EndpointInfo{epInfo})
netio.NewMockNetIO(false, 0), mockCli, NewMockNamespaceClient(), iptables.NewClient(), []*EndpointInfo{epInfo})
Expect(err).To(HaveOccurred())
Expect(ep2).To(BeNil())
assert.Contains(GinkgoT(), err.Error(), "Endpoint already exists")
Expand All @@ -222,17 +222,17 @@ var _ = Describe("Test Endpoint", func() {
// Adding an endpoint with an id.
mockCli := NewMockEndpointClient(nil)
ep2, err := nw.newEndpointImpl(nil, netlink.NewMockNetlink(false, ""), platform.NewMockExecClient(false),
netio.NewMockNetIO(false, 0), mockCli, NewMockNamespaceClient(), iptables.NewIPTablesClient(), []*EndpointInfo{epInfo})
netio.NewMockNetIO(false, 0), mockCli, NewMockNamespaceClient(), iptables.NewClient(), []*EndpointInfo{epInfo})
Expect(err).ToNot(HaveOccurred())
Expect(ep2).ToNot(BeNil())
Expect(len(mockCli.endpoints)).To(Equal(1))
// Deleting the endpoint
//nolint:errcheck // ignore error
nw.deleteEndpointImpl(netlink.NewMockNetlink(false, ""), platform.NewMockExecClient(false), mockCli, netio.NewMockNetIO(false, 0), NewMockNamespaceClient(), iptables.NewIPTablesClient(), ep2)
nw.deleteEndpointImpl(netlink.NewMockNetlink(false, ""), platform.NewMockExecClient(false), mockCli, netio.NewMockNetIO(false, 0), NewMockNamespaceClient(), iptables.NewClient(), ep2)
Expect(len(mockCli.endpoints)).To(Equal(0))
// Deleting same endpoint with same id should not fail
//nolint:errcheck // ignore error
nw.deleteEndpointImpl(netlink.NewMockNetlink(false, ""), platform.NewMockExecClient(false), mockCli, netio.NewMockNetIO(false, 0), NewMockNamespaceClient(), iptables.NewIPTablesClient(), ep2)
nw.deleteEndpointImpl(netlink.NewMockNetlink(false, ""), platform.NewMockExecClient(false), mockCli, netio.NewMockNetIO(false, 0), NewMockNamespaceClient(), iptables.NewClient(), ep2)
Expect(len(mockCli.endpoints)).To(Equal(0))
})
})
Expand All @@ -253,11 +253,11 @@ var _ = Describe("Test Endpoint", func() {
}

return nil
}), NewMockNamespaceClient(), iptables.NewIPTablesClient(), []*EndpointInfo{epInfo})
}), NewMockNamespaceClient(), iptables.NewClient(), []*EndpointInfo{epInfo})
Expect(err).To(HaveOccurred())
Expect(ep).To(BeNil())
ep, err = nw.newEndpointImpl(nil, netlink.NewMockNetlink(false, ""), platform.NewMockExecClient(false),
netio.NewMockNetIO(false, 0), NewMockEndpointClient(nil), NewMockNamespaceClient(), iptables.NewIPTablesClient(), []*EndpointInfo{epInfo})
netio.NewMockNetIO(false, 0), NewMockEndpointClient(nil), NewMockNamespaceClient(), iptables.NewClient(), []*EndpointInfo{epInfo})
Expect(err).NotTo(HaveOccurred())
Expect(ep).NotTo(BeNil())
Expect(ep.Id).To(Equal(epInfo.Id))
Expand All @@ -283,22 +283,22 @@ var _ = Describe("Test Endpoint", func() {
It("Should not endpoint to the network when there is an error", func() {
secondaryEpInfo.MacAddress = netio.BadHwAddr // mock netlink will fail to set link state on bad eth
ep, err := nw.newEndpointImpl(nil, netlink.NewMockNetlink(false, ""), platform.NewMockExecClient(false),
netio.NewMockNetIO(false, 0), nil, NewMockNamespaceClient(), iptables.NewIPTablesClient(), []*EndpointInfo{epInfo, secondaryEpInfo})
netio.NewMockNetIO(false, 0), nil, NewMockNamespaceClient(), iptables.NewClient(), []*EndpointInfo{epInfo, secondaryEpInfo})
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(Equal("SecondaryEndpointClient Error: " + netlink.ErrorMockNetlink.Error()))
Expect(ep).To(BeNil())

secondaryEpInfo.MacAddress = netio.HwAddr
ep, err = nw.newEndpointImpl(nil, netlink.NewMockNetlink(false, ""), platform.NewMockExecClient(false),
netio.NewMockNetIO(false, 0), nil, NewMockNamespaceClient(), iptables.NewIPTablesClient(), []*EndpointInfo{epInfo, secondaryEpInfo})
netio.NewMockNetIO(false, 0), nil, NewMockNamespaceClient(), iptables.NewClient(), []*EndpointInfo{epInfo, secondaryEpInfo})
Expect(err).ToNot(HaveOccurred())
Expect(ep.Id).To(Equal(epInfo.Id))
})

It("Should add endpoint when there are no errors", func() {
secondaryEpInfo.MacAddress = netio.HwAddr
ep, err := nw.newEndpointImpl(nil, netlink.NewMockNetlink(false, ""), platform.NewMockExecClient(false),
netio.NewMockNetIO(false, 0), nil, NewMockNamespaceClient(), iptables.NewIPTablesClient(), []*EndpointInfo{epInfo, secondaryEpInfo})
netio.NewMockNetIO(false, 0), nil, NewMockNamespaceClient(), iptables.NewClient(), []*EndpointInfo{epInfo, secondaryEpInfo})
Expect(err).ToNot(HaveOccurred())
Expect(ep.Id).To(Equal(epInfo.Id))
})
Expand Down
4 changes: 2 additions & 2 deletions network/snat/snat_linux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func TestMain(m *testing.M) {

func TestAllowInboundFromHostToNC(t *testing.T) {
nl := netlink.NewNetlink()
iptc := iptables.NewIPTablesClient()
iptc := iptables.NewClient()
client := &Client{
SnatBridgeIP: "169.254.0.1/16",
localIP: "169.254.0.4/16",
Expand Down Expand Up @@ -69,7 +69,7 @@ func TestAllowInboundFromHostToNC(t *testing.T) {

func TestAllowInboundFromNCToHost(t *testing.T) {
nl := netlink.NewNetlink()
iptc := iptables.NewIPTablesClient()
iptc := iptables.NewClient()
client := &Client{
SnatBridgeIP: "169.254.0.1/16",
localIP: "169.254.0.4/16",
Expand Down

0 comments on commit 25ef4c7

Please sign in to comment.