Skip to content

Commit

Permalink
Set cluster_name var in logs vars separately and not in init (#2777)
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
viveksinghggits and mergify[bot] authored Mar 26, 2024
1 parent 43f70b9 commit 510494c
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 0 deletions.
3 changes: 3 additions & 0 deletions cmd/controller/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
3 changes: 3 additions & 0 deletions cmd/kanctl/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package main

import (
"github.com/kanisterio/kanister/pkg/kanctl"
"github.com/kanisterio/kanister/pkg/log"
)

func init() {
Expand All @@ -25,5 +26,7 @@ func init() {
}

func main() {
log.SetupClusterNameInLogVars()

kanctl.Execute()
}
2 changes: 2 additions & 0 deletions cmd/reposervercontroller/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
Expand Down
4 changes: 4 additions & 0 deletions pkg/log/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down

0 comments on commit 510494c

Please sign in to comment.