Skip to content

Commit

Permalink
Merge pull request #274 from jacobweinstock/fix-nil-pointer
Browse files Browse the repository at this point in the history
Handle potential nil pointer panic:

## Description

<!--- Please describe what this PR is going to change -->
It's possible for opts (*BMCOptions) in NewClientFunc to be nil and cause a panic.

## Why is this needed

<!--- Link to issue you have raised -->

Fixes: #

## How Has This Been Tested?
<!--- Please describe in detail how you tested your changes. -->
<!--- Include details of your testing environment, and the tests you ran to -->
<!--- see how your change affects other areas of the code, etc. -->


## How are existing users impacted? What migration steps/scripts do we need?

<!--- Fixes a bug, unblocks installation, removes a component of the stack etc -->
<!--- Requires a DB migration script, etc. -->


## Checklist:

I have:

- [ ] updated the documentation and/or roadmap (if required)
- [ ] added unit or e2e tests
- [ ] provided instructions on how to upgrade
  • Loading branch information
jacobweinstock authored Nov 24, 2024
2 parents e9d50f1 + 75ad68a commit 8a04fc5
Showing 1 changed file with 16 additions and 11 deletions.
27 changes: 16 additions & 11 deletions controller/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,6 @@ import (
"github.com/tinkerbell/rufio/api/v1alpha1"
)

// convert a slice of ProviderName to a slice of string.
func toStringSlice(p []v1alpha1.ProviderName) []string {
var s []string
for _, v := range p {
s = append(s, v.String())
}
return s
}

// ClientFunc defines a func that returns a bmclib.Client.
type ClientFunc func(ctx context.Context, log logr.Logger, hostIP, username, password string, opts *BMCOptions) (*bmclib.Client, error)

Expand All @@ -33,15 +24,20 @@ func NewClientFunc(timeout time.Duration) ClientFunc {
// Establishes a connection with the bmc with client.Open
// Returns a bmclib.Client.
return func(ctx context.Context, log logr.Logger, hostIP, username, password string, opts *BMCOptions) (*bmclib.Client, error) {
o := opts.Translate(hostIP)
var o []bmclib.Option
if opts != nil {
o = append(o, opts.Translate(hostIP)...)
}
log = log.WithValues("host", hostIP, "username", username)
o = append(o, bmclib.WithLogger(log))
client := bmclib.NewClient(hostIP, username, password, o...)

ctx, cancel := context.WithTimeout(ctx, timeout)
defer cancel()

client.Registry.Drivers = client.Registry.PreferProtocol(toStringSlice(opts.PreferredOrder)...)
if opts != nil && opts.ProviderOptions != nil && len(opts.PreferredOrder) > 0 {
client.Registry.Drivers = client.Registry.PreferProtocol(toStringSlice(opts.PreferredOrder)...)
}
if err := client.Open(ctx); err != nil {
md := client.GetMetadata()
log.Info("Failed to open connection to BMC", "error", err, "providersAttempted", md.ProvidersAttempted, "successfulProvider", md.SuccessfulOpenConns)
Expand Down Expand Up @@ -227,3 +223,12 @@ func toExperimentalOpts(e *v1alpha1.ExperimentalOpts) rpc.Experimental {

return opt
}

// convert a slice of ProviderName to a slice of string.
func toStringSlice(p []v1alpha1.ProviderName) []string {
var s []string
for _, v := range p {
s = append(s, v.String())
}
return s
}

0 comments on commit 8a04fc5

Please sign in to comment.