Skip to content

Commit

Permalink
Search node address in port's DiscoveredBindings instead RealizedBind…
Browse files Browse the repository at this point in the history
…ings

In IPI + NSX-T impactor ufo, the node LSP's addresses are in
discovered_bindings instead of realized_bindings. In previous NSX-T,
the addresses locate in both realized_bindings and discovered_bindings,
so we don't need to worry about the upgrade case.

This patch also will remove some stale functions.
  • Loading branch information
heypnus committed Sep 23, 2021
1 parent 77de513 commit 5e85f71
Showing 1 changed file with 4 additions and 70 deletions.
74 changes: 4 additions & 70 deletions pkg/controller/node/node_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,9 @@ import (
"github.com/vmware/nsx-container-plugin-operator/pkg/controller/statusmanager"
operatortypes "github.com/vmware/nsx-container-plugin-operator/pkg/types"
"github.com/vmware/vsphere-automation-sdk-go/runtime/core"
"github.com/vmware/vsphere-automation-sdk-go/runtime/data"
vspherelog "github.com/vmware/vsphere-automation-sdk-go/runtime/log"
policyclient "github.com/vmware/vsphere-automation-sdk-go/runtime/protocol/client"
"github.com/vmware/vsphere-automation-sdk-go/runtime/security"
"github.com/vmware/vsphere-automation-sdk-go/services/nsxt/model"
"github.com/vmware/vsphere-automation-sdk-go/services/nsxt/search"
"gopkg.in/ini.v1"
corev1 "k8s.io/api/core/v1"
k8serrors "k8s.io/apimachinery/pkg/api/errors"
Expand Down Expand Up @@ -196,15 +193,15 @@ func filterPortsByNodeAddresses(nsxClients *NsxClients, ports *[]nsxtmgr.Logical
if err != nil {
return filteredPorts, err
}
if len(logicalPort.RealizedBindings) == 0 {
if len(logicalPort.DiscoveredBindings) == 0 {
continue
}
var collectedAddresses []string
for _, realizedBinding := range logicalPort.RealizedBindings {
address := realizedBinding.Binding.IpAddress
for _, discoveredBinding := range logicalPort.DiscoveredBindings {
address := discoveredBinding.Binding.IpAddress
if inSlice(address, nodeAddresses) && !inSlice(address, collectedAddresses) {
log.Info(fmt.Sprintf("Node address %s matches port %s", address, port.Id))
// The addresses in logicalPort.RealizedBindings may be duplicate so we use collectedAddresses to ensure the uniqueness in filteredPorts.
// The addresses in logicalPort.DiscoveredBindings may be duplicate so we use collectedAddresses to ensure the uniqueness in filteredPorts.
collectedAddresses = append(collectedAddresses, address)
filteredPorts = append(filteredPorts, &port)
}
Expand All @@ -220,69 +217,6 @@ func filterPortsByNodeAddresses(nsxClients *NsxClients, ports *[]nsxtmgr.Logical
return filteredPorts, err
}

func searchNodePortByVcNameAddress(nsxClients *NsxClients, nodeName string, nodeAddress string) (*model.SegmentPort, error) {
log.Info(fmt.Sprintf("Searching segment port for node %s", nodeName))
connector := nsxClients.PolicyConnector
searchClient := search.NewDefaultQueryClient(connector)
// The format of node segment port display_name:
// <vmx file's parent directory name>/<node vSphere name>.vmx@<tn-id>
// The vmx file's parent directory name can include VM name or a uid string for a vSAN VM
searchString := fmt.Sprintf("resource_type:SegmentPort AND display_name:*\\/%s.vmx*", nodeName)
ports, err := searchClient.List(searchString, nil, nil, nil, nil, nil)
if err != nil {
return nil, err
}
if len(ports.Results) == 0 {
return nil, errors.Errorf("segment port for node %s not found", nodeName)
}
portIndex := 0
portIndex, err = filterSegmentPorts(nsxClients, ports.Results, nodeName, nodeAddress)
if err != nil {
return nil, errors.Errorf("found %d segment ports for node %s, but none with address %s: %s", len(ports.Results), nodeName, nodeAddress, err)
}
portId, err := ports.Results[portIndex].Field("id")
if err != nil {
return nil, err
}
portPath, err := ports.Results[portIndex].Field("parent_path")
if err != nil {
return nil, err
}
portIdValue := (portId).(*data.StringValue).Value()
portPathValue := (portPath).(*data.StringValue).Value()
segmentPort := model.SegmentPort{
Id: &portIdValue,
Path: &portPathValue,
}
return &segmentPort, nil
}

func filterSegmentPorts(nsxClients *NsxClients, ports []*data.StructValue, nodeName string, nodeAddress string) (int, error) {
log.Info(fmt.Sprintf("Found %d segment ports for node %s, checking addresses", len(ports), nodeName))
for idx, port := range ports {
portPolicyId, err := port.Field("id")
if err != nil {
return -1, err
}
portPolicyIdValue := (portPolicyId).(*data.StringValue).Value()
// there's an assumption that the policy ID has format "default:<manager_id>"
portMgrId := string([]byte(portPolicyIdValue)[8:])
nsxClient := nsxClients.ManagerClient
logicalPort, _, err := nsxClient.LogicalSwitchingApi.GetLogicalPortState(nsxClient.Context, portMgrId)
if err != nil {
return -1, err
}
if len(logicalPort.RealizedBindings) == 0 {
continue
}
address := logicalPort.RealizedBindings[0].Binding.IpAddress
if address == nodeAddress {
return idx, nil
}
}
return -1, errors.Errorf("no port matches")
}

func getConnectorTLSConfig(insecure bool, clientCertFile string, clientKeyFile string, caFile string) (*tls.Config, error) {
tlsConfig := tls.Config{InsecureSkipVerify: insecure}

Expand Down

0 comments on commit 5e85f71

Please sign in to comment.