@@ -19,6 +19,7 @@ import (
19
19
"github.com/pkg/errors"
20
20
"github.com/vmware/govmomi/find"
21
21
"github.com/vmware/govmomi/object"
22
+ "github.com/vmware/govmomi/property"
22
23
"github.com/vmware/govmomi/vim25"
23
24
"github.com/vmware/govmomi/vim25/mo"
24
25
vimtypes "github.com/vmware/govmomi/vim25/types"
@@ -367,7 +368,7 @@ func (np *netOpNetworkProvider) getNetworkRef(ctx goctx.Context, networkType, ne
367
368
case VdsNetworkType :
368
369
return np .networkForPortGroupID (networkID )
369
370
case NsxtNetworkType :
370
- return searchNsxtNetworkReference (ctx , np .finder , np . cluster , networkID )
371
+ return searchNsxtNetworkReference (ctx , np .cluster , networkID )
371
372
default :
372
373
return nil , fmt .Errorf ("unsupported NetOP network type %s" , networkType )
373
374
}
@@ -603,7 +604,7 @@ func (np *nsxtNetworkProvider) createEthernetCard(
603
604
return nil , err
604
605
}
605
606
606
- networkRef , err := searchNsxtNetworkReference (vmCtx , np .finder , np . cluster , vnetIf .Status .ProviderStatus .NsxLogicalSwitchID )
607
+ networkRef , err := searchNsxtNetworkReference (vmCtx , np .cluster , vnetIf .Status .ProviderStatus .NsxLogicalSwitchID )
607
608
if err != nil {
608
609
// Log message used by VMC LINT. Refer to before making changes
609
610
vmCtx .Logger .Error (err , "Failed to search for nsx-t network associated with vnetIf" , "vnetIf" , vnetIf )
@@ -727,108 +728,51 @@ func (np *nsxtNetworkProvider) getNetplanEthernet(vnetIf *ncpv1alpha1.VirtualNet
727
728
return eth
728
729
}
729
730
730
- // matchOpaqueNetwork takes the network ID, returns whether the opaque network matches the networkID.
731
- func matchOpaqueNetwork (ctx goctx.Context , network object.NetworkReference , networkID string ) bool {
732
- obj , ok := network .(* object.OpaqueNetwork )
733
- if ! ok {
734
- return false
735
- }
736
-
737
- var opaqueNet mo.OpaqueNetwork
738
- if err := obj .Properties (ctx , obj .Reference (), []string {"summary" }, & opaqueNet ); err != nil {
739
- return false
740
- }
741
-
742
- summary , _ := opaqueNet .Summary .(* vimtypes.OpaqueNetworkSummary )
743
- return summary .OpaqueNetworkId == networkID
744
- }
745
-
746
- // matchDistributedPortGroup takes the network ID, returns whether the distributed port group matches the networkID.
747
- func matchDistributedPortGroup (
731
+ // searchNsxtNetworkReference takes in nsx-t logical switch UUID and returns the reference of the network.
732
+ func searchNsxtNetworkReference (
748
733
ctx goctx.Context ,
749
- network object.NetworkReference ,
750
- networkID string ,
751
- hostMoIDs []vimtypes.ManagedObjectReference ) bool {
734
+ ccr * object.ClusterComputeResource ,
735
+ networkID string ) (object.NetworkReference , error ) {
752
736
753
- obj , ok := network .(* object.DistributedVirtualPortgroup )
754
- if ! ok {
755
- return false
756
- }
757
-
758
- var configInfo []vimtypes.ObjectContent
759
- err := obj .Properties (ctx , obj .Reference (), []string {"config.logicalSwitchUuid" , "host" }, & configInfo )
760
- if err != nil {
761
- return false
737
+ var obj mo.ClusterComputeResource
738
+ if err := ccr .Properties (ctx , ccr .Reference (), []string {"network" }, & obj ); err != nil {
739
+ return nil , err
762
740
}
763
741
764
- if len (configInfo ) > 0 {
765
- // Check "logicalSwitchUuid" property
766
- lsIDMatch := false
767
- for _ , dynamicProperty := range configInfo [0 ].PropSet {
768
- if dynamicProperty .Name == "config.logicalSwitchUuid" && dynamicProperty .Val == networkID {
769
- lsIDMatch = true
770
- break
771
- }
772
- }
773
-
774
- // logicalSwitchUuid did not match
775
- if ! lsIDMatch {
776
- return false
777
- }
778
-
779
- foundAllHosts := false
780
- for _ , dynamicProperty := range configInfo [0 ].PropSet {
781
- // In the case of a single NSX Overlay Transport Zone for all the clusters and DVS's,
782
- // multiple DVPGs(associated with different DVS's) will have the same "logicalSwitchUuid".
783
- // So matching "logicalSwitchUuid" is necessary condition, but not sufficient.
784
- // Checking if the DPVG has all the hosts in the cluster, along with the above would be sufficient
785
- if dynamicProperty .Name == "host" {
786
- if hosts , ok := dynamicProperty .Val .(vimtypes.ArrayOfManagedObjectReference ); ok {
787
- foundAllHosts = true
788
- dvsHostSet := make (map [string ]bool , len (hosts .ManagedObjectReference ))
789
- for _ , dvsHost := range hosts .ManagedObjectReference {
790
- dvsHostSet [dvsHost .Value ] = true
791
- }
792
-
793
- for _ , hostMoRef := range hostMoIDs {
794
- if _ , ok := dvsHostSet [hostMoRef .Value ]; ! ok {
795
- foundAllHosts = false
796
- break
797
- }
798
- }
799
- }
800
- }
742
+ var dvpgsMoRefs []vimtypes.ManagedObjectReference
743
+ for _ , n := range obj .Network {
744
+ if n .Type == "DistributedVirtualPortgroup" {
745
+ dvpgsMoRefs = append (dvpgsMoRefs , n .Reference ())
801
746
}
747
+ }
802
748
803
- // Implicit that lsID Matches at this point
804
- return foundAllHosts
749
+ if len ( dvpgsMoRefs ) == 0 {
750
+ return nil , fmt . Errorf ( "ClusterComputeResource %s has no DVPGs" , ccr . Reference (). Value )
805
751
}
806
- return false
807
- }
808
752
809
- // searchNsxtNetworkReference takes in nsx-t logical switch UUID and returns the reference of the network.
810
- func searchNsxtNetworkReference (ctx goctx.Context , finder * find.Finder , cluster * object.ClusterComputeResource , networkID string ) (object.NetworkReference , error ) {
811
- networks , err := finder .NetworkList (ctx , "*" )
753
+ var dvpgs []mo.DistributedVirtualPortgroup
754
+ err := property .DefaultCollector (ccr .Client ()).Retrieve (ctx , dvpgsMoRefs , []string {"config.logicalSwitchUuid" }, & dvpgs )
812
755
if err != nil {
813
756
return nil , err
814
757
}
815
758
816
- // Get the list of ESX host moRef objects for this cluster
817
- // TODO: ps: []string{"host"} instead of nil?
818
- var computeResource mo. ComputeResource
819
- if err := cluster . Properties ( ctx , cluster .Reference (), nil , & computeResource ); err != nil {
820
- return nil , err
759
+ var dvpgMoRefs []vimtypes. ManagedObjectReference
760
+ for _ , dvpg := range dvpgs {
761
+ if dvpg . Config . LogicalSwitchUuid == networkID {
762
+ dvpgMoRefs = append ( dvpgMoRefs , dvpg .Reference ())
763
+ }
821
764
}
822
765
823
- for _ , network := range networks {
824
- if matchDistributedPortGroup (ctx , network , networkID , computeResource .Host ) {
825
- return network , nil
826
- }
827
- if matchOpaqueNetwork (ctx , network , networkID ) {
828
- return network , nil
829
- }
766
+ switch len (dvpgMoRefs ) {
767
+ case 1 :
768
+ return object .NewDistributedVirtualPortgroup (ccr .Client (), dvpgMoRefs [0 ]), nil
769
+ case 0 :
770
+ return nil , fmt .Errorf ("no DVPG with NSX-T network ID %q found" , networkID )
771
+ default :
772
+ // The LogicalSwitchUuid is supposed to be unique per CCR, so this is likely an NCP
773
+ // misconfiguration, and we don't know which one to pick.
774
+ return nil , fmt .Errorf ("multiple DVPGs (%d) with NSX-T network ID %q found" , len (dvpgMoRefs ), networkID )
830
775
}
831
- return nil , fmt .Errorf ("opaque network with ID '%s' not found" , networkID )
832
776
}
833
777
834
778
// ToCidrNotation takes ip and mask as ip addresses and returns a cidr notation.
0 commit comments