@@ -45,6 +45,7 @@ import (
45
45
"k8s.io/cloud-provider-openstack/pkg/util"
46
46
"k8s.io/cloud-provider-openstack/pkg/util/errors"
47
47
"k8s.io/cloud-provider-openstack/pkg/util/metadata"
48
+ "k8s.io/cloud-provider-openstack/pkg/util/openstack"
48
49
)
49
50
50
51
// Instances encapsulates an implementation of Instances for OpenStack.
@@ -241,7 +242,7 @@ func (i *Instances) NodeAddressesByProviderID(ctx context.Context, providerID st
241
242
return []v1.NodeAddress {}, err
242
243
}
243
244
244
- addresses , err := nodeAddresses (server , ports , i .networkingOpts )
245
+ addresses , err := nodeAddresses (server , ports , i .network , i . networkingOpts )
245
246
if err != nil {
246
247
return []v1.NodeAddress {}, err
247
248
}
@@ -346,7 +347,7 @@ func (i *Instances) InstanceMetadata(ctx context.Context, node *v1.Node) (*cloud
346
347
if err != nil {
347
348
return nil , err
348
349
}
349
- addresses , err := nodeAddresses (srv , ports , i .networkingOpts )
350
+ addresses , err := nodeAddresses (srv , ports , i .network , i . networkingOpts )
350
351
if err != nil {
351
352
return nil , err
352
353
}
@@ -569,21 +570,15 @@ func getServerByName(client *gophercloud.ServiceClient, name types.NodeName) (*S
569
570
return & serverList [0 ], nil
570
571
}
571
572
572
- type Address struct {
573
- IPType string `mapstructure:"OS-EXT-IPS:type"`
574
- Addr string
575
- }
576
-
577
573
// IP addresses order:
578
574
// * interfaces private IPs
579
575
// * access IPs
580
576
// * metadata hostname
581
577
// * 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 ) {
583
579
addrs := []v1.NodeAddress {}
584
580
585
581
// parse private IP addresses first in an ordered manner
586
- addressesByNetworkID := make (map [string ][]Address )
587
582
for _ , port := range ports {
588
583
for _ , fixedIP := range port .FixedIPs {
589
584
if port .Status == "ACTIVE" {
@@ -595,8 +590,6 @@ func nodeAddresses(srv *servers.Server, ports []ports.Port, networkingOpts Netwo
595
590
Address : fixedIP .IPAddress ,
596
591
},
597
592
)
598
- addr := Address {IPType : "fixed" , Addr : fixedIP .IPAddress }
599
- addressesByNetworkID [port .NetworkID ] = append (addressesByNetworkID [port .NetworkID ], addr )
600
593
}
601
594
}
602
595
}
@@ -631,17 +624,43 @@ func nodeAddresses(srv *servers.Server, ports []ports.Port, networkingOpts Netwo
631
624
}
632
625
633
626
// process the rest
627
+ type Address struct {
628
+ IPType string `mapstructure:"OS-EXT-IPS:type"`
629
+ Addr string
630
+ }
631
+
634
632
var addresses map [string ][]Address
635
633
err := mapstructure .Decode (srv .Addresses , & addresses )
636
634
if err != nil {
637
635
return nil , err
638
636
}
639
637
640
638
// 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
+ }
645
664
}
646
665
647
666
var networks []string
@@ -697,58 +716,8 @@ func nodeAddresses(srv *servers.Server, ports []ports.Port, networkingOpts Netwo
697
716
return addrs , nil
698
717
}
699
718
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 ) {
752
721
listOpts := ports.ListOpts {
753
722
DeviceID : serverID ,
754
723
DeviceOwner : "compute:nova" ,
@@ -759,20 +728,19 @@ func getAttachedPorts(client *gophercloud.ServiceClient, serverID string) ([]por
759
728
trunk_details.TrunkDetailsExt
760
729
}
761
730
762
- var allPorts []ports.Port
731
+ var subports []ports.Port
763
732
var allPortDetails []portWithTrunkDetails
764
733
765
734
allPages , err := ports .List (client , listOpts ).AllPages ()
766
735
if err != nil {
767
- return allPorts , err
736
+ return subports , err
768
737
}
769
738
err = ports .ExtractPortsInto (allPages , & allPortDetails )
770
739
if err != nil {
771
- return allPorts , err
740
+ return subports , err
772
741
}
773
742
774
743
for _ , portExt := range allPortDetails {
775
- allPorts = append (allPorts , portExt .Port )
776
744
// Get subport addresses
777
745
for _ , subport := range portExt .TrunkDetails .SubPorts {
778
746
p , err := ports .Get (client , subport .PortID ).Extract ()
@@ -785,12 +753,34 @@ func getAttachedPorts(client *gophercloud.ServiceClient, serverID string) ([]por
785
753
klog .Errorf ("Failed to get subport %s network details: %v" , subport .PortID , err )
786
754
continue
787
755
}
788
- p .Status = "ACTIVE"
789
756
p .NetworkID = n .Name
790
- allPorts = append (allPorts , * p )
757
+ subports = append (subports , * p )
791
758
}
792
759
}
793
760
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 )
796
786
}
0 commit comments