Skip to content

Commit

Permalink
Add to store after all entities realized
Browse files Browse the repository at this point in the history
Creating VPC need to check realized entities including vpc/lbs/
attachment.
Only add those entities to store after the realization successful
  • Loading branch information
TaoZou1 committed Dec 17, 2024
1 parent 652107c commit 6989fc1
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 23 deletions.
50 changes: 31 additions & 19 deletions pkg/nsx/services/vpc/vpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down Expand Up @@ -871,16 +878,21 @@ 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 {
if err != nil {
log.Error(err, "Failed to read LBS object after creating or updating", "LBS", createdLBS.Id)
return err
return nil, err
}

if newLBS.ConnectivityPath == nil {
err = fmt.Errorf("connectivity path is nil")
log.Error(err, "Failed to create or update LBS", "LBS", createdLBS.Id)
return nil, err
}
s.LbsStore.Add(&newLBS)

log.V(2).Info("Check LBS realization state", "LBS", *createdLBS.Id)
realizeService := realizestate.InitializeRealizeState(s.Service)
Expand All @@ -891,22 +903,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)
Expand All @@ -917,12 +929,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) {
Expand Down
13 changes: 9 additions & 4 deletions pkg/nsx/services/vpc/vpc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down Expand Up @@ -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{{
Expand Down

0 comments on commit 6989fc1

Please sign in to comment.