1 /* 2 Copyright 2019 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 zap 18 19 import ( 20 "fmt" 21 "reflect" 22 23 "go.uber.org/zap/buffer" 24 "go.uber.org/zap/zapcore" 25 "k8s.io/apimachinery/pkg/api/meta" 26 "k8s.io/apimachinery/pkg/runtime" 27 ) 28 29 // KubeAwareEncoder is a Kubernetes-aware Zap Encoder. 30 // Instead of trying to force Kubernetes objects to implement 31 // ObjectMarshaller, we just implement a wrapper around a normal 32 // ObjectMarshaller that checks for Kubernetes objects. 33 type KubeAwareEncoder struct { 34 // Encoder is the zapcore.Encoder that this encoder delegates to 35 zapcore.Encoder 36 37 // Verbose controls whether or not the full object is printed. 38 // If false, only name, namespace, api version, and kind are printed. 39 // Otherwise, the full object is logged. 40 Verbose bool 41 } 42 43 // kubeObjectWrapper is a zapcore.ObjectMarshaler for Kubernetes objects. 44 type kubeObjectWrapper struct { 45 obj runtime.Object 46 } 47 48 // MarshalLogObject implements zapcore.ObjectMarshaler. 49 func (w kubeObjectWrapper) MarshalLogObject(enc zapcore.ObjectEncoder) error { 50 // TODO(directxman12): log kind and apiversion if not set explicitly (common case) 51 // -- needs an a scheme to convert to the GVK. 52 53 if reflect.ValueOf(w.obj).IsNil() { 54 return fmt.Errorf("got nil for runtime.Object") 55 } 56 57 if gvk := w.obj.GetObjectKind().GroupVersionKind(); gvk.Version != "" { 58 enc.AddString("apiVersion", gvk.GroupVersion().String()) 59 enc.AddString("kind", gvk.Kind) 60 } 61 62 objMeta, err := meta.Accessor(w.obj) 63 if err != nil { 64 return fmt.Errorf("got runtime.Object without object metadata: %v", w.obj) 65 } 66 67 if ns := objMeta.GetNamespace(); ns != "" { 68 enc.AddString("namespace", ns) 69 } 70 enc.AddString("name", objMeta.GetName()) 71 72 return nil 73 } 74 75 // NB(directxman12): can't just override AddReflected, since the encoder calls AddReflected on itself directly 76 77 // Clone implements zapcore.Encoder. 78 func (k *KubeAwareEncoder) Clone() zapcore.Encoder { 79 return &KubeAwareEncoder{ 80 Encoder: k.Encoder.Clone(), 81 } 82 } 83 84 // EncodeEntry implements zapcore.Encoder. 85 func (k *KubeAwareEncoder) EncodeEntry(entry zapcore.Entry, fields []zapcore.Field) (*buffer.Buffer, error) { 86 if k.Verbose { 87 // Kubernetes objects implement fmt.Stringer, so if we 88 // want verbose output, just delegate to that. 89 return k.Encoder.EncodeEntry(entry, fields) 90 } 91 92 for i, field := range fields { 93 // intercept stringer fields that happen to be Kubernetes runtime.Object or 94 // types.NamespacedName values (Kubernetes runtime.Objects commonly 95 // implement String, apparently). 96 // *unstructured.Unstructured does NOT implement fmt.Striger interface. 97 // We have handle it specially. 98 if field.Type == zapcore.StringerType || field.Type == zapcore.ReflectType { 99 switch val := field.Interface.(type) { 100 case runtime.Object: 101 fields[i] = zapcore.Field{ 102 Type: zapcore.ObjectMarshalerType, 103 Key: field.Key, 104 Interface: kubeObjectWrapper{obj: val}, 105 } 106 } 107 } 108 } 109 110 return k.Encoder.EncodeEntry(entry, fields) 111 } 112