diff --git a/pkg/nsx/services/vpc/vpc.go b/pkg/nsx/services/vpc/vpc.go index e980e7d44..efd0fb9a7 100644 --- a/pkg/nsx/services/vpc/vpc.go +++ b/pkg/nsx/services/vpc/vpc.go @@ -814,17 +814,24 @@ func (s *VPCService) CreateOrUpdateVPC(ctx context.Context, obj *v1alpha1.Networ return nil, err } - if err := s.VpcStore.Add(&newVpc); err != nil { + // Check LBS realization + newLBS, err := s.checkLBSRealization(createdLBS, createdVpc, nc, *newVpc.Path) + if err != nil { return nil, err } - // Check LBS realization - if err := s.checkLBSRealization(createdLBS, createdVpc, nc, *newVpc.Path); err != nil { + // Check VpcAttachment realization + _, err = s.checkVpcAttachmentRealization(createdAttachment, createdVpc, nc, *newVpc.Path) + if err != nil { return nil, err } - // Check VpcAttachment realization - if err := s.checkVpcAttachmentRealization(createdAttachment, createdVpc, nc, *newVpc.Path); err != nil { + // update the store + if err := s.VpcStore.Add(&newVpc); err != nil { + return nil, err + } + + if err := s.LbsStore.Add(newLBS); err != nil { return nil, err } @@ -871,16 +878,15 @@ func (s *VPCService) checkVPCRealizationState(createdVpc *model.Vpc, newVpcPath return nil } -func (s *VPCService) checkLBSRealization(createdLBS *model.LBService, createdVpc *model.Vpc, nc *common.VPCNetworkConfigInfo, newVpcPath string) error { +func (s *VPCService) checkLBSRealization(createdLBS *model.LBService, createdVpc *model.Vpc, nc *common.VPCNetworkConfigInfo, newVpcPath string) (*model.LBService, error) { if createdLBS == nil { - return nil + return nil, nil } newLBS, err := s.NSXClient.VPCLBSClient.Get(nc.Org, nc.NSXProject, *createdVpc.Id, *createdLBS.Id) if err != nil || newLBS.ConnectivityPath == nil { log.Error(err, "Failed to read LBS object after creating or updating", "LBS", createdLBS.Id) - return err + return &newLBS, err } - s.LbsStore.Add(&newLBS) log.V(2).Info("Check LBS realization state", "LBS", *createdLBS.Id) realizeService := realizestate.InitializeRealizeState(s.Service) @@ -891,22 +897,22 @@ func (s *VPCService) checkLBSRealization(createdLBS *model.LBService, createdVpc // delete the nsx vpc object and re-create it in the next loop if err := s.DeleteVPC(newVpcPath); err != nil { log.Error(err, "Cleanup VPC failed", "VPC", *createdVpc.Id) - return err + return nil, err } } - return err + return nil, err } - return nil + return &newLBS, nil } -func (s *VPCService) checkVpcAttachmentRealization(createdAttachment *model.VpcAttachment, createdVpc *model.Vpc, nc *common.VPCNetworkConfigInfo, newVpcPath string) error { +func (s *VPCService) checkVpcAttachmentRealization(createdAttachment *model.VpcAttachment, createdVpc *model.Vpc, nc *common.VPCNetworkConfigInfo, newVpcPath string) (*model.VpcAttachment, error) { if createdAttachment == nil { - return nil + return nil, nil } newAttachment, err := s.NSXClient.VpcAttachmentClient.Get(nc.Org, nc.NSXProject, *createdVpc.Id, *createdAttachment.Id) if err != nil || newAttachment.VpcConnectivityProfile == nil { log.Error(err, "Failed to read VPC attachment object after creating or updating", "VpcAttachment", createdAttachment.Id) - return err + return nil, err } log.V(2).Info("Check VPC attachment realization state", "VpcAttachment", *createdAttachment.Id) realizeService := realizestate.InitializeRealizeState(s.Service) @@ -917,12 +923,12 @@ func (s *VPCService) checkVpcAttachmentRealization(createdAttachment *model.VpcA // delete the nsx vpc object and re-create it in the next loop if err := s.DeleteVPC(newVpcPath); err != nil { log.Error(err, "Cleanup VPC failed", "VPC", *createdVpc.Id) - return err + return nil, err } } - return err + return nil, err } - return nil + return &newAttachment, nil } func (s *VPCService) GetGatewayConnectionTypeFromConnectionPath(connectionPath string) (string, error) { diff --git a/pkg/nsx/services/vpc/vpc_test.go b/pkg/nsx/services/vpc/vpc_test.go index 350e416ec..d6e9bba6b 100644 --- a/pkg/nsx/services/vpc/vpc_test.go +++ b/pkg/nsx/services/vpc/vpc_test.go @@ -1750,7 +1750,12 @@ func createFakeVPCService(t *testing.T, objs []client.Object) *VPCService { }, }, } + LbsStore := &LBSStore{ResourceStore: common.ResourceStore{ + Indexer: cache.NewIndexer(keyFunc, cache.Indexers{}), + BindingType: model.LBServiceBindingType(), + }} service.VpcStore = vpcStore + service.LbsStore = LbsStore return service } @@ -1999,11 +2004,11 @@ func TestVPCService_CreateOrUpdateVPC(t *testing.T) { patches.ApplyPrivateMethod(reflect.TypeOf(vpcService), "checkVPCRealizationState", func(_ *VPCService, createdVpc *model.Vpc, newVpcPath string) error { return nil }) - patches.ApplyPrivateMethod(reflect.TypeOf(vpcService), "checkLBSRealization", func(_ *VPCService, createdLBS *model.LBService, createdVpc *model.Vpc, nc *common.VPCNetworkConfigInfo, newVpcPath string) error { - return nil + patches.ApplyPrivateMethod(reflect.TypeOf(vpcService), "checkLBSRealization", func(_ *VPCService, createdLBS *model.LBService, createdVpc *model.Vpc, nc *common.VPCNetworkConfigInfo, newVpcPath string) (*model.LBService, error) { + return &model.LBService{ConnectivityPath: common.String("default"), Id: common.String("1234")}, nil }) - patches.ApplyPrivateMethod(reflect.TypeOf(vpcService), "checkVpcAttachmentRealization", func(_ *VPCService, createdAttachment *model.VpcAttachment, createdVpc *model.Vpc, nc *common.VPCNetworkConfigInfo, newVpcPath string) error { - return nil + patches.ApplyPrivateMethod(reflect.TypeOf(vpcService), "checkVpcAttachmentRealization", func(_ *VPCService, createdAttachment *model.VpcAttachment, createdVpc *model.Vpc, nc *common.VPCNetworkConfigInfo, newVpcPath string) (*model.VpcAttachment, error) { + return &model.VpcAttachment{VpcConnectivityProfile: common.String("default")}, nil }) vpcPath := "/vpc/1" patches.ApplyMethodSeq(reflect.TypeOf(vpcService.NSXClient.VPCClient), "Get", []gomonkey.OutputCell{{