Skip to content

Commit d75d8ce

Browse files
authored
fix: Dereferencing on nil pointers panic fix (#446)
* fixed SeclistID nil pointer deference * Fixed some panics on nil pointer deference * modified test with correct error string
1 parent 6c8e028 commit d75d8ce

File tree

3 files changed

+16
-7
lines changed

3 files changed

+16
-7
lines changed

cloud/scope/managed_machine_pool.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import (
2828
"github.com/go-logr/logr"
2929
infrastructurev1beta2 "github.com/oracle/cluster-api-provider-oci/api/v1beta2"
3030
"github.com/oracle/cluster-api-provider-oci/cloud/ociutil"
31+
"github.com/oracle/cluster-api-provider-oci/cloud/ociutil/ptr"
3132
"github.com/oracle/cluster-api-provider-oci/cloud/services/computemanagement"
3233
"github.com/oracle/cluster-api-provider-oci/cloud/services/containerengine"
3334
expinfra1 "github.com/oracle/cluster-api-provider-oci/exp/api/v1beta2"
@@ -555,8 +556,8 @@ func (m *ManagedMachinePoolScope) buildPlacementConfig(configs []expinfra1.Place
555556
for _, config := range configs {
556557
subnetId := m.getWorkerMachineSubnet(config.SubnetName)
557558
if subnetId == nil {
558-
return nil, errors.New(fmt.Sprintf("worker subnet with name %s is not present in spec",
559-
*config.SubnetName))
559+
return nil, errors.New(fmt.Sprintf("worker subnet %s is not present in placementConfigs spec",
560+
ptr.ToString(config.SubnetName)))
560561
}
561562
placementConfigs = append(placementConfigs, oke.NodePoolPlacementConfigDetails{
562563
AvailabilityDomain: config.AvailabilityDomain,
@@ -581,7 +582,7 @@ func (m *ManagedMachinePoolScope) getInitialNodeKeyValuePairs() []oke.KeyValue {
581582

582583
func (m *ManagedMachinePoolScope) getWorkerMachineSubnet(name *string) *string {
583584
for _, subnet := range m.OCIManagedCluster.Spec.NetworkSpec.Vcn.Subnets {
584-
if subnet != nil && subnet.ID != nil && subnet.Name == *name {
585+
if subnet != nil && subnet.ID != nil && subnet.Name == ptr.ToString(name) {
585586
return subnet.ID
586587
}
587588
}

cloud/scope/managed_machine_pool_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -789,7 +789,7 @@ func TestManagedMachinePoolCreate(t *testing.T) {
789789
{
790790
name: "nodepool no worker subnets",
791791
errorExpected: true,
792-
matchError: errors.New(fmt.Sprintf("worker subnet with name %s is not present in spec",
792+
matchError: errors.New(fmt.Sprintf("worker subnet %s is not present in placementConfigs spec",
793793
"worker-subnet-invalid")),
794794
testSpecificSetup: func(cs *ManagedMachinePoolScope, okeClient *mock_containerengine.MockClient) {
795795
ms.OCIManagedCluster.Spec.OCIResourceIdentifier = "resource_uid"

cloud/scope/subnet_reconciler.go

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,11 @@ func (s *ClusterScope) UpdateSubnet(ctx context.Context, spec infrastructurev1be
175175
CidrBlock: common.String(spec.CIDR),
176176
}
177177
if spec.SecurityList != nil {
178+
if spec.SecurityList.ID == nil {
179+
err := errors.New("SecurityListID required")
180+
s.Logger.Error(err, "failed updating subnet")
181+
return err
182+
}
178183
updateSubnetDetails.SecurityListIds = []string{*spec.SecurityList.ID}
179184
}
180185
subnetResponse, err := s.VCNClient.UpdateSubnet(ctx, core.UpdateSubnetRequest{
@@ -297,10 +302,13 @@ func (s *ClusterScope) IsSubnetsEqual(actual *core.Subnet, desired infrastructur
297302
if desired.CIDR != *actual.CidrBlock {
298303
return false
299304
}
300-
if desired.SecurityList != nil {
301-
if *desired.SecurityList.ID != actual.SecurityListIds[0] {
302-
return false
305+
if desired.SecurityList != nil && desired.SecurityList.ID != nil {
306+
for _, securityListId := range actual.SecurityListIds {
307+
if *desired.SecurityList.ID == securityListId {
308+
return true
309+
}
303310
}
311+
return false
304312
}
305313
return true
306314
}

0 commit comments

Comments
 (0)