-
Notifications
You must be signed in to change notification settings - Fork 236
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7bda4a7
commit 725cf8c
Showing
7 changed files
with
269 additions
and
153 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,153 @@ | ||
/* | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package logging | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"fmt" | ||
"log" | ||
"time" | ||
|
||
"github.com/blendle/zapdriver" | ||
"github.com/go-logr/logr" | ||
"github.com/go-logr/zapr" | ||
"github.com/samber/lo" | ||
"go.uber.org/zap" | ||
"go.uber.org/zap/zapcore" | ||
"go.uber.org/zap/zapio" | ||
"k8s.io/apimachinery/pkg/api/errors" | ||
"k8s.io/apimachinery/pkg/types" | ||
"k8s.io/client-go/informers" | ||
"k8s.io/client-go/kubernetes" | ||
"k8s.io/client-go/rest" | ||
"k8s.io/klog/v2" | ||
"knative.dev/pkg/changeset" | ||
"knative.dev/pkg/logging" | ||
"knative.dev/pkg/logging/logkey" | ||
"knative.dev/pkg/system" | ||
|
||
"github.com/aws/karpenter-core/pkg/operator/injection" | ||
) | ||
|
||
const ( | ||
loggerCfgConfigMapName = "config-logging" | ||
loggerCfgConfigMapKey = "zap-logger-config" | ||
) | ||
|
||
func DefaultZapConfig() zap.Config { | ||
cfg := zapdriver.NewProductionConfig() | ||
cfg.EncoderConfig.EncodeDuration = zapcore.StringDurationEncoder | ||
return cfg | ||
} | ||
|
||
// NewLogger returns a configured *zap.SugaredLogger | ||
func NewLogger(ctx context.Context, component string, kubernetesInterface kubernetes.Interface) *zap.SugaredLogger { | ||
if logger := loggerFromConfigMap(ctx, component, kubernetesInterface); logger != nil { | ||
if injection.GetOptions(ctx).LogLevel != nil { | ||
log.Fatalf(`--log-level cannot be set while using the "config-logging" ConfigMap`) | ||
} | ||
return logger | ||
} | ||
return defaultLogger(ctx) | ||
} | ||
|
||
func withCommit(logger *zap.SugaredLogger) *zap.SugaredLogger { | ||
revision := changeset.Get() | ||
if revision == changeset.Unknown { | ||
logger.Info("Unable to read vcs.revision from binary") | ||
return logger | ||
} | ||
// Enrich logs with the components git revision. | ||
return logger.With(zap.String(logkey.Commit, revision)) | ||
} | ||
|
||
func defaultLogger(ctx context.Context) *zap.SugaredLogger { | ||
cfg := DefaultZapConfig() | ||
if injection.GetOptions(ctx).LogLevel != nil { | ||
cfg.Level = lo.FromPtr(injection.GetOptions(ctx).LogLevel) | ||
} | ||
return withCommit(lo.Must(cfg.Build()).Sugar()) | ||
} | ||
|
||
func loggerFromConfigMap(ctx context.Context, component string, kubernetesInterface kubernetes.Interface) *zap.SugaredLogger { | ||
cancelCtx, cancel := context.WithTimeout(ctx, time.Second*5) | ||
defer cancel() | ||
|
||
factory := informers.NewSharedInformerFactoryWithOptions(kubernetesInterface, time.Second*30, informers.WithNamespace(system.Namespace())) | ||
informer := factory.Core().V1().ConfigMaps().Informer() | ||
factory.Start(cancelCtx.Done()) | ||
|
||
cm, err := injection.WaitForConfigMap(ctx, loggerCfgConfigMapName, informer) | ||
if err != nil { | ||
if errors.IsNotFound(err) { | ||
return nil | ||
} | ||
log.Fatalf("retrieving logging config map from %q", types.NamespacedName{Namespace: system.Namespace(), Name: loggerCfgConfigMapName}) | ||
} | ||
cfg := DefaultZapConfig() | ||
lo.Must0(json.Unmarshal([]byte(cm.Data[loggerCfgConfigMapKey]), &cfg)) | ||
|
||
if v := cm.Data[fmt.Sprintf("loglevel.%s", component)]; v != "" { | ||
cfg.Level = lo.Must(zap.ParseAtomicLevel(v)) | ||
} | ||
if injection.GetOptions(ctx).LogLevel != nil { | ||
cfg.Level = lo.FromPtr(injection.GetOptions(ctx).LogLevel) | ||
} | ||
return withCommit(lo.Must(cfg.Build()).Sugar()) | ||
} | ||
|
||
// 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) | ||
rest.SetDefaultWarningHandler(&logging.WarningHandler{Logger: logging.FromContext(ctx)}) | ||
} | ||
|
||
type ignoreDebugEventsSink struct { | ||
name string | ||
sink logr.LogSink | ||
} | ||
|
||
func (i ignoreDebugEventsSink) Init(ri logr.RuntimeInfo) { | ||
i.sink.Init(ri) | ||
} | ||
func (i ignoreDebugEventsSink) Enabled(level int) bool { return i.sink.Enabled(level) } | ||
func (i ignoreDebugEventsSink) Info(level int, msg string, keysAndValues ...interface{}) { | ||
// ignore debug "events" logs | ||
if level == 1 && i.name == "events" { | ||
return | ||
} | ||
i.sink.Info(level, msg, keysAndValues...) | ||
} | ||
func (i ignoreDebugEventsSink) Error(err error, msg string, keysAndValues ...interface{}) { | ||
i.sink.Error(err, msg, keysAndValues...) | ||
} | ||
func (i ignoreDebugEventsSink) WithValues(keysAndValues ...interface{}) logr.LogSink { | ||
return i.sink.WithValues(keysAndValues...) | ||
} | ||
func (i ignoreDebugEventsSink) WithName(name string) logr.LogSink { | ||
return &ignoreDebugEventsSink{name: name, sink: i.sink.WithName(name)} | ||
} | ||
|
||
// IgnoreDebugEvents wraps the logger with one that ignores any debug logs coming from a logger named "events". This | ||
// prevents every event we write from creating a debug log which spams the log file during scale-ups due to recording | ||
// pod scheduling decisions as events for visibility. | ||
func IgnoreDebugEvents(logger logr.Logger) logr.Logger { | ||
return logr.New(&ignoreDebugEventsSink{sink: logger.GetSink()}) | ||
} |
Oops, something went wrong.