Skip to content

Commit 1b5d7c8

Browse files
committed
config: Refactor configuration representation
Utilize a new structure, which holds the relevant information regarding the desired CVO configuration and its status to encapsulate this information to a simple data structure.
1 parent ea86970 commit 1b5d7c8

File tree

2 files changed

+42
-27
lines changed

2 files changed

+42
-27
lines changed

pkg/cvo/configuration/configuration.go

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,11 @@ import (
2424

2525
const ClusterVersionOperatorConfigurationName = "cluster"
2626

27+
type configuration struct {
28+
lastObservedGeneration int64
29+
desiredLogLevel operatorv1.LogLevel
30+
}
31+
2732
type ClusterVersionOperatorConfiguration struct {
2833
queueKey string
2934
// queue tracks checking for the CVO configuration.
@@ -37,8 +42,7 @@ type ClusterVersionOperatorConfiguration struct {
3742

3843
started bool
3944

40-
desiredLogLevel operatorv1.LogLevel
41-
lastObservedGeneration int64
45+
configuration configuration
4246
}
4347

4448
func (config *ClusterVersionOperatorConfiguration) Queue() workqueue.TypedRateLimitingInterface[any] {
@@ -80,9 +84,9 @@ func NewClusterVersionOperatorConfiguration(client operatorclientset.Interface,
8084
queue: workqueue.NewTypedRateLimitingQueueWithConfig[any](
8185
workqueue.DefaultTypedControllerRateLimiter[any](),
8286
workqueue.TypedRateLimitingQueueConfig[any]{Name: "configuration"}),
83-
client: client.OperatorV1alpha1().ClusterVersionOperators(),
84-
factory: factory,
85-
desiredLogLevel: desiredLogLevel,
87+
client: client.OperatorV1alpha1().ClusterVersionOperators(),
88+
factory: factory,
89+
configuration: configuration{desiredLogLevel: desiredLogLevel},
8690
}
8791
}
8892

@@ -142,11 +146,11 @@ func (config *ClusterVersionOperatorConfiguration) sync(ctx context.Context, des
142146
}
143147
}
144148

145-
config.lastObservedGeneration = desiredConfig.Generation
146-
config.desiredLogLevel = desiredConfig.Spec.OperatorLogLevel
147-
if config.desiredLogLevel == "" {
148-
config.desiredLogLevel = operatorv1.Normal
149+
config.configuration.lastObservedGeneration = desiredConfig.Generation
150+
config.configuration.desiredLogLevel = desiredConfig.Spec.OperatorLogLevel
151+
if config.configuration.desiredLogLevel == "" {
152+
config.configuration.desiredLogLevel = operatorv1.Normal
149153
}
150154

151-
return applyLogLevel(config.desiredLogLevel)
155+
return applyLogLevel(config.configuration.desiredLogLevel)
152156
}

pkg/cvo/configuration/configuration_test.go

Lines changed: 28 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ func TestClusterVersionOperatorConfiguration_sync(t *testing.T) {
2222
name string
2323
config operatorv1alpha1.ClusterVersionOperator
2424
expectedConfig operatorv1alpha1.ClusterVersionOperator
25-
internalConfig ClusterVersionOperatorConfiguration
26-
expectedInternalConfig ClusterVersionOperatorConfiguration
25+
internalConfig configuration
26+
expectedInternalConfig configuration
2727
}{
2828
{
2929
name: "first sync run correctly updates the status",
@@ -46,11 +46,11 @@ func TestClusterVersionOperatorConfiguration_sync(t *testing.T) {
4646
ObservedGeneration: 1,
4747
},
4848
},
49-
internalConfig: ClusterVersionOperatorConfiguration{
49+
internalConfig: configuration{
5050
desiredLogLevel: operatorv1.Normal,
5151
lastObservedGeneration: 0,
5252
},
53-
expectedInternalConfig: ClusterVersionOperatorConfiguration{
53+
expectedInternalConfig: configuration{
5454
desiredLogLevel: operatorv1.Normal,
5555
lastObservedGeneration: 1,
5656
},
@@ -79,11 +79,11 @@ func TestClusterVersionOperatorConfiguration_sync(t *testing.T) {
7979
ObservedGeneration: 3,
8080
},
8181
},
82-
internalConfig: ClusterVersionOperatorConfiguration{
82+
internalConfig: configuration{
8383
desiredLogLevel: operatorv1.Normal,
8484
lastObservedGeneration: 2,
8585
},
86-
expectedInternalConfig: ClusterVersionOperatorConfiguration{
86+
expectedInternalConfig: configuration{
8787
desiredLogLevel: operatorv1.Normal,
8888
lastObservedGeneration: 3,
8989
},
@@ -112,11 +112,11 @@ func TestClusterVersionOperatorConfiguration_sync(t *testing.T) {
112112
ObservedGeneration: 4,
113113
},
114114
},
115-
internalConfig: ClusterVersionOperatorConfiguration{
115+
internalConfig: configuration{
116116
desiredLogLevel: operatorv1.Normal,
117117
lastObservedGeneration: 3,
118118
},
119-
expectedInternalConfig: ClusterVersionOperatorConfiguration{
119+
expectedInternalConfig: configuration{
120120
desiredLogLevel: operatorv1.Trace,
121121
lastObservedGeneration: 4,
122122
},
@@ -145,11 +145,11 @@ func TestClusterVersionOperatorConfiguration_sync(t *testing.T) {
145145
ObservedGeneration: 40,
146146
},
147147
},
148-
internalConfig: ClusterVersionOperatorConfiguration{
148+
internalConfig: configuration{
149149
desiredLogLevel: operatorv1.Normal,
150150
lastObservedGeneration: 3,
151151
},
152-
expectedInternalConfig: ClusterVersionOperatorConfiguration{
152+
expectedInternalConfig: configuration{
153153
desiredLogLevel: operatorv1.TraceAll,
154154
lastObservedGeneration: 40,
155155
},
@@ -158,24 +158,35 @@ func TestClusterVersionOperatorConfiguration_sync(t *testing.T) {
158158
for _, tt := range tests {
159159
t.Run(tt.name, func(t *testing.T) {
160160
// Initialize testing logic
161+
tt.config.Name = ClusterVersionOperatorConfigurationName
162+
tt.expectedConfig.Name = ClusterVersionOperatorConfigurationName
163+
161164
client := operatorclientsetfake.NewClientset(&tt.config)
162-
tt.internalConfig.client = client.OperatorV1alpha1().ClusterVersionOperators()
165+
factory := operatorexternalversions.NewSharedInformerFactoryWithOptions(client, time.Minute)
166+
167+
configController := NewClusterVersionOperatorConfiguration(client, factory)
168+
163169
ctx, cancelFunc := context.WithDeadline(context.Background(), time.Now().Add(time.Minute))
164170

171+
if err := configController.Start(ctx); err != nil {
172+
t.Errorf("unexpected error %v", err)
173+
}
174+
configController.configuration = tt.internalConfig
175+
165176
// Run tested functionality
166-
if err := tt.internalConfig.sync(ctx, &tt.config); err != nil {
177+
if err := configController.Sync(ctx, "key"); err != nil {
167178
t.Errorf("unexpected error %v", err)
168179
}
169180

170181
// Verify results
171-
if tt.internalConfig.lastObservedGeneration != tt.expectedInternalConfig.lastObservedGeneration {
172-
t.Errorf("unexpected 'lastObservedGeneration' value; wanted=%v, got=%v", tt.expectedInternalConfig.lastObservedGeneration, tt.internalConfig.lastObservedGeneration)
182+
if configController.configuration.lastObservedGeneration != tt.expectedInternalConfig.lastObservedGeneration {
183+
t.Errorf("unexpected 'lastObservedGeneration' value; wanted=%v, got=%v", tt.expectedInternalConfig.lastObservedGeneration, configController.configuration.lastObservedGeneration)
173184
}
174-
if tt.internalConfig.desiredLogLevel != tt.expectedInternalConfig.desiredLogLevel {
175-
t.Errorf("unexpected 'desiredLogLevel' value; wanted=%v, got=%v", tt.expectedInternalConfig.desiredLogLevel, tt.internalConfig.desiredLogLevel)
185+
if configController.configuration.desiredLogLevel != tt.expectedInternalConfig.desiredLogLevel {
186+
t.Errorf("unexpected 'desiredLogLevel' value; wanted=%v, got=%v", tt.expectedInternalConfig.desiredLogLevel, configController.configuration.desiredLogLevel)
176187
}
177188

178-
config, err := client.OperatorV1alpha1().ClusterVersionOperators().Get(ctx, "", metav1.GetOptions{})
189+
config, err := client.OperatorV1alpha1().ClusterVersionOperators().Get(ctx, ClusterVersionOperatorConfigurationName, metav1.GetOptions{})
179190
if err != nil {
180191
t.Errorf("unexpected error %v", err)
181192
}

0 commit comments

Comments
 (0)