Skip to content

Commit 91e9026

Browse files
author
Jing Zhang
committed
Better code for review
1 parent b2b4d2d commit 91e9026

File tree

1 file changed

+36
-39
lines changed

1 file changed

+36
-39
lines changed

pkg/openstack/instances.go

Lines changed: 36 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,13 @@ package openstack
1919
import (
2020
"bytes"
2121
"context"
22+
"encoding/json"
2223
"fmt"
2324
"net"
2425
sysos "os"
2526
"regexp"
2627
"sort"
2728
"strings"
28-
"unsafe"
2929

3030
"github.com/gophercloud/gophercloud"
3131
"github.com/gophercloud/gophercloud/openstack/compute/v2/flavors"
@@ -638,53 +638,50 @@ func nodeAddresses(srv *servers.Server, ports []neutronports.Port, client *gophe
638638

639639
// Add the addresses assigned on subports via trunk
640640
// This exposes the vlan networks to which subports are attached
641+
subportAddresses := make(map[string][]Address)
642+
641643
type portWithTrunkDetails struct {
642644
neutronports.Port
643645
trunk_details.TrunkDetailsExt
644646
}
647+
var portDetails []portWithTrunkDetails
645648

646-
var subports []neutronports.Port
647-
for _, port := range ports {
648-
portExt := *((*portWithTrunkDetails)(unsafe.Pointer(&port)))
649-
continue
650-
// under testing
651-
klog.V(5).Infof("Node '%s' port '%s' trunk_details '%v'", srv.Name, portExt.Name, portExt.TrunkDetails)
652-
for _, subport := range portExt.TrunkDetails.SubPorts {
653-
p, err := neutronports.Get(client, subport.PortID).Extract()
654-
if err != nil {
655-
klog.Errorf("Failed to get subport %s details: %v", subport.PortID, err)
656-
continue
657-
}
658-
n, err := networks.Get(client, p.NetworkID).Extract()
659-
if err != nil {
660-
klog.Errorf("Failed to get subport %s network details: %v", subport.PortID, err)
661-
continue
649+
bodyBytes, err := json.Marshal(ports)
650+
if err == nil {
651+
if json.Unmarshal(bodyBytes, &portDetails) == nil {
652+
for _, portExt := range portDetails {
653+
klog.V(5).Infof("Node '%s' port '%s' trunk_details '%v'", srv.Name, portExt.Name, portExt.TrunkDetails)
654+
for _, subport := range portExt.TrunkDetails.SubPorts {
655+
p, err := neutronports.Get(client, subport.PortID).Extract()
656+
if err != nil {
657+
klog.Errorf("Failed to get subport %s details: %v", subport.PortID, err)
658+
continue
659+
}
660+
n, err := networks.Get(client, p.NetworkID).Extract()
661+
if err != nil {
662+
klog.Errorf("Failed to get subport %s network details: %v", subport.PortID, err)
663+
continue
664+
}
665+
for _, fixedIP := range p.FixedIPs {
666+
isIPv6 := net.ParseIP(fixedIP.IPAddress).To4() == nil
667+
if !(isIPv6 && networkingOpts.IPv6SupportDisabled) {
668+
addr := Address{IPType: "fixed", Addr: fixedIP.IPAddress}
669+
subportAddresses[n.Name] = append(subportAddresses[n.Name], addr)
670+
}
671+
}
672+
}
662673
}
663-
p.NetworkID = n.Name
664-
subports = append(subports, *p)
665674
}
666675
}
667676

668-
if len(subports) > 0 {
669-
subportAddresses := make(map[string][]Address)
670-
for _, subport := range subports {
671-
for _, fixedIP := range subport.FixedIPs {
672-
isIPv6 := net.ParseIP(fixedIP.IPAddress).To4() == nil
673-
if !(isIPv6 && networkingOpts.IPv6SupportDisabled) {
674-
addr := Address{IPType: "fixed", Addr: fixedIP.IPAddress}
675-
subportAddresses[subport.NetworkID] = append(subportAddresses[subport.NetworkID], addr)
676-
}
677-
}
678-
}
679-
for nw, ips := range subportAddresses {
680-
srvAddresses, ok := addresses[nw]
681-
if !ok {
682-
addresses[nw] = ips
683-
} else {
684-
// this is to take care the corner case
685-
// where the same network is attached to the node both directly and via trunk
686-
addresses[nw] = append(srvAddresses, ips...)
687-
}
677+
for nw, ips := range subportAddresses {
678+
srvAddresses, ok := addresses[nw]
679+
if !ok {
680+
addresses[nw] = ips
681+
} else {
682+
// this is to take care the corner case
683+
// where the same network is attached to the node both directly and via trunk
684+
addresses[nw] = append(srvAddresses, ips...)
688685
}
689686
}
690687

0 commit comments

Comments
 (0)