Skip to content

Commit

Permalink
Handle potential nil pointer panic:
Browse files Browse the repository at this point in the history
It possible for opts (*BMCOptions) in NewClientFunc
to be nil and cause a panic.

Signed-off-by: Jacob Weinstock <[email protected]>
  • Loading branch information
jacobweinstock committed Nov 24, 2024
1 parent e9d50f1 commit 75ad68a
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)...)
}

Check warning on line 30 in controller/client.go

View check run for this annotation

Codecov / codecov/patch

controller/client.go#L27-L30

Added lines #L27 - L30 were not covered by tests
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)...)
}

Check warning on line 40 in controller/client.go

View check run for this annotation

Codecov / codecov/patch

controller/client.go#L38-L40

Added lines #L38 - L40 were not covered by tests
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

Check warning on line 233 in controller/client.go

View check run for this annotation

Codecov / codecov/patch

controller/client.go#L228-L233

Added lines #L228 - L233 were not covered by tests
}

0 comments on commit 75ad68a

Please sign in to comment.