Skip to content

Commit be55d1e

Browse files
authored
logs: Reduce logs spamming and introduce levels (#2491)
* logs: Reduce logs spamming Remove repetitive reconciliation logs. Signed-off-by: Or Shoval <[email protected]> * logs: Migrate to pkg/log Allows to use levels. Signed-off-by: Or Shoval <[email protected]> * logger: Support configurable log level Signed-off-by: Or Shoval <[email protected]> * logs: Reduce verbosity of repetitive logs In order to enable set CNAO_LOG_LEVEL=-1. Signed-off-by: Or Shoval <[email protected]> --------- Signed-off-by: Or Shoval <[email protected]>
1 parent ca9c899 commit be55d1e

File tree

12 files changed

+137
-84
lines changed

12 files changed

+137
-84
lines changed

README.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,31 @@ spec:
279279
node-role.kubernetes.io/worker: ""
280280
```
281281

282+
## Operator Log Level Configuration
283+
284+
The CNAO operator supports configurable log levels to help with debugging and reducing log verbosity in production environments.
285+
286+
### Available Log Levels
287+
288+
| Value | Level | Description |
289+
|-------|---------|--------------------------------------|
290+
| `-1` | Debug | Most verbose, detailed debug info |
291+
| `0` | Info | Default, general operational info |
292+
| `1` | Warn | Warnings and errors only |
293+
| `2` | Error | Least verbose, errors only |
294+
295+
### Configuration methods
296+
297+
**Enable debug logging via shell:**
298+
299+
```shell
300+
kubectl set env deployment/cluster-network-addons-operator -n cluster-network-addons CNAO_LOG_LEVEL=-1
301+
```
302+
303+
**Change in release manifests:**
304+
305+
Edit `manifests/cluster-network-addons/<version>/operator.yaml` and change the `CNAO_LOG_LEVEL` value.
306+
282307
# Deployment
283308

284309
First install the operator itself:

cmd/manager/main.go

Lines changed: 41 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,19 @@ package main
33
import (
44
"flag"
55
"fmt"
6-
"log"
76
"net/http"
87
"os"
98
"runtime"
9+
"strconv"
1010

11+
"github.com/go-logr/logr"
12+
"go.uber.org/zap/zapcore"
1113
"k8s.io/apimachinery/pkg/api/meta"
1214
"k8s.io/client-go/rest"
15+
ctrl "sigs.k8s.io/controller-runtime"
1316
"sigs.k8s.io/controller-runtime/pkg/cache"
1417
"sigs.k8s.io/controller-runtime/pkg/client/apiutil"
18+
"sigs.k8s.io/controller-runtime/pkg/log/zap"
1519
metricsserver "sigs.k8s.io/controller-runtime/pkg/metrics/server"
1620

1721
k8snetworkplumbingwgv1 "github.com/k8snetworkplumbingwg/network-attachment-definition-client/pkg/apis/k8s.cni.cncf.io/v1"
@@ -50,29 +54,46 @@ func init() {
5054
// +kubebuilder:scaffold:scheme
5155
}
5256

53-
func printVersion() {
54-
log.Printf("Go Version: %s", runtime.Version())
55-
log.Printf("Go OS/Arch: %s/%s", runtime.GOOS, runtime.GOARCH)
56-
log.Printf("version of cluster-network-addons-operator: %v", os.Getenv("OPERATOR_VERSION"))
57+
func printVersion(logger logr.Logger) {
58+
logger.Info("Go Version", "version", runtime.Version())
59+
logger.Info("Go OS/Arch", "os", runtime.GOOS, "arch", runtime.GOARCH)
60+
logger.Info("cluster-network-addons-operator version", "version", os.Getenv("OPERATOR_VERSION"))
5761
}
5862

59-
func main() {
60-
// Add flags registered by imported packages (e.g. controller-runtime)
63+
func setupLogger() logr.Logger {
64+
logLevel := zapcore.InfoLevel
65+
if level, err := strconv.Atoi(os.Getenv("CNAO_LOG_LEVEL")); err == nil {
66+
logLevel = zapcore.Level(level)
67+
}
68+
69+
opts := zap.Options{
70+
Development: false,
71+
Level: logLevel,
72+
}
73+
opts.BindFlags(flag.CommandLine)
6174
pflag.CommandLine.AddGoFlagSet(flag.CommandLine)
6275
pflag.Parse()
6376

64-
printVersion()
77+
logger := zap.New(zap.UseFlagOptions(&opts))
78+
ctrl.SetLogger(logger)
79+
return logger.WithName("manager")
80+
}
81+
82+
func main() {
83+
logger := setupLogger()
84+
85+
printVersion(logger)
6586

6687
watchNamespace, err := k8s.GetWatchNamespace()
6788
if err != nil {
68-
log.Printf("failed to get watch namespace: %v", err)
89+
logger.Error(err, "failed to get watch namespace")
6990
os.Exit(1)
7091
}
7192

7293
// Get a config to talk to the apiserver
7394
cfg, err := config.GetConfig()
7495
if err != nil {
75-
log.Printf("failed to get apiserver config: %v", err)
96+
logger.Error(err, "failed to get apiserver config")
7697
os.Exit(1)
7798
}
7899

@@ -93,46 +114,46 @@ func main() {
93114
HealthProbeBindAddress: fmt.Sprintf(":%d", components.HealthProbePort),
94115
})
95116
if err != nil {
96-
log.Printf("failed to instantiate new operator manager: %v", err)
117+
logger.Error(err, "failed to instantiate new operator manager")
97118
os.Exit(1)
98119
}
99120

100121
// Setup Monitoring
101122
operatormetrics.Register = controllerruntimemetrics.Registry.Register
102123
err = metrics.SetupMetrics()
103124
if err != nil {
104-
log.Printf("failed to setup metrics: %v", err)
125+
logger.Error(err, "failed to setup metrics")
105126
os.Exit(1)
106127
}
107128

108-
log.Print("registering Components")
129+
logger.Info("registering Components")
109130

110131
if err := osv1.Install(mgr.GetScheme()); err != nil {
111-
log.Printf("failed adding openshift scheme to the client: %v", err)
132+
logger.Error(err, "failed adding openshift scheme to the client")
112133
os.Exit(1)
113134
}
114135

115-
log.Print("Add readiness and liveness probes")
136+
logger.Info("adding readiness and liveness probes")
116137
if err := mgr.AddHealthzCheck("healthz", healthz.Ping); err != nil {
117-
log.Printf("unable to set up health check: %v", err)
138+
logger.Error(err, "unable to set up health check")
118139
os.Exit(1)
119140
}
120141
if err := mgr.AddReadyzCheck("readyz", healthz.Ping); err != nil {
121-
log.Printf("unable to set up ready check: %v", err)
142+
logger.Error(err, "unable to set up ready check")
122143
os.Exit(1)
123144
}
124145

125146
// Setup all Controllers
126147
if err := controller.AddToManager(mgr); err != nil {
127-
log.Printf("failed setting up operator controllers: %v", err)
148+
logger.Error(err, "failed setting up operator controllers")
128149
os.Exit(1)
129150
}
130151

131-
log.Print("starting the operator manager")
152+
logger.Info("starting the operator manager")
132153

133154
// Start the operator manager
134155
if err := mgr.Start(signals.SetupSignalHandler()); err != nil {
135-
log.Printf("manager exited with non-zero: %v", err)
156+
logger.Error(err, "manager exited with non-zero")
136157
os.Exit(1)
137158
}
138159
}

go.mod

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ require (
1313
github.com/ghodss/yaml v1.0.1-0.20190212211648-25d852aebe32
1414
github.com/github-release/github-release v0.10.0
1515
github.com/go-git/go-git/v5 v5.16.2
16+
github.com/go-logr/logr v1.4.3
1617
github.com/gobwas/glob v0.2.3
1718
github.com/google/go-github/v32 v32.1.0
1819
github.com/k8snetworkplumbingwg/network-attachment-definition-client v1.3.0
@@ -31,6 +32,7 @@ require (
3132
github.com/rhobs/operator-observability-toolkit v0.0.29
3233
github.com/spf13/pflag v1.0.7
3334
github.com/thanhpk/randstr v1.0.4
35+
go.uber.org/zap v1.27.0
3436
golang.org/x/oauth2 v0.30.0
3537
golang.org/x/tools v0.38.0
3638
gopkg.in/yaml.v2 v2.4.0
@@ -109,7 +111,6 @@ require (
109111
github.com/go-git/go-billy/v5 v5.6.2 // indirect
110112
github.com/go-gorp/gorp/v3 v3.1.0 // indirect
111113
github.com/go-jose/go-jose/v4 v4.1.0 // indirect
112-
github.com/go-logr/logr v1.4.3 // indirect
113114
github.com/go-logr/stdr v1.2.2 // indirect
114115
github.com/go-logr/zapr v1.3.0 // indirect
115116
github.com/go-openapi/analysis v0.23.0 // indirect
@@ -258,7 +259,6 @@ require (
258259
go.opentelemetry.io/proto/otlp v1.7.0 // indirect
259260
go.starlark.net v0.0.0-20230525235612-a134d8f9ddca // indirect
260261
go.uber.org/multierr v1.11.0 // indirect
261-
go.uber.org/zap v1.27.0 // indirect
262262
go.yaml.in/yaml/v2 v2.4.2 // indirect
263263
go.yaml.in/yaml/v3 v3.0.4 // indirect
264264
golang.org/x/crypto v0.45.0 // indirect

pkg/apply/apply.go

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package apply
33
import (
44
"context"
55
"fmt"
6-
"log"
76
"strings"
87

98
"github.com/pkg/errors"
@@ -13,8 +12,11 @@ import (
1312
uns "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
1413
"k8s.io/apimachinery/pkg/types"
1514
k8sclient "sigs.k8s.io/controller-runtime/pkg/client"
15+
logf "sigs.k8s.io/controller-runtime/pkg/log"
1616
)
1717

18+
var applyLog = logf.Log.WithName("apply")
19+
1820
// ApplyObject applies the desired object against the apiserver,
1921
// merging it with any existing objects if already present.
2022
func ApplyObject(ctx context.Context, client k8sclient.Client, obj *uns.Unstructured) error {
@@ -26,7 +28,7 @@ func ApplyObject(ctx context.Context, client k8sclient.Client, obj *uns.Unstruct
2628
gvk := obj.GroupVersionKind()
2729
// used for logging and errors
2830
objDesc := fmt.Sprintf("(%s) %s/%s", gvk.String(), namespace, name)
29-
log.Printf("reconciling %s", objDesc)
31+
applyLog.V(1).Info("reconciling", "kind", gvk.String(), "namespace", namespace, "name", name)
3032

3133
if err := IsObjectSupported(obj); err != nil {
3234
return errors.Wrapf(err, "object %s unsupported", objDesc)
@@ -38,20 +40,20 @@ func ApplyObject(ctx context.Context, client k8sclient.Client, obj *uns.Unstruct
3840
err := client.Get(ctx, types.NamespacedName{Name: obj.GetName(), Namespace: obj.GetNamespace()}, existing)
3941

4042
if err != nil && apierrors.IsNotFound(err) {
41-
log.Printf("does not exist, creating %s", objDesc)
43+
applyLog.Info("creating object", "kind", gvk.String(), "namespace", namespace, "name", name)
4244
err := client.Create(ctx, obj)
4345
if err != nil {
4446
return errors.Wrapf(err, "could not create %s", objDesc)
4547
}
46-
log.Printf("successfully created %s", objDesc)
48+
applyLog.Info("successfully created object", "kind", gvk.String(), "namespace", namespace, "name", name)
4749
return nil
4850
}
4951
if err != nil {
5052
return errors.Wrapf(err, "could not retrieve existing %s", objDesc)
5153
}
5254

5355
if isTLSSecret(obj) {
54-
log.Printf("Ignoring TLS secret %s at reconcile", obj.GetName())
56+
applyLog.V(1).Info("ignoring TLS secret at reconcile", "name", obj.GetName())
5557
return nil
5658
}
5759

@@ -69,19 +71,18 @@ func ApplyObject(ctx context.Context, client k8sclient.Client, obj *uns.Unstruct
6971
// that possible, this exception should be dropped.
7072
bridgeMarkerDaemonSetUpdateError := "DaemonSet.apps \"bridge-marker\" is invalid: spec.selector: Invalid value: v1.LabelSelector{MatchLabels:map[string]string{\"name\":\"bridge-marker\"}, MatchExpressions:[]v1.LabelSelectorRequirement(nil)}: field is immutable"
7173
if strings.Contains(err.Error(), bridgeMarkerDaemonSetUpdateError) {
72-
log.Print("update failed due to change in DaemonSet API group; removing original object and recreating")
74+
applyLog.Info("update failed due to change in DaemonSet API group; removing original object and recreating")
7375
if err := client.Delete(ctx, existing); err != nil {
7476
return errors.Wrapf(err, "could not delete %s", objDesc)
7577
}
7678
if err := client.Create(ctx, obj); err != nil {
7779
return errors.Wrapf(err, "could not create %s", objDesc)
7880
}
79-
log.Print("update of conflicting DaemonSet was successful")
81+
applyLog.Info("update of conflicting DaemonSet was successful")
8082
}
8183

8284
return errors.Wrapf(err, "could not update object %s", objDesc)
8385
}
84-
log.Print("update was successful")
8586
}
8687

8788
return nil
@@ -116,7 +117,7 @@ func DeleteOwnedObject(ctx context.Context, client k8sclient.Client, obj *uns.Un
116117
if !cnaoOwns(existing) {
117118
return nil
118119
}
119-
log.Printf("Handling deletion of %s", objDesc)
120+
applyLog.Info("handling deletion", "kind", gvk.String(), "namespace", namespace, "name", name)
120121
if err := client.Delete(ctx, existing); err != nil {
121122
return errors.Wrapf(err, "failed deleting owned %s", objDesc)
122123
}

pkg/components/components.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -307,6 +307,10 @@ func GetDeployment(version string, operatorVersion string, namespace string, rep
307307
Name: "RUNBOOK_URL_TEMPLATE",
308308
Value: alerts.GetRunbookURLTemplate(),
309309
},
310+
{
311+
Name: "CNAO_LOG_LEVEL",
312+
Value: "0",
313+
},
310314
},
311315
SecurityContext: &corev1.SecurityContext{
312316
AllowPrivilegeEscalation: &allowPrivilegeEscalation,

0 commit comments

Comments
 (0)