From 510494cb11c0f39b13a161bd8b2a5fd887219ceb Mon Sep 17 00:00:00 2001 From: Vivek Singh Date: Tue, 26 Mar 2024 18:11:29 +0100 Subject: [PATCH] Set `cluster_name` var in logs vars separately and not in `init` (#2777) If we set the `cluster_name` field in log variables (extran fields that are printed in logs), inside the `init` function of `log` package, we would un necessarily be calling k8s api even from the places where we don't have to do anything with the k8s api. To fix this, this commit introduces a new function that can be used to setup that field in the logs vars and that function will only be called from the places where we need the logs to have to the `cluster_name` field, for example `kanctl`, `controller` and `repository-controller`. Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com> --- cmd/controller/main.go | 3 +++ cmd/kanctl/main.go | 3 +++ cmd/reposervercontroller/main.go | 2 ++ pkg/log/log.go | 4 ++++ 4 files changed, 12 insertions(+) diff --git a/cmd/controller/main.go b/cmd/controller/main.go index 742ea39319..7ed9dcb24d 100644 --- a/cmd/controller/main.go +++ b/cmd/controller/main.go @@ -17,8 +17,11 @@ package main import ( "github.com/kanisterio/kanister/pkg/kancontroller" + "github.com/kanisterio/kanister/pkg/log" ) func main() { + log.SetupClusterNameInLogVars() + kancontroller.Execute() } diff --git a/cmd/kanctl/main.go b/cmd/kanctl/main.go index 81aef8fbeb..07f3cb2328 100644 --- a/cmd/kanctl/main.go +++ b/cmd/kanctl/main.go @@ -17,6 +17,7 @@ package main import ( "github.com/kanisterio/kanister/pkg/kanctl" + "github.com/kanisterio/kanister/pkg/log" ) func init() { @@ -25,5 +26,7 @@ func init() { } func main() { + log.SetupClusterNameInLogVars() + kanctl.Execute() } diff --git a/cmd/reposervercontroller/main.go b/cmd/reposervercontroller/main.go index a08bca184b..7ecd238aa6 100644 --- a/cmd/reposervercontroller/main.go +++ b/cmd/reposervercontroller/main.go @@ -68,6 +68,8 @@ func main() { flag.StringVar(&probeAddr, "health-probe-bind-address", ":8081", "The address the probe endpoint binds to.") logLevel := getLogLevel() + log.SetupClusterNameInLogVars() + opts := zap.Options{ Level: logLevel, } diff --git a/pkg/log/log.go b/pkg/log/log.go index a5155afb82..d40f22f4b4 100644 --- a/pkg/log/log.go +++ b/pkg/log/log.go @@ -93,7 +93,11 @@ func initEnvVarFields() { envVarFields = field.Add(envVarFields, strings.ToLower(e), ev) } } +} +// SetupClusterNameInLogVars sets up the `cluster_name` field in `envVarFields` +// so that it can be printed with the logs. +func SetupClusterNameInLogVars() { if clsName, err := config.GetClusterName(nil); err == nil { envVarFields = field.Add(envVarFields, "cluster_name", clsName) }