From d1a475cbd2865b3a58ee9e3900500d73416a019d Mon Sep 17 00:00:00 2001 From: Shoaeb Jindani Date: Thu, 4 Apr 2024 14:00:16 +0530 Subject: [PATCH] Fixed intergration for operator --- integration/operator.go | 20 +++++++------ pkg/command/operator.go | 62 +++++++++++++++-------------------------- 2 files changed, 34 insertions(+), 48 deletions(-) diff --git a/integration/operator.go b/integration/operator.go index 5922a7d7..7ee7017f 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,24 @@ 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) + // ulevel := uzap.NewAtomicLevelAt(2) + // if os.Getenv("LOG_LEVEL") == "debug" { + // ulevel = uzap.NewAtomicLevelAt(-1) + // } + // level := zap.Level(&ulevel) + // logger := zap.New(zap.Opts(level)) + 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 +121,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/pkg/command/operator.go b/pkg/command/operator.go index 064a2535..b910e61d 100644 --- a/pkg/command/operator.go +++ b/pkg/command/operator.go @@ -26,6 +26,7 @@ import ( "runtime" "time" + "go.uber.org/zap/zapcore" k8sruntime "k8s.io/apimachinery/pkg/runtime" "github.com/go-logr/zapr" @@ -46,18 +47,17 @@ import ( "github.com/IBM-Blockchain/fabric-operator/pkg/offering" openshiftv1 "github.com/openshift/api/config/v1" - commonutility "github.com/IBM-Blockchain/fabric-operator/pkg/util" - uberzap "go.uber.org/zap" - "go.uber.org/zap/zapcore" "k8s.io/apimachinery/pkg/types" _ "k8s.io/client-go/plugin/pkg/client/auth/gcp" - _ "k8s.io/client-go/plugin/pkg/client/auth/oidc" "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" ) -var log *uberzap.SugaredLogger -var c1 commonutility.Client +var log = logf.Log.WithName("cmd_operator") + var ( scheme = k8sruntime.NewScheme() setupLog = ctrl.Log.WithName("setup") @@ -70,7 +70,7 @@ func init() { } func printVersion() { - log.Info("Go Version: %s", runtime.Version()) + log.Info(fmt.Sprintf("Go Version: %s", runtime.Version())) log.Info(fmt.Sprintf("Go OS/Arch: %s/%s", runtime.GOOS, runtime.GOARCH)) } @@ -105,23 +105,24 @@ 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(zap.New(zap.UseDevMode(false))) - log = operatorCfg.Logger.Sugar().Named("operator") - _ = err - log.Info("Installing operator in all namespace mode") - } else { - // Use the unstructured log formatter when running locally. - //logf.SetLogger(zap.New(zap.UseDevMode(false))) - ctrl.SetLogger(zap.New(zap.UseDevMode(false))) - // Create logger + config := uberzap.NewProductionConfig() + config.EncoderConfig.TimeKey = "timestamp" + config.EncoderConfig.EncodeTime = zapcore.ISO8601TimeEncoder + logger, err := config.Build() + if err != nil { + panic(fmt.Sprintf("failed to initialize logger: %v", err)) + } - //logm, err := util.SetupLogging("DEBUG") - log = operatorCfg.Logger.Sugar().Named("operator") - _ = err - log.Info("Installing operator in all namespace mode") + // 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))) + ctrl.SetLogger(zap.New(zap.UseDevMode(true))) } printVersion() @@ -169,23 +170,7 @@ func OperatorWithSignal(operatorCfg *oconfig.Config, signalHandler context.Conte } flag.Parse() - // Create a new logger with UTC format - config := uberzap.NewProductionConfig() - config.EncoderConfig.TimeKey = "timestamp" - config.EncoderConfig.EncodeTime = zapcore.ISO8601TimeEncoder - logger, err := config.Build() - if err != nil { - panic(fmt.Sprintf("failed to initialize logger: %v", err)) - } - log := zapr.NewLogger(logger) - - // Log some messages - log.Info("Logging in UTC format", "time", time.Now().UTC()) - - // Example of logging with additional context - log.Info("Example with additional context", "key", "value") - - mgr, err := ctrl.New(ctrl.GetConfigOrDie(), ctrl.Options{ + mgr, err := ctrl.NewManager(ctrl.GetConfigOrDie(), ctrl.Options{ Scheme: scheme, MetricsBindAddress: metricsAddr, Port: 9443, @@ -193,7 +178,6 @@ func OperatorWithSignal(operatorCfg *oconfig.Config, signalHandler context.Conte LeaderElectionID: "c30dd930.ibp.com", LeaderElectionNamespace: operatorNamespace, Namespace: watchNamespace, - Logger: log, }) if err != nil { setupLog.Error(err, "unable to start manager")