Skip to content

Commit

Permalink
Fixed intergration for operator
Browse files Browse the repository at this point in the history
  • Loading branch information
Shoaeb Jindani committed Apr 4, 2024
1 parent 09f50f6 commit d1a475c
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 48 deletions.
20 changes: 11 additions & 9 deletions integration/operator.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ package integration
import (
"context"
"fmt"
"os"
"path/filepath"

config "github.com/IBM-Blockchain/fabric-operator/operatorconfig"
Expand All @@ -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{
Expand Down Expand Up @@ -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"),
Expand Down
62 changes: 23 additions & 39 deletions pkg/command/operator.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"runtime"
"time"

"go.uber.org/zap/zapcore"
k8sruntime "k8s.io/apimachinery/pkg/runtime"

"github.com/go-logr/zapr"
Expand All @@ -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")
Expand All @@ -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))
}

Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -169,31 +170,14 @@ 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,
// LeaderElection: enableLeaderElection,
LeaderElectionID: "c30dd930.ibp.com",
LeaderElectionNamespace: operatorNamespace,
Namespace: watchNamespace,
Logger: log,
})
if err != nil {
setupLog.Error(err, "unable to start manager")
Expand Down

0 comments on commit d1a475c

Please sign in to comment.