github.com/kubeflow/training-operator@v1.7.0/pkg/util/logger.go (about) 1 // Copyright 2018 The Kubeflow Authors 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package util 16 17 import ( 18 "strings" 19 20 log "github.com/sirupsen/logrus" 21 v1 "k8s.io/api/core/v1" 22 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" 23 metav1unstructured "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" 24 ) 25 26 func LoggerForReplica(job metav1.Object, rtype string) *log.Entry { 27 return log.WithFields(log.Fields{ 28 // We use job to match the key used in controller.go 29 // Its more common in K8s to use a period to indicate namespace.name. So that's what we use. 30 "job": job.GetNamespace() + "." + job.GetName(), 31 "uid": job.GetUID(), 32 "replica-type": rtype, 33 }) 34 } 35 36 func LoggerForJob(job metav1.Object) *log.Entry { 37 return log.WithFields(log.Fields{ 38 // We use job to match the key used in controller.go 39 // Its more common in K8s to use a period to indicate namespace.name. So that's what we use. 40 "job": job.GetNamespace() + "." + job.GetName(), 41 "uid": job.GetUID(), 42 }) 43 } 44 45 func LoggerForPod(pod *v1.Pod, kind string) *log.Entry { 46 job := "" 47 if controllerRef := metav1.GetControllerOf(pod); controllerRef != nil { 48 if controllerRef.Kind == kind { 49 job = pod.Namespace + "." + controllerRef.Name 50 } 51 } 52 return log.WithFields(log.Fields{ 53 // We use job to match the key used in controller.go 54 // In controller.go we log the key used with the workqueue. 55 "job": job, 56 "pod": pod.Namespace + "." + pod.Name, 57 "uid": pod.ObjectMeta.UID, 58 }) 59 } 60 61 func LoggerForService(svc *v1.Service, kind string) *log.Entry { 62 job := "" 63 if controllerRef := metav1.GetControllerOf(svc); controllerRef != nil { 64 if controllerRef.Kind == kind { 65 job = svc.Namespace + "." + controllerRef.Name 66 } 67 } 68 return log.WithFields(log.Fields{ 69 // We use job to match the key used in controller.go 70 // In controller.go we log the key used with the workqueue. 71 "job": job, 72 "service": svc.Namespace + "." + svc.Name, 73 "uid": svc.ObjectMeta.UID, 74 }) 75 } 76 77 func LoggerForKey(key string) *log.Entry { 78 return log.WithFields(log.Fields{ 79 // The key used by the workQueue should be namespace + "/" + name. 80 // Its more common in K8s to use a period to indicate namespace.name. So that's what we use. 81 "job": strings.Replace(key, "/", ".", -1), 82 }) 83 } 84 85 func LoggerForUnstructured(obj *metav1unstructured.Unstructured, kind string) *log.Entry { 86 job := "" 87 if obj.GetKind() == kind { 88 job = obj.GetNamespace() + "." + obj.GetName() 89 } 90 return log.WithFields(log.Fields{ 91 // We use job to match the key used in controller.go 92 // In controller.go we log the key used with the workqueue. 93 "job": job, 94 "uid": obj.GetUID(), 95 }) 96 }