github.com/verrazzano/verrazzano@v1.7.1/pkg/log/vzlog/vzlog.go (about) 1 // Copyright (c) 2022, 2023, Oracle and/or its affiliates. 2 // Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl. 3 4 package vzlog 5 6 import ( 7 modulelog "github.com/verrazzano/verrazzano-modules/pkg/vzlog" 8 9 "go.uber.org/zap" 10 ) 11 12 // ResourceConfig is the configuration of a logger for a resource that is being reconciled 13 type ResourceConfig struct { 14 // Name is the name of the resource 15 Name string 16 17 // Namespace is the namespace of the resource 18 Namespace string 19 20 // ID is the resource uid 21 ID string 22 23 // Generation is the resource generation 24 Generation int64 25 26 // Controller name is the name of the controller 27 ControllerName string 28 } 29 30 // VerrazzanoLogger is a logger interface that provides sugared and progress logging 31 type VerrazzanoLogger modulelog.VerrazzanoLogger 32 33 // DefaultLogger ensures the default logger exists. This is typically used for testing 34 func DefaultLogger() VerrazzanoLogger { 35 return modulelog.DefaultLogger() 36 } 37 38 // EnsureResourceLogger ensures that a logger exists for a specific generation of a Kubernetes resource. 39 // When a resource is getting reconciled, the status may frequently get updated during 40 // the reconciliation. This is the case for the Verrazzano resource. As a result, 41 // the controller-runtime queue gets filled with updated instances of a resource that 42 // have the same generation. The side-effect is that after a resource is completely reconciled, 43 // the controller Reconcile method may still be called many times. In this case, the existing 44 // context must be used so that 'once' and 'progress' messages don't start from a new context, 45 // causing them to be displayed when they shouldn't. This mehod ensures that the same 46 // logger is used for a given resource and generation. 47 func EnsureResourceLogger(config *ResourceConfig) (VerrazzanoLogger, error) { 48 return modulelog.EnsureResourceLogger(copyModuleConfig(config)) 49 } 50 51 func ForZapLogger(config *ResourceConfig, zaplog *zap.SugaredLogger) VerrazzanoLogger { 52 return modulelog.ForZapLogger(copyModuleConfig(config), zaplog) 53 } 54 55 func copyModuleConfig(conf *ResourceConfig) *modulelog.ResourceConfig { 56 return &modulelog.ResourceConfig{ 57 Name: conf.Name, 58 Namespace: conf.Namespace, 59 ID: conf.ID, 60 Generation: conf.Generation, 61 ControllerName: conf.ControllerName, 62 } 63 }