Skip to content

Commit ea86970

Browse files
committed
config: Extract apply log level logic to function
1 parent 9c2d6c9 commit ea86970

File tree

2 files changed

+43
-27
lines changed

2 files changed

+43
-27
lines changed

pkg/cvo/configuration/configuration.go

Lines changed: 1 addition & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -148,31 +148,5 @@ func (config *ClusterVersionOperatorConfiguration) sync(ctx context.Context, des
148148
config.desiredLogLevel = operatorv1.Normal
149149
}
150150

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-
}
177-
return nil
151+
return applyLogLevel(config.desiredLogLevel)
178152
}

pkg/cvo/configuration/loglevel.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package configuration
2+
3+
import (
4+
"fmt"
5+
6+
"k8s.io/klog/v2"
7+
8+
operatorv1 "github.com/openshift/api/operator/v1"
9+
"github.com/openshift/library-go/pkg/operator/loglevel"
10+
11+
i "github.com/openshift/cluster-version-operator/pkg/internal"
12+
)
13+
14+
func applyLogLevel(level operatorv1.LogLevel) error {
15+
currentLogLevel, notFound := loglevel.GetLogLevel()
16+
if notFound {
17+
klog.Warningf("The current log level could not be found; an attempt to set the log level to the desired level will be made")
18+
}
19+
20+
if !notFound && currentLogLevel == level {
21+
klog.V(i.Debug).Infof("No need to update the current CVO log level '%s'; it is already set to the desired value", currentLogLevel)
22+
} else {
23+
if err := loglevel.SetLogLevel(level); err != nil {
24+
return fmt.Errorf("failed to set the log level to %q: %w", level, err)
25+
}
26+
27+
// E2E testing will be checking for existence or absence of these logs
28+
switch level {
29+
case operatorv1.Normal:
30+
klog.V(i.Normal).Infof("Successfully updated the log level from '%s' to 'Normal'", currentLogLevel)
31+
case operatorv1.Debug:
32+
klog.V(i.Debug).Infof("Successfully updated the log level from '%s' to 'Debug'", currentLogLevel)
33+
case operatorv1.Trace:
34+
klog.V(i.Trace).Infof("Successfully updated the log level from '%s' to 'Trace'", currentLogLevel)
35+
case operatorv1.TraceAll:
36+
klog.V(i.TraceAll).Infof("Successfully updated the log level from '%s' to 'TraceAll'", currentLogLevel)
37+
default:
38+
klog.Errorf("The CVO logging level has unexpected value '%s'", level)
39+
}
40+
}
41+
return nil
42+
}

0 commit comments

Comments
 (0)