|
| 1 | +/* |
| 2 | +Copyright 2025 The Kubernetes Authors. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +// Package logr contains helpers for setting up a Kubernetes object aware logr.Logger. |
| 18 | +package logr |
| 19 | + |
| 20 | +import ( |
| 21 | + "reflect" |
| 22 | + |
| 23 | + "github.com/go-logr/logr" |
| 24 | + "k8s.io/apimachinery/pkg/api/meta" |
| 25 | + "k8s.io/apimachinery/pkg/runtime" |
| 26 | +) |
| 27 | + |
| 28 | +var _ logr.LogSink = (*KubeAwareSink)(nil) |
| 29 | + |
| 30 | +// KubeAwareSink is a logr.LogSink that understands Kubernetes objects. |
| 31 | +type KubeAwareSink struct { |
| 32 | + sink logr.LogSink |
| 33 | +} |
| 34 | + |
| 35 | +// KubeAware wraps a logr.logger to make it aware of Kubernetes objects. |
| 36 | +func KubeAware(logger logr.Logger) logr.Logger { |
| 37 | + return logr.New( |
| 38 | + &KubeAwareSink{logger.GetSink()}, |
| 39 | + ) |
| 40 | +} |
| 41 | + |
| 42 | +// Init implements logr.LogSink. |
| 43 | +func (k *KubeAwareSink) Init(info logr.RuntimeInfo) { |
| 44 | + k.sink.Init(info) |
| 45 | +} |
| 46 | + |
| 47 | +// Enabled implements logr.LogSink. |
| 48 | +func (k *KubeAwareSink) Enabled(level int) bool { |
| 49 | + return k.sink.Enabled(level) |
| 50 | +} |
| 51 | + |
| 52 | +// Info implements logr.LogSink. |
| 53 | +func (k *KubeAwareSink) Info(level int, msg string, keysAndValues ...interface{}) { |
| 54 | + k.sink.Info(level, msg, k.wrapKeyAndValues(keysAndValues)...) |
| 55 | +} |
| 56 | + |
| 57 | +// Error implements logr.LogSink. |
| 58 | +func (k *KubeAwareSink) Error(err error, msg string, keysAndValues ...interface{}) { |
| 59 | + k.sink.Error(err, msg, k.wrapKeyAndValues(keysAndValues)...) |
| 60 | +} |
| 61 | + |
| 62 | +// WithValues implements logr.LogSink. |
| 63 | +func (k *KubeAwareSink) WithValues(keysAndValues ...interface{}) logr.LogSink { |
| 64 | + return &KubeAwareSink{ |
| 65 | + sink: k.sink.WithValues(k.wrapKeyAndValues(keysAndValues)...), |
| 66 | + } |
| 67 | +} |
| 68 | + |
| 69 | +// WithName implements logr.LogSink. |
| 70 | +func (k *KubeAwareSink) WithName(name string) logr.LogSink { |
| 71 | + return &KubeAwareSink{ |
| 72 | + sink: k.sink.WithName(name), |
| 73 | + } |
| 74 | +} |
| 75 | + |
| 76 | +// wrapKeyAndValues replaces Kubernetes objects with [kubeObjectWrapper]. |
| 77 | +func (k *KubeAwareSink) wrapKeyAndValues(keysAndValues []interface{}) []interface{} { |
| 78 | + result := make([]interface{}, len(keysAndValues)) |
| 79 | + for i, item := range keysAndValues { |
| 80 | + if i%2 == 0 { |
| 81 | + // item is key, no need to resolve |
| 82 | + result[i] = item |
| 83 | + continue |
| 84 | + } |
| 85 | + |
| 86 | + switch val := item.(type) { |
| 87 | + case runtime.Object: |
| 88 | + result[i] = &kubeObjectWrapper{obj: val} |
| 89 | + default: |
| 90 | + result[i] = item |
| 91 | + } |
| 92 | + } |
| 93 | + return result |
| 94 | +} |
| 95 | + |
| 96 | +var _ logr.Marshaler = (*kubeObjectWrapper)(nil) |
| 97 | + |
| 98 | +// kubeObjectWrapper is a wrapper around runtime.Object that implements logr.Marshaler. |
| 99 | +type kubeObjectWrapper struct { |
| 100 | + obj runtime.Object |
| 101 | +} |
| 102 | + |
| 103 | +// MarshalLog implements logr.Marshaler. |
| 104 | +// The implementation mirrors the behavior of kubeObjectWrapper.MarshalLogObject. |
| 105 | +func (w *kubeObjectWrapper) MarshalLog() interface{} { |
| 106 | + result := make(map[string]string) |
| 107 | + |
| 108 | + if reflect.ValueOf(w.obj).IsNil() { |
| 109 | + return "got nil for runtime.Object" |
| 110 | + } |
| 111 | + |
| 112 | + if gvk := w.obj.GetObjectKind().GroupVersionKind(); gvk.Version != "" { |
| 113 | + result["apiVersion"] = gvk.GroupVersion().String() |
| 114 | + result["kind"] = gvk.Kind |
| 115 | + } |
| 116 | + |
| 117 | + objMeta, err := meta.Accessor(w.obj) |
| 118 | + if err != nil { |
| 119 | + return result |
| 120 | + } |
| 121 | + |
| 122 | + if ns := objMeta.GetNamespace(); ns != "" { |
| 123 | + result["namespace"] = ns |
| 124 | + } |
| 125 | + result["name"] = objMeta.GetName() |
| 126 | + return result |
| 127 | +} |
0 commit comments