Skip to content
Open
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
6 changes: 2 additions & 4 deletions controllers/clusterpolicy_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,10 +113,8 @@ func (r *ClusterPolicyReconciler) Reconcile(ctx context.Context, req ctrl.Reques
// Return and don't requeue
return reconcile.Result{}, nil
}
// Error reading the object - requeue the request.
if condErr := r.conditionUpdater.SetConditionsError(ctx, instance, conditions.ReconcileFailed, err.Error()); condErr != nil {
logger.Error(condErr, "failed to set condition")
}
// instance was not populated by the failed Get, so there is no object to
// update status on; just surface the error for requeue.
return reconcile.Result{}, err
}

Expand Down
17 changes: 17 additions & 0 deletions controllers/clusterpolicy_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package controllers

import (
"context"
"errors"
"testing"
"time"

Expand Down Expand Up @@ -514,3 +515,19 @@ func clusterPolicyState(t *testing.T, c client.Client, name string) gpuv1.State
require.NoError(t, c.Get(t.Context(), client.ObjectKey{Name: name}, cp))
return cp.Status.State
}

func TestClusterPolicyReconcileGetFailureDoesNotUpdateCondition(t *testing.T) {
cp := clusterPolicyForUpgradeTest(true)
r, c, _ := newClusterPolicyUpgradeTestReconciler(t, cp)
updater := &FakeConditionUpdater{}
r.conditionUpdater = updater
r.Client = &getFailingClient{
Client: c,
getErr: errors.New("simulated API server connection failure"),
}

_, err := r.Reconcile(t.Context(), ctrl.Request{NamespacedName: client.ObjectKeyFromObject(cp)})
require.ErrorContains(t, err, "simulated API server connection failure")
require.Empty(t, updater.LastErrorReason)
}

7 changes: 2 additions & 5 deletions controllers/nvidiadriver_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,11 +95,8 @@ func (r *NVIDIADriverReconciler) Reconcile(ctx context.Context, req ctrl.Request
}
wrappedErr := fmt.Errorf("error getting NVIDIADriver object: %w", err)
logger.Error(err, "error getting NVIDIADriver object")
instance.Status.State = nvidiav1alpha1.NotReady
if condErr := r.conditionUpdater.SetConditionsError(ctx, instance, conditions.ReconcileFailed, wrappedErr.Error()); condErr != nil {
logger.Error(condErr, "failed to set condition")
}
// Error reading the object - requeue the request.
// instance was not populated by the failed Get, so there is no object to
// update status on; just surface the error for requeue.
return reconcile.Result{}, wrappedErr
}
if instance.HasDeletionTimestamp() {
Expand Down
38 changes: 38 additions & 0 deletions controllers/nvidiadriver_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,18 @@ func (c *patchFailingClient) Patch(ctx context.Context, obj client.Object, patch
return c.patchErr
}

type getFailingClient struct {
client.Client
getErr error
}

func (c *getFailingClient) Get(ctx context.Context, key client.ObjectKey, obj client.Object, opts ...client.GetOption) error {
if c.getErr != nil {
return c.getErr
}
return c.Client.Get(ctx, key, obj, opts...)
}

// newTestLogger creates a zap.Logger that writes to an in-memory buffer for testing
func newTestLogger() (logr.Logger, *bytes.Buffer) {
buf := &bytes.Buffer{}
Expand Down Expand Up @@ -678,3 +690,29 @@ func TestEnqueueNVIDIADriverReconcilersDedupesEventDriver(t *testing.T) {
require.Len(t, requests, 1)
require.Equal(t, "default/driver-a", requests[0].String())
}

func TestNVIDIADriverReconcileGetFailureDoesNotUpdateCondition(t *testing.T) {
scheme := runtime.NewScheme()
require.NoError(t, nvidiav1alpha1.AddToScheme(scheme))

driver := &nvidiav1alpha1.NVIDIADriver{
ObjectMeta: metav1.ObjectMeta{Name: "test-driver"},
}
baseClient := fake.NewClientBuilder().WithScheme(scheme).WithObjects(driver).Build()
updater := &FakeConditionUpdater{}
reconciler := &NVIDIADriverReconciler{
Client: &getFailingClient{
Client: baseClient,
getErr: errors.New("simulated API server connection failure"),
},
Scheme: scheme,
conditionUpdater: updater,
}

_, err := reconciler.Reconcile(t.Context(), ctrl.Request{
NamespacedName: types.NamespacedName{Name: driver.Name},
})
require.ErrorContains(t, err, "simulated API server connection failure")
require.Empty(t, updater.LastErrorReason)
}