Skip to content

Commit 6989fc1

Browse files
committed
Add to store after all entities realized
Creating VPC need to check realized entities including vpc/lbs/ attachment. Only add those entities to store after the realization successful
1 parent 652107c commit 6989fc1

File tree

2 files changed

+40
-23
lines changed

2 files changed

+40
-23
lines changed

pkg/nsx/services/vpc/vpc.go

Lines changed: 31 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -814,17 +814,24 @@ func (s *VPCService) CreateOrUpdateVPC(ctx context.Context, obj *v1alpha1.Networ
814814
return nil, err
815815
}
816816

817-
if err := s.VpcStore.Add(&newVpc); err != nil {
817+
// Check LBS realization
818+
newLBS, err := s.checkLBSRealization(createdLBS, createdVpc, nc, *newVpc.Path)
819+
if err != nil {
818820
return nil, err
819821
}
820822

821-
// Check LBS realization
822-
if err := s.checkLBSRealization(createdLBS, createdVpc, nc, *newVpc.Path); err != nil {
823+
// Check VpcAttachment realization
824+
_, err = s.checkVpcAttachmentRealization(createdAttachment, createdVpc, nc, *newVpc.Path)
825+
if err != nil {
823826
return nil, err
824827
}
825828

826-
// Check VpcAttachment realization
827-
if err := s.checkVpcAttachmentRealization(createdAttachment, createdVpc, nc, *newVpc.Path); err != nil {
829+
// update the store
830+
if err := s.VpcStore.Add(&newVpc); err != nil {
831+
return nil, err
832+
}
833+
834+
if err := s.LbsStore.Add(newLBS); err != nil {
828835
return nil, err
829836
}
830837

@@ -871,16 +878,21 @@ func (s *VPCService) checkVPCRealizationState(createdVpc *model.Vpc, newVpcPath
871878
return nil
872879
}
873880

874-
func (s *VPCService) checkLBSRealization(createdLBS *model.LBService, createdVpc *model.Vpc, nc *common.VPCNetworkConfigInfo, newVpcPath string) error {
881+
func (s *VPCService) checkLBSRealization(createdLBS *model.LBService, createdVpc *model.Vpc, nc *common.VPCNetworkConfigInfo, newVpcPath string) (*model.LBService, error) {
875882
if createdLBS == nil {
876-
return nil
883+
return nil, nil
877884
}
878885
newLBS, err := s.NSXClient.VPCLBSClient.Get(nc.Org, nc.NSXProject, *createdVpc.Id, *createdLBS.Id)
879-
if err != nil || newLBS.ConnectivityPath == nil {
886+
if err != nil {
880887
log.Error(err, "Failed to read LBS object after creating or updating", "LBS", createdLBS.Id)
881-
return err
888+
return nil, err
889+
}
890+
891+
if newLBS.ConnectivityPath == nil {
892+
err = fmt.Errorf("connectivity path is nil")
893+
log.Error(err, "Failed to create or update LBS", "LBS", createdLBS.Id)
894+
return nil, err
882895
}
883-
s.LbsStore.Add(&newLBS)
884896

885897
log.V(2).Info("Check LBS realization state", "LBS", *createdLBS.Id)
886898
realizeService := realizestate.InitializeRealizeState(s.Service)
@@ -891,22 +903,22 @@ func (s *VPCService) checkLBSRealization(createdLBS *model.LBService, createdVpc
891903
// delete the nsx vpc object and re-create it in the next loop
892904
if err := s.DeleteVPC(newVpcPath); err != nil {
893905
log.Error(err, "Cleanup VPC failed", "VPC", *createdVpc.Id)
894-
return err
906+
return nil, err
895907
}
896908
}
897-
return err
909+
return nil, err
898910
}
899-
return nil
911+
return &newLBS, nil
900912
}
901913

902-
func (s *VPCService) checkVpcAttachmentRealization(createdAttachment *model.VpcAttachment, createdVpc *model.Vpc, nc *common.VPCNetworkConfigInfo, newVpcPath string) error {
914+
func (s *VPCService) checkVpcAttachmentRealization(createdAttachment *model.VpcAttachment, createdVpc *model.Vpc, nc *common.VPCNetworkConfigInfo, newVpcPath string) (*model.VpcAttachment, error) {
903915
if createdAttachment == nil {
904-
return nil
916+
return nil, nil
905917
}
906918
newAttachment, err := s.NSXClient.VpcAttachmentClient.Get(nc.Org, nc.NSXProject, *createdVpc.Id, *createdAttachment.Id)
907919
if err != nil || newAttachment.VpcConnectivityProfile == nil {
908920
log.Error(err, "Failed to read VPC attachment object after creating or updating", "VpcAttachment", createdAttachment.Id)
909-
return err
921+
return nil, err
910922
}
911923
log.V(2).Info("Check VPC attachment realization state", "VpcAttachment", *createdAttachment.Id)
912924
realizeService := realizestate.InitializeRealizeState(s.Service)
@@ -917,12 +929,12 @@ func (s *VPCService) checkVpcAttachmentRealization(createdAttachment *model.VpcA
917929
// delete the nsx vpc object and re-create it in the next loop
918930
if err := s.DeleteVPC(newVpcPath); err != nil {
919931
log.Error(err, "Cleanup VPC failed", "VPC", *createdVpc.Id)
920-
return err
932+
return nil, err
921933
}
922934
}
923-
return err
935+
return nil, err
924936
}
925-
return nil
937+
return &newAttachment, nil
926938
}
927939

928940
func (s *VPCService) GetGatewayConnectionTypeFromConnectionPath(connectionPath string) (string, error) {

pkg/nsx/services/vpc/vpc_test.go

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1750,7 +1750,12 @@ func createFakeVPCService(t *testing.T, objs []client.Object) *VPCService {
17501750
},
17511751
},
17521752
}
1753+
LbsStore := &LBSStore{ResourceStore: common.ResourceStore{
1754+
Indexer: cache.NewIndexer(keyFunc, cache.Indexers{}),
1755+
BindingType: model.LBServiceBindingType(),
1756+
}}
17531757
service.VpcStore = vpcStore
1758+
service.LbsStore = LbsStore
17541759
return service
17551760
}
17561761

@@ -1999,11 +2004,11 @@ func TestVPCService_CreateOrUpdateVPC(t *testing.T) {
19992004
patches.ApplyPrivateMethod(reflect.TypeOf(vpcService), "checkVPCRealizationState", func(_ *VPCService, createdVpc *model.Vpc, newVpcPath string) error {
20002005
return nil
20012006
})
2002-
patches.ApplyPrivateMethod(reflect.TypeOf(vpcService), "checkLBSRealization", func(_ *VPCService, createdLBS *model.LBService, createdVpc *model.Vpc, nc *common.VPCNetworkConfigInfo, newVpcPath string) error {
2003-
return nil
2007+
patches.ApplyPrivateMethod(reflect.TypeOf(vpcService), "checkLBSRealization", func(_ *VPCService, createdLBS *model.LBService, createdVpc *model.Vpc, nc *common.VPCNetworkConfigInfo, newVpcPath string) (*model.LBService, error) {
2008+
return &model.LBService{ConnectivityPath: common.String("default"), Id: common.String("1234")}, nil
20042009
})
2005-
patches.ApplyPrivateMethod(reflect.TypeOf(vpcService), "checkVpcAttachmentRealization", func(_ *VPCService, createdAttachment *model.VpcAttachment, createdVpc *model.Vpc, nc *common.VPCNetworkConfigInfo, newVpcPath string) error {
2006-
return nil
2010+
patches.ApplyPrivateMethod(reflect.TypeOf(vpcService), "checkVpcAttachmentRealization", func(_ *VPCService, createdAttachment *model.VpcAttachment, createdVpc *model.Vpc, nc *common.VPCNetworkConfigInfo, newVpcPath string) (*model.VpcAttachment, error) {
2011+
return &model.VpcAttachment{VpcConnectivityProfile: common.String("default")}, nil
20072012
})
20082013
vpcPath := "/vpc/1"
20092014
patches.ApplyMethodSeq(reflect.TypeOf(vpcService.NSXClient.VPCClient), "Get", []gomonkey.OutputCell{{

0 commit comments

Comments
 (0)