Skip to content

Commit

Permalink
put back the config to write logs to the file instead of only stdout …
Browse files Browse the repository at this point in the history
…logs (#2107)

* put log configuration write back

* add log close() back
  • Loading branch information
paulyufan2 authored Aug 9, 2023
1 parent e6a8ea6 commit e9ddb87
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 30 deletions.
20 changes: 16 additions & 4 deletions cni/ipam/plugin/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@ import (

"github.com/Azure/azure-container-networking/cni"
"github.com/Azure/azure-container-networking/cni/ipam"
"github.com/Azure/azure-container-networking/cni/log"
zaplog "github.com/Azure/azure-container-networking/cni/log"
"github.com/Azure/azure-container-networking/common"
"github.com/Azure/azure-container-networking/log"
"go.uber.org/zap/zapcore"
)

Expand All @@ -30,14 +31,25 @@ func main() {
var config common.PluginConfig
config.Version = version

loggerCfg := &log.Config{
logDirectory := "" // Sets the current location as log directory

log.SetName(name)
log.SetLevel(log.LevelInfo)
if err := log.SetTargetLogDirectory(log.TargetLogfile, logDirectory); err != nil {
fmt.Printf("Failed to setup cni logging: %v\n", err)
return
}

defer log.Close()

loggerCfg := &zaplog.Config{
Level: zapcore.DebugLevel,
LogPath: log.LogPath + "azure-ipam.log",
LogPath: zaplog.LogPath + "azure-ipam.log",
MaxSizeInMB: maxLogFileSizeInMb,
MaxBackups: maxLogFileCount,
Name: name,
}
log.Initialize(ctx, loggerCfg)
zaplog.Initialize(ctx, loggerCfg)

ipamPlugin, err := ipam.NewPlugin(name, &config)
if err != nil {
Expand Down
20 changes: 16 additions & 4 deletions cni/ipam/pluginv6/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@ import (

"github.com/Azure/azure-container-networking/cni"
"github.com/Azure/azure-container-networking/cni/ipam"
"github.com/Azure/azure-container-networking/cni/log"
zaplog "github.com/Azure/azure-container-networking/cni/log"
"github.com/Azure/azure-container-networking/common"
"github.com/Azure/azure-container-networking/log"
"go.uber.org/zap/zapcore"
)

Expand All @@ -30,14 +31,25 @@ func main() {
var config common.PluginConfig
config.Version = version

loggerCfg := &log.Config{
logDirectory := "" // Sets the current location as log directory

log.SetName(name)
log.SetLevel(log.LevelInfo)
if err := log.SetTargetLogDirectory(log.TargetLogfile, logDirectory); err != nil {
fmt.Printf("Failed to setup cni logging: %v\n", err)
return
}

defer log.Close()

loggerCfg := &zaplog.Config{
Level: zapcore.DebugLevel,
LogPath: log.LogPath + "azure-ipam.log",
LogPath: zaplog.LogPath + "azure-ipam.log",
MaxSizeInMB: maxLogFileSizeInMb,
MaxBackups: maxLogFileCount,
Name: name,
}
log.Initialize(ctx, loggerCfg)
zaplog.Initialize(ctx, loggerCfg)

ipamPlugin, err := ipam.NewPlugin(name, &config)
if err != nil {
Expand Down
54 changes: 32 additions & 22 deletions cni/network/plugin/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,10 @@ import (
"github.com/Azure/azure-container-networking/aitelemetry"
"github.com/Azure/azure-container-networking/cni"
"github.com/Azure/azure-container-networking/cni/api"
"github.com/Azure/azure-container-networking/cni/log"
zaplog "github.com/Azure/azure-container-networking/cni/log"
"github.com/Azure/azure-container-networking/cni/network"
"github.com/Azure/azure-container-networking/common"
"github.com/Azure/azure-container-networking/log"
"github.com/Azure/azure-container-networking/nns"
"github.com/Azure/azure-container-networking/platform"
"github.com/Azure/azure-container-networking/store"
Expand Down Expand Up @@ -61,11 +62,11 @@ func printVersion() {

// send error report to hostnetagent if CNI encounters any error.
func reportPluginError(reportManager *telemetry.ReportManager, tb *telemetry.TelemetryBuffer, err error) {
log.Logger.Error("Report plugin error")
zaplog.Logger.Error("Report plugin error")
reflect.ValueOf(reportManager.Report).Elem().FieldByName("ErrorMessage").SetString(err.Error())

if err := reportManager.SendReport(tb); err != nil {
log.Logger.Error("SendReport failed", zap.Error(err))
zaplog.Logger.Error("SendReport failed", zap.Error(err))
}
}

Expand All @@ -83,7 +84,7 @@ func validateConfig(jsonBytes []byte) error {
}

func getCmdArgsFromEnv() (string, *skel.CmdArgs, error) {
log.Logger.Info("Going to read from stdin")
zaplog.Logger.Info("Going to read from stdin")
stdinData, err := io.ReadAll(os.Stdin)
if err != nil {
return "", nil, fmt.Errorf("error reading from stdin: %v", err)
Expand All @@ -109,32 +110,32 @@ func handleIfCniUpdate(update func(*skel.CmdArgs) error) (bool, error) {
return false, nil
}

log.Logger.Info("CNI UPDATE received")
zaplog.Logger.Info("CNI UPDATE received")

_, cmdArgs, err := getCmdArgsFromEnv()
if err != nil {
log.Logger.Error("Received error while retrieving cmds from environment", zap.Error(err))
zaplog.Logger.Error("Received error while retrieving cmds from environment", zap.Error(err))
return isupdate, err
}

log.Logger.Info("Retrieved command args for update", zap.Any("args", cmdArgs))
zaplog.Logger.Info("Retrieved command args for update", zap.Any("args", cmdArgs))
err = validateConfig(cmdArgs.StdinData)
if err != nil {
log.Logger.Error("Failed to handle CNI UPDATE", zap.Error(err))
zaplog.Logger.Error("Failed to handle CNI UPDATE", zap.Error(err))
return isupdate, err
}

err = update(cmdArgs)
if err != nil {
log.Logger.Error("Failed to handle CNI UPDATE", zap.Error(err))
zaplog.Logger.Error("Failed to handle CNI UPDATE", zap.Error(err))
return isupdate, err
}

return isupdate, nil
}

func printCNIError(msg string) {
log.Logger.Error(msg)
zaplog.Logger.Error(msg)
cniErr := &cniTypes.Error{
Code: cniTypes.ErrTryAgainLater,
Msg: msg,
Expand Down Expand Up @@ -178,7 +179,7 @@ func rootExecute() error {
cniCmd := os.Getenv(cni.Cmd)

if cniCmd != cni.CmdVersion {
log.Logger.Info("Environment variable set", zap.String("CNI_COMMAND", cniCmd))
zaplog.Logger.Info("Environment variable set", zap.String("CNI_COMMAND", cniCmd))

cniReport.GetReport(pluginName, version, ipamQueryURL)

Expand All @@ -194,7 +195,7 @@ func rootExecute() error {

tb = telemetry.NewTelemetryBuffer()
if tberr := tb.Connect(); tberr != nil {
log.Logger.Error("Cannot connect to telemetry service", zap.Error(tberr))
zaplog.Logger.Error("Cannot connect to telemetry service", zap.Error(tberr))
return errors.Wrap(err, "lock acquire error")
}

Expand All @@ -209,7 +210,7 @@ func rootExecute() error {
}
sendErr := telemetry.SendCNIMetric(&cniMetric, tb)
if sendErr != nil {
log.Logger.Error("Couldn't send cnilocktimeout metric", zap.Error(sendErr))
zaplog.Logger.Error("Couldn't send cnilocktimeout metric", zap.Error(sendErr))
}
}

Expand All @@ -219,7 +220,7 @@ func rootExecute() error {

defer func() {
if errUninit := netPlugin.Plugin.UninitializeKeyValueStore(); errUninit != nil {
log.Logger.Error("Failed to uninitialize key-value store of network plugin", zap.Error(errUninit))
zaplog.Logger.Error("Failed to uninitialize key-value store of network plugin", zap.Error(errUninit))
}

if recover() != nil {
Expand All @@ -246,17 +247,17 @@ func rootExecute() error {

// used to dump state
if cniCmd == cni.CmdGetEndpointsState {
log.Logger.Debug("Retrieving state")
zaplog.Logger.Debug("Retrieving state")
var simpleState *api.AzureCNIState
simpleState, err = netPlugin.GetAllEndpointState("azure")
if err != nil {
log.Logger.Error("Failed to get Azure CNI state", zap.Error(err))
zaplog.Logger.Error("Failed to get Azure CNI state", zap.Error(err))
return errors.Wrap(err, "Get all endpoints error")
}

err = simpleState.PrintResult()
if err != nil {
log.Logger.Error("Failed to print state result to stdout", zap.Error(err))
zaplog.Logger.Error("Failed to print state result to stdout", zap.Error(err))
}

return errors.Wrap(err, "Get cni state printresult error")
Expand All @@ -265,9 +266,9 @@ func rootExecute() error {

handled, _ := handleIfCniUpdate(netPlugin.Update)
if handled {
log.Logger.Info("CNI UPDATE finished.")
zaplog.Logger.Info("CNI UPDATE finished.")
} else if err = netPlugin.Execute(cni.PluginApi(netPlugin)); err != nil {
log.Logger.Error("Failed to execute network plugin", zap.Error(err))
zaplog.Logger.Error("Failed to execute network plugin", zap.Error(err))
}

if cniCmd == cni.CmdVersion {
Expand Down Expand Up @@ -295,14 +296,23 @@ func main() {
os.Exit(0)
}

loggerCfg := &log.Config{
log.SetName(name)
log.SetLevel(log.LevelInfo)
if err := log.SetTargetLogDirectory(log.TargetLogfile, ""); err != nil {
fmt.Printf("Failed to setup cni logging: %v\n", err)
return
}

defer log.Close()

loggerCfg := &zaplog.Config{
Level: zapcore.DebugLevel,
LogPath: log.LogPath + name + ".log",
LogPath: zaplog.LogPath + name + ".log",
MaxSizeInMB: maxLogFileSizeInMb,
MaxBackups: maxLogFileCount,
Name: name,
}
log.Initialize(ctx, loggerCfg)
zaplog.Initialize(ctx, loggerCfg)

if rootExecute() != nil {
os.Exit(1)
Expand Down

0 comments on commit e9ddb87

Please sign in to comment.