Skip to content

Commit afc478a

Browse files
author
Jing Zhang
committed
Add TestNodeAddressesWithSubports() and change nodeAddresses() to go with getSubInterfaces()
Signed-off-by: Jing Zhang <[email protected]>
1 parent ad72a7a commit afc478a

File tree

2 files changed

+267
-42
lines changed

2 files changed

+267
-42
lines changed

pkg/openstack/instances.go

Lines changed: 24 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -596,7 +596,7 @@ func nodeAddresses(srv *servers.Server, interfaces []attachinterfaces.Interface,
596596
Address: fixedIP.IPAddress,
597597
},
598598
)
599-
if len(iface.NetID) > 0 {
599+
if iface.NetID != "" {
600600
addr := Address{IPType: "fixed", Addr: fixedIP.IPAddress}
601601
allPrivates[iface.NetID] = append(allPrivates[iface.NetID], addr)
602602
}
@@ -640,32 +640,18 @@ func nodeAddresses(srv *servers.Server, interfaces []attachinterfaces.Interface,
640640
return nil, err
641641
}
642642

643-
// add subports if exist to the server
644-
extraPrivates := make(map[string][]Address)
645-
for k, v := range allPrivates {
646-
ok := false
647-
for _, a := range v {
648-
for _, v1 := range addresses {
649-
for _, a1 := range v1 {
650-
if a.Addr == a1.Addr {
651-
ok = true
652-
break
653-
}
654-
}
643+
// Add subports as if they are directly attached
644+
klog.V(5).Infof("Node '%s' is directly attached to %d networks '%s'", srv.Name, len(addresses), addresses)
645+
klog.V(5).Infof("Node '%s' is attached to %d networks '%s'", srv.Name, len(allPrivates), allPrivates)
646+
if len(addresses) < len(allPrivates) {
647+
for k, v := range allPrivates {
648+
v1, ok := addresses[k]
649+
if !ok {
650+
addresses[k] = v
651+
} else {
652+
addresses[k] = append(v1, v...)
655653
}
656654
}
657-
if !ok {
658-
extraPrivates[k] = v
659-
}
660-
}
661-
klog.V(5).Infof("Node '%s' extraPrivates '%s'", srv.Name, extraPrivates)
662-
for k, v := range extraPrivates {
663-
v1, ok := addresses[k]
664-
if !ok {
665-
addresses[k] = v
666-
} else {
667-
addresses[k] = append(v1, v...)
668-
}
669655
}
670656

671657
var networks []string
@@ -739,6 +725,7 @@ func getAddressesByName(compute *gophercloud.ServiceClient, name types.NodeName,
739725
func getSubInterfaces(interfaces []attachinterfaces.Interface, network *gophercloud.ServiceClient) ([]attachinterfaces.Interface, error) {
740726
klog.V(5).Infof("Node has %d interfaces '%v'", len(interfaces), interfaces)
741727

728+
var subports []attachinterfaces.Interface
742729
for _, iface := range interfaces {
743730
// Check if trunk ports are attached
744731
listOpts := trunks.ListOpts{
@@ -747,37 +734,32 @@ func getSubInterfaces(interfaces []attachinterfaces.Interface, network *gophercl
747734
allPages, err := trunks.List(network, listOpts).AllPages()
748735
if err != nil {
749736
klog.Errorf("Failed to list trunks: %v", err)
750-
return interfaces, err
737+
return subports, nil
751738
}
752739
allTrunks, err := trunks.ExtractTrunks(allPages)
753740
if err != nil {
754741
klog.Errorf("Failed to extract trunks: %v", err)
755-
return interfaces, err
742+
return subports, err
756743
}
757744
// Get subports attached to the trunks
758745
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 {
746+
if len(trunk.Subports) == 0 {
765747
continue
766748
}
767-
klog.V(5).Infof("Subports for trunk %s: %v", trunk.ID, subports)
749+
klog.V(5).Infof("Subports for trunk %s: %v", trunk.ID, trunk.Subports)
768750
// Arrange subports as for directly attached ports
769-
for _, sport := range subports {
751+
for _, sport := range trunk.Subports {
770752
p, err := ports.Get(network, sport.PortID).Extract()
771753
if err != nil {
772754
klog.Errorf("Failed to get port info for subport %s: %v", sport.PortID, err)
773-
return interfaces, err
755+
return subports, err
774756
}
775757
n, err := networks.Get(network, p.NetworkID).Extract()
776758
if err != nil {
777759
klog.Errorf("Failed to get network info for subport %s: %v", sport.PortID, err)
778-
return interfaces, err
760+
return subports, err
779761
}
780-
var iface = attachinterfaces.Interface{
762+
var sface = attachinterfaces.Interface{
781763
PortState: "ACTIVE",
782764
FixedIPs: []attachinterfaces.FixedIP{},
783765
PortID: p.ID,
@@ -789,14 +771,14 @@ func getSubInterfaces(interfaces []attachinterfaces.Interface, network *gophercl
789771
SubnetID: ip.SubnetID,
790772
IPAddress: ip.IPAddress,
791773
}
792-
iface.FixedIPs = append(iface.FixedIPs, ip2)
774+
sface.FixedIPs = append(sface.FixedIPs, ip2)
793775
}
794-
interfaces = append(interfaces, iface)
776+
subports = append(subports, sface)
795777
}
796778
}
797779
}
798-
klog.V(5).Infof("Node has %d interfaces including subports '%v'", len(interfaces), interfaces)
799-
return interfaces, nil
780+
klog.V(5).Infof("Node has %d subports '%v'", len(subports), subports)
781+
return subports, nil
800782
}
801783

802784
// getAttachedInterfacesByID returns the node interfaces of the specified instance.

pkg/openstack/openstack_test.go

Lines changed: 243 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -878,6 +878,249 @@ func TestNodeAddressesWithAddressSortOrderOptions(t *testing.T) {
878878
}
879879
}
880880

881+
func TestNodeAddressesWithSubports(t *testing.T) {
882+
srv := servers.Server{
883+
Status: "ACTIVE",
884+
HostID: "06eacdcf714f7a2640b25a4a9a2f84a83c2a4e0d9913bb3581d8732a",
885+
Addresses: map[string]interface{}{
886+
"flat_net_phys5": []interface{}{
887+
map[string]interface{}{
888+
"OS-EXT-IPS-MAC:mac_addr": "fa:16:3e:b3:9f:c2",
889+
"version": float64(4),
890+
"addr": "13.13.13.106",
891+
"OS-EXT-IPS:type": "fixed",
892+
},
893+
map[string]interface{}{
894+
"OS-EXT-IPS-MAC:mac_addr": "fa:16:3e:b3:9f:c2",
895+
"version": float64(6),
896+
"addr": "3000:0:0:1::3a3",
897+
"OS-EXT-IPS:type": "fixed",
898+
},
899+
map[string]interface{}{
900+
"OS-EXT-IPS-MAC:mac_addr": "fa:16:3e:1d:b7:a7",
901+
"version": float64(4),
902+
"addr": "13.13.13.186",
903+
"OS-EXT-IPS:type": "fixed",
904+
},
905+
map[string]interface{}{
906+
"OS-EXT-IPS-MAC:mac_addr": "fa:16:3e:1d:b7:a7",
907+
"version": float64(6),
908+
"addr": "3000:0:0:1::38a",
909+
"OS-EXT-IPS:type": "fixed",
910+
},
911+
},
912+
"flat_net_phys6": []interface{}{
913+
map[string]interface{}{
914+
"OS-EXT-IPS-MAC:mac_addr": "fa:16:3e:15:20:50",
915+
"version": float64(4),
916+
"addr": "14.14.14.228",
917+
"OS-EXT-IPS:type": "fixed",
918+
},
919+
map[string]interface{}{
920+
"OS-EXT-IPS-MAC:mac_addr": "fa:16:3e:15:20:50",
921+
"version": float64(6),
922+
"addr": "3000:0:0:1::1a3",
923+
"OS-EXT-IPS:type": "fixed",
924+
},
925+
map[string]interface{}{
926+
"OS-EXT-IPS-MAC:mac_addr": "fa:16:3e:36:b7:8c",
927+
"version": float64(4),
928+
"addr": "14.14.14.98",
929+
"OS-EXT-IPS:type": "fixed",
930+
},
931+
map[string]interface{}{
932+
"OS-EXT-IPS-MAC:mac_addr": "fa:16:3e:36:b7:8c",
933+
"version": float64(6),
934+
"addr": "3000:0:0:1::39d",
935+
"OS-EXT-IPS:type": "fixed",
936+
},
937+
},
938+
},
939+
}
940+
941+
interfaces := []attachinterfaces.Interface{
942+
{
943+
PortState: "ACTIVE",
944+
FixedIPs: []attachinterfaces.FixedIP{
945+
{
946+
SubnetID: "491fba80-095e-4dee-bf4b-69046ba0b7fa",
947+
IPAddress: "13.13.13.106",
948+
},
949+
{
950+
SubnetID: "fb6ed503-7912-45cf-9bb6-15543296e960",
951+
IPAddress: "3000:0:0:1::3a3",
952+
},
953+
},
954+
PortID: "27c8b207-4ef1-4c3f-b27a-029ac5d62df1",
955+
NetID: "7da9e62c-2ec1-4784-91f1-a6b560d7b665",
956+
MACAddr: "fa:16:3e:b3:9f:c2",
957+
},
958+
{
959+
PortState: "ACTIVE",
960+
FixedIPs: []attachinterfaces.FixedIP{
961+
{
962+
SubnetID: "491fba80-095e-4dee-bf4b-69046ba0b7fa",
963+
IPAddress: "13.13.13.186",
964+
},
965+
{
966+
SubnetID: "fb6ed503-7912-45cf-9bb6-15543296e960",
967+
IPAddress: "3000:0:0:1::38a",
968+
},
969+
},
970+
PortID: "97a49a02-3703-4632-be5e-a947a1e44138",
971+
NetID: "7da9e62c-2ec1-4784-91f1-a6b560d7b665",
972+
MACAddr: "fa:16:3e:1d:b7:a7",
973+
},
974+
{
975+
PortState: "ACTIVE",
976+
FixedIPs: []attachinterfaces.FixedIP{
977+
{
978+
SubnetID: "b1c1f54e-f7cc-474f-be4c-f559ffb7fc94",
979+
IPAddress: "14.14.14.228",
980+
},
981+
{
982+
SubnetID: "3d3ab7f7-e235-4476-81a2-62982f9f9673",
983+
IPAddress: "3000:0:0:1::1a3",
984+
},
985+
},
986+
PortID: "6465554c-5ddc-4cd8-8704-e5ed4bde21ca",
987+
NetID: "59c2e3cc-26a2-44c8-b8dc-d10f3995bcd9",
988+
MACAddr: "fa:16:3e:15:20:50",
989+
},
990+
{
991+
PortState: "ACTIVE",
992+
FixedIPs: []attachinterfaces.FixedIP{
993+
{
994+
SubnetID: "b1c1f54e-f7cc-474f-be4c-f559ffb7fc94",
995+
IPAddress: "14.14.14.98",
996+
},
997+
{
998+
SubnetID: "3d3ab7f7-e235-4476-81a2-62982f9f9673",
999+
IPAddress: "3000:0:0:1::39d",
1000+
},
1001+
},
1002+
PortID: "a9145a03-1c58-442e-8ca5-cf85a65fdae4",
1003+
NetID: "59c2e3cc-26a2-44c8-b8dc-d10f3995bcd9",
1004+
MACAddr: "fa:16:3e:36:b7:8c",
1005+
},
1006+
{
1007+
PortState: "ACTIVE",
1008+
FixedIPs: []attachinterfaces.FixedIP{
1009+
{
1010+
SubnetID: "b1c1f54e-f7cc-474f-be4c-f559ffb7fc94",
1011+
IPAddress: "14.14.14.98",
1012+
},
1013+
{
1014+
SubnetID: "3d3ab7f7-e235-4476-81a2-62982f9f9673",
1015+
IPAddress: "3000:0:0:1::39d",
1016+
},
1017+
},
1018+
PortID: "a9145a03-1c58-442e-8ca5-cf85a65fdae4",
1019+
NetID: "59c2e3cc-26a2-44c8-b8dc-d10f3995bcd9",
1020+
MACAddr: "fa:16:3e:36:b7:8c",
1021+
},
1022+
{
1023+
PortState: "ACTIVE",
1024+
FixedIPs: []attachinterfaces.FixedIP{
1025+
{
1026+
SubnetID: "f729eeab-f3ef-4cbd-b90e-87030f617ecf",
1027+
IPAddress: "10.75.11.20",
1028+
},
1029+
},
1030+
PortID: "bf7934ab-9804-4453-b0b9-dd68010e5c56",
1031+
NetID: "ext-net1",
1032+
MACAddr: "fa:16:3e:ea:50:c9",
1033+
},
1034+
{
1035+
PortState: "ACTIVE",
1036+
FixedIPs: []attachinterfaces.FixedIP{
1037+
{
1038+
SubnetID: "c4a00f06-ffc7-4ef8-a135-10b033dbde59",
1039+
IPAddress: "10.75.11.20",
1040+
},
1041+
},
1042+
PortID: "37ee3b4c-b237-4648-86a5-3fbbf141c30b",
1043+
NetID: "ext-net2",
1044+
MACAddr: "fa:16:3e:ea:50:c9",
1045+
},
1046+
{
1047+
PortState: "ACTIVE",
1048+
FixedIPs: []attachinterfaces.FixedIP{
1049+
{
1050+
SubnetID: "bbece9ca-5f7a-4033-80d8-a98bf75220bf",
1051+
IPAddress: "172.16.1.241",
1052+
},
1053+
},
1054+
PortID: "3017537c-d94e-4fc6-beed-6cd6919680c1",
1055+
NetID: "mycluster-01-vlan701_network",
1056+
MACAddr: "fa:16:3e:10:f0:27",
1057+
},
1058+
{
1059+
PortState: "ACTIVE",
1060+
FixedIPs: []attachinterfaces.FixedIP{
1061+
{
1062+
SubnetID: "1236fcc3-f235-41bc-84f8-464329c1c4c5",
1063+
IPAddress: "172.16.1.241",
1064+
},
1065+
},
1066+
PortID: "79f2a2de-ce1b-4424-83d8-a904e732f222",
1067+
NetID: "mycluster-01-vlan701_network2",
1068+
MACAddr: "fa:16:3e:10:f0:27",
1069+
},
1070+
{
1071+
PortState: "ACTIVE",
1072+
FixedIPs: []attachinterfaces.FixedIP{
1073+
{
1074+
SubnetID: "a9f8028e-7a78-4ffe-ac26-f25887b77293",
1075+
IPAddress: "172.16.2.36",
1076+
},
1077+
{
1078+
SubnetID: "a278e619-4f31-4f54-a241-3814410f9247",
1079+
IPAddress: "3000:0:0:1::118",
1080+
},
1081+
},
1082+
PortID: "81936400-6240-4b81-b72b-cac0840a774d",
1083+
NetID: "mycluster-01-vlan702_network",
1084+
MACAddr: "fa:16:3e:c9:b0:7e",
1085+
},
1086+
{
1087+
PortState: "ACTIVE",
1088+
FixedIPs: []attachinterfaces.FixedIP{
1089+
{
1090+
SubnetID: "fe9826f5-dfb5-4a4c-8a57-3a2963068bb6",
1091+
IPAddress: "172.16.2.36",
1092+
},
1093+
{
1094+
SubnetID: "779c28ee-63cf-4dd8-9c87-b82bfae8e1fb",
1095+
IPAddress: "3000:0:0:1::118",
1096+
},
1097+
},
1098+
PortID: "87fac0f8-d46f-4744-940a-7d9526d3cdde",
1099+
NetID: "mycluster-01-vlan702_network2",
1100+
MACAddr: "fa:16:3e:c9:b0:7e",
1101+
},
1102+
}
1103+
1104+
networkingOpts := NetworkingOpts{
1105+
InternalNetworkName: []string{"mycluster-01-vlan701_network", "mycluster-01-vlan701_network2"},
1106+
}
1107+
1108+
addresses, err := nodeAddresses(&srv, interfaces, networkingOpts)
1109+
if err != nil {
1110+
t.Errorf("nodeAddresses returned error: %v", err)
1111+
}
1112+
1113+
t.Logf("addresses are %v", addresses)
1114+
1115+
want := []v1.NodeAddress{
1116+
{Type: v1.NodeInternalIP, Address: "172.16.1.241"},
1117+
}
1118+
1119+
if !reflect.DeepEqual(want, addresses) {
1120+
t.Errorf("nodeAddresses returned incorrect value, want %v", want)
1121+
}
1122+
}
1123+
8811124
func TestNewOpenStack(t *testing.T) {
8821125
cfg := ConfigFromEnv()
8831126
testConfigFromEnv(t, &cfg)

0 commit comments

Comments
 (0)