diff --git a/changelog/v0.21.0/dev-logging.yaml b/changelog/v0.21.0/dev-logging.yaml new file mode 100644 index 000000000..6bb9568c1 --- /dev/null +++ b/changelog/v0.21.0/dev-logging.yaml @@ -0,0 +1,6 @@ +changelog: + - type: BREAKING_CHANGE + description: > + Change Logging options to be more clear. This will only affect bootstrap.Start() and + bootstrap.StartMulti() functions. + issueLink: https://github.com/solo-io/skv2/issues/304 diff --git a/pkg/bootstrap/bootstrap.go b/pkg/bootstrap/bootstrap.go index 34ec0f65f..ea9a5cf01 100644 --- a/pkg/bootstrap/bootstrap.go +++ b/pkg/bootstrap/bootstrap.go @@ -65,8 +65,9 @@ type Options struct { // enables verbose mode VerboseMode bool - // enables json logger (instead of table logger) - JSONLogger bool + // enables dev logger (instead of prod logger) + // NOTE: DO NOT set this to true in Prod, it will crash on DPanic + DevLogger bool // ManagementContext if specified read the KubeConfig for the management cluster from this context. Only applies when running out of cluster. ManagementContext string @@ -83,6 +84,11 @@ func (opts *Options) AddToFlags(flags *pflag.FlagSet) { flags.StringVar(&opts.ManagementContext, "context", "", "If specified, use this context from the selected KubeConfig to connect to the local (management) cluster.") flags.StringVar(&opts.SettingsRef.Name, "settings-name", opts.SettingsRef.Name, "The name of the Settings object this controller should use.") flags.StringVar(&opts.SettingsRef.Namespace, "settings-namespace", opts.SettingsRef.Namespace, "The namespace of the Settings object this controller should use.") + + // This flag disables prod mode when set to false, in other words setting debug to true, + // Which will cause the app to panic on DPanic. + flags.BoolVar(&opts.DevLogger, "dev-logger", false, "Default: false. Set this value to true to enable debug panic logs for development.") + flags.MarkHidden("dev-logger") } // Start a controller with the given start func. The StartFunc will be called with a bootstrapped local manager. If localMode is false, the StartParameters will include initialized multicluster components. @@ -92,7 +98,7 @@ func Start(ctx context.Context, start StartFunc, opts Options, schemes runtime.S // Like Start, but runs multiple StartFuncs concurrently func StartMulti(ctx context.Context, startFuncs map[string]StartFunc, opts Options, schemes runtime.SchemeBuilder, localMode bool, addStatsHandlers ...func(mux *http.ServeMux, profiles map[string]string)) error { - setupLogging(opts.VerboseMode, opts.JSONLogger) + setupLogging(opts.VerboseMode, opts.DevLogger) mgr, err := makeMasterManager(opts, schemes) if err != nil { @@ -191,7 +197,7 @@ func makeMasterManager(opts Options, schemes runtime.SchemeBuilder) (manager.Man return mgr, nil } -func setupLogging(verboseMode, jsonLogging bool) { +func setupLogging(verboseMode, devLogging bool) { level := zapcore.InfoLevel if verboseMode { level = zapcore.DebugLevel @@ -200,7 +206,7 @@ func setupLogging(verboseMode, jsonLogging bool) { zapOpts := []zaputil.Opts{ zaputil.Level(&atomicLevel), } - if !jsonLogging { + if devLogging { zapOpts = append(zapOpts, // Only set debug mode if specified. This will use a non-json (human readable) encoder which makes it impossible // to use any json parsing tools for the log. Should only be enabled explicitly diff --git a/pkg/ezkube/object_ref.go b/pkg/ezkube/object_ref.go index 150b08257..715935722 100644 --- a/pkg/ezkube/object_ref.go +++ b/pkg/ezkube/object_ref.go @@ -6,6 +6,9 @@ import ( ) func MakeObjectRef(resource ResourceId) *v1.ObjectRef { + if resource == nil { + return nil + } return &v1.ObjectRef{ Name: resource.GetName(), Namespace: resource.GetNamespace(), @@ -13,6 +16,9 @@ func MakeObjectRef(resource ResourceId) *v1.ObjectRef { } func MakeClusterObjectRef(resource ClusterResourceId) *v1.ClusterObjectRef { + if resource == nil { + return nil + } return &v1.ClusterObjectRef{ Name: resource.GetName(), Namespace: resource.GetNamespace(), @@ -21,17 +27,28 @@ func MakeClusterObjectRef(resource ClusterResourceId) *v1.ClusterObjectRef { } func RefsMatch(ref1, ref2 ResourceId) bool { + // If either ref is nil, return true if they are both nil + if ref1 == nil || ref2 == nil { + return ref1 == nil && ref2 == nil + } return ref1.GetNamespace() == ref2.GetNamespace() && ref1.GetName() == ref2.GetName() } func ClusterRefsMatch(ref1, ref2 ClusterResourceId) bool { + // If either ref is nil, return true if they are both nil + if ref1 == nil || ref2 == nil { + return ref1 == nil && ref2 == nil + } return ref1.GetNamespace() == ref2.GetNamespace() && ref1.GetName() == ref2.GetName() && ref1.GetClusterName() == ref2.GetClusterName() } func MakeClientObjectKey(ref ResourceId) client.ObjectKey { + if ref == nil { + return client.ObjectKey{} + } return client.ObjectKey{ Namespace: ref.GetNamespace(), Name: ref.GetName(),