Skip to content

Commit 539bd46

Browse files
committed
config: Add handler field to configuration
Using the field we can avoid changing the actual logging during testing by assigning a different function to the field. We can now also verify that the handler field is being executed.
1 parent 1b5d7c8 commit 539bd46

File tree

2 files changed

+28
-1
lines changed

2 files changed

+28
-1
lines changed

pkg/cvo/configuration/configuration.go

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,14 @@ type ClusterVersionOperatorConfiguration struct {
4343
started bool
4444

4545
configuration configuration
46+
47+
// Function to handle an update to the internal configuration.
48+
//
49+
// In the future, the desired configuration may be consumed via other controllers.
50+
// This will require some sort of synchronization upon a configuration change.
51+
// For the moment, the log level is the sole consumer of the configuration.
52+
// Apply the log level directly here for simplicity for the time being.
53+
handler func(configuration) error
4654
}
4755

4856
func (config *ClusterVersionOperatorConfiguration) Queue() workqueue.TypedRateLimitingInterface[any] {
@@ -87,6 +95,7 @@ func NewClusterVersionOperatorConfiguration(client operatorclientset.Interface,
8795
client: client.OperatorV1alpha1().ClusterVersionOperators(),
8896
factory: factory,
8997
configuration: configuration{desiredLogLevel: desiredLogLevel},
98+
handler: func(c configuration) error { return applyLogLevel(c.desiredLogLevel) },
9099
}
91100
}
92101

@@ -152,5 +161,8 @@ func (config *ClusterVersionOperatorConfiguration) sync(ctx context.Context, des
152161
config.configuration.desiredLogLevel = operatorv1.Normal
153162
}
154163

155-
return applyLogLevel(config.configuration.desiredLogLevel)
164+
if config.handler != nil {
165+
return config.handler(config.configuration)
166+
}
167+
return nil
156168
}

pkg/cvo/configuration/configuration_test.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ func TestClusterVersionOperatorConfiguration_sync(t *testing.T) {
2424
expectedConfig operatorv1alpha1.ClusterVersionOperator
2525
internalConfig configuration
2626
expectedInternalConfig configuration
27+
handlerFunctionCalled bool
2728
}{
2829
{
2930
name: "first sync run correctly updates the status",
@@ -54,6 +55,7 @@ func TestClusterVersionOperatorConfiguration_sync(t *testing.T) {
5455
desiredLogLevel: operatorv1.Normal,
5556
lastObservedGeneration: 1,
5657
},
58+
handlerFunctionCalled: true,
5759
},
5860
{
5961
name: "sync updates observed generation correctly",
@@ -87,6 +89,7 @@ func TestClusterVersionOperatorConfiguration_sync(t *testing.T) {
8789
desiredLogLevel: operatorv1.Normal,
8890
lastObservedGeneration: 3,
8991
},
92+
handlerFunctionCalled: true,
9093
},
9194
{
9295
name: "sync updates desired log level correctly",
@@ -120,6 +123,7 @@ func TestClusterVersionOperatorConfiguration_sync(t *testing.T) {
120123
desiredLogLevel: operatorv1.Trace,
121124
lastObservedGeneration: 4,
122125
},
126+
handlerFunctionCalled: true,
123127
},
124128
{
125129
name: "number of not observed generations does not impact sync",
@@ -153,6 +157,7 @@ func TestClusterVersionOperatorConfiguration_sync(t *testing.T) {
153157
desiredLogLevel: operatorv1.TraceAll,
154158
lastObservedGeneration: 40,
155159
},
160+
handlerFunctionCalled: true,
156161
},
157162
}
158163
for _, tt := range tests {
@@ -166,6 +171,12 @@ func TestClusterVersionOperatorConfiguration_sync(t *testing.T) {
166171

167172
configController := NewClusterVersionOperatorConfiguration(client, factory)
168173

174+
called := false
175+
configController.handler = func(_ configuration) error {
176+
called = true
177+
return nil
178+
}
179+
169180
ctx, cancelFunc := context.WithDeadline(context.Background(), time.Now().Add(time.Minute))
170181

171182
if err := configController.Start(ctx); err != nil {
@@ -194,6 +205,10 @@ func TestClusterVersionOperatorConfiguration_sync(t *testing.T) {
194205
t.Errorf("unexpected config (-want, +got) = %v", diff)
195206
}
196207

208+
if tt.handlerFunctionCalled != called {
209+
t.Errorf("unexpected handler function execution; wanted=%v, got=%v", tt.handlerFunctionCalled, called)
210+
}
211+
197212
// Shutdown created resources
198213
cancelFunc()
199214
})

0 commit comments

Comments
 (0)