Skip to content

Commit e88afe9

Browse files
author
Jing Zhang
committed
Better look for nodeAddresses() and getAttachedInterfacesByID()
Signed-off-by: Jing Zhang <[email protected]>
1 parent 9a7db2c commit e88afe9

File tree

4 files changed

+63
-55
lines changed

4 files changed

+63
-55
lines changed

pkg/openstack/instances.go

Lines changed: 53 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ func (i *Instances) AddSSHKeyToAllInstances(ctx context.Context, user string, ke
204204
func (i *Instances) NodeAddresses(ctx context.Context, name types.NodeName) ([]v1.NodeAddress, error) {
205205
klog.V(4).Infof("NodeAddresses(%v) called", name)
206206

207-
addrs, err := getAddressesByName(i.compute, name, i.networkingOpts, i.network)
207+
addrs, err := getAddressesByName(i.compute, i.network, name, i.networkingOpts)
208208
if err != nil {
209209
return nil, err
210210
}
@@ -237,7 +237,7 @@ func (i *Instances) NodeAddressesByProviderID(ctx context.Context, providerID st
237237
return []v1.NodeAddress{}, err
238238
}
239239

240-
interfaces, err := getAttachedInterfacesByID(i.compute, server.ID, i.network)
240+
interfaces, err := getAttachedInterfacesByID(i.compute, i.network, server.ID)
241241
if err != nil {
242242
return []v1.NodeAddress{}, err
243243
}
@@ -343,7 +343,7 @@ func (i *Instances) InstanceMetadata(ctx context.Context, node *v1.Node) (*cloud
343343
return nil, err
344344
}
345345

346-
interfaces, err := getAttachedInterfacesByID(i.compute, srv.ID, i.network)
346+
interfaces, err := getAttachedInterfacesByID(i.compute, i.network, srv.ID)
347347
if err != nil {
348348
return nil, err
349349
}
@@ -644,33 +644,33 @@ func nodeAddresses(srv *servers.Server, interfaces []attachinterfaces.Interface,
644644
if len(addresses) < len(allPrivates) {
645645
extraPrivates := make(map[string][]Address)
646646
// For each private network
647-
for k, v := range allPrivates {
648-
ok := false
647+
for network, interfaceAddresses := range allPrivates {
648+
found := false
649649
// For each address in the private network
650-
for _, a := range v {
651-
// Check if the address is directly connected
652-
for _, v1 := range addresses {
653-
for _, a1 := range v1 {
654-
if a.Addr == a1.Addr {
655-
ok = true
650+
for _, interfaceAddress := range interfaceAddresses {
651+
// Check if the address is directly assigned
652+
for _, srvAddresses := range addresses {
653+
for _, srvAddress := range srvAddresses {
654+
if interfaceAddress.Addr == srvAddress.Addr {
655+
found = true
656656
break
657657
}
658658
}
659659
}
660660
}
661-
// All the addresses in the private network are not directly connected
661+
// All the addresses in the private network are not directly assigned
662662
// Save the private network
663-
if !ok {
664-
extraPrivates[k] = v
663+
if !found {
664+
extraPrivates[network] = interfaceAddresses
665665
}
666666
}
667-
klog.V(5).Infof("Node '%s' extraPrivates '%s'", srv.Name, extraPrivates)
668-
for k, v := range extraPrivates {
669-
v1, ok := addresses[k]
667+
klog.V(5).Infof("Node '%s' extra private interfaces '%s'", srv.Name, extraPrivates)
668+
for network, interfaceAddresses := range extraPrivates {
669+
srvAddresses, ok := addresses[network]
670670
if !ok {
671-
addresses[k] = v
671+
addresses[network] = interfaceAddresses
672672
} else {
673-
addresses[k] = append(v1, v...)
673+
addresses[network] = append(srvAddresses, interfaceAddresses...)
674674
}
675675
}
676676
}
@@ -728,13 +728,13 @@ func nodeAddresses(srv *servers.Server, interfaces []attachinterfaces.Interface,
728728
return addrs, nil
729729
}
730730

731-
func getAddressesByName(compute *gophercloud.ServiceClient, name types.NodeName, networkingOpts NetworkingOpts, network *gophercloud.ServiceClient) ([]v1.NodeAddress, error) {
731+
func getAddressesByName(compute *gophercloud.ServiceClient, network *gophercloud.ServiceClient, name types.NodeName, networkingOpts NetworkingOpts) ([]v1.NodeAddress, error) {
732732
srv, err := getServerByName(compute, name)
733733
if err != nil {
734734
return nil, err
735735
}
736736

737-
interfaces, err := getAttachedInterfacesByID(compute, srv.ID, network)
737+
interfaces, err := getAttachedInterfacesByID(compute, network, srv.ID)
738738
if err != nil {
739739
return nil, err
740740
}
@@ -754,56 +754,56 @@ func getSubInterfaces(interfaces []attachinterfaces.Interface, network *gophercl
754754
}
755755
allPages, err := trunks.List(network, listOpts).AllPages()
756756
if err != nil {
757-
klog.Errorf("Failed to list trunks: %v", err)
757+
klog.Infof("Failed to list trunks: %v", err)
758758
return subports, nil
759759
}
760760
allTrunks, err := trunks.ExtractTrunks(allPages)
761761
if err != nil {
762762
klog.Errorf("Failed to extract trunks: %v", err)
763763
return subports, err
764764
}
765+
if len(allTrunks) > 1 {
766+
klog.Errorf("It is not expected to see more than one trunk on a single port %s", iface.PortID)
767+
return subports, err
768+
}
765769
// Get subports attached to the trunks
766-
for _, trunk := range allTrunks {
767-
if len(trunk.Subports) == 0 {
768-
continue
770+
trunk := allTrunks[0]
771+
klog.V(5).Infof("Subports for trunk %s: %v", trunk.ID, trunk.Subports)
772+
// Arrange subports as for directly attached ports
773+
for _, sport := range trunk.Subports {
774+
p, err := ports.Get(network, sport.PortID).Extract()
775+
if err != nil {
776+
klog.Errorf("Failed to get port info for subport %s: %v", sport.PortID, err)
777+
return subports, err
769778
}
770-
klog.V(5).Infof("Subports for trunk %s: %v", trunk.ID, trunk.Subports)
771-
// Arrange subports as for directly attached ports
772-
for _, sport := range trunk.Subports {
773-
p, err := ports.Get(network, sport.PortID).Extract()
774-
if err != nil {
775-
klog.Errorf("Failed to get port info for subport %s: %v", sport.PortID, err)
776-
return subports, err
777-
}
778-
n, err := networks.Get(network, p.NetworkID).Extract()
779-
if err != nil {
780-
klog.Errorf("Failed to get network info for subport %s: %v", sport.PortID, err)
781-
return subports, err
782-
}
783-
var sface = 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,
794-
}
795-
sface.FixedIPs = append(sface.FixedIPs, ip2)
779+
n, err := networks.Get(network, p.NetworkID).Extract()
780+
if err != nil {
781+
klog.Errorf("Failed to get network info for subport %s: %v", sport.PortID, err)
782+
return subports, err
783+
}
784+
var sface = attachinterfaces.Interface{
785+
PortState: "ACTIVE",
786+
FixedIPs: []attachinterfaces.FixedIP{},
787+
PortID: p.ID,
788+
NetID: n.Name,
789+
MACAddr: p.MACAddress,
790+
}
791+
for _, ip := range p.FixedIPs {
792+
var ip2 = attachinterfaces.FixedIP{
793+
SubnetID: ip.SubnetID,
794+
IPAddress: ip.IPAddress,
796795
}
797-
subports = append(subports, sface)
796+
sface.FixedIPs = append(sface.FixedIPs, ip2)
798797
}
798+
subports = append(subports, sface)
799799
}
800800
}
801801
klog.V(5).Infof("Node has %d sub-interfaces '%v'", len(subports), subports)
802802
return subports, nil
803803
}
804804

805805
// getAttachedInterfacesByID returns the node interfaces of the specified instance.
806-
func getAttachedInterfacesByID(compute *gophercloud.ServiceClient, serviceID string, network *gophercloud.ServiceClient) ([]attachinterfaces.Interface, error) {
806+
func getAttachedInterfacesByID(compute *gophercloud.ServiceClient, network *gophercloud.ServiceClient, serviceID string) ([]attachinterfaces.Interface, error) {
807807
var interfaces []attachinterfaces.Interface
808808

809809
mc := metrics.NewMetricContext("server_os_interface", "list")

pkg/openstack/instancesv2.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ func (i *InstancesV2) InstanceMetadata(ctx context.Context, node *v1.Node) (*clo
123123
return nil, err
124124
}
125125

126-
interfaces, err := getAttachedInterfacesByID(i.compute, server.ID, i.network)
126+
interfaces, err := getAttachedInterfacesByID(i.compute, i.network, server.ID)
127127
if err != nil {
128128
return nil, err
129129
}

pkg/openstack/loadbalancer.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -718,7 +718,7 @@ func getSubnetIDForLB(compute *gophercloud.ServiceClient, node corev1.Node, pref
718718
instanceID = instanceID[(ind + 1):]
719719
}
720720

721-
interfaces, err := getAttachedInterfacesByID(compute, instanceID, network)
721+
interfaces, err := getAttachedInterfacesByID(compute, network, instanceID)
722722
if err != nil {
723723
return "", err
724724
}

pkg/openstack/openstack_test.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -935,6 +935,14 @@ func TestNodeAddressesWithSubports(t *testing.T) {
935935
"OS-EXT-IPS:type": "fixed",
936936
},
937937
},
938+
"mycluster-01-vlan703_network": []interface{}{
939+
map[string]interface{}{
940+
"OS-EXT-IPS-MAC:mac_addr": "fa:16:3e:36:b7:8f",
941+
"version": float64(4),
942+
"addr": "169.252.60.15",
943+
"OS-EXT-IPS:type": "fixed",
944+
},
945+
},
938946
},
939947
}
940948

0 commit comments

Comments
 (0)