Skip to content

Commit 4be9bd7

Browse files
authored
fix: add more nil checks and tests (#464)
1 parent 3aa8888 commit 4be9bd7

File tree

5 files changed

+49
-11
lines changed

5 files changed

+49
-11
lines changed

api/v1beta2/ocicluster_webhook.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -833,7 +833,7 @@ func (c *OCICluster) GetControlPlaneEndpointDefaultEgressRules() []EgressSecurit
833833

834834
func (c *OCICluster) GetControlPlaneEndpointSubnet() *Subnet {
835835
for _, subnet := range c.Spec.NetworkSpec.Vcn.Subnets {
836-
if subnet.Role == ControlPlaneEndpointRole {
836+
if subnet != nil && subnet.Role == ControlPlaneEndpointRole {
837837
return subnet
838838
}
839839
}
@@ -842,7 +842,7 @@ func (c *OCICluster) GetControlPlaneEndpointSubnet() *Subnet {
842842

843843
func (c *OCICluster) GetControlPlaneMachineSubnet() *Subnet {
844844
for _, subnet := range c.Spec.NetworkSpec.Vcn.Subnets {
845-
if subnet.Role == ControlPlaneRole {
845+
if subnet != nil && subnet.Role == ControlPlaneRole {
846846
return subnet
847847
}
848848
}
@@ -851,7 +851,7 @@ func (c *OCICluster) GetControlPlaneMachineSubnet() *Subnet {
851851

852852
func (c *OCICluster) GetServiceLoadBalancerSubnet() *Subnet {
853853
for _, subnet := range c.Spec.NetworkSpec.Vcn.Subnets {
854-
if subnet.Role == ServiceLoadBalancerRole {
854+
if subnet != nil && subnet.Role == ServiceLoadBalancerRole {
855855
return subnet
856856
}
857857
}
@@ -861,7 +861,7 @@ func (c *OCICluster) GetServiceLoadBalancerSubnet() *Subnet {
861861
func (c *OCICluster) GetNodeSubnet() []*Subnet {
862862
var nodeSubnets []*Subnet
863863
for _, subnet := range c.Spec.NetworkSpec.Vcn.Subnets {
864-
if subnet.Role == WorkerRole {
864+
if subnet != nil && subnet.Role == WorkerRole {
865865
nodeSubnets = append(nodeSubnets, subnet)
866866
}
867867
}
@@ -870,7 +870,7 @@ func (c *OCICluster) GetNodeSubnet() []*Subnet {
870870

871871
func (c *OCICluster) IsNSGExitsByRole(role Role) bool {
872872
for _, nsg := range c.Spec.NetworkSpec.Vcn.NetworkSecurityGroup.List {
873-
if role == nsg.Role {
873+
if nsg != nil && role == nsg.Role {
874874
return true
875875
}
876876
}
@@ -879,7 +879,7 @@ func (c *OCICluster) IsNSGExitsByRole(role Role) bool {
879879

880880
func (c *OCICluster) IsSecurityListExitsByRole(role Role) bool {
881881
for _, subnet := range c.Spec.NetworkSpec.Vcn.Subnets {
882-
if role == subnet.Role {
882+
if subnet != nil && role == subnet.Role {
883883
if subnet.SecurityList != nil {
884884
return true
885885
}

cloud/scope/drg_vcn_attachment_reconciler.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@ func (s *ClusterScope) ReconcileDRGVCNAttachment(ctx context.Context) error {
3232
return nil
3333
}
3434

35+
if s.getDRG() == nil {
36+
return errors.New("DRG is nil")
37+
}
38+
3539
attachment, err := s.GetDRGAttachment(ctx)
3640
if err != nil {
3741
return err
@@ -65,6 +69,9 @@ func (s *ClusterScope) ReconcileDRGVCNAttachment(ctx context.Context) error {
6569

6670
// nolint:nilnil
6771
func (s *ClusterScope) GetDRGAttachment(ctx context.Context) (*core.DrgAttachment, error) {
72+
if s.getDRG() == nil {
73+
return nil, errors.New("DRG is nil")
74+
}
6875
if s.getDRG().VcnAttachmentId != nil {
6976
response, err := s.VCNClient.GetDrgAttachment(ctx, core.GetDrgAttachmentRequest{
7077
DrgAttachmentId: s.getDRG().VcnAttachmentId,

cloud/scope/nsg_reconciler.go

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,16 +37,21 @@ func (s *ClusterScope) ReconcileNSG(ctx context.Context) error {
3737
}
3838
desiredNSGs := s.OCIClusterAccessor.GetNetworkSpec().Vcn.NetworkSecurityGroup
3939
for _, desiredNSG := range desiredNSGs.List {
40-
nsg, err := s.GetNSG(ctx, *desiredNSG)
40+
if desiredNSG == nil {
41+
s.Logger.Info("Skipping nil NSG pointer in spec")
42+
continue
43+
}
44+
desiredNSGPtr := *desiredNSG
45+
nsg, err := s.GetNSG(ctx, desiredNSGPtr)
4146
if err != nil {
4247
s.Logger.Error(err, "error to get nsg")
4348
return err
4449
}
4550
if nsg != nil {
4651
nsgOCID := nsg.Id
4752
desiredNSG.ID = nsgOCID
48-
if !s.IsNSGEqual(nsg, *desiredNSG) {
49-
err = s.UpdateNSG(ctx, *desiredNSG)
53+
if !s.IsNSGEqual(nsg, desiredNSGPtr) {
54+
err = s.UpdateNSG(ctx, desiredNSGPtr)
5055
if err != nil {
5156
return err
5257
}
@@ -55,7 +60,7 @@ func (s *ClusterScope) ReconcileNSG(ctx context.Context) error {
5560
continue
5661
}
5762
s.Logger.Info("Creating the network security list")
58-
nsgID, err := s.CreateNSG(ctx, *desiredNSG)
63+
nsgID, err := s.CreateNSG(ctx, desiredNSGPtr)
5964
if err != nil {
6065
return err
6166
}
@@ -64,6 +69,10 @@ func (s *ClusterScope) ReconcileNSG(ctx context.Context) error {
6469

6570
}
6671
for _, desiredNSG := range desiredNSGs.List {
72+
if desiredNSG == nil {
73+
s.Logger.Info("Skipping nil NSG pointer in spec")
74+
continue
75+
}
6776
s.adjustNSGRulesSpec(desiredNSG, desiredNSGs.List)
6877
isNSGUpdated, err := s.UpdateNSGSecurityRulesIfNeeded(ctx, *desiredNSG, desiredNSG.ID)
6978
if err != nil {

cloud/scope/nsg_reconciler_test.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -827,6 +827,23 @@ func TestClusterScope_ReconcileNSG(t *testing.T) {
827827
wantErr: true,
828828
expectedError: "failed add nsg security rules: some error",
829829
},
830+
{
831+
name: "nil NSG in list",
832+
spec: infrastructurev1beta2.OCIClusterSpec{
833+
NetworkSpec: infrastructurev1beta2.NetworkSpec{
834+
Vcn: infrastructurev1beta2.VCN{
835+
ID: common.String("vcn"),
836+
NetworkSecurityGroup: infrastructurev1beta2.NetworkSecurityGroup{
837+
List: []*infrastructurev1beta2.NSG{nil},
838+
},
839+
},
840+
},
841+
},
842+
wantErr: false,
843+
testSpecificSetup: func(clusterScope *ClusterScope, nlbClient *mock_vcn.MockClient) {
844+
// nothing should be called
845+
},
846+
},
830847
}
831848
l := log.FromContext(context.Background())
832849
for _, tt := range tests {

cloud/scope/vcn_reconciler.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,5 +180,10 @@ func (s *ClusterScope) DeleteVCN(ctx context.Context) error {
180180
}
181181

182182
func (s *ClusterScope) getVcnId() *string {
183-
return s.OCIClusterAccessor.GetNetworkSpec().Vcn.ID
183+
id := s.OCIClusterAccessor.GetNetworkSpec().Vcn.ID
184+
if id == nil {
185+
s.Logger.Info("VCN ID is nil")
186+
187+
}
188+
return id
184189
}

0 commit comments

Comments
 (0)