Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Operators: better logger handling #832

Merged
merged 1 commit into from
Jan 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 0 additions & 4 deletions operators/cmd/instance-operator/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ import (
instancesnapshot_controller "github.com/netgroup-polito/CrownLabs/operators/pkg/instancesnapshot-controller"
"github.com/netgroup-polito/CrownLabs/operators/pkg/instautoctrl"
"github.com/netgroup-polito/CrownLabs/operators/pkg/instctrl"
"github.com/netgroup-polito/CrownLabs/operators/pkg/utils"
"github.com/netgroup-polito/CrownLabs/operators/pkg/utils/restcfg"
)

Expand Down Expand Up @@ -99,9 +98,6 @@ func main() {
klog.InitFlags(nil)
flag.Parse()

if !klog.V(5).Enabled() {
klog.SetLogFilter(utils.LogShortenerFilter{})
}
ctrl.SetLogger(klogr.NewWithOptions())

log := ctrl.Log.WithName("setup")
Expand Down
4 changes: 0 additions & 4 deletions operators/cmd/tenant-operator/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ import (
clv1alpha2 "github.com/netgroup-polito/CrownLabs/operators/api/v1alpha2"
controllers "github.com/netgroup-polito/CrownLabs/operators/pkg/tenant-controller"
"github.com/netgroup-polito/CrownLabs/operators/pkg/tenantwh"
"github.com/netgroup-polito/CrownLabs/operators/pkg/utils"
)

var (
Expand Down Expand Up @@ -96,9 +95,6 @@ func main() {
klog.InitFlags(nil)
flag.Parse()

if !klog.V(5).Enabled() {
klog.SetLogFilter(utils.LogShortenerFilter{})
}
ctrl.SetLogger(klogr.NewWithOptions())

ctx := ctrl.SetupSignalHandler()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,5 +146,6 @@ func (r *InstanceSnapshotReconciler) SetupWithManager(mgr ctrl.Manager) error {
// The generation changed predicate allow to avoid updates on the status changes of the InstanceSnapshot
For(&crownlabsv1alpha2.InstanceSnapshot{}, builder.WithPredicates(predicate.GenerationChangedPredicate{})).
Owns(&batch.Job{}).
WithLogConstructor(utils.LogConstructor(mgr.GetLogger(), "InstanceSnapshot")).
Complete(r)
}
1 change: 1 addition & 0 deletions operators/pkg/instautoctrl/submission.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ func (r *InstanceSubmissionReconciler) SetupWithManager(mgr ctrl.Manager, concur
WithOptions(controller.Options{
MaxConcurrentReconciles: concurrency,
}).
WithLogConstructor(utils.LogConstructor(mgr.GetLogger(), "InstanceSubmission")).
Complete(r)
}

Expand Down
1 change: 1 addition & 0 deletions operators/pkg/instautoctrl/termination.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ func (r *InstanceTerminationReconciler) SetupWithManager(mgr ctrl.Manager, concu
WithOptions(controller.Options{
MaxConcurrentReconciles: concurrency,
}).
WithLogConstructor(utils.LogConstructor(mgr.GetLogger(), "InstanceTermination")).
Complete(r)
}

Expand Down
1 change: 1 addition & 0 deletions operators/pkg/instctrl/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,7 @@ func (r *InstanceReconciler) SetupWithManager(mgr ctrl.Manager, concurrency int)
WithOptions(controller.Options{
MaxConcurrentReconciles: concurrency,
}).
WithLogConstructor(utils.LogConstructor(mgr.GetLogger(), "Instance")).
Complete(r)
}

Expand Down
2 changes: 2 additions & 0 deletions operators/pkg/tenant-controller/tenant_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ import (
crownlabsv1alpha1 "github.com/netgroup-polito/CrownLabs/operators/api/v1alpha1"
crownlabsv1alpha2 "github.com/netgroup-polito/CrownLabs/operators/api/v1alpha2"
"github.com/netgroup-polito/CrownLabs/operators/pkg/forge"
"github.com/netgroup-polito/CrownLabs/operators/pkg/utils"
)

const (
Expand Down Expand Up @@ -257,6 +258,7 @@ func (r *TenantReconciler) SetupWithManager(mgr ctrl.Manager) error {
WithOptions(controller.Options{
MaxConcurrentReconciles: r.Concurrency,
}).
WithLogConstructor(utils.LogConstructor(mgr.GetLogger(), "Tenant")).
Complete(r)
}

Expand Down
2 changes: 2 additions & 0 deletions operators/pkg/tenant-controller/workspace_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import (

crownlabsv1alpha1 "github.com/netgroup-polito/CrownLabs/operators/api/v1alpha1"
crownlabsv1alpha2 "github.com/netgroup-polito/CrownLabs/operators/api/v1alpha2"
"github.com/netgroup-polito/CrownLabs/operators/pkg/utils"
)

// WorkspaceReconciler reconciles a Workspace object.
Expand Down Expand Up @@ -181,6 +182,7 @@ func (r *WorkspaceReconciler) SetupWithManager(mgr ctrl.Manager) error {
Owns(&v1.Namespace{}).
Owns(&rbacv1.ClusterRoleBinding{}).
Owns(&rbacv1.RoleBinding{}).
WithLogConstructor(utils.LogConstructor(mgr.GetLogger(), "Workspace")).
Complete(r)
}

Expand Down
52 changes: 6 additions & 46 deletions operators/pkg/utils/logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,65 +15,25 @@
package utils

import (
"strings"
"time"

"github.com/go-logr/logr"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
"sigs.k8s.io/controller-runtime/pkg/reconcile"
)

const (
logPrefixMgr = "controller-runtime/manager/"
logShortenedPrefixMgr = "mgr/"
logPrefixCtrl = "controller/"
logShortenedPrefixCtrl = "ctrl/"
logPrefixFull = logPrefixMgr + logPrefixCtrl

// LogInfoLevel -> level associated with informational messages.
LogInfoLevel = 0
// LogDebugLevel -> level associated with debug messages.
LogDebugLevel = 2
)

var logKeysToRemove = map[string]struct{}{
"reconciler group": {},
"reconciler kind": {},
"name": {},
"namespace": {},
}

// LogShortenerFilter implements the klog LogFilter interface, to shorten the log messages
// generated by controller-runtime (through the logr/klogr interfaces).
type LogShortenerFilter struct{}

// Filter does nothing (i.e, only passes through the arguments), as not used in this context.
func (l LogShortenerFilter) Filter(args []interface{}) []interface{} {
return args
}

// FilterF does nothing (i.e, only passes through the arguments), as not used in this context.
func (l LogShortenerFilter) FilterF(format string, args []interface{}) (f string, a []interface{}) {
return format, args
}

// FilterS shortens the argument list, removing a set of common key/value pairs configured by controller-manager.
func (l LogShortenerFilter) FilterS(msg string, keysAndValues []interface{}) (m string, a []interface{}) {
// Remove the key/value pairs only in case of controller-related logs
if strings.HasPrefix(msg, logPrefixFull) {
for i := 0; i < len(keysAndValues); {
if key, ok := keysAndValues[i].(string); ok {
if _, found := logKeysToRemove[key]; found {
keysAndValues = append(keysAndValues[:i], keysAndValues[i+2:]...)
continue
}
}
// Move to the next key
i += 2
}
// LogConstructor returns a constructor for a logger to be used by the given controller.
func LogConstructor(logger logr.Logger, ctrlname string) func(*reconcile.Request) logr.Logger {
return func(r *reconcile.Request) logr.Logger {
return logger.WithName(ctrlname)
}

msg = strings.ReplaceAll(msg, logPrefixMgr, logShortenedPrefixMgr)
msg = strings.ReplaceAll(msg, logPrefixCtrl, logShortenedPrefixCtrl)
return msg, keysAndValues
}

// FromResult returns a logger level, given the result of a CreateOrUpdate operation.
Expand Down