Skip to content

Commit 0c99864

Browse files
committed
consolidate logic and improve uts
1 parent 953464a commit 0c99864

File tree

2 files changed

+51
-36
lines changed

2 files changed

+51
-36
lines changed

network/transparent_vlan_endpointclient_linux.go

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,19 @@ func NewTransparentVlanEndpointClient(
110110
return client
111111
}
112112

113+
// cleanupInterfaceIfExists checks if an interface exists and deletes it if found
114+
// Returns an error if the deletion fails
115+
func (client *TransparentVlanEndpointClient) cleanupInterfaceIfExists(interfaceName string) error {
116+
_, ifExists := client.netioshim.GetNetworkInterfaceByName(interfaceName)
117+
if ifExists == nil {
118+
logger.Info("Interface exists, deleting", zap.String("interfaceName", interfaceName))
119+
if err := client.netlink.DeleteLink(interfaceName); err != nil {
120+
return errors.Wrapf(err, "failed to clean up %s", interfaceName)
121+
}
122+
}
123+
return nil
124+
}
125+
113126
// Adds interfaces to the vnet (created if not existing) and vm namespace
114127
func (client *TransparentVlanEndpointClient) AddEndpoints(epInfo *EndpointInfo) error {
115128
// VM Namespace
@@ -151,13 +164,9 @@ func (client *TransparentVlanEndpointClient) ensureCleanPopulateVM() error {
151164
}
152165
}
153166
// Delete the vlan interface in the VM namespace if it exists
154-
_, vlanIfInVMErr := client.netioshim.GetNetworkInterfaceByName(client.vlanIfName)
155-
if vlanIfInVMErr == nil {
156-
// The vlan interface exists in the VM ns because it failed to move into the network ns previously and needs to be cleaned up
157-
logger.Info("vlan interface exists on the VM namespace, deleting", zap.String("vlanIfName", client.vlanIfName))
158-
if delErr := client.netlink.DeleteLink(client.vlanIfName); delErr != nil {
159-
return errors.Wrap(delErr, "failed to clean up vlan interface")
160-
}
167+
// The vlan interface exists in the VM ns because it failed to move into the network ns previously and needs to be cleaned up
168+
if delErr := client.cleanupInterfaceIfExists(client.vlanIfName); delErr != nil {
169+
return errors.Wrap(delErr, "failed to clean up vlan interface")
161170
}
162171
return nil
163172
}
@@ -188,7 +197,7 @@ func (client *TransparentVlanEndpointClient) setLinkNetNSAndConfirm(name string,
188197
logger.Info("Move link to NS", zap.String("ifName", name), zap.Any("NSFileDescriptor", fd))
189198
err := client.netlink.SetLinkNetNs(name, fd)
190199
if err != nil {
191-
return errors.Wrapf(err, "failed to set %v inside namespace %v", name, fd)
200+
return errors.Wrapf(err, "failed to set %v inside namespace %v (%s)", name, fd, nsName)
192201
}
193202

194203
// confirm veth was moved successfully
@@ -200,7 +209,7 @@ func (client *TransparentVlanEndpointClient) setLinkNetNSAndConfirm(name string,
200209
})
201210
}, numRetries, sleepInMs)
202211
if err != nil {
203-
return errors.Wrapf(err, "failed to detect %v inside namespace %v", name, fd)
212+
return errors.Wrapf(err, "failed to detect %v inside namespace %v (%s)", name, fd, nsName)
204213
}
205214
return nil
206215
}
@@ -311,13 +320,11 @@ func (client *TransparentVlanEndpointClient) PopulateVM(epInfo *EndpointInfo) er
311320
}
312321

313322
// Proactively clean up any leftover veth interfaces before creating new ones
314-
if err = client.netlink.DeleteLink(client.vnetVethName); err != nil {
315-
logger.Info("Could not proactively clean up vnet veth",
316-
zap.String("vnetVethName", client.vnetVethName), zap.Error(err))
323+
if vnetDelErr := client.cleanupInterfaceIfExists(client.vnetVethName); vnetDelErr != nil {
324+
logger.Info("Could not proactively clean up vnet veth", zap.String("vnetVethName", client.vnetVethName), zap.Error(vnetDelErr))
317325
}
318-
if err = client.netlink.DeleteLink(client.containerVethName); err != nil {
319-
logger.Info("Could not proactively clean up container veth",
320-
zap.String("containerVethName", client.containerVethName), zap.Error(err))
326+
if containerDelErr := client.cleanupInterfaceIfExists(client.containerVethName); containerDelErr != nil {
327+
logger.Info("Could not proactively clean up container veth", zap.String("containerVethName", client.containerVethName), zap.Error(containerDelErr))
321328
}
322329

323330
// Create veth pair

network/transparent_vlan_endpointclient_linux_test.go

Lines changed: 29 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -121,12 +121,14 @@ func TestTransparentVlanAddEndpoints(t *testing.T) {
121121
nl := netlink.NewMockNetlink(false, "")
122122
plc := platform.NewMockExecClient(false)
123123

124-
tests := []struct {
125-
name string
126-
client *TransparentVlanEndpointClient
127-
epInfo *EndpointInfo
128-
wantErr bool
129-
wantErrMsg string
124+
setLinkNetNSTests := []struct {
125+
name string
126+
client *TransparentVlanEndpointClient
127+
epInfo *EndpointInfo
128+
moveInterface string
129+
moveNS string
130+
wantErr bool
131+
wantErrMsg string
130132
}{
131133
// Set the link network namespace and confirm that it was moved inside
132134
{
@@ -143,8 +145,10 @@ func TestTransparentVlanAddEndpoints(t *testing.T) {
143145
netioshim: netio.NewMockNetIO(false, 0),
144146
nsClient: NewMockNamespaceClient(),
145147
},
146-
epInfo: &EndpointInfo{},
147-
wantErr: false,
148+
moveInterface: "eth0.1",
149+
moveNS: "az_ns_1",
150+
epInfo: &EndpointInfo{},
151+
wantErr: false,
148152
},
149153
{
150154
name: "Set link netns fail to set",
@@ -160,9 +164,11 @@ func TestTransparentVlanAddEndpoints(t *testing.T) {
160164
netioshim: netio.NewMockNetIO(false, 0),
161165
nsClient: NewMockNamespaceClient(),
162166
},
163-
epInfo: &EndpointInfo{},
164-
wantErr: true,
165-
wantErrMsg: "failed to set eth0.1",
167+
moveInterface: "A1veth0",
168+
moveNS: "az_ns_2",
169+
epInfo: &EndpointInfo{},
170+
wantErr: true,
171+
wantErrMsg: "failed to set A1veth0 inside namespace 1 (az_ns_2)",
166172
},
167173
{
168174
name: "Set link netns fail to detect",
@@ -181,15 +187,17 @@ func TestTransparentVlanAddEndpoints(t *testing.T) {
181187
},
182188
nsClient: NewMockNamespaceClient(),
183189
},
184-
epInfo: &EndpointInfo{},
185-
wantErr: true,
186-
wantErrMsg: "failed to detect eth0.1",
190+
moveInterface: "eth0.1",
191+
moveNS: "az_ns_1",
192+
epInfo: &EndpointInfo{},
193+
wantErr: true,
194+
wantErrMsg: "failed to detect eth0.1 inside namespace 1 (az_ns_1)",
187195
},
188196
}
189-
for _, tt := range tests {
197+
for _, tt := range setLinkNetNSTests {
190198
tt := tt
191199
t.Run(tt.name, func(t *testing.T) {
192-
err := tt.client.setLinkNetNSAndConfirm(tt.client.vlanIfName, 1, tt.client.vnetNSName)
200+
err := tt.client.setLinkNetNSAndConfirm(tt.moveInterface, 1, tt.moveNS)
193201
if tt.wantErr {
194202
require.Error(t, err)
195203
require.Contains(t, err.Error(), tt.wantErrMsg, "Expected:%v actual:%v", tt.wantErrMsg, err.Error())
@@ -199,7 +207,7 @@ func TestTransparentVlanAddEndpoints(t *testing.T) {
199207
})
200208
}
201209

202-
tests = []struct {
210+
tests := []struct {
203211
name string
204212
client *TransparentVlanEndpointClient
205213
epInfo *EndpointInfo
@@ -256,7 +264,7 @@ func TestTransparentVlanAddEndpoints(t *testing.T) {
256264
},
257265
epInfo: &EndpointInfo{},
258266
wantErr: true,
259-
wantErrMsg: "failed to cleanup/delete ns after noticing vlan veth does not exist: netns failure: " + errNetnsMock.Error(),
267+
wantErrMsg: "failed to cleanup/delete ns after noticing vlan interface does not exist: netns failure: " + errNetnsMock.Error(),
260268
},
261269
{
262270
name: "Ensure clean populate VM cleanup straggling vlan if in vm ns",
@@ -380,7 +388,7 @@ func TestTransparentVlanAddEndpoints(t *testing.T) {
380388
},
381389
epInfo: &EndpointInfo{},
382390
wantErr: true,
383-
wantErrMsg: "failed to move or detect vnetVethName in vnet ns, deleting: failed to set A1veth0 inside namespace 1: " + netlink.ErrorMockNetlink.Error() + " : netlink fail",
391+
wantErrMsg: "failed to move or detect vnetVethName in vnet ns, deleting: failed to set A1veth0 inside namespace 1 (az_ns_1): " + netlink.ErrorMockNetlink.Error() + " : netlink fail",
384392
},
385393
{
386394
name: "Add endpoints get interface fail for primary interface (eth0)",
@@ -528,7 +536,7 @@ func TestTransparentVlanAddEndpoints(t *testing.T) {
528536
wantErr: false,
529537
},
530538
{
531-
name: "Add endpoints fail check vlan veth exists",
539+
name: "Add endpoints fail check vlan interface exists",
532540
client: &TransparentVlanEndpointClient{
533541
primaryHostIfName: "eth0",
534542
vlanIfName: "eth0.1",
@@ -542,7 +550,7 @@ func TestTransparentVlanAddEndpoints(t *testing.T) {
542550
},
543551
epInfo: &EndpointInfo{},
544552
wantErr: true,
545-
wantErrMsg: "vlan veth doesn't exist: " + netio.ErrMockNetIOFail.Error() + ":eth0.1",
553+
wantErrMsg: "vlan interface doesn't exist: " + netio.ErrMockNetIOFail.Error() + ":eth0.1",
546554
},
547555
{
548556
name: "Add endpoints fail check vnet veth exists",

0 commit comments

Comments
 (0)