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

fix: Fix log formatting on startup logs #297

Merged
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
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ require (
k8s.io/apimachinery v0.25.4
k8s.io/client-go v0.25.4
k8s.io/csi-translation-lib v0.25.4
k8s.io/klog/v2 v2.80.2-0.20221028030830-9ae4992afb54
k8s.io/utils v0.0.0-20221108210102-8e77b1f39fe2
knative.dev/pkg v0.0.0-20221123154742-05b694ec4d3a
sigs.k8s.io/controller-runtime v0.13.1
Expand Down Expand Up @@ -95,7 +96,6 @@ require (
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
k8s.io/component-base v0.25.4 // indirect
k8s.io/klog/v2 v2.80.2-0.20221028030830-9ae4992afb54 // indirect
k8s.io/kube-openapi v0.0.0-20221012153701-172d655c2280 // indirect
sigs.k8s.io/json v0.0.0-20220713155537-f223a00ba0e2 // indirect
sigs.k8s.io/structured-merge-diff/v4 v4.2.3 // indirect
Expand Down
2 changes: 0 additions & 2 deletions pkg/operator/injection/injection.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import (
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/cache"
"knative.dev/pkg/logging"
"knative.dev/pkg/system"

"github.com/aws/karpenter-core/pkg/apis/settings"
Expand Down Expand Up @@ -85,7 +84,6 @@ func WithSettingsOrDie(ctx context.Context, kubernetesInterface kubernetes.Inter
cancelCtx, cancel := context.WithCancel(ctx)
defer cancel()

logging.FromContext(ctx).Debugf("waiting for configmaps")
factory := informers.NewSharedInformerFactoryWithOptions(kubernetesInterface, time.Second*30, informers.WithNamespace(system.Namespace()))
informer := factory.Core().V1().ConfigMaps().Informer()
factory.Start(cancelCtx.Done())
Expand Down
19 changes: 16 additions & 3 deletions pkg/operator/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,27 +16,40 @@ package operator

import (
"context"
"log"

"github.com/go-logr/logr"
"github.com/go-logr/zapr"
"go.uber.org/zap"
"go.uber.org/zap/zapio"
"k8s.io/client-go/rest"
"k8s.io/klog/v2"
"knative.dev/pkg/configmap/informer"
"knative.dev/pkg/injection"
"knative.dev/pkg/injection/sharedmain"
"knative.dev/pkg/logging"
)

// LoggingContextOrDie injects a logger into the returned context. The logger is
// NewLogger returns a configured *zap.SugaredLogger. The logger is
// configured by the ConfigMap `config-logging` and live updates the level.
func NewLogger(ctx context.Context, componentName string, config *rest.Config, cmw *informer.InformedWatcher) *zap.SugaredLogger {
ctx, startinformers := injection.EnableInjectionOrDie(ctx, config)
ctx, startInformers := injection.EnableInjectionOrDie(logging.WithLogger(ctx, zap.NewNop().Sugar()), config)
logger, atomicLevel := sharedmain.SetupLoggerOrDie(ctx, componentName)
rest.SetDefaultWarningHandler(&logging.WarningHandler{Logger: logger})
sharedmain.WatchLoggingConfigOrDie(ctx, cmw, logger, atomicLevel, componentName)
startinformers()
startInformers()
return logger
}

// ConfigureGlobalLoggers sets up any package-wide loggers like "log" or "klog" that are utilized by other packages
// to use the configured *zap.SugaredLogger from the context
func ConfigureGlobalLoggers(ctx context.Context) {
klog.SetLogger(zapr.NewLogger(logging.FromContext(ctx).Desugar()))
w := &zapio.Writer{Log: logging.FromContext(ctx).Desugar(), Level: zap.DebugLevel}
log.SetFlags(0)
log.SetOutput(w)
}

type ignoreDebugEventsSink struct {
name string
sink logr.LogSink
Expand Down
1 change: 1 addition & 0 deletions pkg/operator/operator.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ func NewOperator() (context.Context, *Operator) {
// Logging
logger := NewLogger(ctx, component, config, configMapWatcher)
ctx = logging.WithLogger(ctx, logger)
ConfigureGlobalLoggers(ctx)

// Inject settings from the ConfigMap(s) into the context
ctx = injection.WithSettingsOrDie(ctx, kubernetesInterface, apis.Settings...)
Expand Down