Skip to content

Commit

Permalink
Fix the panic caused by invalid memory address or nil pointer derefer…
Browse files Browse the repository at this point in the history
…ence when an unexpected NSX response is received. (vmware-tanzu#674)

Signed-off-by: Wenqi Qiu <[email protected]>
  • Loading branch information
wenqiq authored Aug 8, 2024
1 parent 9114e94 commit 3d12cf1
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 3 deletions.
2 changes: 1 addition & 1 deletion pkg/controllers/networkinfo/networkinfo_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ func (r *NetworkInfoReconciler) Reconcile(ctx context.Context, req ctrl.Request)
snatIP, path, cidr := "", "", ""
// currently, auto snat is not exposed, and use default value True
// checking autosnat to support future extension in vpc configuration
if *createdVpc.ServiceGateway.AutoSnat {
if createdVpc.ServiceGateway != nil && createdVpc.ServiceGateway.AutoSnat != nil && *createdVpc.ServiceGateway.AutoSnat {
snatIP, err = r.Service.GetDefaultSNATIP(*createdVpc)
if err != nil {
log.Error(err, "failed to read default SNAT ip from VPC", "VPC", createdVpc.Id)
Expand Down
5 changes: 5 additions & 0 deletions pkg/nsx/services/subnet/subnet.go
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,11 @@ func (service *SubnetService) GetSubnetStatus(subnet *model.VpcSubnet) ([]model.
log.Error(err, "no subnet status found")
return nil, err
}
if statusList.Results[0].NetworkAddress == nil || statusList.Results[0].GatewayAddress == nil {
err := fmt.Errorf("invalid status result: %+v", statusList.Results[0])
log.Error(err, "subnet status does not have network address or gateway address", "subnet.Id", subnet.Id)
return nil, err
}
return statusList.Results, nil
}

Expand Down
6 changes: 6 additions & 0 deletions pkg/nsx/services/subnetport/subnetport.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package subnetport
import (
"context"
"errors"
"fmt"
"sync"
"time"

Expand Down Expand Up @@ -248,6 +249,11 @@ func (service *SubnetPortService) GetGatewayPrefixForSubnetPort(obj *v1alpha1.Su
return "", -1, err
}
status := statusList.Results[0]
if status.GatewayAddress == nil {
err := fmt.Errorf("invalid status result: %+v", status)
log.Error(err, "subnet status does not have gateway address", "nsxSubnetPath", nsxSubnetPath)
return "", -1, err
}
gateway, err := util.RemoveIPPrefix(*status.GatewayAddress)
if err != nil {
return "", -1, err
Expand Down
10 changes: 8 additions & 2 deletions pkg/nsx/services/vpc/vpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,13 +179,13 @@ func InitializeVPC(service common.Service) (*VPCService, error) {
VPCService.VPCNSNetworkConfigStore = VPCNsNetworkConfigStore{
VPCNSNetworkConfigMap: make(map[string]string),
}
//initialize vpc store, lbs store and ip blocks store
// initialize vpc store, lbs store and ip blocks store
go VPCService.InitializeResourceStore(&wg, fatalErrors, common.ResourceTypeVpc, nil, VPCService.VpcStore)
wg.Add(1)
go VPCService.InitializeResourceStore(&wg, fatalErrors, common.ResourceTypeLBService, nil, VPCService.LbsStore)
go VPCService.InitializeResourceStore(&wg, fatalErrors, common.ResourceTypeIPBlock, nil, VPCService.IpblockStore)

//initalize avi rule related store
// initialize avi rule related store
if enableAviAllowRule {
VPCService.RuleStore = &AviRuleStore{ResourceStore: common.ResourceStore{
Indexer: cache.NewIndexer(keyFuncAVI, nil),
Expand Down Expand Up @@ -499,6 +499,12 @@ func (s *VPCService) GetAVISubnetInfo(vpc model.Vpc) (string, string, error) {
return "", "", err
}

if statusList.Results[0].NetworkAddress == nil {
err := fmt.Errorf("invalid status result: %+v", statusList.Results[0])
log.Error(err, "subnet status does not have network address", "Subnet", common.AVISubnetLBID)
return "", "", err
}

cidr := *statusList.Results[0].NetworkAddress
log.Info("read AVI subnet properties", "Path", path, "CIDR", cidr)
return path, cidr, nil
Expand Down

0 comments on commit 3d12cf1

Please sign in to comment.