diff --git a/controllers/ibpca/ibpca_controller.go b/controllers/ibpca/ibpca_controller.go index 937c618e..cb6384e2 100644 --- a/controllers/ibpca/ibpca_controller.go +++ b/controllers/ibpca/ibpca_controller.go @@ -42,6 +42,7 @@ import ( "github.com/IBM-Blockchain/fabric-operator/pkg/util" "github.com/go-test/deep" "github.com/pkg/errors" + "go.uber.org/zap" ctrl "sigs.k8s.io/controller-runtime" yaml "sigs.k8s.io/yaml" @@ -192,7 +193,10 @@ type ReconcileIBPCA struct { func (r *ReconcileIBPCA) Reconcile(ctx context.Context, request reconcile.Request) (reconcile.Result, error) { var err error - reqLogger := log.WithValues("Request.Namespace", request.Namespace, "Request.Name", request.Name) + reqLogger := r.Config.Logger.With( + zap.String("Request.Namespace", request.Namespace), + zap.String("Request.Name", request.Name), + ) // If ca-restart-config configmap is the object being reconciled, reconcile the // restart configmap. diff --git a/controllers/ibpca/ibpca_controller_test.go b/controllers/ibpca/ibpca_controller_test.go index cd2dba6b..b2ccb353 100644 --- a/controllers/ibpca/ibpca_controller_test.go +++ b/controllers/ibpca/ibpca_controller_test.go @@ -25,6 +25,7 @@ import ( "sync" current "github.com/IBM-Blockchain/fabric-operator/api/v1beta1" + opconfig "github.com/IBM-Blockchain/fabric-operator/operatorconfig" v1 "github.com/IBM-Blockchain/fabric-operator/pkg/apis/ca/v1" "github.com/IBM-Blockchain/fabric-operator/pkg/offering/common" "github.com/IBM-Blockchain/fabric-operator/pkg/util" @@ -116,7 +117,10 @@ var _ = Describe("ReconcileIBPCA", func() { scheme: &runtime.Scheme{}, update: map[string][]Update{}, mutex: &sync.Mutex{}, + Config: &opconfig.Config{}, } + zaplogger, _ := util.SetupLogging("DEBUG") + reconciler.Config.Logger = zaplogger request = reconcile.Request{ NamespacedName: types.NamespacedName{ Namespace: "test-namespace", diff --git a/controllers/ibpconsole/ibpconsole_controller.go b/controllers/ibpconsole/ibpconsole_controller.go index a2252c39..430c9857 100644 --- a/controllers/ibpconsole/ibpconsole_controller.go +++ b/controllers/ibpconsole/ibpconsole_controller.go @@ -39,6 +39,7 @@ import ( "github.com/IBM-Blockchain/fabric-operator/pkg/operatorerrors" "github.com/IBM-Blockchain/fabric-operator/pkg/util" "github.com/pkg/errors" + "go.uber.org/zap" "gopkg.in/yaml.v2" appsv1 "k8s.io/api/apps/v1" @@ -153,7 +154,10 @@ type ReconcileIBPConsole struct { func (r *ReconcileIBPConsole) Reconcile(ctx context.Context, request reconcile.Request) (reconcile.Result, error) { var err error - reqLogger := log.WithValues("Request.Namespace", request.Namespace, "Request.Name", request.Name) + reqLogger := r.Config.Logger.With( + zap.String("Request.Namespace", request.Namespace), + zap.String("Request.Name", request.Name), + ) reqLogger.Info(fmt.Sprintf("Reconciling IBPConsole with update values of [ %+v ]", r.update.GetUpdateStackWithTrues())) // Fetch the IBPConsole instance diff --git a/controllers/ibpconsole/ibpconsole_controller_test.go b/controllers/ibpconsole/ibpconsole_controller_test.go index ee562bf8..e9d9fe04 100644 --- a/controllers/ibpconsole/ibpconsole_controller_test.go +++ b/controllers/ibpconsole/ibpconsole_controller_test.go @@ -22,16 +22,16 @@ import ( "context" "fmt" - . "github.com/onsi/ginkgo/v2" - . "github.com/onsi/gomega" - "github.com/pkg/errors" - current "github.com/IBM-Blockchain/fabric-operator/api/v1beta1" consolemocks "github.com/IBM-Blockchain/fabric-operator/controllers/ibpconsole/mocks" "github.com/IBM-Blockchain/fabric-operator/controllers/mocks" config "github.com/IBM-Blockchain/fabric-operator/operatorconfig" "github.com/IBM-Blockchain/fabric-operator/pkg/offering/common" "github.com/IBM-Blockchain/fabric-operator/pkg/operatorerrors" + "github.com/IBM-Blockchain/fabric-operator/pkg/util" + . "github.com/onsi/ginkgo/v2" + . "github.com/onsi/gomega" + "github.com/pkg/errors" corev1 "k8s.io/api/core/v1" k8serror "k8s.io/apimachinery/pkg/api/errors" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" @@ -118,6 +118,8 @@ var _ = Describe("ReconcileIBPConsole", func() { client: mockKubeClient, scheme: &runtime.Scheme{}, } + zaplogger, _ := util.SetupLogging("DEBUG") + reconciler.Config.Logger = zaplogger request = reconcile.Request{ NamespacedName: types.NamespacedName{ Namespace: "test-namespace", diff --git a/controllers/ibporderer/ibporderer_controller.go b/controllers/ibporderer/ibporderer_controller.go index 54a1dd13..b899f1a8 100644 --- a/controllers/ibporderer/ibporderer_controller.go +++ b/controllers/ibporderer/ibporderer_controller.go @@ -43,6 +43,7 @@ import ( "github.com/IBM-Blockchain/fabric-operator/pkg/util" "github.com/IBM-Blockchain/fabric-operator/version" "github.com/pkg/errors" + "go.uber.org/zap" yaml "sigs.k8s.io/yaml" appsv1 "k8s.io/api/apps/v1" @@ -183,8 +184,10 @@ type ReconcileIBPOrderer struct { func (r *ReconcileIBPOrderer) Reconcile(ctx context.Context, request reconcile.Request) (reconcile.Result, error) { var err error - reqLogger := log.WithValues("Request.Namespace", request.Namespace, "Request.Name", request.Name) - + reqLogger := r.Config.Logger.With( + zap.String("Request.Namespace", request.Namespace), + zap.String("Request.Name", request.Name), + ) // If orderer-restart-config configmap is the object being reconciled, reconcile the // restart configmap. if request.Name == "orderer-restart-config" { diff --git a/controllers/ibporderer/ibporderer_controller_test.go b/controllers/ibporderer/ibporderer_controller_test.go index 0a50a0cf..20734c11 100644 --- a/controllers/ibporderer/ibporderer_controller_test.go +++ b/controllers/ibporderer/ibporderer_controller_test.go @@ -33,10 +33,12 @@ import ( current "github.com/IBM-Blockchain/fabric-operator/api/v1beta1" orderermocks "github.com/IBM-Blockchain/fabric-operator/controllers/ibporderer/mocks" "github.com/IBM-Blockchain/fabric-operator/controllers/mocks" + opconfig "github.com/IBM-Blockchain/fabric-operator/operatorconfig" v1 "github.com/IBM-Blockchain/fabric-operator/pkg/apis/orderer/v1" config "github.com/IBM-Blockchain/fabric-operator/pkg/initializer/orderer/config/v1" "github.com/IBM-Blockchain/fabric-operator/pkg/offering/common" "github.com/IBM-Blockchain/fabric-operator/pkg/operatorerrors" + "github.com/IBM-Blockchain/fabric-operator/pkg/util" corev1 "k8s.io/api/core/v1" k8serror "k8s.io/apimachinery/pkg/api/errors" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" @@ -122,7 +124,10 @@ var _ = Describe("ReconcileIBPOrderer", func() { scheme: &runtime.Scheme{}, update: map[string][]Update{}, mutex: &sync.Mutex{}, + Config: &opconfig.Config{}, } + zaplogger, _ := util.SetupLogging("DEBUG") + reconciler.Config.Logger = zaplogger request = reconcile.Request{ NamespacedName: types.NamespacedName{ Namespace: "test-namespace", diff --git a/controllers/ibppeer/ibppeer_controller.go b/controllers/ibppeer/ibppeer_controller.go index 55abca96..85f94b45 100644 --- a/controllers/ibppeer/ibppeer_controller.go +++ b/controllers/ibppeer/ibppeer_controller.go @@ -42,6 +42,7 @@ import ( "github.com/IBM-Blockchain/fabric-operator/pkg/util" "github.com/IBM-Blockchain/fabric-operator/version" "github.com/pkg/errors" + "go.uber.org/zap" "k8s.io/apiextensions-apiserver/pkg/client/clientset/clientset" yaml "sigs.k8s.io/yaml" @@ -203,8 +204,10 @@ type ReconcileIBPPeer struct { func (r *ReconcileIBPPeer) Reconcile(ctx context.Context, request reconcile.Request) (reconcile.Result, error) { var err error - reqLogger := log.WithValues("Request.Namespace", request.Namespace, "Request.Name", request.Name) - + reqLogger := r.Config.Logger.With( + zap.String("Request.Namespace", request.Namespace), + zap.String("Request.Name", request.Name), + ) // If peer-restart-config configmap is the object being reconciled, reconcile the // restart configmap. if request.Name == "peer-restart-config" { diff --git a/controllers/ibppeer/ibppeer_controller_test.go b/controllers/ibppeer/ibppeer_controller_test.go index 85cee8bb..c08d8fdc 100644 --- a/controllers/ibppeer/ibppeer_controller_test.go +++ b/controllers/ibppeer/ibppeer_controller_test.go @@ -24,6 +24,7 @@ import ( "fmt" "sync" + opconfig "github.com/IBM-Blockchain/fabric-operator/operatorconfig" . "github.com/onsi/ginkgo/v2" . "github.com/onsi/gomega" "k8s.io/utils/pointer" @@ -33,6 +34,7 @@ import ( "github.com/IBM-Blockchain/fabric-operator/controllers/mocks" "github.com/IBM-Blockchain/fabric-operator/pkg/offering/common" "github.com/IBM-Blockchain/fabric-operator/pkg/operatorerrors" + "github.com/IBM-Blockchain/fabric-operator/pkg/util" corev1 "k8s.io/api/core/v1" k8serror "k8s.io/apimachinery/pkg/api/errors" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" @@ -125,7 +127,10 @@ var _ = Describe("ReconcileIBPPeer", func() { scheme: &runtime.Scheme{}, update: map[string][]Update{}, mutex: &sync.Mutex{}, + Config: &opconfig.Config{}, } + zaplogger, _ := util.SetupLogging("DEBUG") + reconciler.Config.Logger = zaplogger request = reconcile.Request{ NamespacedName: types.NamespacedName{ Namespace: "test-namespace", diff --git a/integration/operator.go b/integration/operator.go index 5922a7d7..dab8cba4 100644 --- a/integration/operator.go +++ b/integration/operator.go @@ -21,7 +21,6 @@ package integration import ( "context" "fmt" - "os" "path/filepath" config "github.com/IBM-Blockchain/fabric-operator/operatorconfig" @@ -31,21 +30,18 @@ import ( cainit "github.com/IBM-Blockchain/fabric-operator/pkg/initializer/ca" ordererinit "github.com/IBM-Blockchain/fabric-operator/pkg/initializer/orderer" peerinit "github.com/IBM-Blockchain/fabric-operator/pkg/initializer/peer" - uzap "go.uber.org/zap" + "github.com/IBM-Blockchain/fabric-operator/pkg/util" corev1 "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" - "sigs.k8s.io/controller-runtime/pkg/log/zap" ) // GetOperatorConfig returns the operator configuration with the default templating files population // and with default versions set for components. func GetOperatorConfig(configs, caFiles, peerFiles, ordererFiles, consoleFiles string) *config.Config { - ulevel := uzap.NewAtomicLevelAt(2) - if os.Getenv("LOG_LEVEL") == "debug" { - ulevel = uzap.NewAtomicLevelAt(-1) + zaplogger, err := util.SetupLogging("DEBUG") + if err != nil { + fmt.Print("error initiating the logger", err) } - level := zap.Level(&ulevel) - logger := zap.New(zap.Opts(level)) cfg := &config.Config{ CAInitConfig: &cainit.Config{ @@ -119,7 +115,7 @@ func GetOperatorConfig(configs, caFiles, peerFiles, ordererFiles, consoleFiles s NetworkPolicyIngressFile: filepath.Join(consoleFiles, "networkpolicy-ingress.yaml"), NetworkPolicyDenyAllFile: filepath.Join(consoleFiles, "networkpolicy-denyall.yaml"), }, - Logger: &logger, + Logger: zaplogger, Operator: config.Operator{ Restart: config.Restart{ Timeout: common.MustParseDuration("5m"), diff --git a/main.go b/main.go index ba376331..a73b6f28 100644 --- a/main.go +++ b/main.go @@ -19,6 +19,7 @@ package main import ( + "fmt" "path/filepath" "time" @@ -27,6 +28,7 @@ import ( cainit "github.com/IBM-Blockchain/fabric-operator/pkg/initializer/ca" ordererinit "github.com/IBM-Blockchain/fabric-operator/pkg/initializer/orderer" peerinit "github.com/IBM-Blockchain/fabric-operator/pkg/initializer/peer" + "github.com/IBM-Blockchain/fabric-operator/pkg/util" logf "sigs.k8s.io/controller-runtime/pkg/log" @@ -71,7 +73,11 @@ func main() { setDefaultConsoleDefinitions(operatorCfg) operatorCfg.Operator.SetDefaults() - + zaplogger, err := util.SetupLogging("DEBUG") + if err != nil { + fmt.Print("error initiating the logger", err) + } + operatorCfg.Logger = zaplogger if err := command.Operator(operatorCfg); err != nil { log.Error(err, "failed to start operator") time.Sleep(15 * time.Second) diff --git a/operatorconfig/config.go b/operatorconfig/config.go index 961a3513..8884572e 100644 --- a/operatorconfig/config.go +++ b/operatorconfig/config.go @@ -23,7 +23,7 @@ import ( ordererinit "github.com/IBM-Blockchain/fabric-operator/pkg/initializer/orderer" peerinit "github.com/IBM-Blockchain/fabric-operator/pkg/initializer/peer" "github.com/IBM-Blockchain/fabric-operator/pkg/offering" - "github.com/go-logr/logr" + "go.uber.org/zap" ) type Config struct { @@ -33,7 +33,7 @@ type Config struct { ConsoleInitConfig *ConsoleConfig Offering offering.Type Operator Operator - Logger *logr.Logger + Logger *zap.Logger } type ConsoleConfig struct { diff --git a/pkg/command/operator.go b/pkg/command/operator.go index ec584045..8ce55085 100644 --- a/pkg/command/operator.go +++ b/pkg/command/operator.go @@ -26,8 +26,10 @@ import ( "runtime" "time" + "go.uber.org/zap/zapcore" k8sruntime "k8s.io/apimachinery/pkg/runtime" + "github.com/go-logr/zapr" routev1 "github.com/openshift/api/route/v1" "github.com/operator-framework/operator-lib/leader" utilruntime "k8s.io/apimachinery/pkg/util/runtime" @@ -49,6 +51,7 @@ import ( _ "k8s.io/client-go/plugin/pkg/client/auth/gcp" "sigs.k8s.io/controller-runtime/pkg/client" + uberzap "go.uber.org/zap" logf "sigs.k8s.io/controller-runtime/pkg/log" "sigs.k8s.io/controller-runtime/pkg/manager/signals" ) @@ -102,8 +105,20 @@ func OperatorWithSignal(operatorCfg *oconfig.Config, signalHandler context.Conte // be propagated through the whole operator, generating // uniform and structured logs. if operatorCfg.Logger != nil { - logf.SetLogger(*operatorCfg.Logger) - ctrl.SetLogger(*operatorCfg.Logger) + + config := uberzap.NewProductionConfig() + config.EncoderConfig.TimeKey = "timestamp" + config.EncoderConfig.EncodeTime = zapcore.ISO8601TimeEncoder + logger, err := config.Build() + if err != nil { + return err + } + + // Wrap the zap.Logger with go-logr/zapr to satisfy the logr.Logger interface + log := zapr.NewLogger(logger) + + logf.SetLogger(log) + ctrl.SetLogger(log) } else { // Use the unstructured log formatter when running locally. logf.SetLogger(zap.New(zap.UseDevMode(local))) diff --git a/pkg/util/util.go b/pkg/util/util.go index a43443cd..aca30ea5 100644 --- a/pkg/util/util.go +++ b/pkg/util/util.go @@ -41,6 +41,8 @@ import ( "github.com/IBM-Blockchain/fabric-operator/pkg/k8s/clientset" routev1 "github.com/openshift/api/route/v1" "github.com/pkg/errors" + uberzap "go.uber.org/zap" + "go.uber.org/zap/zapcore" appsv1 "k8s.io/api/apps/v1" corev1 "k8s.io/api/core/v1" networkingv1 "k8s.io/api/networking/v1" @@ -942,3 +944,24 @@ func GetServerVersion() (*version.Info, error) { } return version, nil } + +func SetupLogging(loglevel string) (*uberzap.Logger, error) { + // set up logging + var level zapcore.Level + err := level.Set(loglevel) + if err != nil { + return nil, err + } + zapConfig := uberzap.NewProductionConfig() + zapConfig.Level = uberzap.NewAtomicLevelAt(level) + zapConfig.EncoderConfig.EncodeTime = zapcore.ISO8601TimeEncoder + logger, err := zapConfig.Build() + defer logger.Sync() + if err != nil { + return nil, err + } + // redirect uses of standard logger + uberzap.RedirectStdLog(logger) + + return logger, nil +}