@@ -6,15 +6,20 @@ import (
6
6
"time"
7
7
8
8
apierrors "k8s.io/apimachinery/pkg/api/errors"
9
+ metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
9
10
"k8s.io/client-go/tools/cache"
10
11
"k8s.io/client-go/util/workqueue"
11
12
"k8s.io/klog/v2"
12
13
14
+ operatorv1 "github.com/openshift/api/operator/v1"
13
15
operatorv1alpha1 "github.com/openshift/api/operator/v1alpha1"
14
16
operatorclientset "github.com/openshift/client-go/operator/clientset/versioned"
15
17
cvoclientv1alpha1 "github.com/openshift/client-go/operator/clientset/versioned/typed/operator/v1alpha1"
16
18
operatorexternalversions "github.com/openshift/client-go/operator/informers/externalversions"
17
19
operatorlistersv1alpha1 "github.com/openshift/client-go/operator/listers/operator/v1alpha1"
20
+ "github.com/openshift/library-go/pkg/operator/loglevel"
21
+
22
+ i "github.com/openshift/cluster-version-operator/pkg/internal"
18
23
)
19
24
20
25
const ClusterVersionOperatorConfigurationName = "cluster"
@@ -31,6 +36,9 @@ type ClusterVersionOperatorConfiguration struct {
31
36
factory operatorexternalversions.SharedInformerFactory
32
37
33
38
started bool
39
+
40
+ desiredLogLevel operatorv1.LogLevel
41
+ lastObservedGeneration int64
34
42
}
35
43
36
44
func (config * ClusterVersionOperatorConfiguration ) Queue () workqueue.TypedRateLimitingInterface [any ] {
@@ -43,26 +51,38 @@ func (config *ClusterVersionOperatorConfiguration) clusterVersionOperatorEventHa
43
51
return cache.ResourceEventHandlerFuncs {
44
52
AddFunc : func (_ interface {}) {
45
53
config .queue .Add (config .queueKey )
54
+ klog .V (i .Debug ).Infof ("ClusterVersionOperator resource was added; queuing a sync" )
46
55
},
47
56
UpdateFunc : func (_ , _ interface {}) {
48
57
config .queue .Add (config .queueKey )
58
+ klog .V (i .Debug ).Infof ("ClusterVersionOperator resource was modified or resync period has passed; queuing a sync" )
49
59
},
50
60
DeleteFunc : func (_ interface {}) {
51
61
config .queue .Add (config .queueKey )
62
+ klog .V (i .Debug ).Infof ("ClusterVersionOperator resource was deleted; queuing a sync" )
52
63
},
53
64
}
54
65
}
55
66
56
67
// NewClusterVersionOperatorConfiguration returns ClusterVersionOperatorConfiguration, which might be used
57
68
// to synchronize with the ClusterVersionOperator resource.
58
69
func NewClusterVersionOperatorConfiguration (client operatorclientset.Interface , factory operatorexternalversions.SharedInformerFactory ) * ClusterVersionOperatorConfiguration {
70
+ var desiredLogLevel operatorv1.LogLevel
71
+ if currentLogLevel , notFound := loglevel .GetLogLevel (); notFound {
72
+ klog .Warningf ("The current log level could not be found; assuming the 'Normal' level is the currently desired" )
73
+ desiredLogLevel = operatorv1 .Normal
74
+ } else {
75
+ desiredLogLevel = currentLogLevel
76
+ }
77
+
59
78
return & ClusterVersionOperatorConfiguration {
60
79
queueKey : fmt .Sprintf ("ClusterVersionOperator/%s" , ClusterVersionOperatorConfigurationName ),
61
80
queue : workqueue .NewTypedRateLimitingQueueWithConfig [any ](
62
81
workqueue .DefaultTypedControllerRateLimiter [any ](),
63
82
workqueue.TypedRateLimitingQueueConfig [any ]{Name : "configuration" }),
64
- client : client .OperatorV1alpha1 ().ClusterVersionOperators (),
65
- factory : factory ,
83
+ client : client .OperatorV1alpha1 ().ClusterVersionOperators (),
84
+ factory : factory ,
85
+ desiredLogLevel : desiredLogLevel ,
66
86
}
67
87
}
68
88
@@ -92,9 +112,9 @@ func (config *ClusterVersionOperatorConfiguration) Sync(ctx context.Context, key
92
112
panic ("ClusterVersionOperatorConfiguration instance was not properly started before its synchronization." )
93
113
}
94
114
startTime := time .Now ()
95
- klog .V (2 ).Infof ("Started syncing CVO configuration %q" , key )
115
+ klog .V (i . Normal ).Infof ("Started syncing CVO configuration %q" , key )
96
116
defer func () {
97
- klog .V (2 ).Infof ("Finished syncing CVO configuration (%v)" , time .Since (startTime ))
117
+ klog .V (i . Normal ).Infof ("Finished syncing CVO configuration (%v)" , time .Since (startTime ))
98
118
}()
99
119
100
120
desiredConfig , err := config .lister .Get (ClusterVersionOperatorConfigurationName )
@@ -108,7 +128,47 @@ func (config *ClusterVersionOperatorConfiguration) Sync(ctx context.Context, key
108
128
return config .sync (ctx , desiredConfig )
109
129
}
110
130
111
- func (config * ClusterVersionOperatorConfiguration ) sync (_ context.Context , _ * operatorv1alpha1.ClusterVersionOperator ) error {
112
- klog .Infof ("ClusterVersionOperator configuration has been synced" )
131
+ // sync synchronizes the local configuration based on the desired configuration
132
+ // and updates the status of the Kubernetes resource if needed.
133
+ //
134
+ // desiredConfig is a read-only representation of the desired configuration.
135
+ func (config * ClusterVersionOperatorConfiguration ) sync (ctx context.Context , desiredConfig * operatorv1alpha1.ClusterVersionOperator ) error {
136
+ if desiredConfig .Status .ObservedGeneration != desiredConfig .Generation {
137
+ newConfig := desiredConfig .DeepCopy ()
138
+ newConfig .Status .ObservedGeneration = desiredConfig .Generation
139
+ _ , err := config .client .UpdateStatus (ctx , newConfig , metav1.UpdateOptions {})
140
+ if err != nil {
141
+ return fmt .Errorf ("failed to update the ClusterVersionOperator resource: %w" , err )
142
+ }
143
+ }
144
+ config .desiredLogLevel = desiredConfig .Spec .OperatorLogLevel
145
+ config .lastObservedGeneration = desiredConfig .Generation
146
+
147
+ currentLogLevel , notFound := loglevel .GetLogLevel ()
148
+ if notFound {
149
+ klog .Warningf ("The current log level could not be found; an attempt to set the log level to the desired level will be made" )
150
+ }
151
+
152
+ if ! notFound && currentLogLevel == config .desiredLogLevel {
153
+ klog .V (i .Debug ).Infof ("No need to update the current CVO log level '%s'; it is already set to the desired value" , currentLogLevel )
154
+ } else {
155
+ if err := loglevel .SetLogLevel (config .desiredLogLevel ); err != nil {
156
+ return fmt .Errorf ("failed to set the log level to %q: %w" , config .desiredLogLevel , err )
157
+ }
158
+
159
+ // E2E testing will be checking for existence or absence of these logs
160
+ switch config .desiredLogLevel {
161
+ case operatorv1 .Normal :
162
+ klog .V (i .Normal ).Infof ("Successfully updated the log level from '%s' to 'Normal'" , currentLogLevel )
163
+ case operatorv1 .Debug :
164
+ klog .V (i .Debug ).Infof ("Successfully updated the log level from '%s' to 'Debug'" , currentLogLevel )
165
+ case operatorv1 .Trace :
166
+ klog .V (i .Trace ).Infof ("Successfully updated the log level from '%s' to 'Trace'" , currentLogLevel )
167
+ case operatorv1 .TraceAll :
168
+ klog .V (i .TraceAll ).Infof ("Successfully updated the log level from '%s' to 'TraceAll'" , currentLogLevel )
169
+ default :
170
+ klog .Errorf ("The CVO logging level has unexpected value '%s'" , config .desiredLogLevel )
171
+ }
172
+ }
113
173
return nil
114
174
}
0 commit comments