Skip to content

Prevent nil pointer exception while reconciling w/ RPC provider #304

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

Merged
merged 2 commits into from
Mar 27, 2025
Merged
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion controller/machine.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ func (r *MachineReconciler) doReconcile(ctx context.Context, bm *v1alpha1.Machin
}
if bm.Spec.Connection.ProviderOptions != nil && bm.Spec.Connection.ProviderOptions.RPC != nil {
opts.ProviderOptions = bm.Spec.Connection.ProviderOptions
if len(bm.Spec.Connection.ProviderOptions.RPC.HMAC.Secrets) > 0 {
if bm.Spec.Connection.ProviderOptions.RPC.HMAC != nil && len(bm.Spec.Connection.ProviderOptions.RPC.HMAC.Secrets) > 0 {
se, err := retrieveHMACSecrets(ctx, r.client, bm.Spec.Connection.ProviderOptions.RPC.HMAC.Secrets)
if err != nil {
return ctrl.Result{}, fmt.Errorf("unable to get hmac secrets: %w", err)
Expand Down
33 changes: 22 additions & 11 deletions controller/machine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,12 @@ func TestMachineReconcile(t *testing.T) {
machine: createMachineWithRPC(createHMACSecret()),
},

"success power on with RPC provider w/o secrets": {
provider: &testProvider{Powerstate: "on", Proto: "rpc"},
secret: createSecret(),
machine: createMachineWithRPC(nil),
},

"fail to find secret with RPC provider": {
provider: &testProvider{Powerstate: "on", Proto: "rpc"},
secret: createHMACSecret(),
Expand Down Expand Up @@ -149,7 +155,7 @@ func TestMachineReconcile(t *testing.T) {
}

func createMachineWithRPC(secret *corev1.Secret) *v1alpha1.Machine {
return &v1alpha1.Machine{
machine := &v1alpha1.Machine{
ObjectMeta: metav1.ObjectMeta{
Name: "test-bm",
Namespace: "test-namespace",
Expand All @@ -161,21 +167,26 @@ func createMachineWithRPC(secret *corev1.Secret) *v1alpha1.Machine {
ProviderOptions: &v1alpha1.ProviderOptions{
RPC: &v1alpha1.RPCOptions{
ConsumerURL: "http://127.0.0.1:7777",
HMAC: &v1alpha1.HMACOpts{
Secrets: v1alpha1.HMACSecrets{
"sha256": []corev1.SecretReference{
{
Name: secret.Name,
Namespace: secret.Namespace,
},
},
},
},
},
},
},
},
}

if secret != nil {
machine.Spec.Connection.ProviderOptions.RPC.HMAC = &v1alpha1.HMACOpts{
Secrets: v1alpha1.HMACSecrets{
"sha256": []corev1.SecretReference{
{
Name: secret.Name,
Namespace: secret.Namespace,
},
},
},
}
}

return machine
}

func createMachine() *v1alpha1.Machine {
Expand Down
2 changes: 1 addition & 1 deletion controller/task.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ func (r *TaskReconciler) doReconcile(ctx context.Context, task *v1alpha1.Task, t
}
if task.Spec.Connection.ProviderOptions != nil && task.Spec.Connection.ProviderOptions.RPC != nil {
opts.ProviderOptions = task.Spec.Connection.ProviderOptions
if len(task.Spec.Connection.ProviderOptions.RPC.HMAC.Secrets) > 0 {
if task.Spec.Connection.ProviderOptions.RPC.HMAC != nil && len(task.Spec.Connection.ProviderOptions.RPC.HMAC.Secrets) > 0 {
se, err := retrieveHMACSecrets(ctx, r.client, task.Spec.Connection.ProviderOptions.RPC.HMAC.Secrets)
if err != nil {
return ctrl.Result{}, fmt.Errorf("unable to get hmac secrets: %w", err)
Expand Down
42 changes: 27 additions & 15 deletions controller/task_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,12 @@ func TestTaskReconcile(t *testing.T) {
task: createTaskWithRPC("PowerOn", getAction("PowerOn"), createHMACSecret()),
},

"success power on with RPC provider w/o secrets": {
taskName: "PowerOn",
action: getAction("PowerOn"),
provider: &testProvider{Powerstate: "on", PowerSetOK: true, Proto: "rpc"},
},

"failure on bmc open": {
taskName: "PowerOn", action: getAction("PowerOn"),
provider: &testProvider{ErrOpen: errors.New("failed to open")},
Expand Down Expand Up @@ -259,7 +265,7 @@ func createTask(name string, action v1alpha1.Action, secret *corev1.Secret) *v1a
}

func createTaskWithRPC(name string, action v1alpha1.Action, secret *corev1.Secret) *v1alpha1.Task {
return &v1alpha1.Task{
machine := &v1alpha1.Task{
ObjectMeta: metav1.ObjectMeta{
Name: name,
Namespace: "default",
Expand All @@ -269,26 +275,32 @@ func createTaskWithRPC(name string, action v1alpha1.Action, secret *corev1.Secre
Connection: v1alpha1.Connection{
Host: "host",
Port: 22,
AuthSecretRef: corev1.SecretReference{
Name: secret.Name,
Namespace: secret.Namespace,
},
ProviderOptions: &v1alpha1.ProviderOptions{
RPC: &v1alpha1.RPCOptions{
ConsumerURL: "http://127.0.0.1:7777",
HMAC: &v1alpha1.HMACOpts{
Secrets: v1alpha1.HMACSecrets{
"sha256": []corev1.SecretReference{
{
Name: secret.Name,
Namespace: secret.Namespace,
},
},
},
},
},
},
},
},
}

if secret != nil {
machine.Spec.Connection.AuthSecretRef = corev1.SecretReference{
Name: secret.Name,
Namespace: secret.Namespace,
}

machine.Spec.Connection.ProviderOptions.RPC.HMAC = &v1alpha1.HMACOpts{
Secrets: v1alpha1.HMACSecrets{
"sha256": []corev1.SecretReference{
{
Name: secret.Name,
Namespace: secret.Namespace,
},
},
},
}
}

return machine
}
Loading