Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Override zap log level from old flag #2587

Merged
merged 3 commits into from
Jul 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions charts/fleet/templates/deployment_gitjob.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ spec:
- "{{ template "system_default_registry" $ }}{{ $.Values.image.repository }}:{{ $.Values.image.tag }}"
{{- if $.Values.debug }}
- --debug
- --debug-level
- {{ quote $.Values.debugLevel }}
{{- end }}
{{- if $shard.id }}
- --shard-id
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ require (
github.com/stretchr/testify v1.9.0
github.com/testcontainers/testcontainers-go v0.31.0
go.uber.org/mock v0.4.0
go.uber.org/zap v1.26.0
golang.org/x/crypto v0.24.0
golang.org/x/sync v0.7.0
gopkg.in/go-playground/webhooks.v5 v5.17.0
Expand Down Expand Up @@ -220,7 +221,6 @@ require (
go.opentelemetry.io/otel/trace v1.24.0 // indirect
go.starlark.net v0.0.0-20230525235612-a134d8f9ddca // indirect
go.uber.org/multierr v1.11.0 // indirect
go.uber.org/zap v1.26.0 // indirect
golang.org/x/exp v0.0.0-20240613232115-7f521ea00fb8 // indirect
golang.org/x/mod v0.18.0 // indirect
golang.org/x/net v0.26.0 // indirect
Expand Down
4 changes: 2 additions & 2 deletions internal/cmd/agent/clusterstatus.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,12 @@ func (cs *ClusterStatus) PersistentPre(cmd *cobra.Command, _ []string) error {
if err := cs.SetupDebug(); err != nil {
return fmt.Errorf("failed to setup debug logging: %w", err)
}
zopts = cs.OverrideZapOpts(zopts)
return nil
}

func (cs *ClusterStatus) Run(cmd *cobra.Command, args []string) error {
zopts.Development = cs.Debug
ctrl.SetLogger(zap.New(zap.UseFlagOptions(&zopts)))
ctrl.SetLogger(zap.New(zap.UseFlagOptions(zopts)))
ctx := log.IntoContext(cmd.Context(), ctrl.Log)

var err error
Expand Down
4 changes: 2 additions & 2 deletions internal/cmd/agent/register.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,12 @@ func (r *Register) PersistentPre(cmd *cobra.Command, _ []string) error {
if err := r.SetupDebug(); err != nil {
return fmt.Errorf("failed to setup debug logging: %w", err)
}
zopts = r.OverrideZapOpts(zopts)
return nil
}

func (r *Register) Run(cmd *cobra.Command, args []string) error {
zopts.Development = r.Debug
ctrl.SetLogger(zap.New(zap.UseFlagOptions(&zopts)))
ctrl.SetLogger(zap.New(zap.UseFlagOptions(zopts)))
ctx := log.IntoContext(cmd.Context(), ctrl.Log)

clientConfig := kubeconfig.GetNonInteractiveClientConfig(r.Kubeconfig)
Expand Down
7 changes: 3 additions & 4 deletions internal/cmd/agent/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ type FleetAgent struct {

var (
setupLog = ctrl.Log.WithName("setup")
zopts = zap.Options{
zopts = &zap.Options{
Development: true,
}
)
Expand All @@ -38,13 +38,12 @@ func (a *FleetAgent) PersistentPre(cmd *cobra.Command, _ []string) error {
if err := a.SetupDebug(); err != nil {
return fmt.Errorf("failed to setup debug logging: %w", err)
}
zopts = a.OverrideZapOpts(zopts)
return nil
}

func (a *FleetAgent) Run(cmd *cobra.Command, args []string) error {
// for compatibility, override zap opts with legacy debug opts. remove once manifests are updated.
zopts.Development = a.Debug
ctrl.SetLogger(zap.New(zap.UseFlagOptions(&zopts)))
ctrl.SetLogger(zap.New(zap.UseFlagOptions(zopts)))
ctx := log.IntoContext(cmd.Context(), ctrl.Log)

localConfig := ctrl.GetConfigOrDie()
Expand Down
3 changes: 1 addition & 2 deletions internal/cmd/controller/gitops/operator.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,13 +74,12 @@ func (g *GitOperator) PersistentPre(_ *cobra.Command, _ []string) error {
if err := g.SetupDebug(); err != nil {
return fmt.Errorf("failed to setup debug logging: %w", err)
}
zopts = g.OverrideZapOpts(zopts)

return nil
}

func (g *GitOperator) Run(cmd *cobra.Command, args []string) error {
// TODO for compatibility, override zap opts with legacy debug opts. remove once manifests are updated.
zopts.Development = g.Debug
ctrl.SetLogger(zap.New(zap.UseFlagOptions(zopts)))
ctx := clog.IntoContext(cmd.Context(), ctrl.Log.WithName("gitjob-reconciler"))

Expand Down
9 changes: 4 additions & 5 deletions internal/cmd/controller/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ type BindAddresses struct {

var (
setupLog = ctrl.Log.WithName("setup")
zopts = zap.Options{
zopts = &zap.Options{
Development: true,
}
)
Expand All @@ -59,14 +59,13 @@ func (f *FleetManager) PersistentPre(_ *cobra.Command, _ []string) error {
if err := f.SetupDebug(); err != nil {
return fmt.Errorf("failed to setup debug logging: %w", err)
}
zopts = f.OverrideZapOpts(zopts)

return nil
}

func (f *FleetManager) Run(cmd *cobra.Command, args []string) error {
// for compatibility, override zap opts with legacy debug opts. remove once manifests are updated.
zopts.Development = f.Debug
ctrl.SetLogger(zap.New(zap.UseFlagOptions(&zopts)))
ctrl.SetLogger(zap.New(zap.UseFlagOptions(zopts)))
ctx := clog.IntoContext(cmd.Context(), ctrl.Log)

kubeconfig := ctrl.GetConfigOrDie()
Expand Down Expand Up @@ -136,7 +135,7 @@ func App() *cobra.Command {
root.AddCommand(
cleanup.App(),
agentmanagement.App(),
gitops.App(&zopts),
gitops.App(zopts),
)
return root
}
Expand Down
18 changes: 18 additions & 0 deletions internal/cmd/debug.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@ import (
"fmt"

"github.com/sirupsen/logrus"
"go.uber.org/zap/zapcore"

"k8s.io/klog/v2"
crzap "sigs.k8s.io/controller-runtime/pkg/log/zap"
)

type DebugConfig struct {
Expand Down Expand Up @@ -35,3 +38,18 @@ func (c *DebugConfig) SetupDebug() error {

return nil
}

// OverrideZapOpts, for compatibility override zap opts with legacy debug opts.
func (c *DebugConfig) OverrideZapOpts(zopts *crzap.Options) *crzap.Options {
if zopts == nil {
zopts = &crzap.Options{}
}

zopts.Development = c.Debug

if c.Debug && c.DebugLevel > 0 {
zopts.Level = zapcore.Level(c.DebugLevel * -1)
}

return zopts
}
Loading