Skip to content

Commit

Permalink
Better look for nodeAddresses() and getAttachedInterfacesByID()
Browse files Browse the repository at this point in the history
Signed-off-by: Jing Zhang <[email protected]>
  • Loading branch information
Jing Zhang committed May 12, 2023
1 parent 9a7db2c commit e88afe9
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 55 deletions.
106 changes: 53 additions & 53 deletions pkg/openstack/instances.go
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ func (i *Instances) AddSSHKeyToAllInstances(ctx context.Context, user string, ke
func (i *Instances) NodeAddresses(ctx context.Context, name types.NodeName) ([]v1.NodeAddress, error) {
klog.V(4).Infof("NodeAddresses(%v) called", name)

addrs, err := getAddressesByName(i.compute, name, i.networkingOpts, i.network)
addrs, err := getAddressesByName(i.compute, i.network, name, i.networkingOpts)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -237,7 +237,7 @@ func (i *Instances) NodeAddressesByProviderID(ctx context.Context, providerID st
return []v1.NodeAddress{}, err
}

interfaces, err := getAttachedInterfacesByID(i.compute, server.ID, i.network)
interfaces, err := getAttachedInterfacesByID(i.compute, i.network, server.ID)
if err != nil {
return []v1.NodeAddress{}, err
}
Expand Down Expand Up @@ -343,7 +343,7 @@ func (i *Instances) InstanceMetadata(ctx context.Context, node *v1.Node) (*cloud
return nil, err
}

interfaces, err := getAttachedInterfacesByID(i.compute, srv.ID, i.network)
interfaces, err := getAttachedInterfacesByID(i.compute, i.network, srv.ID)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -644,33 +644,33 @@ func nodeAddresses(srv *servers.Server, interfaces []attachinterfaces.Interface,
if len(addresses) < len(allPrivates) {
extraPrivates := make(map[string][]Address)
// For each private network
for k, v := range allPrivates {
ok := false
for network, interfaceAddresses := range allPrivates {
found := false
// For each address in the private network
for _, a := range v {
// Check if the address is directly connected
for _, v1 := range addresses {
for _, a1 := range v1 {
if a.Addr == a1.Addr {
ok = true
for _, interfaceAddress := range interfaceAddresses {
// Check if the address is directly assigned
for _, srvAddresses := range addresses {
for _, srvAddress := range srvAddresses {
if interfaceAddress.Addr == srvAddress.Addr {
found = true
break
}
}
}
}
// All the addresses in the private network are not directly connected
// All the addresses in the private network are not directly assigned
// Save the private network
if !ok {
extraPrivates[k] = v
if !found {
extraPrivates[network] = interfaceAddresses
}
}
klog.V(5).Infof("Node '%s' extraPrivates '%s'", srv.Name, extraPrivates)
for k, v := range extraPrivates {
v1, ok := addresses[k]
klog.V(5).Infof("Node '%s' extra private interfaces '%s'", srv.Name, extraPrivates)
for network, interfaceAddresses := range extraPrivates {
srvAddresses, ok := addresses[network]
if !ok {
addresses[k] = v
addresses[network] = interfaceAddresses
} else {
addresses[k] = append(v1, v...)
addresses[network] = append(srvAddresses, interfaceAddresses...)
}
}
}
Expand Down Expand Up @@ -728,13 +728,13 @@ func nodeAddresses(srv *servers.Server, interfaces []attachinterfaces.Interface,
return addrs, nil
}

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

interfaces, err := getAttachedInterfacesByID(compute, srv.ID, network)
interfaces, err := getAttachedInterfacesByID(compute, network, srv.ID)
if err != nil {
return nil, err
}
Expand All @@ -754,56 +754,56 @@ func getSubInterfaces(interfaces []attachinterfaces.Interface, network *gophercl
}
allPages, err := trunks.List(network, listOpts).AllPages()
if err != nil {
klog.Errorf("Failed to list trunks: %v", err)
klog.Infof("Failed to list trunks: %v", err)
return subports, nil
}
allTrunks, err := trunks.ExtractTrunks(allPages)
if err != nil {
klog.Errorf("Failed to extract trunks: %v", err)
return subports, err
}
if len(allTrunks) > 1 {
klog.Errorf("It is not expected to see more than one trunk on a single port %s", iface.PortID)
return subports, err
}
// Get subports attached to the trunks
for _, trunk := range allTrunks {
if len(trunk.Subports) == 0 {
continue
trunk := allTrunks[0]
klog.V(5).Infof("Subports for trunk %s: %v", trunk.ID, trunk.Subports)
// Arrange subports as for directly attached ports
for _, sport := range trunk.Subports {
p, err := ports.Get(network, sport.PortID).Extract()
if err != nil {
klog.Errorf("Failed to get port info for subport %s: %v", sport.PortID, err)
return subports, err
}
klog.V(5).Infof("Subports for trunk %s: %v", trunk.ID, trunk.Subports)
// Arrange subports as for directly attached ports
for _, sport := range trunk.Subports {
p, err := ports.Get(network, sport.PortID).Extract()
if err != nil {
klog.Errorf("Failed to get port info for subport %s: %v", sport.PortID, err)
return subports, err
}
n, err := networks.Get(network, p.NetworkID).Extract()
if err != nil {
klog.Errorf("Failed to get network info for subport %s: %v", sport.PortID, err)
return subports, err
}
var sface = attachinterfaces.Interface{
PortState: "ACTIVE",
FixedIPs: []attachinterfaces.FixedIP{},
PortID: p.ID,
NetID: n.Name,
MACAddr: p.MACAddress,
}
for _, ip := range p.FixedIPs {
var ip2 = attachinterfaces.FixedIP{
SubnetID: ip.SubnetID,
IPAddress: ip.IPAddress,
}
sface.FixedIPs = append(sface.FixedIPs, ip2)
n, err := networks.Get(network, p.NetworkID).Extract()
if err != nil {
klog.Errorf("Failed to get network info for subport %s: %v", sport.PortID, err)
return subports, err
}
var sface = attachinterfaces.Interface{
PortState: "ACTIVE",
FixedIPs: []attachinterfaces.FixedIP{},
PortID: p.ID,
NetID: n.Name,
MACAddr: p.MACAddress,
}
for _, ip := range p.FixedIPs {
var ip2 = attachinterfaces.FixedIP{
SubnetID: ip.SubnetID,
IPAddress: ip.IPAddress,
}
subports = append(subports, sface)
sface.FixedIPs = append(sface.FixedIPs, ip2)
}
subports = append(subports, sface)
}
}
klog.V(5).Infof("Node has %d sub-interfaces '%v'", len(subports), subports)
return subports, nil
}

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

mc := metrics.NewMetricContext("server_os_interface", "list")
Expand Down
2 changes: 1 addition & 1 deletion pkg/openstack/instancesv2.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ func (i *InstancesV2) InstanceMetadata(ctx context.Context, node *v1.Node) (*clo
return nil, err
}

interfaces, err := getAttachedInterfacesByID(i.compute, server.ID, i.network)
interfaces, err := getAttachedInterfacesByID(i.compute, i.network, server.ID)
if err != nil {
return nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/openstack/loadbalancer.go
Original file line number Diff line number Diff line change
Expand Up @@ -718,7 +718,7 @@ func getSubnetIDForLB(compute *gophercloud.ServiceClient, node corev1.Node, pref
instanceID = instanceID[(ind + 1):]
}

interfaces, err := getAttachedInterfacesByID(compute, instanceID, network)
interfaces, err := getAttachedInterfacesByID(compute, network, instanceID)
if err != nil {
return "", err
}
Expand Down
8 changes: 8 additions & 0 deletions pkg/openstack/openstack_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -935,6 +935,14 @@ func TestNodeAddressesWithSubports(t *testing.T) {
"OS-EXT-IPS:type": "fixed",
},
},
"mycluster-01-vlan703_network": []interface{}{
map[string]interface{}{
"OS-EXT-IPS-MAC:mac_addr": "fa:16:3e:36:b7:8f",
"version": float64(4),
"addr": "169.252.60.15",
"OS-EXT-IPS:type": "fixed",
},
},
},
}

Expand Down

0 comments on commit e88afe9

Please sign in to comment.