Skip to content

Commit 7c3350a

Browse files
committed
fix: handle some more nil checks and possible panics
1 parent 093e54e commit 7c3350a

File tree

9 files changed

+66
-10
lines changed

9 files changed

+66
-10
lines changed

cloud/ociutil/ptr/from_ptr.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,17 @@ func ToString(p *string) (v string) {
1313
return *p
1414
}
1515

16+
// ToBool returns bool value dereferenced if the passed
17+
// in pointer was not nil. Returns a bool zero value (false) if the
18+
// pointer was nil.
19+
func ToBool(p *bool) (v bool) {
20+
if p == nil {
21+
return v
22+
}
23+
24+
return *p
25+
}
26+
1627
// ToStringSlice returns a slice of string values, that are
1728
// dereferenced if the passed in pointer was not nil. Returns a string
1829
// zero value if the pointer was nil.

cloud/ociutil/ptr/from_ptr_test.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,38 @@ func TestToString(t *testing.T) {
3333
}
3434
}
3535

36+
func TestToBool(t *testing.T) {
37+
tests := []struct {
38+
name string
39+
input *bool
40+
want bool
41+
}{
42+
{
43+
name: "nil pointer",
44+
input: nil,
45+
want: false,
46+
},
47+
{
48+
name: "pointer to true",
49+
input: func() *bool { b := true; return &b }(),
50+
want: true,
51+
},
52+
{
53+
name: "pointer to false",
54+
input: func() *bool { b := false; return &b }(),
55+
want: false,
56+
},
57+
}
58+
59+
for _, tt := range tests {
60+
t.Run(tt.name, func(t *testing.T) {
61+
if got := ToBool(tt.input); got != tt.want {
62+
t.Errorf("ToBool() = %v, want %v", got, tt.want)
63+
}
64+
})
65+
}
66+
}
67+
3668
func TestToStringSlice(t *testing.T) {
3769
tests := []struct {
3870
name string

cloud/scope/load_balancer_reconciler.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,7 @@ func (s *ClusterScope) getLoadbalancerIp(lb loadbalancer.LoadBalancer) (*string,
230230
if len(lb.IpAddresses) < 1 {
231231
return nil, errors.New("lb does not have valid ip addresses")
232232
}
233-
if *lb.IsPrivate {
233+
if ptr.ToBool(lb.IsPrivate) {
234234
lbIp = lb.IpAddresses[0].IpAddress
235235
} else {
236236
for _, ip := range lb.IpAddresses {

cloud/scope/machine.go

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -221,8 +221,14 @@ func (m *MachineScope) GetOrCreateMachine(ctx context.Context) (*core.Instance,
221221
m.Logger.Error(err, "Failure domain should be a value between 1 and 3")
222222
return nil, err
223223
}
224-
faultDomain = m.OCIClusterAccessor.GetFailureDomains()[*failureDomain].Attributes[FaultDomain]
225-
availabilityDomain = m.OCIClusterAccessor.GetFailureDomains()[*failureDomain].Attributes[AvailabilityDomain]
224+
fd, exists := m.OCIClusterAccessor.GetFailureDomains()[*failureDomain]
225+
if !exists {
226+
err = errors.New("failure domain not found in cluster failure domains")
227+
m.Logger.Error(err, "Failure domain not found", "failure-domain", *failureDomain)
228+
return nil, err
229+
}
230+
faultDomain = fd.Attributes[FaultDomain]
231+
availabilityDomain = fd.Attributes[AvailabilityDomain]
226232
} else {
227233
randomFailureDomain, err := rand.Int(rand.Reader, big.NewInt(3))
228234
if err != nil {
@@ -231,7 +237,13 @@ func (m *MachineScope) GetOrCreateMachine(ctx context.Context) (*core.Instance,
231237
}
232238
// the random number generated is between zero and two, whereas we need a number between one and three
233239
failureDomain = common.String(strconv.Itoa(int(randomFailureDomain.Int64()) + 1))
234-
availabilityDomain = m.OCIClusterAccessor.GetFailureDomains()[*failureDomain].Attributes[AvailabilityDomain]
240+
fd, exists := m.OCIClusterAccessor.GetFailureDomains()[*failureDomain]
241+
if !exists {
242+
err = errors.New("failure domain not found in cluster failure domains")
243+
m.Logger.Error(err, "Failure domain not found", "failure-domain", *failureDomain)
244+
return nil, err
245+
}
246+
availabilityDomain = fd.Attributes[AvailabilityDomain]
235247
}
236248

237249
metadata := m.OCIMachine.Spec.Metadata

cloud/scope/managed_machine_pool.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -345,7 +345,7 @@ func (m *ManagedMachinePoolScope) CreateNodePool(ctx context.Context) (*oke.Node
345345
resources := wrResponse.Resources
346346
var nodePoolId *string
347347
for _, resource := range resources {
348-
if *resource.EntityType == "nodepool" {
348+
if ptr.ToString(resource.EntityType) == "nodepool" {
349349
nodePoolId = resource.Identifier
350350
}
351351
}

cloud/scope/network_load_balancer_reconciler.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,7 @@ func (s *ClusterScope) getNetworkLoadbalancerIp(nlb networkloadbalancer.NetworkL
246246
if len(nlb.IpAddresses) < 1 {
247247
return nil, errors.New("nlb does not have valid ip addresses")
248248
}
249-
if *nlb.IsPrivate {
249+
if ptr.ToBool(nlb.IsPrivate) {
250250
nlbIp = nlb.IpAddresses[0].IpAddress
251251
} else {
252252
for _, ip := range nlb.IpAddresses {

cloud/scope/nsg_reconciler.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ func (s *ClusterScope) GetNSG(ctx context.Context, spec infrastructurev1beta2.NS
131131
return nil, errors.Wrap(err, "failed to list network security groups")
132132
}
133133
for _, nsg := range nsgs.Items {
134-
if &nsg != nil && s.IsResourceCreatedByClusterAPI(nsg.FreeformTags) {
134+
if s.IsResourceCreatedByClusterAPI(nsg.FreeformTags) {
135135
return &nsg, nil
136136
}
137137
}

cloud/scope/route_table_reconciler.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,8 @@ func (s *ClusterScope) CreateRouteTable(ctx context.Context, routeTableType stri
163163
}
164164
resp, err := s.VCNClient.GetVcn(ctx, core.GetVcnRequest{VcnId: s.getVcnId()})
165165
if err != nil {
166-
panic(err)
166+
s.Logger.Error(err, "failed to get VCN")
167+
return nil, errors.Wrap(err, "failed to get VCN for route table creation")
167168
}
168169
if resp.Vcn.Ipv6CidrBlocks != nil {
169170
routeRules = append(routeRules, core.RouteRule{

cloud/scope/subnet_reconciler.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,15 +119,15 @@ func (s *ClusterScope) CreateSubnet(ctx context.Context, spec infrastructurev1be
119119
var ipv6subnetCIDR_Ptr *string
120120

121121
// Constructing IPv6 Subnet CIDR
122-
if resp.Vcn.Ipv6CidrBlocks != nil {
122+
if len(resp.Vcn.Ipv6CidrBlocks) > 0 {
123123

124124
// VCNs can have multiple IPv6 CIDR Blocks, and the CIDR block with IPv6 GUA Allocated by Oracle is the first (index 0) in the list
125125
vcnCIDR := resp.Vcn.Ipv6CidrBlocks[0]
126126

127127
// Split CIDR block into hextets
128128
ip, _, err := net.ParseCIDR(vcnCIDR)
129129
if err != nil {
130-
panic(err)
130+
return nil, errors.Wrap(err, "failed to parse IPv6 CIDR block")
131131
}
132132
hextets := strings.Split(ip.String(), ":")
133133

0 commit comments

Comments
 (0)