Skip to content

Commit ea9b53c

Browse files
Merge pull request #318 from shiftstack/fix-errror-handling
OCPBUGS-44340: Fix error handling for starter controller
2 parents 95537a1 + 096e992 commit ea9b53c

File tree

3 files changed

+93
-5
lines changed

3 files changed

+93
-5
lines changed

pkg/driver/openstack-manila/openstack_manila.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -169,12 +169,17 @@ func GetOpenStackManilaOperatorControllerConfig(ctx context.Context, flavour gen
169169
return false, errors.New("manila does not provide any share types")
170170
}
171171

172-
syncCSIDriver(ctx, c.KubeClient, c.EventRecorder)
172+
err = syncCSIDriver(ctx, c.KubeClient, c.EventRecorder)
173+
if err != nil {
174+
return true, err
175+
}
173176

174-
syncStorageClasses(ctx, shareTypes, c.KubeClient, c.EventRecorder, c.GuestNamespace)
177+
err = syncStorageClasses(ctx, shareTypes, c.KubeClient, c.EventRecorder, c.GuestNamespace)
178+
if err != nil {
179+
return true, err
180+
}
175181

176182
return true, nil
177-
178183
}
179184

180185
cfg.PreconditionInformers = []factory.Informer{c.GetCSIDriverInformer().Informer(), c.GetStorageClassInformer().Informer()}

pkg/operator/starter_controller.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,10 +67,11 @@ func (c *Controller) sync(ctx context.Context, syncCtx factory.SyncContext) erro
6767
return nil
6868
}
6969

70-
_, err = c.operatorControllerConfig.Precondition()
71-
if err != nil {
70+
valid, err := c.operatorControllerConfig.Precondition()
71+
if !valid {
7272
return c.setDisabledCondition(ctx, fmt.Sprintf("Pre-condition not satisfied: %v", err))
7373
}
74+
7475
if !c.controllersRunning {
7576
// Start controllers
7677
for _, controller := range c.factoryControllers {
@@ -88,6 +89,11 @@ func (c *Controller) sync(ctx context.Context, syncCtx factory.SyncContext) erro
8889
c.controllersRunning = true
8990
}
9091

92+
// If the precondition was valid, let the controllers start and return any error after.
93+
if err != nil {
94+
return err
95+
}
96+
9197
return c.setEnabledCondition(ctx)
9298
}
9399

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
package operator
2+
3+
import (
4+
"context"
5+
"errors"
6+
"testing"
7+
8+
opv1 "github.com/openshift/api/operator/v1"
9+
"github.com/openshift/csi-operator/pkg/operator/config"
10+
"github.com/openshift/library-go/pkg/operator/v1helpers"
11+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
12+
)
13+
14+
func TestValidatePreCondition(t *testing.T) {
15+
testProvider := "test-driver.csi.k8s.io"
16+
17+
cases := []struct {
18+
name string
19+
cfg *config.OperatorControllerConfig
20+
expectSyncError bool
21+
expectControllersRunning bool
22+
}{
23+
{
24+
name: "Precondition valid without error",
25+
cfg: &config.OperatorControllerConfig{
26+
Precondition: func() (bool, error) { return true, nil },
27+
},
28+
expectSyncError: false,
29+
expectControllersRunning: true,
30+
},
31+
{
32+
name: "Precondition valid with error from internal operation",
33+
cfg: &config.OperatorControllerConfig{
34+
Precondition: func() (bool, error) { return true, errors.New("") },
35+
},
36+
expectSyncError: true,
37+
expectControllersRunning: true,
38+
},
39+
{
40+
name: "Precondition not valid without error",
41+
cfg: &config.OperatorControllerConfig{
42+
Precondition: func() (bool, error) { return false, nil },
43+
},
44+
expectSyncError: false,
45+
expectControllersRunning: false,
46+
},
47+
{
48+
name: "Precondition not valid with error from internal operation",
49+
cfg: &config.OperatorControllerConfig{
50+
Precondition: func() (bool, error) { return false, errors.New("") },
51+
},
52+
expectSyncError: false,
53+
expectControllersRunning: false,
54+
},
55+
}
56+
57+
for _, tc := range cases {
58+
t.Run(tc.name, func(t *testing.T) {
59+
operator := &FakeOperator{
60+
ObjectMeta: metav1.ObjectMeta{Name: testProvider},
61+
Spec: opv1.OperatorSpec{ManagementState: opv1.Managed},
62+
}
63+
c := &Controller{
64+
operatorClient: v1helpers.NewFakeOperatorClientWithObjectMeta(&operator.ObjectMeta, &operator.Spec, &operator.Status, nil),
65+
operatorControllerConfig: tc.cfg,
66+
}
67+
ctx := context.Background()
68+
err := c.sync(ctx, nil)
69+
if c.controllersRunning != tc.expectControllersRunning {
70+
t.Fatalf("Unexpected CSI controller running state: %v", c.controllersRunning)
71+
}
72+
if err != nil && !tc.expectSyncError {
73+
t.Fatalf("Did not expect an error when syncing")
74+
}
75+
})
76+
}
77+
}

0 commit comments

Comments
 (0)