Skip to content

Commit ad72a7a

Browse files
author
Jing Zhang
committed
Re-arrange getSubInterface() per review comments:
Check for trunks attached to VM interfaces only Collapse two loop (trunks and subports) into one loop Signed-off-by: Jing Zhang <[email protected]>
1 parent 6ba85bd commit ad72a7a

File tree

1 file changed

+51
-52
lines changed

1 file changed

+51
-52
lines changed

pkg/openstack/instances.go

Lines changed: 51 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -735,68 +735,67 @@ func getAddressesByName(compute *gophercloud.ServiceClient, name types.NodeName,
735735
return nodeAddresses(&srv.Server, interfaces, networkingOpts)
736736
}
737737

738-
// getSubInterfaces
739-
func getSubInterfaces(network *gophercloud.ServiceClient) ([]attachinterfaces.Interface, error) {
740-
var interfaces []attachinterfaces.Interface
741-
742-
// Check if trunk ports are attached
743-
listOpts := trunks.ListOpts{}
744-
allPages, err := trunks.List(network, listOpts).AllPages()
745-
if err != nil {
746-
klog.Errorf("Failed to list trunks: %v", err)
747-
return interfaces, err
748-
}
749-
allTrunks, err := trunks.ExtractTrunks(allPages)
750-
if err != nil {
751-
klog.Errorf("Failed to extract trunks: %v", err)
752-
return interfaces, err
753-
}
738+
// getSubInterfaces returns the sub-interfaces attached via trunks to the specified node interfaces
739+
func getSubInterfaces(interfaces []attachinterfaces.Interface, network *gophercloud.ServiceClient) ([]attachinterfaces.Interface, error) {
740+
klog.V(5).Infof("Node has %d interfaces '%v'", len(interfaces), interfaces)
754741

755-
// Get subports attached to the trunk
756-
var subports []trunks.Subport
757-
for _, trunk := range allTrunks {
758-
for _, iface := range interfaces {
759-
if iface.PortID == trunk.PortID {
760-
s, err := trunks.GetSubports(network, trunk.ID).Extract()
761-
if err != nil {
762-
klog.Errorf("Failed to get subports for trunk %s: %v", trunk.ID, err)
763-
return interfaces, err
764-
}
765-
subports = append(subports, s...)
766-
}
742+
for _, iface := range interfaces {
743+
// Check if trunk ports are attached
744+
listOpts := trunks.ListOpts{
745+
PortID: iface.PortID,
767746
}
768-
}
769-
klog.V(5).Infof("subports %v", subports)
770-
771-
// Arrange subports as for directly attached ports
772-
for _, sport := range subports {
773-
p, err := ports.Get(network, sport.PortID).Extract()
747+
allPages, err := trunks.List(network, listOpts).AllPages()
774748
if err != nil {
775-
klog.Errorf("Failed to get port info for subport %s: %v", sport.PortID, err)
749+
klog.Errorf("Failed to list trunks: %v", err)
776750
return interfaces, err
777751
}
778-
n, err := networks.Get(network, p.NetworkID).Extract()
752+
allTrunks, err := trunks.ExtractTrunks(allPages)
779753
if err != nil {
780-
klog.Errorf("Failed to get network info for subport %s: %v", sport.PortID, err)
754+
klog.Errorf("Failed to extract trunks: %v", err)
781755
return interfaces, err
782756
}
783-
var iface = attachinterfaces.Interface{
784-
PortState: "ACTIVE",
785-
FixedIPs: []attachinterfaces.FixedIP{},
786-
PortID: p.ID,
787-
NetID: n.Name,
788-
MACAddr: p.MACAddress,
789-
}
790-
for _, ip := range p.FixedIPs {
791-
var ip2 = attachinterfaces.FixedIP{
792-
SubnetID: ip.SubnetID,
793-
IPAddress: ip.IPAddress,
757+
// Get subports attached to the trunks
758+
for _, trunk := range allTrunks {
759+
subports, err := trunks.GetSubports(network, trunk.ID).Extract()
760+
if err != nil {
761+
klog.Errorf("Failed to get subports for trunk %s: %v", trunk.ID, err)
762+
return interfaces, err
763+
}
764+
if len(subports) == 0 {
765+
continue
766+
}
767+
klog.V(5).Infof("Subports for trunk %s: %v", trunk.ID, subports)
768+
// Arrange subports as for directly attached ports
769+
for _, sport := range subports {
770+
p, err := ports.Get(network, sport.PortID).Extract()
771+
if err != nil {
772+
klog.Errorf("Failed to get port info for subport %s: %v", sport.PortID, err)
773+
return interfaces, err
774+
}
775+
n, err := networks.Get(network, p.NetworkID).Extract()
776+
if err != nil {
777+
klog.Errorf("Failed to get network info for subport %s: %v", sport.PortID, err)
778+
return interfaces, err
779+
}
780+
var iface = attachinterfaces.Interface{
781+
PortState: "ACTIVE",
782+
FixedIPs: []attachinterfaces.FixedIP{},
783+
PortID: p.ID,
784+
NetID: n.Name,
785+
MACAddr: p.MACAddress,
786+
}
787+
for _, ip := range p.FixedIPs {
788+
var ip2 = attachinterfaces.FixedIP{
789+
SubnetID: ip.SubnetID,
790+
IPAddress: ip.IPAddress,
791+
}
792+
iface.FixedIPs = append(iface.FixedIPs, ip2)
793+
}
794+
interfaces = append(interfaces, iface)
794795
}
795-
iface.FixedIPs = append(iface.FixedIPs, ip2)
796796
}
797-
interfaces = append(interfaces, iface)
798797
}
799-
klog.V(5).Infof("interfaces %v", interfaces)
798+
klog.V(5).Infof("Node has %d interfaces including subports '%v'", len(interfaces), interfaces)
800799
return interfaces, nil
801800
}
802801

@@ -817,7 +816,7 @@ func getAttachedInterfacesByID(compute *gophercloud.ServiceClient, serviceID str
817816
if mc.ObserveRequest(err) != nil {
818817
return interfaces, err
819818
}
820-
subInterfaces, err := getSubInterfaces(network)
819+
subInterfaces, err := getSubInterfaces(interfaces, network)
821820
if err != nil {
822821
return interfaces, err
823822
}

0 commit comments

Comments
 (0)