Skip to content

Commit f166db4

Browse files
committed
Use real VPC path in NSXServiceAccount
Signed-off-by: Kumar Atish <kumar.atish@broadcom.com>
1 parent c3a71ec commit f166db4

6 files changed

Lines changed: 230 additions & 19 deletions

File tree

cmd/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,7 @@ func startServiceController(mgr manager.Manager, nsxClient *nsx.Client) {
254254

255255
// Add the NSXServiceAccount controller.
256256
if cf.EnableAntreaNSXInterworking {
257-
reconcilerList = append(reconcilerList, nsxserviceaccountcontroller.NewNSXServiceAccountReconciler(mgr, commonService))
257+
reconcilerList = append(reconcilerList, nsxserviceaccountcontroller.NewNSXServiceAccountReconciler(mgr, commonService, vpcService))
258258
}
259259

260260
if restoreMode {

pkg/clean/clean.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ func InitializeCleanupService(cf *config.NSXOperatorConfig, nsxClient *nsx.Clien
185185
}
186186
wrapInitializeNSXServiceAccount := func(service common.Service) cleanupFunc {
187187
return func() (interface{}, error) {
188-
return nsxserviceaccount.InitializeNSXServiceAccount(service)
188+
return nsxserviceaccount.InitializeNSXServiceAccount(service, vpcService)
189189
}
190190
}
191191

pkg/controllers/nsxserviceaccount/nsxserviceaccount_controller.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -314,14 +314,14 @@ func (r *NSXServiceAccountReconciler) StartController(mgr ctrl.Manager, _ webhoo
314314
return nil
315315
}
316316

317-
func NewNSXServiceAccountReconciler(mgr ctrl.Manager, commonService servicecommon.Service) *NSXServiceAccountReconciler {
317+
func NewNSXServiceAccountReconciler(mgr ctrl.Manager, commonService servicecommon.Service, vpcService servicecommon.VPCServiceProvider) *NSXServiceAccountReconciler {
318318
log.Info("Initializing NSXServiceAccountController")
319319
nsxServiceAccountReconcile := &NSXServiceAccountReconciler{
320320
Client: mgr.GetClient(),
321321
Scheme: mgr.GetScheme(),
322322
Recorder: mgr.GetEventRecorderFor("nsxserviceaccount-controller"),
323323
}
324-
nsxServiceAccountService, err := nsxserviceaccount.InitializeNSXServiceAccount(commonService)
324+
nsxServiceAccountService, err := nsxserviceaccount.InitializeNSXServiceAccount(commonService, vpcService)
325325
if err != nil {
326326
log.Error(err, "Failed to initialize service", "controller", "NSXServiceAccount")
327327
os.Exit(1)

pkg/controllers/nsxserviceaccount/nsxserviceaccount_controller_test.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ import (
3636
"github.com/vmware-tanzu/nsx-operator/pkg/nsx"
3737
servicecommon "github.com/vmware-tanzu/nsx-operator/pkg/nsx/services/common"
3838
"github.com/vmware-tanzu/nsx-operator/pkg/nsx/services/nsxserviceaccount"
39+
"github.com/vmware-tanzu/nsx-operator/pkg/nsx/services/vpc"
3940
)
4041

4142
type fakeRecorder struct {
@@ -630,7 +631,8 @@ func TestNSXServiceAccountReconciler_StartController(t *testing.T) {
630631
return
631632
})
632633
defer patches.Reset()
633-
reconciler := NewNSXServiceAccountReconciler(mockMgr, service)
634+
vpcService := &vpc.VPCService{}
635+
reconciler := NewNSXServiceAccountReconciler(mockMgr, service, vpcService)
634636
err := reconciler.StartController(mockMgr, nil)
635637
assert.Nil(t, err)
636638
}

pkg/nsx/services/nsxserviceaccount/cluster.go

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -63,16 +63,20 @@ type NSXServiceAccountService struct {
6363
common.Service
6464
PrincipalIdentityStore *PrincipalIdentityStore
6565
ClusterControlPlaneStore *ClusterControlPlaneStore
66+
VPCService common.VPCServiceProvider
6667
}
6768

6869
// InitializeNSXServiceAccount sync NSX resources
69-
func InitializeNSXServiceAccount(service common.Service) (*NSXServiceAccountService, error) {
70+
func InitializeNSXServiceAccount(service common.Service, vpcService common.VPCServiceProvider) (*NSXServiceAccountService, error) {
7071
wg := sync.WaitGroup{}
7172
wgDone := make(chan bool)
7273
fatalErrors := make(chan error)
7374

7475
wg.Add(2)
75-
nsxServiceAccountService := &NSXServiceAccountService{Service: service}
76+
nsxServiceAccountService := &NSXServiceAccountService{
77+
Service: service,
78+
VPCService: vpcService,
79+
}
7680

7781
nsxServiceAccountService.SetUpStore()
7882
go nsxServiceAccountService.InitializeResourceStore(&wg, fatalErrors, common.ResourceTypePrincipalIdentity, nil, nsxServiceAccountService.PrincipalIdentityStore)
@@ -106,10 +110,17 @@ func (s *NSXServiceAccountService) SetUpStore() {
106110
func (s *NSXServiceAccountService) CreateOrUpdateNSXServiceAccount(ctx context.Context, obj *v1alpha1.NSXServiceAccount) error {
107111
clusterName := s.getClusterName(obj.Namespace, obj.Name)
108112
normalizedClusterName := util.NormalizeId(clusterName)
109-
// TODO: Use WCPConfig.NSXTProject as project when WCPConfig.EnableWCPVPCNetwork is true
110-
project := s.NSXConfig.CoeConfig.Cluster
111-
vpcName := obj.Namespace + "-default-vpc"
112-
vpcPath := fmt.Sprintf("/orgs/default/projects/%s/vpcs/%s", util.NormalizeId(project), vpcName)
113+
var vpcPath string
114+
if obj.Status.VPCPath != "" {
115+
vpcPath = obj.Status.VPCPath
116+
} else {
117+
vpcInfo := s.VPCService.ListVPCInfo(obj.Namespace)
118+
if len(vpcInfo) > 0 {
119+
vpcPath = vpcInfo[0].GetVPCPath()
120+
} else {
121+
return fmt.Errorf("failed to listVPCInfo for namespace %s", obj.Namespace)
122+
}
123+
}
113124

114125
// get proxy
115126
proxyEndpoints, err := s.getProxyEndpoints(ctx)

0 commit comments

Comments
 (0)