@@ -7,10 +7,6 @@ package network
77import (
88 "context"
99 "fmt"
10- "sync"
11- "time"
12-
13- ctrl "sigs.k8s.io/controller-runtime"
1410
1511 "github.com/vmware/govmomi/object"
1612 "github.com/vmware/govmomi/property"
@@ -59,60 +55,15 @@ func (n nsxOpaqueBacking) Summary(_ context.Context) (*vimtypes.OpaqueNetworkSum
5955 }, nil
6056}
6157
62- var (
63- // uuidToDVPGCache contains the cache used look up the CCR's DPVG for a
64- // given LogicalSwitchUUID.
65- uuidToDVPGCache sync.Map
66-
67- clearUUIDToDVPGCache sync.Once
68- )
69-
58+ // searchNsxtNetworkReference takes in NSX-T LogicalSwitchUUID and returns the reference of the network.
7059func searchNsxtNetworkReference (
7160 ctx context.Context ,
7261 ccr * object.ClusterComputeResource ,
7362 networkID string ) (object.NetworkReference , error ) {
7463
75- clearUUIDToDVPGCache .Do (func () {
76- // Start goroutine to periodically clear the cache.
77- go func () {
78- logger := ctrl .Log .WithName ("nsx-dvpg-cache-clearer" )
79- ticker := time .NewTicker (time .Minute * 60 )
80- for range ticker .C {
81- logger .Info ("Clearing NSX LogicalSwitchUUID to DVPG cache" )
82- uuidToDVPGCache .Clear ()
83- }
84- }()
85- })
86-
87- key := ccr .Reference ().Value
88-
89- if c , ok := uuidToDVPGCache .Load (key ); ok {
90- cc := c .(map [string ]vimtypes.ManagedObjectReference )
91- if dvpg , ok := cc [networkID ]; ok {
92- return object .NewDistributedVirtualPortgroup (ccr .Client (), dvpg ), nil
93- }
94- }
95-
96- // On either miss - CCR or UUID not found - try to refresh the DVPGs for the CCR,
97- // and always store the latest results in the cache. We could CompareAndSwap()
98- // instead but let's have the latest win.
99- uuidsToDPVG , err := getDVPGsForCCR (ctx , ccr )
100- if err != nil {
101- return nil , err
102- }
103- uuidToDVPGCache .Store (key , uuidsToDPVG )
104-
105- if dvpg , ok := uuidsToDPVG [networkID ]; ok {
106- return object .NewDistributedVirtualPortgroup (ccr .Client (), dvpg ), nil
107- }
108-
109- return nil , fmt .Errorf ("no DVPG with NSX network ID %q found" , networkID )
110- }
111-
112- func getDVPGsForCCR (
113- ctx context.Context ,
114- ccr * object.ClusterComputeResource ) (map [string ]vimtypes.ManagedObjectReference , error ) {
115-
64+ // This is more or less how the old code did it. We could save repeated work by moving this
65+ // into the callers since it will always be for the same CCR, but the common case is one NIC,
66+ // or at most a handful, so that's for later.
11667 var obj mo.ClusterComputeResource
11768 if err := ccr .Properties (ctx , ccr .Reference (), []string {"network" }, & obj ); err != nil {
11869 return nil , err
@@ -126,7 +77,7 @@ func getDVPGsForCCR(
12677 }
12778
12879 if len (dvpgsMoRefs ) == 0 {
129- return nil , nil
80+ return nil , fmt . Errorf ( "ClusterComputeResource %s has no DVPGs" , ccr . Reference (). Value )
13081 }
13182
13283 var dvpgs []mo.DistributedVirtualPortgroup
@@ -135,12 +86,21 @@ func getDVPGsForCCR(
13586 return nil , err
13687 }
13788
138- uuidToDPVG := make ( map [ string ]vimtypes.ManagedObjectReference , len ( dvpgs ))
89+ var dvpgMoRefs [ ]vimtypes.ManagedObjectReference
13990 for _ , dvpg := range dvpgs {
140- if uuid := dvpg .Config .LogicalSwitchUuid ; uuid != "" {
141- uuidToDPVG [ uuid ] = dvpg .Reference ()
91+ if dvpg .Config .LogicalSwitchUuid == networkID {
92+ dvpgMoRefs = append ( dvpgMoRefs , dvpg .Reference () )
14293 }
14394 }
14495
145- return uuidToDPVG , nil
96+ switch len (dvpgMoRefs ) {
97+ case 1 :
98+ return object .NewDistributedVirtualPortgroup (ccr .Client (), dvpgMoRefs [0 ]), nil
99+ case 0 :
100+ return nil , fmt .Errorf ("no DVPG with NSX network ID %q found" , networkID )
101+ default :
102+ // The LogicalSwitchUuid is supposed to be unique per CCR, so this is likely an NCP
103+ // misconfiguration, and we don't know which one to pick.
104+ return nil , fmt .Errorf ("multiple DVPGs (%d) with NSX network ID %q found" , len (dvpgMoRefs ), networkID )
105+ }
146106}
0 commit comments