Skip to content

Commit 55b7593

Browse files
Merge pull request #1170 from DavidHurta/cvo-configuration-controller-add-sync-logic
OTA-209: Add sync logic to the CVO configuration controller
2 parents 5dbef31 + 07c50b1 commit 55b7593

File tree

140 files changed

+15838
-6
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

140 files changed

+15838
-6
lines changed

go.mod

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,10 +64,13 @@ require (
6464
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
6565
github.com/mwitkow/go-conntrack v0.0.0-20190716064945-2f068394615f // indirect
6666
github.com/prometheus/procfs v0.15.1 // indirect
67+
github.com/robfig/cron v1.2.0 // indirect
6768
github.com/sirupsen/logrus v1.9.3 // indirect
6869
github.com/spf13/pflag v1.0.5 // indirect
6970
github.com/stoewer/go-strcase v1.3.0 // indirect
7071
github.com/x448/float16 v0.8.4 // indirect
72+
go.opentelemetry.io/otel v1.28.0 // indirect
73+
go.opentelemetry.io/otel/trace v1.28.0 // indirect
7174
golang.org/x/exp v0.0.0-20240719175910-8a7402abbf56 // indirect
7275
golang.org/x/oauth2 v0.23.0 // indirect
7376
golang.org/x/sys v0.27.0 // indirect
@@ -82,6 +85,7 @@ require (
8285
gopkg.in/yaml.v2 v2.4.0 // indirect
8386
gopkg.in/yaml.v3 v3.0.1 // indirect
8487
k8s.io/apiserver v0.32.1 // indirect
88+
k8s.io/component-base v0.32.1 // indirect
8589
k8s.io/kube-openapi v0.0.0-20241105132330-32ad38e42d3f // indirect
8690
sigs.k8s.io/controller-runtime v0.12.1 // indirect
8791
sigs.k8s.io/json v0.0.0-20241010143419-9aa6b5e7a4b3 // indirect

go.sum

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,8 @@ github.com/prometheus/common v0.55.0 h1:KEi6DK7lXW/m7Ig5i47x0vRzuBsHuvJdi5ee6Y3G
113113
github.com/prometheus/common v0.55.0/go.mod h1:2SECS4xJG1kd8XF9IcM1gMX6510RAEL65zxzNImwdc8=
114114
github.com/prometheus/procfs v0.15.1 h1:YagwOFzUgYfKKHX6Dr+sHT7km/hxC76UB0learggepc=
115115
github.com/prometheus/procfs v0.15.1/go.mod h1:fB45yRUv8NstnjriLhBQLuOUt+WW4BsoGhij/e3PBqk=
116+
github.com/robfig/cron v1.2.0 h1:ZjScXvvxeQ63Dbyxy76Fj3AT3Ut0aKsyd2/tl3DTMuQ=
117+
github.com/robfig/cron v1.2.0/go.mod h1:JGuDeoQd7Z6yL4zQhZ3OPEVHB7fL6Ka6skscFHfmt2k=
116118
github.com/rogpeppe/go-internal v1.12.0 h1:exVL4IDcn6na9z1rAb56Vxr+CgyK3nn3O+epU5NdKM8=
117119
github.com/rogpeppe/go-internal v1.12.0/go.mod h1:E+RYuTGaKKdloAfM02xzb0FW3Paa99yedzYV+kq4uf4=
118120
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
@@ -138,6 +140,10 @@ github.com/x448/float16 v0.8.4 h1:qLwI1I70+NjRFUR3zs1JPUCgaCXSh3SW62uAKT1mSBM=
138140
github.com/x448/float16 v0.8.4/go.mod h1:14CWIYCyZA/cWjXOioeEpHeN/83MdbZDRQHoFcYsOfg=
139141
github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
140142
github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
143+
go.opentelemetry.io/otel v1.28.0 h1:/SqNcYk+idO0CxKEUOtKQClMK/MimZihKYMruSMViUo=
144+
go.opentelemetry.io/otel v1.28.0/go.mod h1:q68ijF8Fc8CnMHKyzqL6akLO46ePnjkgfIMIjUIX9z4=
145+
go.opentelemetry.io/otel/trace v1.28.0 h1:GhQ9cUuQGmNDd5BTCP2dAvv75RdMxEfTmYejp+lkx9g=
146+
go.opentelemetry.io/otel/trace v1.28.0/go.mod h1:jPyXzNPg6da9+38HEwElrQiHlVMTnVfM3/yv2OlIHaI=
141147
go.uber.org/goleak v1.3.0 h1:2K3zAYmnTNqV73imy9J1T3WC+gmCePx2hEGkimedGto=
142148
go.uber.org/goleak v1.3.0/go.mod h1:CoHD4mav9JJNrW/WLlf7HGZPjdw8EucARQHekz1X6bE=
143149
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
@@ -216,6 +222,8 @@ k8s.io/apiserver v0.32.1 h1:oo0OozRos66WFq87Zc5tclUX2r0mymoVHRq8JmR7Aak=
216222
k8s.io/apiserver v0.32.1/go.mod h1:UcB9tWjBY7aryeI5zAgzVJB/6k7E97bkr1RgqDz0jPw=
217223
k8s.io/client-go v0.32.1 h1:otM0AxdhdBIaQh7l1Q0jQpmo7WOFIk5FFa4bg6YMdUU=
218224
k8s.io/client-go v0.32.1/go.mod h1:aTTKZY7MdxUaJ/KiUs8D+GssR9zJZi77ZqtzcGXIiDg=
225+
k8s.io/component-base v0.32.1 h1:/5IfJ0dHIKBWysGV0yKTFfacZ5yNV1sulPh3ilJjRZk=
226+
k8s.io/component-base v0.32.1/go.mod h1:j1iMMHi/sqAHeG5z+O9BFNCF698a1u0186zkjMZQ28w=
219227
k8s.io/klog/v2 v2.130.1 h1:n9Xl7H1Xvksem4KFG4PYbdQCQxqc/tTUyrgXaOhHSzk=
220228
k8s.io/klog/v2 v2.130.1/go.mod h1:3Jpz1GvMt720eyJH1ckRHK1EDfpxISzJ7I9OYgaDtPE=
221229
k8s.io/kube-aggregator v0.32.1 h1:cztPyIHbo6tgrhYHDqmdmvxUufJKuxgAC/vog7yeWek=

pkg/cvo/configuration/configuration.go

Lines changed: 70 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,20 @@ import (
66
"time"
77

88
apierrors "k8s.io/apimachinery/pkg/api/errors"
9+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
910
"k8s.io/client-go/tools/cache"
1011
"k8s.io/client-go/util/workqueue"
1112
"k8s.io/klog/v2"
1213

14+
operatorv1 "github.com/openshift/api/operator/v1"
1315
operatorv1alpha1 "github.com/openshift/api/operator/v1alpha1"
1416
operatorclientset "github.com/openshift/client-go/operator/clientset/versioned"
1517
cvoclientv1alpha1 "github.com/openshift/client-go/operator/clientset/versioned/typed/operator/v1alpha1"
1618
operatorexternalversions "github.com/openshift/client-go/operator/informers/externalversions"
1719
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"
1823
)
1924

2025
const ClusterVersionOperatorConfigurationName = "cluster"
@@ -31,6 +36,9 @@ type ClusterVersionOperatorConfiguration struct {
3136
factory operatorexternalversions.SharedInformerFactory
3237

3338
started bool
39+
40+
desiredLogLevel operatorv1.LogLevel
41+
lastObservedGeneration int64
3442
}
3543

3644
func (config *ClusterVersionOperatorConfiguration) Queue() workqueue.TypedRateLimitingInterface[any] {
@@ -43,26 +51,38 @@ func (config *ClusterVersionOperatorConfiguration) clusterVersionOperatorEventHa
4351
return cache.ResourceEventHandlerFuncs{
4452
AddFunc: func(_ interface{}) {
4553
config.queue.Add(config.queueKey)
54+
klog.V(i.Debug).Infof("ClusterVersionOperator resource was added; queuing a sync")
4655
},
4756
UpdateFunc: func(_, _ interface{}) {
4857
config.queue.Add(config.queueKey)
58+
klog.V(i.Debug).Infof("ClusterVersionOperator resource was modified or resync period has passed; queuing a sync")
4959
},
5060
DeleteFunc: func(_ interface{}) {
5161
config.queue.Add(config.queueKey)
62+
klog.V(i.Debug).Infof("ClusterVersionOperator resource was deleted; queuing a sync")
5263
},
5364
}
5465
}
5566

5667
// NewClusterVersionOperatorConfiguration returns ClusterVersionOperatorConfiguration, which might be used
5768
// to synchronize with the ClusterVersionOperator resource.
5869
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+
5978
return &ClusterVersionOperatorConfiguration{
6079
queueKey: fmt.Sprintf("ClusterVersionOperator/%s", ClusterVersionOperatorConfigurationName),
6180
queue: workqueue.NewTypedRateLimitingQueueWithConfig[any](
6281
workqueue.DefaultTypedControllerRateLimiter[any](),
6382
workqueue.TypedRateLimitingQueueConfig[any]{Name: "configuration"}),
64-
client: client.OperatorV1alpha1().ClusterVersionOperators(),
65-
factory: factory,
83+
client: client.OperatorV1alpha1().ClusterVersionOperators(),
84+
factory: factory,
85+
desiredLogLevel: desiredLogLevel,
6686
}
6787
}
6888

@@ -92,9 +112,9 @@ func (config *ClusterVersionOperatorConfiguration) Sync(ctx context.Context, key
92112
panic("ClusterVersionOperatorConfiguration instance was not properly started before its synchronization.")
93113
}
94114
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)
96116
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))
98118
}()
99119

100120
desiredConfig, err := config.lister.Get(ClusterVersionOperatorConfigurationName)
@@ -108,7 +128,51 @@ func (config *ClusterVersionOperatorConfiguration) Sync(ctx context.Context, key
108128
return config.sync(ctx, desiredConfig)
109129
}
110130

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+
145+
config.lastObservedGeneration = desiredConfig.Generation
146+
config.desiredLogLevel = desiredConfig.Spec.OperatorLogLevel
147+
if config.desiredLogLevel == "" {
148+
config.desiredLogLevel = operatorv1.Normal
149+
}
150+
151+
currentLogLevel, notFound := loglevel.GetLogLevel()
152+
if notFound {
153+
klog.Warningf("The current log level could not be found; an attempt to set the log level to the desired level will be made")
154+
}
155+
156+
if !notFound && currentLogLevel == config.desiredLogLevel {
157+
klog.V(i.Debug).Infof("No need to update the current CVO log level '%s'; it is already set to the desired value", currentLogLevel)
158+
} else {
159+
if err := loglevel.SetLogLevel(config.desiredLogLevel); err != nil {
160+
return fmt.Errorf("failed to set the log level to %q: %w", config.desiredLogLevel, err)
161+
}
162+
163+
// E2E testing will be checking for existence or absence of these logs
164+
switch config.desiredLogLevel {
165+
case operatorv1.Normal:
166+
klog.V(i.Normal).Infof("Successfully updated the log level from '%s' to 'Normal'", currentLogLevel)
167+
case operatorv1.Debug:
168+
klog.V(i.Debug).Infof("Successfully updated the log level from '%s' to 'Debug'", currentLogLevel)
169+
case operatorv1.Trace:
170+
klog.V(i.Trace).Infof("Successfully updated the log level from '%s' to 'Trace'", currentLogLevel)
171+
case operatorv1.TraceAll:
172+
klog.V(i.TraceAll).Infof("Successfully updated the log level from '%s' to 'TraceAll'", currentLogLevel)
173+
default:
174+
klog.Errorf("The CVO logging level has unexpected value '%s'", config.desiredLogLevel)
175+
}
176+
}
113177
return nil
114178
}

0 commit comments

Comments
 (0)