Skip to content

Commit f748284

Browse files
ibrokethecloudbk201
authored andcommitted
The PR improves nic filtering when iscsi sessions are detected.
With this change, in case a vlan interface is used for an iscsi session, then the parent interface is filtered from the list of interfaces and is no longer visible in the list of interfaces If there is no iscsi session then the behaviour of the installer remains unchanged.
1 parent bf37545 commit f748284

File tree

1 file changed

+54
-20
lines changed

1 file changed

+54
-20
lines changed

pkg/console/network.go

+54-20
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ func upAllLinks() error {
127127
}
128128

129129
func getNICs() ([]netlink.Link, error) {
130-
var nics []netlink.Link
130+
var nics, vlanNics []netlink.Link
131131

132132
links, err := netlink.LinkList()
133133
if err != nil {
@@ -138,9 +138,24 @@ func getNICs() ([]netlink.Link, error) {
138138
if l.Type() == "device" && l.Attrs().EncapType != "loopback" {
139139
nics = append(nics, l)
140140
}
141+
if l.Type() == "vlan" {
142+
vlanNics = append(vlanNics, l)
143+
}
144+
}
145+
146+
iscsi := goiscsi.NewLinuxISCSI(nil)
147+
sessions, err := iscsi.GetSessions()
148+
if err != nil {
149+
return nil, fmt.Errorf("error querying iscsi sessions: %v", err)
141150
}
142151

143-
return filterISCSIInterfaces(nics)
152+
// no iscsi sessions detected so no additional filtering based on usage for iscsi device
153+
// access is needed and we can break here
154+
if len(sessions) == 0 {
155+
return nics, nil
156+
}
157+
158+
return filterISCSIInterfaces(nics, vlanNics, sessions)
144159
}
145160

146161
func getNICState(name string) int {
@@ -196,44 +211,63 @@ func getManagementInterfaceName(mgmtInterface config.Network) string {
196211

197212
// filterISCSIInterfaces will query the host to identify iscsi sessions, and skip interfaces
198213
// used by the existing iscsi session.
199-
func filterISCSIInterfaces(links []netlink.Link) ([]netlink.Link, error) {
200-
iscsi := goiscsi.NewLinuxISCSI(nil)
201-
sessions, err := iscsi.GetSessions()
202-
if err != nil {
203-
return nil, fmt.Errorf("error querying iscsi sessions: %v", err)
214+
func filterISCSIInterfaces(nics, vlanNics []netlink.Link, sessions []goiscsi.ISCSISession) ([]netlink.Link, error) {
215+
hwDeviceMap := make(map[string]netlink.Link)
216+
for _, v := range nics {
217+
hwDeviceMap[v.Attrs().HardwareAddr.String()] = v
204218
}
205219

206-
var returnLinks []netlink.Link
220+
// temporary sessionMap to make it easy to correlate interface addressses with session ip address
221+
// should speed up identification of interfaces in use with iscsi sessions
222+
sessionMap := make(map[string]string)
223+
for _, session := range sessions {
224+
sessionMap[session.IfaceIPaddress] = ""
225+
}
226+
227+
if err := filterNICSBySession(hwDeviceMap, nics, sessionMap); err != nil {
228+
return nil, err
229+
}
230+
231+
if err := filterNICSBySession(hwDeviceMap, vlanNics, sessionMap); err != nil {
232+
return nil, err
233+
}
234+
logrus.Debugf("identified following iscsi sessions: %v", sessionMap)
235+
// we need to filter the filteredNics to also isolate parent nics if a vlan if is in use
236+
returnedNics := make([]netlink.Link, 0, len(hwDeviceMap))
237+
for _, v := range hwDeviceMap {
238+
returnedNics = append(returnedNics, v)
239+
}
240+
return returnedNics, nil
241+
}
242+
243+
func filterNICSBySession(hwDeviceMap map[string]netlink.Link, links []netlink.Link, sessionMap map[string]string) error {
207244
for _, link := range links {
208-
var inuse bool
245+
logrus.Debugf("checking if link %s is in use", link.Attrs().Name)
209246
if getNICState(link.Attrs().Name) == NICStateUP {
210247
iface, err := net.InterfaceByName(link.Attrs().Name)
211248
if err != nil {
212-
return nil, fmt.Errorf("error fetching interface details: %v", err)
249+
return fmt.Errorf("error fetching interface details: %v", err)
213250
}
214251

215252
addresses, err := iface.Addrs()
216253
if err != nil {
217-
return nil, fmt.Errorf("error fetching addresses from interface: %v", err)
254+
return fmt.Errorf("error fetching addresses from interface: %v", err)
218255
}
219256

220257
for _, address := range addresses {
221258
// interface addresses are in cidr format, and need to be converted before comparison
222259
// since iscsi session contains just the ip address
223260
ipAddress, _, err := net.ParseCIDR(address.String())
224261
if err != nil {
225-
return nil, fmt.Errorf("error parsing ip address: %v", err)
262+
return fmt.Errorf("error parsing ip address: %v", err)
226263
}
227-
for _, session := range sessions {
228-
if session.IfaceIPaddress == ipAddress.String() {
229-
inuse = true
230-
}
264+
if _, ok := sessionMap[ipAddress.String()]; ok {
265+
logrus.Debugf("filtering interface %s", link.Attrs().Name)
266+
delete(hwDeviceMap, link.Attrs().HardwareAddr.String())
267+
break //device is already removed, no point checking for other addresses
231268
}
232269
}
233270
}
234-
if !inuse {
235-
returnLinks = append(returnLinks, link)
236-
}
237271
}
238-
return returnLinks, nil
272+
return nil
239273
}

0 commit comments

Comments
 (0)