Skip to content

Commit 75fb842

Browse files
authored
Prevent nil pointer exception while reconciling w/ RPC provider (#304)
2 parents 7969e5f + a0365e7 commit 75fb842

File tree

4 files changed

+51
-28
lines changed

4 files changed

+51
-28
lines changed

controller/machine.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ func (r *MachineReconciler) doReconcile(ctx context.Context, bm *v1alpha1.Machin
9898
}
9999
if bm.Spec.Connection.ProviderOptions != nil && bm.Spec.Connection.ProviderOptions.RPC != nil {
100100
opts.ProviderOptions = bm.Spec.Connection.ProviderOptions
101-
if len(bm.Spec.Connection.ProviderOptions.RPC.HMAC.Secrets) > 0 {
101+
if bm.Spec.Connection.ProviderOptions.RPC.HMAC != nil && len(bm.Spec.Connection.ProviderOptions.RPC.HMAC.Secrets) > 0 {
102102
se, err := retrieveHMACSecrets(ctx, r.client, bm.Spec.Connection.ProviderOptions.RPC.HMAC.Secrets)
103103
if err != nil {
104104
return ctrl.Result{}, fmt.Errorf("unable to get hmac secrets: %w", err)

controller/machine_test.go

+22-11
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,12 @@ func TestMachineReconcile(t *testing.T) {
3838
machine: createMachineWithRPC(createHMACSecret()),
3939
},
4040

41+
"success power on with RPC provider w/o secrets": {
42+
provider: &testProvider{Powerstate: "on", Proto: "rpc"},
43+
secret: createSecret(),
44+
machine: createMachineWithRPC(nil),
45+
},
46+
4147
"fail to find secret with RPC provider": {
4248
provider: &testProvider{Powerstate: "on", Proto: "rpc"},
4349
secret: createHMACSecret(),
@@ -149,7 +155,7 @@ func TestMachineReconcile(t *testing.T) {
149155
}
150156

151157
func createMachineWithRPC(secret *corev1.Secret) *v1alpha1.Machine {
152-
return &v1alpha1.Machine{
158+
machine := &v1alpha1.Machine{
153159
ObjectMeta: metav1.ObjectMeta{
154160
Name: "test-bm",
155161
Namespace: "test-namespace",
@@ -161,21 +167,26 @@ func createMachineWithRPC(secret *corev1.Secret) *v1alpha1.Machine {
161167
ProviderOptions: &v1alpha1.ProviderOptions{
162168
RPC: &v1alpha1.RPCOptions{
163169
ConsumerURL: "http://127.0.0.1:7777",
164-
HMAC: &v1alpha1.HMACOpts{
165-
Secrets: v1alpha1.HMACSecrets{
166-
"sha256": []corev1.SecretReference{
167-
{
168-
Name: secret.Name,
169-
Namespace: secret.Namespace,
170-
},
171-
},
172-
},
173-
},
174170
},
175171
},
176172
},
177173
},
178174
}
175+
176+
if secret != nil {
177+
machine.Spec.Connection.ProviderOptions.RPC.HMAC = &v1alpha1.HMACOpts{
178+
Secrets: v1alpha1.HMACSecrets{
179+
"sha256": []corev1.SecretReference{
180+
{
181+
Name: secret.Name,
182+
Namespace: secret.Namespace,
183+
},
184+
},
185+
},
186+
}
187+
}
188+
189+
return machine
179190
}
180191

181192
func createMachine() *v1alpha1.Machine {

controller/task.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ func (r *TaskReconciler) doReconcile(ctx context.Context, task *v1alpha1.Task, t
9393
}
9494
if task.Spec.Connection.ProviderOptions != nil && task.Spec.Connection.ProviderOptions.RPC != nil {
9595
opts.ProviderOptions = task.Spec.Connection.ProviderOptions
96-
if len(task.Spec.Connection.ProviderOptions.RPC.HMAC.Secrets) > 0 {
96+
if task.Spec.Connection.ProviderOptions.RPC.HMAC != nil && len(task.Spec.Connection.ProviderOptions.RPC.HMAC.Secrets) > 0 {
9797
se, err := retrieveHMACSecrets(ctx, r.client, task.Spec.Connection.ProviderOptions.RPC.HMAC.Secrets)
9898
if err != nil {
9999
return ctrl.Result{}, fmt.Errorf("unable to get hmac secrets: %w", err)

controller/task_test.go

+27-15
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,12 @@ func TestTaskReconcile(t *testing.T) {
8181
task: createTaskWithRPC("PowerOn", getAction("PowerOn"), createHMACSecret()),
8282
},
8383

84+
"success power on with RPC provider w/o secrets": {
85+
taskName: "PowerOn",
86+
action: getAction("PowerOn"),
87+
provider: &testProvider{Powerstate: "on", PowerSetOK: true, Proto: "rpc"},
88+
},
89+
8490
"failure on bmc open": {
8591
taskName: "PowerOn", action: getAction("PowerOn"),
8692
provider: &testProvider{ErrOpen: errors.New("failed to open")},
@@ -259,7 +265,7 @@ func createTask(name string, action v1alpha1.Action, secret *corev1.Secret) *v1a
259265
}
260266

261267
func createTaskWithRPC(name string, action v1alpha1.Action, secret *corev1.Secret) *v1alpha1.Task {
262-
return &v1alpha1.Task{
268+
machine := &v1alpha1.Task{
263269
ObjectMeta: metav1.ObjectMeta{
264270
Name: name,
265271
Namespace: "default",
@@ -269,26 +275,32 @@ func createTaskWithRPC(name string, action v1alpha1.Action, secret *corev1.Secre
269275
Connection: v1alpha1.Connection{
270276
Host: "host",
271277
Port: 22,
272-
AuthSecretRef: corev1.SecretReference{
273-
Name: secret.Name,
274-
Namespace: secret.Namespace,
275-
},
276278
ProviderOptions: &v1alpha1.ProviderOptions{
277279
RPC: &v1alpha1.RPCOptions{
278280
ConsumerURL: "http://127.0.0.1:7777",
279-
HMAC: &v1alpha1.HMACOpts{
280-
Secrets: v1alpha1.HMACSecrets{
281-
"sha256": []corev1.SecretReference{
282-
{
283-
Name: secret.Name,
284-
Namespace: secret.Namespace,
285-
},
286-
},
287-
},
288-
},
289281
},
290282
},
291283
},
292284
},
293285
}
286+
287+
if secret != nil {
288+
machine.Spec.Connection.AuthSecretRef = corev1.SecretReference{
289+
Name: secret.Name,
290+
Namespace: secret.Namespace,
291+
}
292+
293+
machine.Spec.Connection.ProviderOptions.RPC.HMAC = &v1alpha1.HMACOpts{
294+
Secrets: v1alpha1.HMACSecrets{
295+
"sha256": []corev1.SecretReference{
296+
{
297+
Name: secret.Name,
298+
Namespace: secret.Namespace,
299+
},
300+
},
301+
},
302+
}
303+
}
304+
305+
return machine
294306
}

0 commit comments

Comments
 (0)