Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle potential nil pointer panic: #274

Merged
merged 1 commit into from
Nov 24, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@
"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 @@
// 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 @@

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
}
Loading