@@ -45,6 +45,7 @@ import (
4545 "k8s.io/cloud-provider-openstack/pkg/util"
4646 "k8s.io/cloud-provider-openstack/pkg/util/errors"
4747 "k8s.io/cloud-provider-openstack/pkg/util/metadata"
48+ "k8s.io/cloud-provider-openstack/pkg/util/openstack"
4849)
4950
5051// Instances encapsulates an implementation of Instances for OpenStack.
@@ -241,7 +242,7 @@ func (i *Instances) NodeAddressesByProviderID(ctx context.Context, providerID st
241242 return []v1.NodeAddress {}, err
242243 }
243244
244- addresses , err := nodeAddresses (server , ports , i .networkingOpts )
245+ addresses , err := nodeAddresses (server , ports , i .network , i . networkingOpts )
245246 if err != nil {
246247 return []v1.NodeAddress {}, err
247248 }
@@ -346,7 +347,7 @@ func (i *Instances) InstanceMetadata(ctx context.Context, node *v1.Node) (*cloud
346347 if err != nil {
347348 return nil , err
348349 }
349- addresses , err := nodeAddresses (srv , ports , i .networkingOpts )
350+ addresses , err := nodeAddresses (srv , ports , i .network , i . networkingOpts )
350351 if err != nil {
351352 return nil , err
352353 }
@@ -569,21 +570,15 @@ func getServerByName(client *gophercloud.ServiceClient, name types.NodeName) (*S
569570 return & serverList [0 ], nil
570571}
571572
572- type Address struct {
573- IPType string `mapstructure:"OS-EXT-IPS:type"`
574- Addr string
575- }
576-
577573// IP addresses order:
578574// * interfaces private IPs
579575// * access IPs
580576// * metadata hostname
581577// * server object Addresses (floating type)
582- func nodeAddresses (srv * servers.Server , ports []ports.Port , networkingOpts NetworkingOpts ) ([]v1.NodeAddress , error ) {
578+ func nodeAddresses (srv * servers.Server , ports []ports.Port , client * gophercloud. ServiceClient , networkingOpts NetworkingOpts ) ([]v1.NodeAddress , error ) {
583579 addrs := []v1.NodeAddress {}
584580
585581 // parse private IP addresses first in an ordered manner
586- addressesByNetworkID := make (map [string ][]Address )
587582 for _ , port := range ports {
588583 for _ , fixedIP := range port .FixedIPs {
589584 if port .Status == "ACTIVE" {
@@ -595,8 +590,6 @@ func nodeAddresses(srv *servers.Server, ports []ports.Port, networkingOpts Netwo
595590 Address : fixedIP .IPAddress ,
596591 },
597592 )
598- addr := Address {IPType : "fixed" , Addr : fixedIP .IPAddress }
599- addressesByNetworkID [port .NetworkID ] = append (addressesByNetworkID [port .NetworkID ], addr )
600593 }
601594 }
602595 }
@@ -631,17 +624,43 @@ func nodeAddresses(srv *servers.Server, ports []ports.Port, networkingOpts Netwo
631624 }
632625
633626 // process the rest
627+ type Address struct {
628+ IPType string `mapstructure:"OS-EXT-IPS:type"`
629+ Addr string
630+ }
631+
634632 var addresses map [string ][]Address
635633 err := mapstructure .Decode (srv .Addresses , & addresses )
636634 if err != nil {
637635 return nil , err
638636 }
639637
640638 // Add the addresses assigned on subports via trunk
641- // This adds the vlan networks to which subports are attached
642- if len (addresses ) < len (addressesByNetworkID ) {
643- nrSubportAddresses := addSubportNodeAddresses (addresses , addressesByNetworkID )
644- klog .V (5 ).Infof ("Node '%s' is added %d subport addresses" , srv .Name , nrSubportAddresses )
639+ // This exposes the vlan networks to which subports are attached
640+ if client != nil {
641+ subports , err := getSubports (client , srv .ID )
642+ if err == nil {
643+ var subportAddresses map [string ][]Address
644+ for _ , subport := range subports {
645+ for _ , fixedIP := range subport .FixedIPs {
646+ isIPv6 := net .ParseIP (fixedIP .IPAddress ).To4 () == nil
647+ if ! (isIPv6 && networkingOpts .IPv6SupportDisabled ) {
648+ addr := Address {IPType : "fixed" , Addr : fixedIP .IPAddress }
649+ subportAddresses [subport .NetworkID ] = append (subportAddresses [subport .NetworkID ], addr )
650+ }
651+ }
652+ }
653+ for nw , ips := range subportAddresses {
654+ srvAddresses , ok := addresses [nw ]
655+ if ! ok {
656+ addresses [nw ] = ips
657+ } else {
658+ // this is to take care the corner case
659+ // where the same network is attached to the node both directly and via trunk
660+ addresses [nw ] = append (srvAddresses , ips ... )
661+ }
662+ }
663+ }
645664 }
646665
647666 var networks []string
@@ -697,58 +716,8 @@ func nodeAddresses(srv *servers.Server, ports []ports.Port, networkingOpts Netwo
697716 return addrs , nil
698717}
699718
700- // addSubportNodeAddresses adds addresses assigned on subports
701- func addSubportNodeAddresses (addresses , addressesByNetworkID map [string ][]Address ) int {
702- subportAddresses := make (map [string ][]Address )
703- for network , portAddresses := range addressesByNetworkID {
704- found := false
705- for _ , portAddress := range portAddresses {
706- // Check if the address is directly attached to the node
707- for _ , nodeAddresses := range addresses {
708- for _ , nodeAddress := range nodeAddresses {
709- if portAddress .Addr == nodeAddress .Addr {
710- found = true
711- break
712- }
713- }
714- }
715- }
716- // All the addresses in the vlan network are not directly attached to the node
717- // Save the vlan network
718- if ! found {
719- subportAddresses [network ] = portAddresses
720- }
721- }
722- // add subport addresses to the node addresses
723- for network , portAddresses := range subportAddresses {
724- srvAddresses , ok := addresses [network ]
725- if ! ok {
726- addresses [network ] = portAddresses
727- } else {
728- // this is to take care the corner case
729- // where the same network is attached to the node both directly and via trunk
730- addresses [network ] = append (srvAddresses , portAddresses ... )
731- }
732- }
733- return len (subportAddresses )
734- }
735-
736- func getAddressesByName (client * gophercloud.ServiceClient , name types.NodeName , networkingOpts NetworkingOpts ) ([]v1.NodeAddress , error ) {
737- srv , err := getServerByName (client , name )
738- if err != nil {
739- return nil , err
740- }
741-
742- ports , err := getAttachedPorts (client , srv .ID )
743- if err != nil {
744- return nil , err
745- }
746-
747- return nodeAddresses (& srv .Server , ports , networkingOpts )
748- }
749-
750- // getAttachedPorts returns a list of ports attached to a server.
751- func getAttachedPorts (client * gophercloud.ServiceClient , serverID string ) ([]ports.Port , error ) {
719+ // getSubports returns subports attached to the node via trunk
720+ func getSubports (client * gophercloud.ServiceClient , serverID string ) ([]ports.Port , error ) {
752721 listOpts := ports.ListOpts {
753722 DeviceID : serverID ,
754723 DeviceOwner : "compute:nova" ,
@@ -759,20 +728,19 @@ func getAttachedPorts(client *gophercloud.ServiceClient, serverID string) ([]por
759728 trunk_details.TrunkDetailsExt
760729 }
761730
762- var allPorts []ports.Port
731+ var subports []ports.Port
763732 var allPortDetails []portWithTrunkDetails
764733
765734 allPages , err := ports .List (client , listOpts ).AllPages ()
766735 if err != nil {
767- return allPorts , err
736+ return subports , err
768737 }
769738 err = ports .ExtractPortsInto (allPages , & allPortDetails )
770739 if err != nil {
771- return allPorts , err
740+ return subports , err
772741 }
773742
774743 for _ , portExt := range allPortDetails {
775- allPorts = append (allPorts , portExt .Port )
776744 // Get subport addresses
777745 for _ , subport := range portExt .TrunkDetails .SubPorts {
778746 p , err := ports .Get (client , subport .PortID ).Extract ()
@@ -785,12 +753,34 @@ func getAttachedPorts(client *gophercloud.ServiceClient, serverID string) ([]por
785753 klog .Errorf ("Failed to get subport %s network details: %v" , subport .PortID , err )
786754 continue
787755 }
788- p .Status = "ACTIVE"
789756 p .NetworkID = n .Name
790- allPorts = append (allPorts , * p )
757+ subports = append (subports , * p )
791758 }
792759 }
793760
794- klog .V (5 ).Infof ("Node %s has %d ports '%v'" , serverID , len (allPorts ), allPorts )
795- return allPorts , nil
761+ return subports , nil
762+ }
763+
764+ func getAddressesByName (client * gophercloud.ServiceClient , name types.NodeName , networkingOpts NetworkingOpts ) ([]v1.NodeAddress , error ) {
765+ srv , err := getServerByName (client , name )
766+ if err != nil {
767+ return nil , err
768+ }
769+
770+ ports , err := getAttachedPorts (client , srv .ID )
771+ if err != nil {
772+ return nil , err
773+ }
774+
775+ return nodeAddresses (& srv .Server , ports , client , networkingOpts )
776+ }
777+
778+ // getAttachedPorts returns a list of ports attached to a server.
779+ func getAttachedPorts (client * gophercloud.ServiceClient , serverID string ) ([]ports.Port , error ) {
780+ listOpts := ports.ListOpts {
781+ DeviceID : serverID ,
782+ DeviceOwner : "compute:nova" ,
783+ }
784+
785+ return openstack .GetPorts (client , listOpts )
796786}
0 commit comments